001: ///////////////////////////////////////////////////////////////////////////////
002: //
003: // Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
004: //
005: // All Rights Reserved
006: //
007: // This program is free software; you can redistribute it and/or modify
008: // it under the terms of the GNU General Public License and GNU Library
009: // General Public License as published by the Free Software Foundation;
010: // either version 2, or (at your option) any later version.
011: //
012: // This program 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 General Public License and GNU Library General Public License
016: // for more details.
017: //
018: // You should have received a copy of the GNU General Public License
019: // and GNU Library General Public License along with this program; if
020: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
021: // MA 02139, USA.
022: //
023: ///////////////////////////////////////////////////////////////////////////////
024: package org.myoodb.util;
025:
026: public class Logger {
027: private static volatile boolean s_configured = false;
028: private static volatile Object s_globalLogger = null;
029:
030: private static volatile boolean s_infoEnabledFlag = true;
031: private static volatile boolean s_debugEnabledFlag = false;
032: private static volatile boolean s_traceEnabledFlag = false;
033:
034: private static volatile java.lang.reflect.Method s_info = null;
035: private static volatile java.lang.reflect.Method s_debug = null;
036: private static volatile java.lang.reflect.Method s_trace = null;
037: private static volatile java.lang.reflect.Method s_warn = null;
038: private static volatile java.lang.reflect.Method s_error = null;
039:
040: private static volatile java.lang.reflect.Method s_infoEnabled = null;
041: private static volatile java.lang.reflect.Method s_debugEnabled = null;
042: private static volatile java.lang.reflect.Method s_traceEnabled = null;
043:
044: public static Logger getLogger(Class classType) {
045: return new Logger(classType.getName());
046: }
047:
048: public static Logger getLogger(String classType) {
049: return new Logger(classType);
050: }
051:
052: public static void setInfoEnabled(boolean flag) {
053: s_infoEnabledFlag = flag;
054: }
055:
056: public static void setDebugEnabled(boolean flag) {
057: s_debugEnabledFlag = flag;
058: }
059:
060: public static void setTraceEnabled(boolean flag) {
061: s_traceEnabledFlag = flag;
062: }
063:
064: private Object s_logger = null;
065:
066: private Logger(String classType)
067: {
068: try
069: {
070: if (s_configured == false)
071: {
072: synchronized(Logger.class)
073: {
074: if (s_configured == false)
075: {
076: s_configured = true;
077:
078: if (@initializeLog4j@ == true)
079: {
080: Class loggerClass = ClassLoader.getSystemClassLoader().loadClass("org.apache.log4j.BasicConfigurator");
081: if (loggerClass != null)
082: {
083: java.lang.reflect.Method[] methods = loggerClass.getMethods();
084: for (int i = 0; i < methods.length; i++)
085: {
086: if (methods[i].getName().equals("configure") == true)
087: {
088: java.lang.reflect.Method method = methods[i];
089: if (method.getParameterTypes().length == 0)
090: {
091: method.invoke(null, (java.lang.Object[]) null);
092: }
093: }
094: }
095: }
096: }
097:
098: Class loggerClass = ClassLoader.getSystemClassLoader().loadClass("org.apache.log4j.Logger");
099: if (loggerClass != null)
100: {
101: java.lang.reflect.Method[] methods = loggerClass.getMethods();
102: for (int i = 0; i < methods.length; i++)
103: {
104: if (methods[i].getParameterTypes().length == 0)
105: {
106: if (methods[i].getName().equals("isInfoEnabled") == true)
107: {
108: s_infoEnabled = methods[i];
109: }
110: else if (methods[i].getName().equals("isDebugEnabled") == true)
111: {
112: s_debugEnabled = methods[i];
113: }
114: else if (methods[i].getName().equals("isTraceEnabled") == true)
115: {
116: s_traceEnabled = methods[i];
117: }
118: }
119: else if (methods[i].getParameterTypes().length == 1)
120: {
121: Class paramType = methods[i].getParameterTypes()[0];
122: if (Object.class.equals(paramType) == true)
123: {
124: if (methods[i].getName().equals("info") == true)
125: {
126: s_info = methods[i];
127: }
128: else if (methods[i].getName().equals("debug") == true)
129: {
130: s_debug = methods[i];
131: }
132: else if (methods[i].getName().equals("trace") == true)
133: {
134: s_trace = methods[i];
135: }
136: else if (methods[i].getName().equals("warn") == true)
137: {
138: s_warn = methods[i];
139: }
140: else if (methods[i].getName().equals("error") == true)
141: {
142: s_error = methods[i];
143: }
144: }
145: }
146: }
147: }
148: }
149: }
150: }
151: }
152: catch (Exception e)
153: {
154: // nothing to do
155: }
156:
157: try
158: {
159: Class loggerClass = ClassLoader.getSystemClassLoader().loadClass("org.apache.log4j.Logger");
160: if (loggerClass != null)
161: {
162: java.lang.reflect.Method[] methods = loggerClass.getMethods();
163: for (int i = 0; i < methods.length; i++)
164: {
165: if (methods[i].getName().equals("getLogger") == true)
166: {
167: java.lang.reflect.Method method = methods[i];
168: if (method.getParameterTypes().length == 1)
169: {
170: Class paramType = method.getParameterTypes()[0];
171: if (String.class.equals(paramType) == true)
172: {
173: s_logger = method.invoke(null, new Object[] {classType});
174: break;
175: }
176: }
177: }
178: }
179: }
180: }
181: catch (java.lang.Exception e)
182: {
183: if (s_globalLogger == null)
184: {
185: synchronized(Logger.class)
186: {
187: if (s_globalLogger == null)
188: {
189: java.lang.reflect.Method[] methods = java.io.PrintStream.class.getMethods();
190: for (int i = 0; i < methods.length; i++)
191: {
192: if (methods[i].getParameterTypes().length == 1)
193: {
194: Class paramType = methods[i].getParameterTypes()[0];
195: if (Object.class.equals(paramType) == true)
196: {
197: if (methods[i].getName().equals("println") == true)
198: {
199: s_info = methods[i];
200: s_debug = methods[i];
201: s_trace = methods[i];
202: s_warn = methods[i];
203: s_error = methods[i];
204: break;
205: }
206: }
207: }
208: }
209:
210: s_globalLogger = System.out;
211: }
212: }
213: }
214:
215: s_logger = s_globalLogger;
216: }
217: }
218:
219: private String trace(Throwable t) {
220: String trace = "";
221: java.io.StringWriter stringWriter = new java.io.StringWriter();
222: java.io.PrintWriter printWriter = new java.io.PrintWriter(
223: stringWriter);
224:
225: t.printStackTrace(printWriter);
226: String ste[] = stringWriter.toString().split("\n");
227:
228: for (int i = 0; i < ste.length; i++) {
229: String line = ste[i].toString();
230: trace += line + "\n";
231: }
232:
233: return trace;
234: }
235:
236: public void info(Object message) {
237: try {
238: s_info.invoke(s_logger, new Object[] { message });
239: } catch (java.lang.Exception e) {
240: e.printStackTrace();
241: }
242: }
243:
244: public void info(Object message, Throwable t) {
245: try {
246: if (message != null) {
247: s_info.invoke(s_logger, new Object[] { message });
248: }
249:
250: if (t != null) {
251: s_info.invoke(s_logger, new Object[] { trace(t) });
252: }
253: } catch (java.lang.Exception e) {
254: e.printStackTrace();
255: }
256: }
257:
258: public void debug(Object message) {
259: try {
260: s_debug.invoke(s_logger, new Object[] { message });
261: } catch (java.lang.Exception e) {
262: e.printStackTrace();
263: }
264: }
265:
266: public void debug(Object message, Throwable t) {
267: try {
268: if (message != null) {
269: s_debug.invoke(s_logger, new Object[] { message });
270: }
271:
272: if (t != null) {
273: s_debug.invoke(s_logger, new Object[] { trace(t) });
274: }
275: } catch (java.lang.Exception e) {
276: e.printStackTrace();
277: }
278: }
279:
280: public void trace(Object message) {
281: try {
282: s_trace.invoke(s_logger, new Object[] { message });
283: } catch (java.lang.Exception e) {
284: e.printStackTrace();
285: }
286: }
287:
288: public void trace(Object message, Throwable t) {
289: try {
290: if (message != null) {
291: s_trace.invoke(s_logger, new Object[] { message });
292: }
293:
294: if (t != null) {
295: s_trace.invoke(s_logger, new Object[] { trace(t) });
296: }
297: } catch (java.lang.Exception e) {
298: e.printStackTrace();
299: }
300: }
301:
302: public void warn(Object message) {
303: try {
304: s_warn.invoke(s_logger, new Object[] { message });
305: } catch (java.lang.Exception e) {
306: e.printStackTrace();
307: }
308: }
309:
310: public void warn(Object message, Throwable t) {
311: try {
312: if (message != null) {
313: s_warn.invoke(s_logger, new Object[] { message });
314: }
315:
316: if (t != null) {
317: s_warn.invoke(s_logger, new Object[] { trace(t) });
318: }
319: } catch (java.lang.Exception e) {
320: e.printStackTrace();
321: }
322: }
323:
324: public void error(Object message) {
325: try {
326: s_error.invoke(s_logger, new Object[] { message });
327: } catch (java.lang.Exception e) {
328: e.printStackTrace();
329: }
330: }
331:
332: public void error(Object message, Throwable t) {
333: try {
334: if (message != null) {
335: s_error.invoke(s_logger, new Object[] { message });
336: }
337:
338: if (t != null) {
339: s_error.invoke(s_logger, new Object[] { trace(t) });
340: }
341: } catch (java.lang.Exception e) {
342: e.printStackTrace();
343: }
344: }
345:
346: public boolean isInfoEnabled() {
347: try {
348: return (s_infoEnabled != null) ? (Boolean) s_infoEnabled
349: .invoke(s_logger, (java.lang.Object[]) null)
350: : s_infoEnabledFlag;
351: } catch (Exception e) {
352: return false;
353: }
354: }
355:
356: public boolean isDebugEnabled() {
357: try {
358: return (s_debugEnabled != null) ? (Boolean) s_debugEnabled
359: .invoke(s_logger, (java.lang.Object[]) null)
360: : s_debugEnabledFlag;
361: } catch (Exception e) {
362: return false;
363: }
364: }
365:
366: public boolean isTraceEnabled() {
367: try {
368: return (s_traceEnabled != null) ? (Boolean) s_traceEnabled
369: .invoke(s_logger, (java.lang.Object[]) null)
370: : s_traceEnabledFlag;
371: } catch (Exception e) {
372: return false;
373: }
374: }
375: }
|