1 22 package fr.dyade.aaa.util; 23 24 import java.io.*; 25 import java.util.*; 26 27 import org.objectweb.util.monolog.api.*; 28 import org.objectweb.util.monolog.wrapper.remote.lib.MonologFactoryMBeanImpl; 29 import fr.dyade.aaa.util.management.MXWrapper; 30 31 34 public class Debug { 35 36 public final static boolean debug = true; 37 38 43 public final static String DEBUG_DIR_PROPERTY = "fr.dyade.aaa.DEBUG_DIR"; 44 45 public final static String DEFAULT_DEBUG_DIR = "."; 46 47 public final static String DEBUG_FILE_PROPERTY = "fr.dyade.aaa.DEBUG_FILE"; 48 49 public final static String DEFAULT_DEBUG_FILE = "a3debug.cfg"; 50 51 52 protected static LoggerFactory factory; 53 54 58 private static String debugDir = null; 59 60 63 private static String debugFileName = null; 64 65 public static void setDebugDir(String debugDir) { 66 Debug.debugDir = debugDir; 67 } 68 69 public static void setDebugFileName(String debugFileName) { 70 Debug.debugFileName = debugFileName; 71 } 72 73 public static void reinit() throws Exception { 74 initialize(); 75 } 76 77 protected static void init() throws Exception { 78 try { 79 initialize(); 80 } catch(Exception exc) { 81 System.err.println("Monolog configuration file not found, use defaults"); 82 try { 83 ((MonologFactory) factory).configure(null); 84 Logger[] loggers = factory.getLoggers(); 85 for (int i=0; i<loggers.length; i++) { 86 loggers[i].setIntLevel(BasicLevel.ERROR); 87 } 88 } catch (Exception e) { 89 System.err.println("Unable to configure monolog wrapper"); 90 throw new Exception ("Unable to configure monolog wrapper"); 91 } 92 } 93 94 97 109 } 110 111 private static PrivateLogger logger = null; 112 113 116 private static void initialize() throws Exception { 117 String ldebugDir = debugDir; 118 if (ldebugDir == null) 119 ldebugDir = System.getProperty(DEBUG_DIR_PROPERTY); 120 String ldebugFileName = debugFileName; 121 if (ldebugFileName == null) 122 ldebugFileName = System.getProperty(DEBUG_FILE_PROPERTY, 123 DEFAULT_DEBUG_FILE); 124 if (ldebugDir != null) { 125 File debugFile = new File(ldebugDir, ldebugFileName); 126 try { 127 if ((debugFile != null) && 128 debugFile.exists() && 129 debugFile.isFile() && 130 (debugFile.length() != 0)) { 131 ldebugFileName = debugFile.getPath(); 132 } else { 133 throw new IOException(); 134 } 135 } catch (IOException exc) { 136 System.err.println("Unable to find \"" + debugFile.getPath() + "\"."); 139 ldebugDir = null; 140 } 141 } 142 143 try { 144 System.setProperty(org.objectweb.util.monolog.Monolog.MONOLOG_FILE_NAME, 145 ldebugFileName); 146 factory = org.objectweb.util.monolog.Monolog.init(); 147 if (factory == null) { 148 System.err.println("Error in Monolog initialization: null factory"); 149 } else { 150 Logger dl = factory.getLogger("fr.dyade.aaa.util.debug"); 151 dl.log(BasicLevel.INFO, "Debug.initialize() - " + ldebugFileName); 152 } 153 } catch(Throwable exc) { 154 System.err.println("Unable to instantiate monolog wrapper"); 155 exc.printStackTrace(); 156 } 157 } 158 159 public static Logger getLogger(String topic) { 160 try { 161 if (factory == null) init(); 162 return factory.getLogger(topic); 163 } catch (Exception exc) { 164 } 165 if (logger == null) 166 logger = new PrivateLogger(); 167 return logger; 168 } 169 170 public static void setLoggerLevel(String topic, int level) { 171 getLogger(topic).setIntLevel(level); 172 } 173 174 178 public static void setLoggerFactory(LoggerFactory loggerFactory) { 179 factory = loggerFactory; 180 } 181 182 private static class PrivateLogger implements Logger { 183 PrivateLogger() { 184 BasicLevel.FATAL = 1000; 185 BasicLevel.ERROR = 800; 186 BasicLevel.WARN = 600; 187 BasicLevel.INFO = 400; 188 BasicLevel.DEBUG = 200; 189 } 190 191 193 196 public String getName() { 197 return toString(); 198 } 199 200 203 public void setName(String name) {} 204 205 208 public String getType() { 209 return "console"; 210 } 211 212 215 public String [] getAttributeNames() { 216 return null; 217 } 218 219 223 public Object getAttribute(String name) { 224 return null; 225 } 226 227 233 public Object setAttribute(String name, Object value) { 234 return null; 235 } 236 237 239 244 public void setIntLevel(int l) {} 245 246 251 public void setLevel(Level l) {} 252 253 258 public int getCurrentIntLevel() { 259 return BasicLevel.ERROR; 260 } 261 262 267 public Level getCurrentLevel() { 268 return null; 269 } 270 271 277 public boolean isLoggable(int level) { 278 if (level >= BasicLevel.ERROR) 279 return true; 280 return false; 281 } 282 283 289 public boolean isLoggable(Level l) { 290 if (l.getIntValue() >= BasicLevel.ERROR) 291 return true; 292 return false; 293 } 294 295 300 public boolean isOn() { 301 return true; 302 } 303 304 309 public void log(int level, Object o) { 310 if (level >= BasicLevel.ERROR) 311 System.err.println(o.toString()); 312 } 313 318 public void log(Level l, Object o) { 319 if (l.getIntValue() >= BasicLevel.ERROR) 320 System.err.println(o.toString()); 321 } 322 323 327 public void log(int level, Object o, Throwable t) { 328 if (level >= BasicLevel.ERROR) 329 System.err.println(o.toString() + ":" + t.toString()); 330 } 331 335 public void log(Level l, Object o, Throwable t) { 336 if (l.getIntValue() >= BasicLevel.ERROR) 337 System.err.println(o.toString() + ":" + t.toString()); 338 } 339 340 341 348 public void log(int level, Object o, Object location, Object method) { 349 if (level >= BasicLevel.ERROR) 350 System.err.println(location.toString() + "." + method.toString() + 351 "(...) :" + o.toString()); 352 } 353 360 public void log(Level l, Object o, Object location, 361 Object method) { 362 if (l.getIntValue() >= BasicLevel.ERROR) 363 System.err.println(location.toString() + "." + method.toString() + 364 "(...) :" + o.toString()); 365 } 366 367 375 public void log(int level, Object o, Throwable t, Object location, Object method) { 376 if (level >= BasicLevel.ERROR) 377 System.err.println(location.toString() + "." + method.toString() + 378 "(...) :" + o.toString() + " " + t.toString()); 379 } 380 388 public void log(Level l, Object o, Throwable t, Object location, 389 Object method) { 390 if (l.getIntValue() >= BasicLevel.ERROR) 391 System.err.println(location.toString() + "." + method.toString() + 392 "(...) :" + o.toString() + " " + t.toString()); 393 } 394 395 396 public void turnOn() {} 397 398 399 public void turnOff() {} 400 401 public String toString() { 402 return "Private ScalAgent D.T. default implementation"; 403 } 404 } 405 } 406 | Popular Tags |