001: /*
002: * <copyright>
003: *
004: * Copyright 1997-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.core.logging;
028:
029: import org.cougaar.bootstrap.SystemProperties;
030: import org.cougaar.core.service.LoggingService;
031: import org.cougaar.util.log.Logger;
032: import org.cougaar.util.log.LoggerAdapter;
033:
034: /**
035: * Utility class to wrap all {@link LoggingService} messages with a
036: * prefix string, unless already wrapped by a
037: * <tt>LoggingServiceWithPrefix</tt> with that prefix.
038: *
039: * @property org.cougaar.core.logging.trackDuplicateLoggingPrefix
040: * Enable debug tracking of duplicate "AgentX: AgentX:"
041: * LoggingService log messages within the LoggingServiceWithPrefix,
042: * possibly due to enabling
043: * "-Dorg.cougaar.core.logging.addAgentPrefix". Performs an
044: * expensive check at DETAIL log level, regardless of logger
045: * configuration, and logs any duplicate prefixes at SHOUT level.
046: * Defaults to false.
047: */
048: public class LoggingServiceWithPrefix extends LoggerAdapter implements
049: LoggingService {
050:
051: private static final boolean TRACK_DUPLICATE_LOGGING_PREFIX = SystemProperties
052: .getBoolean("org.cougaar.core.logging.trackDuplicateLoggingPrefix");
053:
054: private final String prefix;
055: private final Logger logger;
056:
057: public static LoggingService add(LoggingService ls, String prefix) {
058: if (ls instanceof LoggingServiceWithPrefix) {
059: LoggingServiceWithPrefix lswp = (LoggingServiceWithPrefix) ls;
060: String s = lswp.prefix;
061: if (prefix == null ? s == null : prefix.equals(s)) {
062: // already prefixed
063: return lswp;
064: }
065: }
066: return new LoggingServiceWithPrefix(ls, prefix);
067: }
068:
069: public static LoggingService add(Logger logger, String prefix) {
070: if (logger instanceof LoggingService) {
071: return add(((LoggingService) logger), prefix);
072: }
073: return new LoggingServiceWithPrefix(logger, prefix);
074: }
075:
076: public LoggingServiceWithPrefix(Logger logger, String prefix) {
077: this .logger = logger;
078: this .prefix = prefix;
079: }
080:
081: public boolean isEnabledFor(int level) {
082: if (TRACK_DUPLICATE_LOGGING_PREFIX) {
083: // we want to see all messages, even they won't be logged.
084: // note that this causes extra string consing!
085: return true;
086: }
087: return logger.isEnabledFor(level);
088: }
089:
090: public void log(int level, String message, Throwable t) {
091: if (TRACK_DUPLICATE_LOGGING_PREFIX
092: && message.regionMatches(0, prefix, 0,
093: prefix.length() - 1)) {
094: StackTraceElement[] stack = (new Throwable())
095: .getStackTrace();
096: logger.log(SHOUT, "DuplicateLoggingPrefix - " + stack[2]
097: + ": " + message, null);
098: }
099: logger.log(level, prefix + message, t);
100: }
101:
102: public void printDot(String dot) {
103: logger.printDot(dot);
104: }
105: }
|