001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v2.log.jdk14logging;
024:
025: import java.util.*;
026: import java.util.logging.*;
027: import com.mchange.v2.log.*;
028: import com.mchange.v2.util.DoubleWeakHashMap;
029:
030: public final class Jdk14MLog extends MLog {
031: private static String[] UNKNOWN_ARRAY = new String[] {
032: "UNKNOWN_CLASS", "UNKNOWN_METHOD" };
033:
034: private final static String CHECK_CLASS = "java.util.logging.Logger";
035:
036: private final Map namedLoggerMap = new DoubleWeakHashMap();
037:
038: MLogger global = null;
039:
040: public Jdk14MLog() throws ClassNotFoundException {
041: Class.forName(CHECK_CLASS);
042: }
043:
044: public synchronized MLogger getMLogger(String name) {
045: name = name.intern();
046:
047: MLogger out = (MLogger) namedLoggerMap.get(name);
048: if (out == null) {
049: Logger lg = Logger.getLogger(name);
050: out = new Jdk14MLogger(lg);
051: namedLoggerMap.put(name, out);
052: }
053: return out;
054: }
055:
056: public synchronized MLogger getMLogger(Class cl) {
057: return getLogger(cl.getName());
058: }
059:
060: public synchronized MLogger getMLogger() {
061: if (global == null)
062: global = new Jdk14MLogger(LogManager.getLogManager()
063: .getLogger("global"));
064: return global;
065: }
066:
067: /*
068: * We have to do this ourselves when class and method aren't provided,
069: * because the automatic extraction of this information will find the
070: * (not very informative) calls in this class.
071: */
072: private static String[] findCallingClassAndMethod() {
073: StackTraceElement[] ste = new Throwable().getStackTrace();
074: for (int i = 0, len = ste.length; i < len; ++i) {
075: StackTraceElement check = ste[i];
076: String cn = check.getClassName();
077: if (cn != null
078: && !cn
079: .startsWith("com.mchange.v2.log.jdk14logging"))
080: return new String[] { check.getClassName(),
081: check.getMethodName() };
082: }
083: return UNKNOWN_ARRAY;
084: }
085:
086: private final static class Jdk14MLogger implements MLogger {
087: volatile Logger logger;
088:
089: Jdk14MLogger(Logger logger) {
090: this .logger = logger;
091: //System.err.println("LOGGER: " + this.logger);
092: }
093:
094: private static Level level(MLevel lvl) {
095: return (Level) lvl.asJdk14Level();
096: }
097:
098: public ResourceBundle getResourceBundle() {
099: return logger.getResourceBundle();
100: }
101:
102: public String getResourceBundleName() {
103: return logger.getResourceBundleName();
104: }
105:
106: public void setFilter(Object java14Filter)
107: throws SecurityException {
108: if (!(java14Filter instanceof Filter))
109: throw new IllegalArgumentException(
110: "MLogger.setFilter( ... ) requires a java.util.logging.Filter. "
111: + "This is not enforced by the compiler only to permit building under jdk 1.3");
112: logger.setFilter((Filter) java14Filter);
113: }
114:
115: public Object getFilter() {
116: return logger.getFilter();
117: }
118:
119: public void log(MLevel l, String msg) {
120: if (!logger.isLoggable(level(l)))
121: return;
122:
123: String[] sa = findCallingClassAndMethod();
124: logger.logp(level(l), sa[0], sa[1], msg);
125: }
126:
127: public void log(MLevel l, String msg, Object param) {
128: if (!logger.isLoggable(level(l)))
129: return;
130:
131: String[] sa = findCallingClassAndMethod();
132: logger.logp(level(l), sa[0], sa[1], msg, param);
133: }
134:
135: public void log(MLevel l, String msg, Object[] params) {
136: if (!logger.isLoggable(level(l)))
137: return;
138:
139: String[] sa = findCallingClassAndMethod();
140: logger.logp(level(l), sa[0], sa[1], msg, params);
141: }
142:
143: public void log(MLevel l, String msg, Throwable t) {
144: if (!logger.isLoggable(level(l)))
145: return;
146:
147: String[] sa = findCallingClassAndMethod();
148: logger.logp(level(l), sa[0], sa[1], msg, t);
149: }
150:
151: public void logp(MLevel l, String srcClass, String srcMeth,
152: String msg) {
153: if (!logger.isLoggable(level(l)))
154: return;
155:
156: if (srcClass == null && srcMeth == null) {
157: String[] sa = findCallingClassAndMethod();
158: srcClass = sa[0];
159: srcMeth = sa[1];
160: }
161: logger.logp(level(l), srcClass, srcMeth, msg);
162: }
163:
164: public void logp(MLevel l, String srcClass, String srcMeth,
165: String msg, Object param) {
166: if (!logger.isLoggable(level(l)))
167: return;
168:
169: if (srcClass == null && srcMeth == null) {
170: String[] sa = findCallingClassAndMethod();
171: srcClass = sa[0];
172: srcMeth = sa[1];
173: }
174: logger.logp(level(l), srcClass, srcMeth, msg, param);
175: }
176:
177: public void logp(MLevel l, String srcClass, String srcMeth,
178: String msg, Object[] params) {
179: if (!logger.isLoggable(level(l)))
180: return;
181:
182: if (srcClass == null && srcMeth == null) {
183: String[] sa = findCallingClassAndMethod();
184: srcClass = sa[0];
185: srcMeth = sa[1];
186: }
187: logger.logp(level(l), srcClass, srcMeth, msg, params);
188: }
189:
190: public void logp(MLevel l, String srcClass, String srcMeth,
191: String msg, Throwable t) {
192: if (!logger.isLoggable(level(l)))
193: return;
194:
195: if (srcClass == null && srcMeth == null) {
196: String[] sa = findCallingClassAndMethod();
197: srcClass = sa[0];
198: srcMeth = sa[1];
199: }
200: logger.logp(level(l), srcClass, srcMeth, msg, t);
201: }
202:
203: public void logrb(MLevel l, String srcClass, String srcMeth,
204: String rb, String msg) {
205: if (!logger.isLoggable(level(l)))
206: return;
207:
208: if (srcClass == null && srcMeth == null) {
209: String[] sa = findCallingClassAndMethod();
210: srcClass = sa[0];
211: srcMeth = sa[1];
212: }
213: logger.logrb(level(l), srcClass, srcMeth, rb, msg);
214: }
215:
216: public void logrb(MLevel l, String srcClass, String srcMeth,
217: String rb, String msg, Object param) {
218: if (!logger.isLoggable(level(l)))
219: return;
220:
221: if (srcClass == null && srcMeth == null) {
222: String[] sa = findCallingClassAndMethod();
223: srcClass = sa[0];
224: srcMeth = sa[1];
225: }
226: logger.logrb(level(l), srcClass, srcMeth, rb, msg, param);
227: }
228:
229: public void logrb(MLevel l, String srcClass, String srcMeth,
230: String rb, String msg, Object[] params) {
231: if (!logger.isLoggable(level(l)))
232: return;
233:
234: if (srcClass == null && srcMeth == null) {
235: String[] sa = findCallingClassAndMethod();
236: srcClass = sa[0];
237: srcMeth = sa[1];
238: }
239: logger.logrb(level(l), srcClass, srcMeth, rb, msg, params);
240: }
241:
242: public void logrb(MLevel l, String srcClass, String srcMeth,
243: String rb, String msg, Throwable t) {
244: if (!logger.isLoggable(level(l)))
245: return;
246:
247: if (srcClass == null && srcMeth == null) {
248: String[] sa = findCallingClassAndMethod();
249: srcClass = sa[0];
250: srcMeth = sa[1];
251: }
252: logger.logrb(level(l), srcClass, srcMeth, rb, msg, t);
253: }
254:
255: public void entering(String srcClass, String srcMeth) {
256: if (!logger.isLoggable(Level.FINER))
257: return;
258:
259: logger.entering(srcClass, srcMeth);
260: }
261:
262: public void entering(String srcClass, String srcMeth,
263: Object param) {
264: if (!logger.isLoggable(Level.FINER))
265: return;
266:
267: logger.entering(srcClass, srcMeth, param);
268: }
269:
270: public void entering(String srcClass, String srcMeth,
271: Object params[]) {
272: if (!logger.isLoggable(Level.FINER))
273: return;
274:
275: logger.entering(srcClass, srcMeth, params);
276: }
277:
278: public void exiting(String srcClass, String srcMeth) {
279: if (!logger.isLoggable(Level.FINER))
280: return;
281:
282: logger.exiting(srcClass, srcMeth);
283: }
284:
285: public void exiting(String srcClass, String srcMeth,
286: Object result) {
287: if (!logger.isLoggable(Level.FINER))
288: return;
289:
290: logger.exiting(srcClass, srcMeth, result);
291: }
292:
293: public void throwing(String srcClass, String srcMeth,
294: Throwable t) {
295: if (!logger.isLoggable(Level.FINER))
296: return;
297:
298: logger.throwing(srcClass, srcMeth, t);
299: }
300:
301: public void severe(String msg) {
302: if (!logger.isLoggable(Level.SEVERE))
303: return;
304:
305: String[] sa = findCallingClassAndMethod();
306: logger.logp(Level.SEVERE, sa[0], sa[1], msg);
307: }
308:
309: public void warning(String msg) {
310: if (!logger.isLoggable(Level.WARNING))
311: return;
312:
313: String[] sa = findCallingClassAndMethod();
314: logger.logp(Level.WARNING, sa[0], sa[1], msg);
315: }
316:
317: public void info(String msg) {
318: if (!logger.isLoggable(Level.INFO))
319: return;
320:
321: String[] sa = findCallingClassAndMethod();
322: logger.logp(Level.INFO, sa[0], sa[1], msg);
323: }
324:
325: public void config(String msg) {
326: if (!logger.isLoggable(Level.CONFIG))
327: return;
328:
329: String[] sa = findCallingClassAndMethod();
330: logger.logp(Level.CONFIG, sa[0], sa[1], msg);
331: }
332:
333: public void fine(String msg) {
334: if (!logger.isLoggable(Level.FINE))
335: return;
336:
337: String[] sa = findCallingClassAndMethod();
338: logger.logp(Level.FINE, sa[0], sa[1], msg);
339: }
340:
341: public void finer(String msg) {
342: if (!logger.isLoggable(Level.FINER))
343: return;
344:
345: String[] sa = findCallingClassAndMethod();
346: logger.logp(Level.FINER, sa[0], sa[1], msg);
347: }
348:
349: public void finest(String msg) {
350: if (!logger.isLoggable(Level.FINEST))
351: return;
352:
353: String[] sa = findCallingClassAndMethod();
354: logger.logp(Level.FINEST, sa[0], sa[1], msg);
355: }
356:
357: public void setLevel(MLevel l) throws SecurityException {
358: logger.setLevel(level(l));
359: }
360:
361: public MLevel getLevel() {
362: return MLevel.fromIntValue(logger.getLevel().intValue());
363: }
364:
365: public boolean isLoggable(MLevel l) {
366: return logger.isLoggable(level(l));
367: }
368:
369: public String getName() {
370: return logger.getName();
371: }
372:
373: public void addHandler(Object h) throws SecurityException {
374: if (!(h instanceof Handler))
375: throw new IllegalArgumentException(
376: "MLogger.addHandler( ... ) requires a java.util.logging.Handler. "
377: + "This is not enforced by the compiler only to permit building under jdk 1.3");
378: logger.addHandler((Handler) h);
379: }
380:
381: public void removeHandler(Object h) throws SecurityException {
382: if (!(h instanceof Handler))
383: throw new IllegalArgumentException(
384: "MLogger.removeHandler( ... ) requires a java.util.logging.Handler. "
385: + "This is not enforced by the compiler only to permit building under jdk 1.3");
386: logger.removeHandler((Handler) h);
387: }
388:
389: public Object[] getHandlers() {
390: return logger.getHandlers();
391: }
392:
393: public void setUseParentHandlers(boolean uph) {
394: logger.setUseParentHandlers(uph);
395: }
396:
397: public boolean getUseParentHandlers() {
398: return logger.getUseParentHandlers();
399: }
400: }
401: }
|