blob: 774b208cedde6db63f196ffe7c0c8cb2f0bedb5f [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23package util;
24
25import java.util.logging.Logger;
26import java.util.logging.Level;
27
28public class TestLogger {
29
30 final Logger logger;
31 final String className;
32
33 static String getClassName(Class clazz) {
34 if (clazz == null) return null;
35 if (clazz.isArray())
36 return getClassName(clazz.getComponentType()) + "[]";
37 final String fullname = clazz.getName();
38 final int lastpoint = fullname.lastIndexOf('.');
39 final int len = fullname.length();
40 if ((lastpoint < 0) || (lastpoint >= len))
41 return fullname;
42 else return fullname.substring(lastpoint+1,len);
43 }
44
45 static String getLoggerName(Class clazz) {
46 if (clazz == null) return "sun.management.test";
47 Package p = clazz.getPackage();
48 if (p == null) return "sun.management.test";
49 final String pname = p.getName();
50 if (pname == null) return "sun.management.test";
51 else return pname;
52 }
53
54 public TestLogger(Class clazz) {
55 this(getLoggerName(clazz),getClassName(clazz));
56 }
57
58 public TestLogger(Class clazz, String postfix) {
59 this(getLoggerName(clazz)+((postfix==null)?"":"."+postfix),
60 getClassName(clazz));
61 }
62
63 public TestLogger(String className) {
64 this("sun.management.test",className);
65 }
66
67 public TestLogger(String loggerName, String className) {
68 Logger l = null;
69 try {
70 l = Logger.getLogger(loggerName);
71 } catch (Exception x) {
72 // OK. Should not happen
73 }
74 logger = l;
75 this.className=className;
76 }
77
78 protected Logger getLogger() {
79 return logger;
80 }
81
82 public boolean isTraceOn() {
83 final Logger l = getLogger();
84 if (l==null) return false;
85 return l.isLoggable(Level.FINE);
86 }
87
88 public boolean isDebugOn() {
89 final Logger l = getLogger();
90 if (l==null) return false;
91 return l.isLoggable(Level.FINEST);
92 }
93
94 public void error(String func, String msg) {
95 final Logger l = getLogger();
96 if (l!=null) l.logp(Level.SEVERE,className,
97 func,msg);
98 }
99
100 public void trace(String func, String msg) {
101 final Logger l = getLogger();
102 if (l!=null) l.logp(Level.FINE,className,
103 func,msg);
104 }
105
106 public void trace(String func, Throwable t) {
107 final Logger l = getLogger();
108 if (l!=null) l.logp(Level.FINE,className,
109 func,t.toString(),t);
110 }
111
112 public void trace(String func, String msg, Throwable t) {
113 final Logger l = getLogger();
114 if (l!=null) l.logp(Level.FINE,className,
115 func,msg,t);
116 }
117
118 public void debug(String func, String msg) {
119 final Logger l = getLogger();
120 if (l!=null) l.logp(Level.FINEST,className,
121 func,msg);
122 }
123
124 public void debug(String func, Throwable t) {
125 final Logger l = getLogger();
126 if (l!=null) l.logp(Level.FINEST,className,
127 func,t.toString(),t);
128 }
129
130 public void debug(String func, String msg, Throwable t) {
131 final Logger l = getLogger();
132 if (l!=null) l.logp(Level.FINEST,className,
133 func,msg,t);
134 }
135}