1 package gov.nasa.jpf.util; 20 21 import gov.nasa.jpf.Config; 22 import java.util.HashMap ; 23 import java.util.logging.Logger ; 24 import java.util.logging.Level ; 25 import java.util.logging.Formatter ; 26 import java.util.logging.LogRecord ; 27 28 41 public class LogManager { 42 43 static class DefaultFormatter extends Formatter { 44 public String format (LogRecord r) { 46 String msg = "[JPF-" + r.getLevel().getName() + "]: " + r.getMessage() + '\n'; 47 48 return msg; 49 } 50 } 51 52 static HashMap loggers = new HashMap (); 54 static Level defaultLevel; 55 static LogHandler handler; 57 static String [] activeSevere; 59 static String [] activeWarning; 60 static String [] activeInfo; 61 static String [] activeConfig; 62 static String [] activeFine; 63 static String [] activeFiner; 64 static String [] activeFinest; 65 66 70 public static void init (Config conf) { 71 try { 72 defaultLevel = Level.parse( conf.getString("log.level", "INFO").toUpperCase()); 73 } catch (Throwable 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 [] actives, String 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 getLevel (String 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 getLogger (String name) { 115 Logger logger = (Logger ) loggers.get(name); 117 118 if (logger == null) { 119 logger = Logger.getLogger(name); 121 logger.setLevel( getLevel(name)); 122 logger.addHandler(handler); 123 logger.setUseParentHandlers(false); 125 loggers.put(name, logger); 126 } 127 128 return logger; 129 } 130 131 public static void printStatus (Logger log) { 132 handler.printStatus(log); 133 } 134 } 135 | Popular Tags |