View Javadoc
1   /*
2    * Copyright (c) 2003, 2008, Oracle and/or its affiliates. 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.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package com.sun.jmx.remote.util;
27  
28  import java.util.logging.Logger;
29  
30  public class ClassLogger {
31  
32      private static final boolean ok;
33      private final String className;
34      private final Logger logger;
35  
36      static {
37          /* We attempt to work even if we are running in J2SE 1.3, where
38             there is no java.util.logging.  The technique we use here is
39             not strictly portable, but it does work with Sun's J2SE 1.3
40             at least.  This is just a best effort: the Right Thing is for
41             people to use at least J2SE 1.4.  */
42          boolean loaded = false;
43          try {
44              Class<?> c = java.util.logging.Logger.class;
45              loaded = true;
46          } catch (Error e) {
47              // OK.
48              // java.util.logger package is not available in this jvm.
49          }
50          ok = loaded;
51      }
52  
53      public ClassLogger(String subsystem, String className) {
54          if (ok)
55              logger = Logger.getLogger(subsystem);
56          else
57              logger = null;
58          this.className = className;
59      }
60  
61      public final boolean traceOn() {
62          return finerOn();
63      }
64  
65      public final boolean debugOn() {
66          return finestOn();
67      }
68  
69      public final boolean warningOn() {
70          return ok && logger.isLoggable(java.util.logging.Level.WARNING);
71      }
72  
73      public final boolean infoOn() {
74          return ok && logger.isLoggable(java.util.logging.Level.INFO);
75      }
76  
77      public final boolean configOn() {
78          return ok && logger.isLoggable(java.util.logging.Level.CONFIG);
79      }
80  
81      public final boolean fineOn() {
82          return ok && logger.isLoggable(java.util.logging.Level.FINE);
83      }
84  
85      public final boolean finerOn() {
86          return ok && logger.isLoggable(java.util.logging.Level.FINER);
87      }
88  
89      public final boolean finestOn() {
90          return ok && logger.isLoggable(java.util.logging.Level.FINEST);
91      }
92  
93      public final void debug(String func, String msg) {
94          finest(func,msg);
95      }
96  
97      public final void debug(String func, Throwable t) {
98          finest(func,t);
99      }
100 
101     public final void debug(String func, String msg, Throwable t) {
102         finest(func,msg,t);
103     }
104 
105     public final void trace(String func, String msg) {
106         finer(func,msg);
107     }
108 
109     public final void trace(String func, Throwable t) {
110         finer(func,t);
111     }
112 
113     public final void trace(String func, String msg, Throwable t) {
114         finer(func,msg,t);
115     }
116 
117     public final void error(String func, String msg) {
118         severe(func,msg);
119     }
120 
121     public final void error(String func, Throwable t) {
122         severe(func,t);
123     }
124 
125     public final void error(String func, String msg, Throwable t) {
126         severe(func,msg,t);
127     }
128 
129     public final void finest(String func, String msg) {
130         if (ok)
131             logger.logp(java.util.logging.Level.FINEST, className, func, msg);
132     }
133 
134     public final void finest(String func, Throwable t) {
135         if (ok)
136             logger.logp(java.util.logging.Level.FINEST, className, func,
137                         t.toString(), t);
138     }
139 
140     public final void finest(String func, String msg, Throwable t) {
141         if (ok)
142             logger.logp(java.util.logging.Level.FINEST, className, func, msg,
143                         t);
144     }
145 
146     public final void finer(String func, String msg) {
147         if (ok)
148             logger.logp(java.util.logging.Level.FINER, className, func, msg);
149     }
150 
151     public final void finer(String func, Throwable t) {
152         if (ok)
153             logger.logp(java.util.logging.Level.FINER, className, func,
154                         t.toString(), t);
155     }
156 
157     public final void finer(String func, String msg, Throwable t) {
158         if (ok)
159             logger.logp(java.util.logging.Level.FINER, className, func, msg,t);
160     }
161 
162     public final void fine(String func, String msg) {
163         if (ok)
164             logger.logp(java.util.logging.Level.FINE, className, func, msg);
165     }
166 
167     public final void fine(String func, Throwable t) {
168         if (ok)
169             logger.logp(java.util.logging.Level.FINE, className, func,
170                         t.toString(), t);
171     }
172 
173     public final void fine(String func, String msg, Throwable t) {
174         if (ok)
175             logger.logp(java.util.logging.Level.FINE, className, func, msg,
176                         t);
177     }
178 
179     public final void config(String func, String msg) {
180         if (ok)
181             logger.logp(java.util.logging.Level.CONFIG, className, func, msg);
182     }
183 
184     public final void config(String func, Throwable t) {
185         if (ok)
186             logger.logp(java.util.logging.Level.CONFIG, className, func,
187                         t.toString(), t);
188     }
189 
190     public final void config(String func, String msg, Throwable t) {
191         if (ok)
192             logger.logp(java.util.logging.Level.CONFIG, className, func, msg,
193                         t);
194     }
195 
196     public final void info(String func, String msg) {
197         if (ok)
198             logger.logp(java.util.logging.Level.INFO, className, func, msg);
199     }
200 
201     public final void info(String func, Throwable t) {
202         if (ok)
203             logger.logp(java.util.logging.Level.INFO, className, func,
204                         t.toString(), t);
205     }
206 
207     public final void info(String func, String msg, Throwable t) {
208         if (ok)
209             logger.logp(java.util.logging.Level.INFO, className, func, msg,
210                         t);
211     }
212 
213     public final void warning(String func, String msg) {
214         if (ok)
215             logger.logp(java.util.logging.Level.WARNING, className, func, msg);
216     }
217 
218     public final void warning(String func, Throwable t) {
219         if (ok)
220             logger.logp(java.util.logging.Level.WARNING, className, func,
221                         t.toString(), t);
222     }
223 
224     public final void warning(String func, String msg, Throwable t) {
225         if (ok)
226             logger.logp(java.util.logging.Level.WARNING, className, func, msg,
227                         t);
228     }
229 
230     public final void severe(String func, String msg) {
231         if (ok)
232             logger.logp(java.util.logging.Level.SEVERE, className, func, msg);
233     }
234 
235     public final void severe(String func, Throwable t) {
236         if (ok)
237             logger.logp(java.util.logging.Level.SEVERE, className, func,
238                         t.toString(), t);
239     }
240 
241     public final void severe(String func, String msg, Throwable t) {
242         if (ok)
243             logger.logp(java.util.logging.Level.SEVERE, className, func, msg,
244                         t);
245     }
246 }