001: // @@
002: // @@
003: /*
004: * Wi.Ser Framework
005: *
006: * Version: 1.8.1, 20-September-2007
007: * Copyright (C) 2005 Dirk von der Weiden <dvdw@imail.de>
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library located in LGPL.txt in the
021: * license directory; if not, write to the
022: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
023: * Boston, MA 02111-1307, USA.
024: *
025: * If this agreement does not cover your requirements, please contact us
026: * via email to get detailed information about the commercial license
027: * or our service offerings!
028: *
029: */
030: // @@
031: package de.ug2t.kernel;
032:
033: import java.io.*;
034: import java.text.*;
035: import java.util.*;
036:
037: class KeBackTrace extends Exception {
038: };
039:
040: public class KeLog {
041: private static KeLog pem_log = null;
042: private static String pem_logTpl = "[$TIME][$THREAD][$APPL][$PRIO][$MESS]@[$CLASS->$OBJ]$STACK";
043:
044: public static final int SILENT = 0;
045: public static final int TRACE = 1;
046: public static final int DEBUG = 2;
047: public static final int STATISTICS = 3;
048: public static final int MESSAGE = 4;
049: public static final int ERROR = 5;
050: public static final int APPL = 6;
051: public static final int FATAL = 7;
052:
053: private final static SimpleDateFormat pem_formater = new SimpleDateFormat();
054: private final static Map pem_classes = Collections
055: .synchronizedMap(new HashMap());
056: public static ArrayList pem_alerters = null;
057:
058: private static IKeLogWriter pem_writer = new StdOutLogWriter();
059: private static int pem_prio = KeLog.MESSAGE;
060:
061: private void pemf_logMethod(String xAppl, String xMessage,
062: Object xReporter, int xPrio) {
063: String l_helper = null;
064: String l_out = KeLog.pem_logTpl;
065: Exception l_exc = null;
066: ByteArrayOutputStream l_as = null;
067:
068: // String zusammenbauen
069: Date l_date = new Date();
070:
071: l_out = KeTools.pcmf_stringSubst(l_out, "$THREAD", Thread
072: .currentThread().getName());
073: l_out = KeTools.pcmf_stringSubst(l_out, "$APPL", xAppl);
074: l_out = KeTools.pcmf_stringSubst(l_out, "$TIME", pem_formater
075: .format(l_date));
076:
077: switch (xPrio) {
078: case KeLog.ERROR:
079: l_out = KeTools.pcmf_stringSubst(l_out, "$PRIO", "ERROR");
080: break;
081: case KeLog.APPL:
082: l_out = KeTools.pcmf_stringSubst(l_out, "$PRIO", "APPL");
083: break;
084: case KeLog.FATAL:
085: l_out = KeTools.pcmf_stringSubst(l_out, "$PRIO", "FATAL");
086: l_exc = new KeBackTrace();
087: l_as = new ByteArrayOutputStream();
088: l_exc.printStackTrace(new PrintStream(l_as));
089: l_helper = l_as.toString();
090: l_out = KeTools.pcmf_stringSubst(l_out, "$STACK", ":"
091: + l_helper);
092: break;
093: case KeLog.STATISTICS:
094: l_out = KeTools.pcmf_stringSubst(l_out, "$PRIO",
095: "STATISTICS");
096: break;
097: case KeLog.MESSAGE:
098: l_out = KeTools.pcmf_stringSubst(l_out, "$PRIO", "MESSAGE");
099: break;
100: case KeLog.DEBUG:
101: l_out = KeTools.pcmf_stringSubst(l_out, "$PRIO", "DEBUG");
102: break;
103: case KeLog.TRACE:
104: l_out = KeTools.pcmf_stringSubst(l_out, "$PRIO", "TRACE");
105: l_exc = new KeBackTrace();
106: l_as = new ByteArrayOutputStream();
107: l_exc.printStackTrace(new PrintStream(l_as));
108: l_helper = l_as.toString();
109: l_out = KeTools.pcmf_stringSubst(l_out, "$STACK", ":"
110: + l_helper);
111: break;
112: default:
113: l_out = KeTools.pcmf_stringSubst(l_out, "$PRIO", "CUSTOM");
114:
115: try {
116: if (l_as != null)
117: l_as.close();
118: } catch (Exception e) {
119: System.out.println("ERROR in logging system");
120: }
121: }
122: ;
123:
124: l_out = KeTools.pcmf_stringSubst(l_out, "$MESS", xMessage);
125:
126: if (xReporter == null) {
127: l_out = KeTools.pcmf_stringSubst(l_out, "$CLASS", "static");
128: l_out = KeTools.pcmf_stringSubst(l_out, "$OBJ", "static");
129: } else {
130: l_out = KeTools.pcmf_stringSubst(l_out, "$CLASS", xReporter
131: .getClass().toString());
132: if (xReporter instanceof KeRegisteredObject)
133: l_out = KeTools.pcmf_stringSubst(l_out, "$OBJ",
134: ((KeRegisteredObject) xReporter)
135: .pcmf_getObjName());
136: else
137: l_out = KeTools.pcmf_stringSubst(l_out, "$OBJ", "HASH:"
138: + Integer.toString(xReporter.hashCode()));
139: }
140:
141: l_out = KeTools.pcmf_stringSubst(l_out, "$STACK", "");
142:
143: if (KeLog.pem_alerters != null) {
144: Iterator l_it = KeLog.pem_alerters.iterator();
145: while (l_it.hasNext()) {
146: switch (xPrio) {
147: case KeLog.ERROR:
148: ((IKeLogAlerter) l_it.next())
149: .pcmf_reportError(l_out);
150: break;
151: case KeLog.APPL:
152: ((IKeLogAlerter) l_it.next())
153: .pcmf_reportAppl(l_out);
154: break;
155: case KeLog.FATAL:
156: ((IKeLogAlerter) l_it.next())
157: .pcmf_reportFatal(l_out);
158: break;
159: case KeLog.MESSAGE:
160: ((IKeLogAlerter) l_it.next())
161: .pcmf_reportMessage(l_out);
162: break;
163: case KeLog.DEBUG:
164: ((IKeLogAlerter) l_it.next())
165: .pcmf_reportDebug(l_out);
166: break;
167: case KeLog.TRACE:
168: ((IKeLogAlerter) l_it.next())
169: .pcmf_reportTrace(l_out);
170: break;
171: }
172: }
173: }
174:
175: try {
176: KeLog.pem_writer.pcmf_log(l_out);
177: } catch (Exception e) {
178: System.out.println("ERROR in logging system");
179: }
180: return;
181: };
182:
183: public static final synchronized void pcmf_addLogAlerter(
184: IKeLogAlerter xLogAl) {
185: if (KeLog.pem_alerters == null)
186: KeLog.pem_alerters = new ArrayList();
187:
188: KeLog.pem_alerters.add(xLogAl);
189: }
190:
191: public static final synchronized void pcmf_removeLogAlerter(
192: IKeLogAlerter xLogAl) {
193: if (KeLog.pem_alerters == null)
194: return;
195:
196: KeLog.pem_alerters.remove(xLogAl);
197: if (KeLog.pem_alerters.size() == 0)
198: KeLog.pem_alerters = null;
199: }
200:
201: public final static boolean pcmf_testLog(int xPrio) {
202: if (xPrio >= pem_prio && pem_prio != KeLog.SILENT)
203: return (true);
204: else
205: return (false);
206: }
207:
208: public final synchronized static void pcmf_setLogWriter(
209: IKeLogWriter xWriter) {
210: KeLog.pcmf_log("ug2t", "log writer switched", null,
211: KeLog.MESSAGE);
212: KeLog.pem_writer = xWriter;
213: }
214:
215: public boolean pcmf_testLogPrio(int xPrio) {
216: return (xPrio >= pem_prio && pem_prio != KeLog.SILENT);
217: }
218:
219: public final synchronized static void pcmf_log(String xAppl,
220: String xMessage, Object xReporter, int xPrio) {
221: KeLog l_log = null;
222:
223: if (pem_log == null)
224: pem_log = new KeLog();
225:
226: if (xPrio >= pem_prio && pem_prio != KeLog.SILENT) {
227: if (xReporter == null || KeLog.pem_classes.size() == 0)
228: l_log = pem_log;
229: else {
230: Class l_class = xReporter.getClass();
231: do {
232: l_log = (KeLog) KeLog.pem_classes.get(l_class
233: .toString());
234: if (l_log != null)
235: break;
236: l_class = l_class.getSuperclass();
237: } while (l_class != null);
238: }
239:
240: if (l_log != null)
241: l_log.pemf_logMethod(xAppl, xMessage, xReporter, xPrio);
242: }
243: ;
244:
245: return;
246: };
247:
248: public final static void pcmf_logEnter(String xAppl,
249: Object xReporter) {
250: KeLog.pcmf_log(xAppl, "==> entering", xReporter, KeLog.TRACE);
251:
252: return;
253: };
254:
255: public final static void pcmf_logNotSupported(String xOp) {
256: KeLog.pcmf_log("ug2t", "operation not supported: " + xOp, null,
257: KeLog.DEBUG);
258:
259: return;
260: };
261:
262: public final static void pcmf_logNotSupportedFatal(String xOp) {
263: KeLog.pcmf_log("ug2t", "operation not supported: " + xOp, null,
264: KeLog.FATAL);
265:
266: return;
267: };
268:
269: public final static void pcmf_logNotSupportedFatal() {
270: KeLog.pcmf_log("ug2t",
271: "operation not supported - see stack trace", null,
272: KeLog.FATAL);
273:
274: return;
275: };
276:
277: public final static void pcmf_logLeave(String xAppl,
278: Object xReporter) {
279: KeLog.pcmf_log(xAppl, "<== leaving", xReporter, KeLog.TRACE);
280:
281: return;
282: };
283:
284: public final static void pcmf_logException(String xAppl,
285: Object xReporter, Exception e) {
286: String l_helper = null;
287: ByteArrayOutputStream l_as = null;
288:
289: l_as = new ByteArrayOutputStream();
290: e.printStackTrace(new PrintStream(l_as));
291: l_helper = l_as.toString();
292:
293: KeLog.pcmf_log(xAppl, l_helper, xReporter, KeLog.FATAL);
294:
295: try {
296: if (l_as != null)
297: l_as.close();
298: } catch (Exception le) {
299: System.out.println("ERROR in logging system");
300: }
301:
302: return;
303: };
304:
305: public final static void pcmf_logThrowable(String xAppl,
306: Object xReporter, Throwable e) {
307: String l_helper = null;
308: ByteArrayOutputStream l_as = null;
309:
310: l_as = new ByteArrayOutputStream();
311: e.printStackTrace(new PrintStream(l_as));
312: l_helper = l_as.toString();
313:
314: KeLog.pcmf_log(xAppl, l_helper, xReporter, KeLog.FATAL);
315:
316: try {
317: if (l_as != null)
318: l_as.close();
319: } catch (Exception le) {
320: System.out.println("ERROR in logging system");
321: }
322:
323: return;
324: };
325:
326: public final static void pcmf_setLogger(KeLog xLog) {
327: pem_log = xLog;
328:
329: return;
330: };
331:
332: public final static void pcmf_setPrio(int xPrio) {
333: pem_prio = xPrio;
334:
335: return;
336: };
337:
338: public final static void pcmf_setLogTpl(String xLogTpl) {
339: KeLog.pem_logTpl = xLogTpl;
340:
341: return;
342: };
343:
344: public final static void pcmf_addClass(Class xClass) {
345: if (pem_log == null)
346: pem_log = new KeLog();
347:
348: KeLog.pem_classes.put(xClass.toString(), KeLog.pem_log);
349:
350: return;
351: };
352:
353: public final static void pcmf_addClass(String xClass) {
354: if (pem_log == null)
355: pem_log = new KeLog();
356:
357: KeLog.pem_classes.put(xClass, KeLog.pem_log);
358:
359: return;
360: };
361:
362: public final static void pcmf_addClass(String xClass, KeLog xLog) {
363: KeLog.pem_classes.put(xClass, xLog);
364:
365: return;
366: };
367:
368: public final static void pcmf_addClass(Class xClass, KeLog xLog) {
369: KeLog.pem_classes.put(xClass.toString(), xLog);
370:
371: return;
372: };
373:
374: public final static void pcmf_removeClass(Class xClass) {
375: KeLog.pem_classes.remove(xClass.toString());
376:
377: return;
378: };
379: }
|