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