1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package org.conscrypt; 19 20import java.io.PrintStream; 21import java.util.Locale; 22import org.conscrypt.util.EmptyArray; 23 24/** 25 * This class provides debug logging for JSSE provider implementation 26 * TODO: Use java.util.logging 27 */ 28public class Logger { 29 30 public static class Stream extends PrintStream { 31 private final String prefix; 32 private static int indent = 0; 33 34 public Stream(String name) { 35 super(System.err); 36 prefix = name + "["+Thread.currentThread().getName()+"] "; 37 } 38 39 @Override 40 public void print(String msg) { 41 for (int i=0; i<indent; i++) { 42 super.print(" "); 43 } 44 super.print(msg); 45 } 46 47 public void newIndent() { 48 indent ++; 49 } 50 51 public void endIndent() { 52 indent --; 53 } 54 55 @Override 56 public void println(String msg) { 57 print(prefix); 58 super.println(msg); 59 } 60 61 public void print(byte[] data) { 62 printAsHex(16, " ", "", data, 0, data.length); 63 } 64 65 public void print(byte[] data, int offset, int len) { 66 printAsHex(16, " ", "", data, offset, len); 67 } 68 69 public void printAsHex(int perLine, String prefix, String delimiter, byte[] data) { 70 printAsHex(perLine, prefix, delimiter, data, 0, data.length); 71 } 72 73 public void printAsHex(int perLine, String prefix, String delimiter, 74 byte[] data, int offset, int len) { 75 StringBuilder line = new StringBuilder(); 76 for (int i = 0; i < len; i++) { 77 line.append(prefix); 78 line.append(Byte.toHexString(data[i+offset], false)); 79 line.append(delimiter); 80 81 if (((i+1)%perLine) == 0) { 82 super.println(line.toString()); 83 line = new StringBuilder(); 84 } 85 } 86 super.println(line.toString()); 87 } 88 } 89 90 private static String[] names; 91 92 static { 93 try { 94 names = System.getProperty("jsse", "").split(","); 95 } catch (Exception e) { 96 names = EmptyArray.STRING; 97 } 98 } 99 100 public static Stream getStream(String name) { 101 for (int i=0; i<names.length; i++) { 102 if (names[i].equals(name)) { 103 return new Stream(name); 104 } 105 } 106 return null; 107 } 108} 109