KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > util > LogManager


1 //
2
//Copyright (C) 2005 United States Government as represented by the
3
//Administrator of the National Aeronautics and Space Administration
4
//(NASA). All Rights Reserved.
5
//
6
//This software is distributed under the NASA Open Source Agreement
7
//(NOSA), version 1.3. The NOSA has been approved by the Open Source
8
//Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
//directory tree for the complete NOSA document.
10
//
11
//THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
//KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
//LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
//SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
//A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
//THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
//DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package gov.nasa.jpf.util;
20
21 import gov.nasa.jpf.Config;
22 import java.util.HashMap JavaDoc;
23 import java.util.logging.Logger JavaDoc;
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.Formatter JavaDoc;
26 import java.util.logging.LogRecord JavaDoc;
27
28 /**
29  * this class is responsible for returning properly JPF-configured
30  * Loggers. It is not supposed to be used directly by clients, but rather
31  * is a JPF delegatee.
32  *
33  * While we could modify/replace the standard java.util.logging facility
34  * at various levels (own LogManager, own initialization class etc.), we choose
35  * the approach to piggyback on it, because these mechanisms either require
36  * changing system properties, rely on only partly documented features, or
37  * don't give us the full functionality we need. By having our own log
38  * encapsulator, we could also replace the underlying mechanism if we really
39  * want to
40  */

41 public class LogManager {
42   
43   static class DefaultFormatter extends Formatter JavaDoc {
44     // we might want to parameterize this
45
public String JavaDoc format (LogRecord JavaDoc r) {
46       String JavaDoc msg = "[JPF-" + r.getLevel().getName() + "]: " + r.getMessage() + '\n';
47       
48       return msg;
49     }
50   }
51   
52   static HashMap JavaDoc loggers = new HashMap JavaDoc(); // our own set
53

54   static Level JavaDoc defaultLevel;
55   static LogHandler handler; // we have only one
56

57   // I don't like these categories too much, but we want to act as a stand in
58
static String JavaDoc[] activeSevere;
59   static String JavaDoc[] activeWarning;
60   static String JavaDoc[] activeInfo;
61   static String JavaDoc[] activeConfig;
62   static String JavaDoc[] activeFine;
63   static String JavaDoc[] activeFiner;
64   static String JavaDoc[] activeFinest;
65   
66   /**
67    * note - this is not allowed to fail, since we couldn't log that. Hardcoded default
68    * values have to do in this case (make sure we catch the proper Config exceptions)
69    */

70   public static void init (Config conf) {
71     try {
72       defaultLevel = Level.parse( conf.getString("log.level", "INFO").toUpperCase());
73     } catch (Throwable JavaDoc x) {
74       defaultLevel = Level.WARNING;
75     }
76     
77     activeSevere = conf.getStringArray("log.severe");
78     activeWarning = conf.getStringArray("log.warning");
79     activeInfo = conf.getStringArray("log.info");
80     activeConfig = conf.getStringArray("log.config");
81     activeFine = conf.getStringArray("log.fine");
82     activeFiner = conf.getStringArray("log.finer");
83     activeFinest = conf.getStringArray("log.finest");
84     
85     handler = new LogHandler(conf);
86   }
87   
88   static boolean checkInclusion (String JavaDoc[] actives, String JavaDoc name) {
89     if (actives == null) {
90       return false;
91     }
92     
93     for (int i=0; i<actives.length; i++) {
94       if (name.matches(actives[i])) {
95         return true;
96       }
97     }
98     
99     return false;
100   }
101   
102   static Level JavaDoc getLevel (String JavaDoc name) {
103     if (checkInclusion(activeSevere, name)) return Level.SEVERE;
104     if (checkInclusion(activeWarning, name)) return Level.WARNING;
105     if (checkInclusion(activeInfo, name)) return Level.INFO;
106     if (checkInclusion(activeConfig, name)) return Level.CONFIG;
107     if (checkInclusion(activeFine, name)) return Level.FINE;
108     if (checkInclusion(activeFiner, name)) return Level.FINER;
109     if (checkInclusion(activeFinest, name)) return Level.FINEST;
110     
111     return defaultLevel;
112   }
113   
114   public static Logger JavaDoc getLogger (String JavaDoc name) {
115     // how often can you say 'Logger' in one method..
116
Logger JavaDoc logger = (Logger JavaDoc) loggers.get(name);
117     
118     if (logger == null) {
119       // we haven't had this one yet - create and init a new one from the host logging system
120
logger = Logger.getLogger(name);
121       logger.setLevel( getLevel(name));
122       logger.addHandler(handler);
123       logger.setUseParentHandlers(false); // we don't want to pass this up
124

125       loggers.put(name, logger);
126     }
127     
128     return logger;
129   }
130   
131   public static void printStatus (Logger JavaDoc log) {
132     handler.printStatus(log);
133   }
134 }
135
Popular Tags