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;
024:
025: import java.text.*;
026: import java.util.*;
027: import java.util.logging.*;
028: import com.mchange.lang.ThrowableUtils;
029:
030: public final class FallbackMLog extends MLog {
031: final static MLevel DEFAULT_CUTOFF_LEVEL;
032:
033: static {
034: MLevel dflt = null;
035: String dfltName = MLog.CONFIG
036: .getProperty("com.mchange.v2.log.FallbackMLog.DEFAULT_CUTOFF_LEVEL");
037: if (dfltName != null)
038: dflt = MLevel.fromSeverity(dfltName);
039: if (dflt == null)
040: dflt = MLevel.INFO;
041: DEFAULT_CUTOFF_LEVEL = dflt;
042: }
043:
044: MLogger logger = new FallbackMLogger();
045:
046: public synchronized MLogger getMLogger(String name) {
047: return logger;
048: }
049:
050: public MLogger getMLogger(Class cl) {
051: return getLogger(cl.getName());
052: }
053:
054: public MLogger getMLogger() {
055: return logger;
056: }
057:
058: private final static class FallbackMLogger implements MLogger {
059: MLevel cutoffLevel = DEFAULT_CUTOFF_LEVEL;
060:
061: private void formatrb(MLevel l, String srcClass,
062: String srcMeth, String rbname, String msg,
063: Object[] params, Throwable t) {
064: ResourceBundle rb = ResourceBundle.getBundle(rbname);
065: if (msg != null && rb != null) {
066: String check = rb.getString(msg);
067: if (check != null)
068: msg = check;
069: }
070: format(l, srcClass, srcMeth, msg, params, t);
071: }
072:
073: private void format(MLevel l, String srcClass, String srcMeth,
074: String msg, Object[] params, Throwable t) {
075: System.err.println(formatString(l, srcClass, srcMeth, msg,
076: params, t));
077: }
078:
079: private String formatString(MLevel l, String srcClass,
080: String srcMeth, String msg, Object[] params, Throwable t) {
081: boolean add_parens = (srcMeth != null && !srcMeth
082: .endsWith(")"));
083:
084: StringBuffer sb = new StringBuffer(256);
085: sb.append(l.getLineHeader());
086: sb.append(' ');
087: if (srcClass != null && srcMeth != null) {
088: sb.append('[');
089: sb.append(srcClass);
090: sb.append('.');
091: sb.append(srcMeth);
092: if (add_parens)
093: sb.append("()");
094: sb.append(']');
095: } else if (srcClass != null) {
096: sb.append('[');
097: sb.append(srcClass);
098: sb.append(']');
099: } else if (srcMeth != null) {
100: sb.append('[');
101: sb.append(srcMeth);
102: if (add_parens)
103: sb.append("()");
104: sb.append(']');
105: }
106: if (msg == null) {
107: if (params != null) {
108: sb.append("params: ");
109: for (int i = 0, len = params.length; i < len; ++i) {
110: if (i != 0)
111: sb.append(", ");
112: sb.append(params[i]);
113: }
114: }
115: } else {
116: if (params == null)
117: sb.append(msg);
118: else {
119: MessageFormat mfmt = new MessageFormat(msg);
120: sb.append(mfmt.format(params));
121: }
122: }
123:
124: if (t != null)
125: sb.append(ThrowableUtils.extractStackTrace(t));
126:
127: return sb.toString();
128: }
129:
130: public ResourceBundle getResourceBundle() {
131: //warn("Using logger " + this.getClass().getName() + ", which does not support ResourceBundles.");
132: return null;
133: }
134:
135: public String getResourceBundleName() {
136: return null;
137: }
138:
139: public void setFilter(Object java14Filter)
140: throws SecurityException {
141: warning("Using FallbackMLog -- Filters not supported!");
142: }
143:
144: public Object getFilter() {
145: return null;
146: }
147:
148: public void log(MLevel l, String msg) {
149: if (isLoggable(l))
150: format(l, null, null, msg, null, null);
151: }
152:
153: public void log(MLevel l, String msg, Object param) {
154: if (isLoggable(l))
155: format(l, null, null, msg, new Object[] { param }, null);
156: }
157:
158: public void log(MLevel l, String msg, Object[] params) {
159: if (isLoggable(l))
160: format(l, null, null, msg, params, null);
161: }
162:
163: public void log(MLevel l, String msg, Throwable t) {
164: if (isLoggable(l))
165: format(l, null, null, msg, null, t);
166: }
167:
168: public void logp(MLevel l, String srcClass, String srcMeth,
169: String msg) {
170: if (isLoggable(l))
171: format(l, srcClass, srcMeth, msg, null, null);
172: }
173:
174: public void logp(MLevel l, String srcClass, String srcMeth,
175: String msg, Object param) {
176: if (isLoggable(l))
177: format(l, srcClass, srcMeth, msg,
178: new Object[] { param }, null);
179: }
180:
181: public void logp(MLevel l, String srcClass, String srcMeth,
182: String msg, Object[] params) {
183: if (isLoggable(l))
184: format(l, srcClass, srcMeth, msg, params, null);
185: }
186:
187: public void logp(MLevel l, String srcClass, String srcMeth,
188: String msg, Throwable t) {
189: if (isLoggable(l))
190: format(l, srcClass, srcMeth, msg, null, t);
191: }
192:
193: public void logrb(MLevel l, String srcClass, String srcMeth,
194: String rb, String msg) {
195: if (isLoggable(l))
196: formatrb(l, srcClass, srcMeth, rb, msg, null, null);
197: }
198:
199: public void logrb(MLevel l, String srcClass, String srcMeth,
200: String rb, String msg, Object param) {
201: if (isLoggable(l))
202: formatrb(l, srcClass, srcMeth, rb, msg,
203: new Object[] { param }, null);
204: }
205:
206: public void logrb(MLevel l, String srcClass, String srcMeth,
207: String rb, String msg, Object[] params) {
208: if (isLoggable(l))
209: formatrb(l, srcClass, srcMeth, rb, msg, params, null);
210: }
211:
212: public void logrb(MLevel l, String srcClass, String srcMeth,
213: String rb, String msg, Throwable t) {
214: if (isLoggable(l))
215: formatrb(l, srcClass, srcMeth, rb, msg, null, t);
216: }
217:
218: public void entering(String srcClass, String srcMeth) {
219: if (isLoggable(MLevel.FINER))
220: format(MLevel.FINER, srcClass, srcMeth,
221: "Entering method.", null, null);
222: }
223:
224: public void entering(String srcClass, String srcMeth,
225: Object param) {
226: if (isLoggable(MLevel.FINER))
227: format(MLevel.FINER, srcClass, srcMeth,
228: "Entering method with argument " + param, null,
229: null);
230: }
231:
232: public void entering(String srcClass, String srcMeth,
233: Object[] params) {
234: if (isLoggable(MLevel.FINER)) {
235: if (params == null)
236: entering(srcClass, srcMeth);
237: else {
238: StringBuffer sb = new StringBuffer(128);
239: sb.append("( ");
240: for (int i = 0, len = params.length; i < len; ++i) {
241: if (i != 0)
242: sb.append(", ");
243: sb.append(params[i]);
244: }
245: sb.append(" )");
246: format(MLevel.FINER, srcClass, srcMeth,
247: "Entering method with arguments "
248: + sb.toString(), null, null);
249: }
250: }
251: }
252:
253: public void exiting(String srcClass, String srcMeth) {
254: if (isLoggable(MLevel.FINER))
255: format(MLevel.FINER, srcClass, srcMeth,
256: "Exiting method.", null, null);
257: }
258:
259: public void exiting(String srcClass, String srcMeth,
260: Object result) {
261: if (isLoggable(MLevel.FINER))
262: format(MLevel.FINER, srcClass, srcMeth,
263: "Exiting method with result " + result, null,
264: null);
265: }
266:
267: public void throwing(String srcClass, String srcMeth,
268: Throwable t) {
269: if (isLoggable(MLevel.FINE))
270: format(MLevel.FINE, srcClass, srcMeth,
271: "Throwing exception.", null, t);
272: }
273:
274: public void severe(String msg) {
275: if (isLoggable(MLevel.SEVERE))
276: format(MLevel.SEVERE, null, null, msg, null, null);
277: }
278:
279: public void warning(String msg) {
280: if (isLoggable(MLevel.WARNING))
281: format(MLevel.WARNING, null, null, msg, null, null);
282: }
283:
284: public void info(String msg) {
285: if (isLoggable(MLevel.INFO))
286: format(MLevel.INFO, null, null, msg, null, null);
287: }
288:
289: public void config(String msg) {
290: if (isLoggable(MLevel.CONFIG))
291: format(MLevel.CONFIG, null, null, msg, null, null);
292: }
293:
294: public void fine(String msg) {
295: if (isLoggable(MLevel.FINE))
296: format(MLevel.FINE, null, null, msg, null, null);
297: }
298:
299: public void finer(String msg) {
300: if (isLoggable(MLevel.FINER))
301: format(MLevel.FINER, null, null, msg, null, null);
302: }
303:
304: public void finest(String msg) {
305: if (isLoggable(MLevel.FINEST))
306: format(MLevel.FINEST, null, null, msg, null, null);
307: }
308:
309: public void setLevel(MLevel l) throws SecurityException {
310: this .cutoffLevel = l;
311: }
312:
313: public synchronized MLevel getLevel() {
314: return cutoffLevel;
315: }
316:
317: public synchronized boolean isLoggable(MLevel l) {
318: return (l.intValue() >= cutoffLevel.intValue());
319: }
320:
321: public String getName() {
322: return "global";
323: }
324:
325: public void addHandler(Object h) throws SecurityException {
326: warning("Using FallbackMLog -- Handlers not supported.");
327: }
328:
329: public void removeHandler(Object h) throws SecurityException {
330: warning("Using FallbackMLog -- Handlers not supported.");
331: }
332:
333: public Object[] getHandlers() {
334: warning("Using FallbackMLog -- Handlers not supported.");
335: return new Object[0];
336: }
337:
338: public void setUseParentHandlers(boolean uph) {
339: warning("Using FallbackMLog -- Handlers not supported.");
340: }
341:
342: public boolean getUseParentHandlers() {
343: return false;
344: }
345: }
346: }
|