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.util.List;
026: import java.util.ArrayList;
027: import com.mchange.v1.util.StringTokenizerUtils;
028: import com.mchange.v2.cfg.MultiPropertiesConfig;
029:
030: public abstract class MLog {
031: final static NameTransformer transformer;
032: final static MLog mlog;
033:
034: final static MultiPropertiesConfig CONFIG;
035:
036: final static MLogger logger;
037:
038: static {
039: String[] defaults = new String[] {
040: "/com/mchange/v2/log/default-mchange-log.properties",
041: "/mchange-log.properties", "/" };
042: CONFIG = MultiPropertiesConfig.readVmConfig(defaults, null);
043:
044: String classnamesStr = CONFIG
045: .getProperty("com.mchange.v2.log.MLog");
046: String[] classnames = null;
047: if (classnamesStr == null)
048: classnamesStr = CONFIG
049: .getProperty("com.mchange.v2.log.mlog");
050: if (classnamesStr != null)
051: classnames = StringTokenizerUtils.tokenizeToArray(
052: classnamesStr, ", \t\r\n");
053:
054: boolean warn = false;
055: MLog tmpml = null;
056: if (classnames != null)
057: tmpml = findByClassnames(classnames);
058: if (tmpml == null)
059: tmpml = findByClassnames(MLogClasses.CLASSNAMES);
060: if (tmpml == null) {
061: warn = true;
062: tmpml = new FallbackMLog();
063: }
064: mlog = tmpml;
065: if (warn)
066: info("Using "
067: + mlog.getClass().getName()
068: + " -- Named logger's not supported, everything goes to System.err.");
069:
070: logger = mlog.getLogger(MLog.class);
071: String loggerDesc = mlog.getClass().getName();
072: if ("com.mchange.v2.log.jdk14logging.Jdk14MLog"
073: .equals(loggerDesc))
074: loggerDesc = "java 1.4+ standard";
075: else if ("com.mchange.v2.log.log4j.Log4jMLog"
076: .equals(loggerDesc))
077: loggerDesc = "log4j";
078:
079: if (logger.isLoggable(MLevel.INFO))
080: logger.log(MLevel.INFO, "MLog clients using " + loggerDesc
081: + " logging.");
082:
083: NameTransformer tmpt = null;
084: String tClassName = CONFIG
085: .getProperty("com.mchange.v2.log.NameTransformer");
086: if (tClassName == null)
087: tClassName = CONFIG
088: .getProperty("com.mchange.v2.log.nametransformer");
089: try {
090: if (tClassName != null)
091: tmpt = (NameTransformer) Class.forName(tClassName)
092: .newInstance();
093: } catch (Exception e) {
094: System.err
095: .println("Failed to instantiate com.mchange.v2.log.NameTransformer '"
096: + tClassName + "'!");
097: e.printStackTrace();
098: }
099: transformer = tmpt;
100:
101: //System.err.println(mlog);
102: }
103:
104: public static MLog findByClassnames(String[] classnames) {
105: List attempts = null;
106: for (int i = 0, len = classnames.length; i < len; ++i) {
107: try {
108: return (MLog) Class.forName(classnames[i])
109: .newInstance();
110: } catch (Exception e) {
111: if (attempts == null)
112: attempts = new ArrayList();
113: attempts.add(classnames[i]);
114: // System.err.println("com.mchange.v2.log.MLog '" + classnames[i] + "' could not be loaded!");
115: // e.printStackTrace();
116: }
117: }
118: System.err
119: .println("Tried without success to load the following MLog classes:");
120: for (int i = 0, len = attempts.size(); i < len; ++i)
121: System.err.println("\t" + attempts.get(i));
122: return null;
123: }
124:
125: public static MLog instance() {
126: return mlog;
127: }
128:
129: public static MLogger getLogger(String name) {
130: MLogger out;
131: if (transformer == null)
132: out = instance().getMLogger(name);
133: else {
134: String xname = transformer.transformName(name);
135: if (xname != null)
136: out = instance().getMLogger(xname);
137: else
138: out = instance().getMLogger(name);
139: }
140: return out;
141: }
142:
143: public static MLogger getLogger(Class cl) {
144: MLogger out;
145: if (transformer == null)
146: out = instance().getMLogger(cl);
147: else {
148: String xname = transformer.transformName(cl);
149: if (xname != null)
150: out = instance().getMLogger(xname);
151: else
152: out = instance().getMLogger(cl);
153: }
154: return out;
155: }
156:
157: public static MLogger getLogger() {
158: MLogger out;
159: if (transformer == null)
160: out = instance().getMLogger();
161: else {
162: String xname = transformer.transformName();
163: if (xname != null)
164: out = instance().getMLogger(xname);
165: else
166: out = instance().getMLogger();
167: }
168: return out;
169: }
170:
171: public static void log(MLevel l, String msg) {
172: instance().getLogger().log(l, msg);
173: }
174:
175: public static void log(MLevel l, String msg, Object param) {
176: instance().getLogger().log(l, msg, param);
177: }
178:
179: public static void log(MLevel l, String msg, Object[] params) {
180: instance().getLogger().log(l, msg, params);
181: }
182:
183: public static void log(MLevel l, String msg, Throwable t) {
184: instance().getLogger().log(l, msg, t);
185: }
186:
187: public static void logp(MLevel l, String srcClass, String srcMeth,
188: String msg) {
189: instance().getLogger().logp(l, srcClass, srcMeth, msg);
190: }
191:
192: public static void logp(MLevel l, String srcClass, String srcMeth,
193: String msg, Object param) {
194: instance().getLogger().logp(l, srcClass, srcMeth, msg, param);
195: }
196:
197: public static void logp(MLevel l, String srcClass, String srcMeth,
198: String msg, Object[] params) {
199: instance().getLogger().logp(l, srcClass, srcMeth, msg, params);
200: }
201:
202: public static void logp(MLevel l, String srcClass, String srcMeth,
203: String msg, Throwable t) {
204: instance().getLogger().logp(l, srcClass, srcMeth, msg, t);
205: }
206:
207: public static void logrb(MLevel l, String srcClass, String srcMeth,
208: String rb, String msg) {
209: instance().getLogger().logp(l, srcClass, srcMeth, rb, msg);
210: }
211:
212: public static void logrb(MLevel l, String srcClass, String srcMeth,
213: String rb, String msg, Object param) {
214: instance().getLogger().logrb(l, srcClass, srcMeth, rb, msg,
215: param);
216: }
217:
218: public static void logrb(MLevel l, String srcClass, String srcMeth,
219: String rb, String msg, Object[] params) {
220: instance().getLogger().logrb(l, srcClass, srcMeth, rb, msg,
221: params);
222: }
223:
224: public static void logrb(MLevel l, String srcClass, String srcMeth,
225: String rb, String msg, Throwable t) {
226: instance().getLogger().logrb(l, srcClass, srcMeth, rb, msg, t);
227: }
228:
229: public static void entering(String srcClass, String srcMeth) {
230: instance().getLogger().entering(srcClass, srcMeth);
231: }
232:
233: public static void entering(String srcClass, String srcMeth,
234: Object param) {
235: instance().getLogger().entering(srcClass, srcMeth, param);
236: }
237:
238: public static void entering(String srcClass, String srcMeth,
239: Object params[]) {
240: instance().getLogger().entering(srcClass, srcMeth, params);
241: }
242:
243: public static void exiting(String srcClass, String srcMeth) {
244: instance().getLogger().exiting(srcClass, srcMeth);
245: }
246:
247: public static void exiting(String srcClass, String srcMeth,
248: Object result) {
249: instance().getLogger().exiting(srcClass, srcMeth, result);
250: }
251:
252: public static void throwing(String srcClass, String srcMeth,
253: Throwable t) {
254: instance().getLogger().throwing(srcClass, srcMeth, t);
255: }
256:
257: public static void severe(String msg) {
258: instance().getLogger().severe(msg);
259: }
260:
261: public static void warning(String msg) {
262: instance().getLogger().warning(msg);
263: }
264:
265: public static void info(String msg) {
266: instance().getLogger().info(msg);
267: }
268:
269: public static void config(String msg) {
270: instance().getLogger().config(msg);
271: }
272:
273: public static void fine(String msg) {
274: instance().getLogger().fine(msg);
275: }
276:
277: public static void finer(String msg) {
278: instance().getLogger().finer(msg);
279: }
280:
281: public static void finest(String msg) {
282: instance().getLogger().finest(msg);
283: }
284:
285: public abstract MLogger getMLogger(String name);
286:
287: public abstract MLogger getMLogger(Class cl);
288:
289: public abstract MLogger getMLogger();
290: }
|