Logger.java revision ad41624e761bcf1af9c8008eb45187fc13983717
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.apache.harmony.xnet.provider.jsse; 19 20import java.io.PrintStream; 21import libcore.base.EmptyArray; 22 23/** 24 * This class provides debug logging for JSSE provider implementation 25 * TODO: Use java.util.logging 26 */ 27public class Logger { 28 29 public static class Stream extends PrintStream { 30 private final String prefix; 31 private static int indent = 0; 32 33 public Stream(String name) { 34 super(System.err); 35 prefix = name + "["+Thread.currentThread().getName()+"] "; 36 } 37 38 @Override 39 public void print(String msg) { 40 for (int i=0; i<indent; i++) { 41 super.print(" "); 42 } 43 super.print(msg); 44 } 45 46 public void newIndent() { 47 indent ++; 48 } 49 50 public void endIndent() { 51 indent --; 52 } 53 54 @Override 55 public void println(String msg) { 56 print(prefix); 57 super.println(msg); 58 } 59 60 public void print(byte[] data) { 61 printAsHex(16, " ", "", data, 0, data.length); 62 } 63 64 public void print(byte[] data, int offset, int len) { 65 printAsHex(16, " ", "", data, offset, len); 66 } 67 68 public void printAsHex(int perLine, 69 String prefix, 70 String delimiter, 71 byte[] data) { 72 printAsHex(perLine, prefix, delimiter, data, 0, data.length); 73 } 74 75 public void printAsHex(int perLine, 76 String prefix, 77 String delimiter, 78 byte[] data, int offset, int len) { 79 String line = ""; 80 for (int i=0; i<len; i++) { 81 String tail = 82 Integer.toHexString(0x00ff & data[i+offset]).toUpperCase(); 83 if (tail.length() == 1) { 84 tail = "0" + tail; 85 } 86 line += prefix + tail + delimiter; 87 88 if (((i+1)%perLine) == 0) { 89 super.println(line); 90 line = ""; 91 } 92 } 93 super.println(line); 94 } 95 } 96 97 private static String[] names; 98 99 static { 100 try { 101 names = System.getProperty("jsse", "").split(","); 102 } catch (Exception e) { 103 names = EmptyArray.STRING; 104 } 105 } 106 107 public static Stream getStream(String name) { 108 for (int i=0; i<names.length; i++) { 109 if (names[i].equals(name)) { 110 return new Stream(name); 111 } 112 } 113 return null; 114 } 115} 116