KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > context > LogContext


1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2006 - Javolution (http://javolution.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package javolution.context;
10
11 import javolution.util.StandardLog;
12 import j2me.lang.CharSequence;
13
14 /**
15  * <p> This class represents a logging context; it allows for
16  * object-based/thread-based logging and logging specializations
17  * (such as {@link StandardLog StandardLog} to leverage
18  * <code>java.util.logging</code> capabilities).</p>
19  *
20  * <p> The default logging context is {@link #STANDARD} (log events forwarded
21  * to the root <code>java.util.logging.Logger</code>). Users may
22  * changes the default logging for all threads:[code]
23  * // Logging disabled by default.
24  * LogContext.setDefault(LogContext.NULL);
25  * [/code]
26  * and/or perform custom logging for particular objects/threads:[code]
27  * LogContext.enter(LogContext.SYSTEM);
28  * try {
29  * LogContext.info("Writes this message to System.out");
30  * ...
31  * } finally {
32  * LogContext.exit(LogContext.SYSTEM);
33  * }[/code]</p>
34  *
35  * <p> Applications may extend this base class to address specific logging
36  * requirements. For example:[code]
37  * // This class allows for custom logging of session events.
38  * public abstract class SessionLog extends LogContext {
39  * public static void start(Session session) {
40  * LogContext log = LogContext.current();
41  * if (log instanceof SessionLog.Loggable) {
42  * ((SessionLog.Loggable)log).logStart(session);
43  * } else if (log.isInfoLogged()) {
44  * log.logInfo("Session " + session.id() + " started");
45  * }
46  * }
47  * public static void end(Session session) { ... }
48  * public interface Loggable {
49  * void logStart(Session session);
50  * void logEnd(Session session);
51  * }
52  * }[/code]</p>
53  *
54  * <p> The use of interfaces (such as <code>Loggable</code> above) makes it easy
55  * for any context to support customs logging events.
56  * For example:[code]
57  * class MyLog extends StandardLog implements SessionLog.Loggable, DatabaseLog.Loggable {
58  * ... // Specialized logging for session and database events.
59  * }
60  * MyLog myLog = new MyLog();
61  * LogContext.enter(myLog);
62  * try {
63  * ...
64  * LogContext.info("Informative message"); // Standard logging.
65  * ...
66  * DatabaseLog.fail(transaction); // Database custom logging.
67  * ...
68  * SessionLog.start(session); // Session custom logging.
69  * ...
70  * } finally {
71  * LogContext.exit(myLog);
72  * }[/code]</p>
73  *
74  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
75  * @version 3.6, November 16, 2005
76  */

77 public abstract class LogContext extends Context {
78     
79     /**
80      * Holds a context forwarding log events to the root <code>
81      * java.util.logging.Logger</code> framework (default logging context).
82      * The info/warning/error events are mapped to the info/warning/severe
83      * log levels respectively.
84      */

85     public static final StandardLog STANDARD = new StandardLog();
86
87     /**
88      * Holds a context ignoring logging events.
89      */

90     public static final LogContext NULL = new NullLog();
91
92     /**
93      * Holds a context logging informative messages to
94      * <code>System.out</code> and warnings/errors events to
95      * <code>System.err</code>.
96      */

97     public static final LogContext SYSTEM = new SystemLog();
98
99     /**
100      * Holds a context logging warnings/errors events to
101      * <code>System.err</code> and ignoring informative messages.
102      */

103     public static final LogContext SYSTEM_ERR = new SystemErrLog();
104
105     /**
106      * Holds the default logging contexts.
107      */

108     private static volatile LogContext Default = STANDARD;
109
110     /**
111      * Default constructor.
112      */

113     public LogContext() {
114     }
115
116     /**
117      * Returns the current logging context (or {@link #getDefault()}
118      * if the current thread has not entered any logging context).
119      *
120      * @return the current logging context.
121      */

122     public static/*LogContext*/Context current() {
123         for (Context ctx = Context.current(); ctx != null; ctx = ctx.getOuter()) {
124             if (ctx instanceof LogContext)
125                 return (LogContext) ctx;
126         }
127         return LogContext.Default;
128     }
129
130     /**
131      * Returns the default logging context for new threads
132      * ({@link #STANDARD} when not explicitly {@link #setDefault set}).
133      *
134      * @return the default logging contexts.
135      */

136     public static LogContext getDefault() {
137         return LogContext.Default;
138     }
139
140     /**
141      * Sets the specified logging context as default.
142      *
143      * @param defaultLog the default logging context.
144      */

145     public static void setDefault(LogContext defaultLog) {
146         LogContext.Default = defaultLog;
147     }
148
149     /**
150      * Logs the specified informative message.
151      *
152      * @param message the informative message being logged.
153      * @see #logInfo(CharSequence)
154      */

155     public static void info(CharSequence JavaDoc message) {
156         LogContext logContext = (LogContext) LogContext.current();
157         logContext.logInfo(message);
158     }
159
160     /**
161      * Logs the specified warning message to the current logging context.
162      *
163      * @param message the warning message being logged.
164      * @see #logWarning(CharSequence)
165      */

166     public static void warning(CharSequence JavaDoc message) {
167         LogContext logContext = (LogContext) LogContext.current();
168         logContext.logWarning(message);
169     }
170
171     /**
172      * Logs the specified error to the current logging context.
173      *
174      * @param error the error being logged.
175      */

176     public static void error(Throwable JavaDoc error) {
177         LogContext logContext = (LogContext) LogContext.current();
178         logContext.logError(error, null);
179     }
180
181     /**
182      * Logs the specified error and error message to the current logging
183      * context.
184      *
185      * @param error the error being logged.
186      * @param message the supplementary message or <code>null</code> if none.
187      */

188     public static void error(Throwable JavaDoc error, CharSequence JavaDoc message) {
189         LogContext logContext = (LogContext) LogContext.current();
190         logContext.logError(error, message);
191     }
192
193     /**
194      * Indicates if informative messages are logged.
195      *
196      * @return <code>true</code> if informative messages are logged;
197      * <code>false</code> otherwise.
198      */

199     public abstract boolean isInfoLogged();
200
201     /**
202      * Logs the specified informative message.
203      *
204      * @param message the informative message being logged.
205      */

206     public abstract void logInfo(CharSequence JavaDoc message);
207
208     /**
209      * Indicates if warning messages are logged.
210      *
211      * @return <code>true</code> if warnings message are logged;
212      * <code>false</code> otherwise.
213      */

214     public abstract boolean isWarningLogged();
215     
216     /**
217      * Logs the specified warning message.
218      *
219      * @param message the warning message being logged.
220      */

221     public abstract void logWarning(CharSequence JavaDoc message);
222
223     /**
224      * Indicates if errors are logged.
225      *
226      * @return <code>true</code> if errors are logged;
227      * <code>false</code> otherwise.
228      */

229     public abstract boolean isErrorLogged();
230
231     /**
232      * Logs the specified error.
233      *
234      * @param error the error being logged.
235      * @param message the associated message or <code>null</code> if none.
236      */

237     public abstract void logError(Throwable JavaDoc error, CharSequence JavaDoc message);
238
239     // Implements Context abstract method.
240
protected void enterAction() {
241         // Do nothing.
242
}
243
244     // Implements Context abstract method.
245
protected void exitAction() {
246         // Do nothing.
247
}
248
249     /**
250      * This class represents a non-logging context.
251      */

252     private static class NullLog extends LogContext {
253
254         public boolean isInfoLogged() {
255             return false;
256         }
257
258         public boolean isWarningLogged() {
259             return false;
260         }
261
262         public boolean isErrorLogged() {
263             return false;
264         }
265
266         public void logInfo(CharSequence JavaDoc message) {
267             // Do nothing.
268
}
269
270         public void logWarning(CharSequence JavaDoc message) {
271             // Do nothing.
272
}
273
274         public void logError(Throwable JavaDoc error, CharSequence JavaDoc message) {
275             // Do nothing.
276
}
277     }
278
279     /**
280      * This class represents the system logging context.
281      */

282     private static class SystemLog extends LogContext {
283
284         public boolean isInfoLogged() {
285             return true;
286         }
287
288         public boolean isWarningLogged() {
289             return true;
290         }
291
292         public boolean isErrorLogged() {
293             return true;
294         }
295
296         public void logInfo(CharSequence JavaDoc message) {
297             System.out.print("[info] ");
298             System.out.println(message);
299         }
300
301         public void logWarning(CharSequence JavaDoc message) {
302             System.err.print("[warning] ");
303             System.err.println(message);
304         }
305
306         public void logError(Throwable JavaDoc error, CharSequence JavaDoc message) {
307             System.err.print("[error] ");
308             System.err.print(error.getClass().getName());
309             if (message != null) {
310                 System.err.print(" - ");
311                 System.err.println(message);
312             } else {
313                 String JavaDoc errorMessage = error.getMessage();
314                 if (errorMessage != null) {
315                     System.err.print(" - ");
316                     System.err.println(errorMessage);
317                 } else {
318                     System.err.println();
319                 }
320             }
321             error.printStackTrace();
322         }
323     }
324
325     /**
326      * This class represents the system error logging context.
327      */

328     private static class SystemErrLog extends LogContext {
329
330         public boolean isInfoLogged() {
331             return false;
332         }
333
334         public boolean isWarningLogged() {
335             return true;
336         }
337
338         public boolean isErrorLogged() {
339             return true;
340         }
341
342         public void logInfo(CharSequence JavaDoc message) {
343             // Do nothing.
344
}
345
346         public void logWarning(CharSequence JavaDoc message) {
347             System.err.print("[warning] ");
348             System.err.println(message);
349         }
350
351         public void logError(Throwable JavaDoc error, CharSequence JavaDoc message) {
352             System.err.print("[error] ");
353             System.err.print(error.getClass().getName());
354             if (message != null) {
355                 System.err.print(" - ");
356                 System.err.println(message);
357             } else {
358                 String JavaDoc errorMessage = error.getMessage();
359                 if (errorMessage != null) {
360                     System.err.print(" - ");
361                     System.err.println(errorMessage);
362                 } else {
363                     System.err.println();
364                 }
365             }
366             error.printStackTrace();
367         }
368     }
369 }
Popular Tags