001: /*
002: * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
003: * Copyright (C) 2006 - Javolution (http://javolution.org/)
004: * All rights reserved.
005: *
006: * Permission to use, copy, modify, and distribute this software is
007: * freely granted, provided that this notice is preserved.
008: */
009: package javolution.util;
010:
011: import javolution.Javolution;
012: import javolution.context.LogContext;
013: import j2me.lang.CharSequence;
014: import j2me.util.logging.Level;
015: import j2me.util.logging.Logger;
016: import j2me.util.logging.LogRecord;
017:
018: /**
019: * <p> This class represents a specialized logging context forwarding events
020: * to a standard logger (<code>java.util.logging.Logger</code>).</p>
021: *
022: * <p> This class leverages the capabilities of the standard logging facility
023: * and extends it to support specialized {@link LogContext logging} on a
024: * thread or object basis. For example:[code]
025: * StandardLog remoteLog = new StandardLog(Logger.getLogger("remote"));
026: * StandardLog.enter(remoteLog);
027: * try {
028: * StandardLog.fine("Current thread uses a remote logger");
029: * ...
030: * } finally {
031: * StandardLog.exit(remoteLog); // Reverts to previous logging context.
032: * }[/code]</p>
033: *
034: *
035: * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
036: * @version 5.1, August 1, 2007
037: */
038: public class StandardLog extends LogContext {
039:
040: /**
041: * The logger associated to this logging context.
042: */
043: private Logger _logger;
044:
045: /**
046: * Creates a logging context forwarding events to the root logger
047: * (<code>Logger.getLogger("")</code>).
048: */
049: public StandardLog() {
050: this (Logger.getLogger(""));
051: }
052:
053: /**
054: * Creates a standard log context forwarding events to the specified
055: * logger.
056: *
057: * @param logger the logger to which log events are forwarded to.
058: */
059: public StandardLog(Logger logger) {
060: _logger = logger;
061: }
062:
063: /**
064: * Returns the logger to which this context forwards the events to.
065: *
066: * @return the logger for this standard logging context.
067: */
068: public final Logger getLogger() {
069: return _logger;
070: }
071:
072: /**
073: * Checks if a message of the given level would actually be logged
074: * by this logger.
075: *
076: * @param level the message logging level
077: * @return <code>true</code> if a message of specified level would actually
078: * be logged;<code>false</code> otherwise.
079: */
080: public static boolean isLoggable(Level level) {
081: LogContext log = (LogContext) LogContext.getCurrent();
082: if (log instanceof StandardLog) {
083: return ((StandardLog) log)._logger.isLoggable(level);
084: } else if (level.intValue() >= Level.WARNING.intValue()) {
085: return log.isWarningLogged();
086: } else if (level.intValue() >= Level.INFO.intValue()) {
087: return log.isInfoLogged();
088: } else {
089: return false;
090: }
091: }
092:
093: /**
094: * Log a specific LogRecord. If the current logging context is not a
095: * {@link StandardLog}, an {@link LogContext#logError error},
096: * {@link LogContext#logWarning warning} or
097: * {@link LogContext#logInfo info} is possibly logged.
098: *
099: * @param record the LogRecord to be published.
100: */
101: public static void log(LogRecord record) {
102: LogContext log = (LogContext) LogContext.getCurrent();
103: if (log instanceof StandardLog) {
104: ((StandardLog) log)._logger.log(record);
105: } else {
106: Throwable error = record.getThrown();
107: if (error != null) {
108: if (log.isErrorLogged()) {
109: log.logError(error, Javolution.j2meToCharSeq(record
110: .getMessage()));
111: }
112: } else if (record.getLevel().intValue() > Level.WARNING
113: .intValue()) {
114: if (log.isWarningLogged()) {
115: log.logWarning(Javolution.j2meToCharSeq(record
116: .getMessage()));
117: }
118: } else if (record.getLevel().intValue() > Level.INFO
119: .intValue()) {
120: if (log.isInfoLogged()) {
121: log.logInfo(Javolution.j2meToCharSeq(record
122: .getMessage()));
123: }
124: }
125: }
126: }
127:
128: /**
129: * Logs a {@link Level#SEVERE SEVERE} message. If the current logging
130: * context is not a {@link StandardLog} a {@link LogContext#warning
131: * warning} message is logged.
132: *
133: * @param msg the severe message.
134: */
135: public static void severe(String msg) {
136: LogContext log = (LogContext) LogContext.getCurrent();
137: if (log instanceof StandardLog) {
138: ((StandardLog) log)._logger.severe(msg);
139: } else if (log.isWarningLogged()) {
140: log.logWarning(Javolution.j2meToCharSeq(msg));
141: }
142: }
143:
144: /**
145: * Logs a {@link Level#CONFIG CONFIG} message. If the current logging
146: * context is not a {@link StandardLog} no message is logged.
147: *
148: * @param msg the config message.
149: */
150: public static void config(String msg) {
151: LogContext log = (LogContext) LogContext.getCurrent();
152: if (log instanceof StandardLog) {
153: ((StandardLog) log)._logger.config(msg);
154: }
155: }
156:
157: /**
158: * Logs a {@link Level#FINE FINE} message. If the current logging
159: * context is not a {@link StandardLog} no message is logged.
160: *
161: * @param msg the fine message.
162: */
163: public static void fine(String msg) {
164: LogContext log = (LogContext) LogContext.getCurrent();
165: if (log instanceof StandardLog) {
166: ((StandardLog) log)._logger.fine(msg);
167: }
168: }
169:
170: /**
171: * Logs a {@link Level#FINER FINER} message. If the current logging
172: * context is not a {@link StandardLog} no message is logged.
173: *
174: * @param msg the finer message.
175: */
176: public static void finer(String msg) {
177: LogContext log = (LogContext) LogContext.getCurrent();
178: if (log instanceof StandardLog) {
179: ((StandardLog) log)._logger.finer(msg);
180: }
181: }
182:
183: /**
184: * Logs a {@link Level#FINEST FINEST} message. If the current logging
185: * context is not a {@link StandardLog} no message is logged.
186: *
187: * @param msg the finest message.
188: */
189: public static void finest(String msg) {
190: LogContext log = (LogContext) LogContext.getCurrent();
191: if (log instanceof StandardLog) {
192: ((StandardLog) log)._logger.finest(msg);
193: }
194: }
195:
196: /**
197: * Logs throwing an exception. If the current logging context is not a
198: * {@link StandardLog} an {@link LogContext#logError error} is logged.
199: *
200: * @param sourceClass name of class that issued the logging request.
201: * @param sourceMethod name of the method.
202: * @param thrown the error that is being thrown.
203: */
204: public static void throwing(String sourceClass,
205: String sourceMethod, Throwable thrown) {
206: LogContext log = (LogContext) LogContext.getCurrent();
207: if (log instanceof StandardLog) {
208: ((StandardLog) log)._logger.throwing(sourceClass,
209: sourceMethod, thrown);
210: } else if (log.isErrorLogged()) {
211: log.logError(thrown, (CharSequence) null);
212: }
213: }
214:
215: /**
216: * Log a method entry. If the current logging context is not a
217: * {@link StandardLog} no entry is logged.
218: *
219: * @param sourceClass name of class that issued the logging request.
220: * @param sourceMethod name of method that is being entered.
221: */
222: public static void entering(String sourceClass, String sourceMethod) {
223: LogContext log = (LogContext) LogContext.getCurrent();
224: if (log instanceof StandardLog) {
225: ((StandardLog) log)._logger.entering(sourceClass,
226: sourceMethod);
227: }
228: }
229:
230: /**
231: * Log a method return. If the current logging context is not a
232: * {@link StandardLog} no return is logged.
233: *
234: * @param sourceClass name of class that issued the logging request.
235: * @param sourceMethod name of method that is being returned.
236: */
237: public static void exiting(String sourceClass, String sourceMethod) {
238: LogContext log = (LogContext) LogContext.getCurrent();
239: if (log instanceof StandardLog) {
240: ((StandardLog) log)._logger.exiting(sourceClass,
241: sourceMethod);
242: }
243: }
244:
245: // Implements LogContext abstract method.
246: public boolean isInfoLogged() {
247: return _logger.isLoggable(Level.INFO);
248: }
249:
250: // Implements LogContext abstract method.
251: public boolean isWarningLogged() {
252: return _logger.isLoggable(Level.WARNING);
253: }
254:
255: // Implements LogContext abstract method.
256: public boolean isErrorLogged() {
257: return _logger.isLoggable(Level.SEVERE);
258: }
259:
260: // Implements LogContext abstract method.
261: public void logInfo(CharSequence message) {
262: _logger.info(message.toString());
263: }
264:
265: // Implements LogContext abstract method.
266: public void logWarning(CharSequence message) {
267: _logger.warning(message.toString());
268: }
269:
270: // Implements LogContext abstract method.
271: public void logError(Throwable error, CharSequence message) {
272: String description = (message != null) ? message.toString()
273: : "";
274: if (error != null) {
275: _logger.log(Level.SEVERE, description, error);
276: } else {
277: _logger.log(Level.SEVERE, description);
278: }
279: }
280:
281: }
|