001: /*
002: * <copyright>
003: *
004: * Copyright 2001-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.util.log;
028:
029: import java.util.Map;
030: import java.util.WeakHashMap;
031:
032: /**
033: * Static access to simple logging facilities.
034: * <p>
035: * Cougaar components may be better served by using org.cougaar.core.service.LoggingService LoggingService
036: */
037: public final class Logging {
038: private static Logger dotLogger = null;
039:
040: // cannot be instantiated
041: private Logging() {
042: }
043:
044: /** Alias for {@link LoggerFactory#getInstance()} **/
045: public static LoggerFactory getLoggerFactory() {
046: return LoggerFactory.getInstance();
047: }
048:
049: /** The cache for getLogger() **/
050: private static Map loggerCache = new WeakHashMap(11);
051:
052: /** Similar in function to <pre>
053: * LoggerFactory.getInstance().createLogger(name);
054: * <pre>
055: * except that this accessor memoizes the calls to
056: * reduce memory load.
057: **/
058: public static Logger getLogger(Object name) {
059: synchronized (loggerCache) {
060: String key = getKey(name);
061: Logger l = (Logger) loggerCache.get(key);
062: if (l == null) {
063: // used to use createLogger, but that has been ruined by misuse
064: l = LoggerFactory.getInstance().newLogger(name);
065: // store the key as a new string instance to
066: // make sure it is collectable.
067: loggerCache.put(new String(key), l);
068: }
069: return l;
070: }
071: }
072:
073: public static void printDot(String dot) {
074: synchronized (Logging.class) {
075: if (dotLogger == null) {
076: dotLogger = getLogger("DOTS");
077: }
078: }
079: dotLogger.printDot(dot);
080: }
081:
082: /** The cache for getLoggerController() **/
083: private static Map lcCache = new WeakHashMap(11);
084:
085: /** Similar in function to <pre>
086: * LoggerFactory.getInstance().createLoggerController(name);
087: * <pre>
088: * except that this accessor memoizes the calls to
089: * reduce memory load.
090: **/
091: public static LoggerController getLoggerController(Object name) {
092: synchronized (lcCache) {
093: String key = getKey(name);
094: LoggerController lc = (LoggerController) lcCache.get(key);
095: if (lc == null) {
096: lc = LoggerFactory.getInstance()
097: .createLoggerController(key);
098: // store the key as a new string instance to
099: // make sure it is collectable.
100: lcCache.put(new String(key), lc);
101: }
102: return lc;
103: }
104: }
105:
106: /** store for defaultLogger(), guarded by syncing on the class **/
107: private static Logger defaultLogger = null;
108:
109: /** Returns the default Logger instance - the value returned by {@link #currentLogger()}
110: * if there is no active context.
111: **/
112: public synchronized static Logger defaultLogger() {
113: if (defaultLogger == null) {
114: defaultLogger = getLogger("Default");
115: }
116: return defaultLogger;
117: }
118:
119: /** Store for withLogger and currentLogger **/
120: private static final ThreadLocal loggerContext = new ThreadLocal();
121:
122: /** Call a runnable in the dynamic context of a particular logger.
123: * Anyone within that context can call {@link #currentLogger()}
124: * to get access to an appropriate logger instance.
125: * If no such instance has been installed, will use a generic
126: * logger with the name "Default". This value may be retrieved
127: * by calling {@link #defaultLogger()}.
128: * @note withLogger may be called recursively, as the Logger contexts are
129: * allowed to nest.
130: * @note Child threads do not inherit Logger contexts.
131: **/
132: public static void withLogger(Logger l, Runnable r) {
133: Logger old = (Logger) loggerContext.get();
134: try {
135: loggerContext.set(l);
136: r.run();
137: } finally {
138: loggerContext.set(old);
139: }
140: }
141:
142: /** Return the Logger currently in-force in the current thread, as set
143: * by {@link #withLogger(Logger,Runnable)}.
144: * @return the current logger or the value returned by defaultLogger(). This method will
145: * never return null.
146: * @see #hasLogger()
147: * @note Child threads do not inherit Logger contexts.
148: **/
149: public static Logger currentLogger() {
150: Logger l = (Logger) loggerContext.get();
151: if (l == null) {
152: l = defaultLogger();
153: }
154: return l;
155: }
156:
157: /** Is there a Logger in-force for the current thread?
158: * @return true IFF currentLogger would return something other than the default logger.
159: **/
160: public static boolean hasLogger() {
161: Logger l = (Logger) loggerContext.get();
162: return (l != null);
163: }
164:
165: //
166: // private utilities
167: //
168:
169: /** Compute the Logging Name of the referenced object.
170: * Used by various objects which can create Logger instances.
171: **/
172: public static final String getKey(Object x) {
173: if (x instanceof Class) {
174: return ((Class) x).getName();
175: } else if (x instanceof String) {
176: return (String) x;
177: } else if (x == null) {
178: return "null";
179: } else {
180: return x.getClass().getName();
181: }
182: }
183:
184: }
|