001: //
002: //Copyright (C) 2005 United States Government as represented by the
003: //Administrator of the National Aeronautics and Space Administration
004: //(NASA). All Rights Reserved.
005: //
006: //This software is distributed under the NASA Open Source Agreement
007: //(NOSA), version 1.3. The NOSA has been approved by the Open Source
008: //Initiative. See the file NOSA-1.3-JPF at the top of the distribution
009: //directory tree for the complete NOSA document.
010: //
011: //THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
012: //KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
013: //LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
014: //SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
015: //A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
016: //THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
017: //DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
018: //
019: package gov.nasa.jpf.util;
020:
021: import gov.nasa.jpf.Config;
022: import java.util.HashMap;
023: import java.util.logging.Logger;
024: import java.util.logging.Level;
025: import java.util.logging.Formatter;
026: import java.util.logging.LogRecord;
027:
028: /**
029: * this class is responsible for returning properly JPF-configured
030: * Loggers. It is not supposed to be used directly by clients, but rather
031: * is a JPF delegatee.
032: *
033: * While we could modify/replace the standard java.util.logging facility
034: * at various levels (own LogManager, own initialization class etc.), we choose
035: * the approach to piggyback on it, because these mechanisms either require
036: * changing system properties, rely on only partly documented features, or
037: * don't give us the full functionality we need. By having our own log
038: * encapsulator, we could also replace the underlying mechanism if we really
039: * want to
040: */
041: public class LogManager {
042:
043: static class DefaultFormatter extends Formatter {
044: // we might want to parameterize this
045: public String format(LogRecord r) {
046: String msg = "[JPF-" + r.getLevel().getName() + "]: "
047: + r.getMessage() + '\n';
048:
049: return msg;
050: }
051: }
052:
053: static HashMap loggers = new HashMap(); // our own set
054:
055: static Level defaultLevel;
056: static LogHandler handler; // we have only one
057:
058: // I don't like these categories too much, but we want to act as a stand in
059: static String[] activeSevere;
060: static String[] activeWarning;
061: static String[] activeInfo;
062: static String[] activeConfig;
063: static String[] activeFine;
064: static String[] activeFiner;
065: static String[] activeFinest;
066:
067: /**
068: * note - this is not allowed to fail, since we couldn't log that. Hardcoded default
069: * values have to do in this case (make sure we catch the proper Config exceptions)
070: */
071: public static void init(Config conf) {
072: try {
073: defaultLevel = Level.parse(conf.getString("log.level",
074: "INFO").toUpperCase());
075: } catch (Throwable x) {
076: defaultLevel = Level.WARNING;
077: }
078:
079: activeSevere = conf.getStringArray("log.severe");
080: activeWarning = conf.getStringArray("log.warning");
081: activeInfo = conf.getStringArray("log.info");
082: activeConfig = conf.getStringArray("log.config");
083: activeFine = conf.getStringArray("log.fine");
084: activeFiner = conf.getStringArray("log.finer");
085: activeFinest = conf.getStringArray("log.finest");
086:
087: handler = new LogHandler(conf);
088: }
089:
090: static boolean checkInclusion(String[] actives, String name) {
091: if (actives == null) {
092: return false;
093: }
094:
095: for (int i = 0; i < actives.length; i++) {
096: if (name.matches(actives[i])) {
097: return true;
098: }
099: }
100:
101: return false;
102: }
103:
104: static Level getLevel(String name) {
105: if (checkInclusion(activeSevere, name))
106: return Level.SEVERE;
107: if (checkInclusion(activeWarning, name))
108: return Level.WARNING;
109: if (checkInclusion(activeInfo, name))
110: return Level.INFO;
111: if (checkInclusion(activeConfig, name))
112: return Level.CONFIG;
113: if (checkInclusion(activeFine, name))
114: return Level.FINE;
115: if (checkInclusion(activeFiner, name))
116: return Level.FINER;
117: if (checkInclusion(activeFinest, name))
118: return Level.FINEST;
119:
120: return defaultLevel;
121: }
122:
123: public static Logger getLogger(String name) {
124: // how often can you say 'Logger' in one method..
125: Logger logger = (Logger) loggers.get(name);
126:
127: if (logger == null) {
128: // we haven't had this one yet - create and init a new one from the host logging system
129: logger = Logger.getLogger(name);
130: logger.setLevel(getLevel(name));
131: logger.addHandler(handler);
132: logger.setUseParentHandlers(false); // we don't want to pass this up
133:
134: loggers.put(name, logger);
135: }
136:
137: return logger;
138: }
139:
140: public static void printStatus(Logger log) {
141: handler.printStatus(log);
142: }
143: }
|