1 6 package org.mortbay.log; 7 import java.util.ArrayList ; 8 import java.util.StringTokenizer ; 9 10 import org.mortbay.util.Loader; 11 12 13 28 public class LogImpl implements org.apache.commons.logging.Log 29 { 30 31 public final static String DEBUG= "DEBUG "; 32 public final static String INFO= "INFO "; 33 public final static String TRACE= "TRACE "; 34 public final static String FAIL= "FAIL!! "; 35 public final static String WARN= "WARN!! "; 36 public final static String ERROR= "ERROR! "; 37 38 39 boolean _debugOn=false; 40 private ArrayList _debugPatterns=null; 41 private boolean _initialized = false; 42 private String _patterns=null; 43 44 45 46 public LogSink[] _sinks = null; 47 private boolean _suppressWarnings=false; 48 private int _verbose=0; 49 50 51 54 public LogImpl() 55 { 56 try{ 57 _debugOn= System.getProperty("DEBUG") != null; 58 setDebugPatterns(System.getProperty("DEBUG_PATTERNS")); 59 setVerbose(Integer.getInteger("DEBUG_VERBOSE",0).intValue()); 60 } 61 catch (Exception e) 62 { 63 System.err.println("Exception from getProperty!\n"+ 64 "Probably running in applet\n"+ 65 "Use Code.initParamsFromApplet or Code.setOption to control debug output."); 66 } 67 } 68 69 70 73 public synchronized void add(LogSink logSink) 74 throws Exception 75 { 76 logSink.setLogImpl(this); 77 if (!logSink.isStarted()) 78 logSink.start(); 79 80 if (_sinks==null) 81 { 82 _sinks=new LogSink[1]; 83 _sinks[0]=logSink; 84 } 85 else 86 { 87 boolean slotFree = false; 88 for( int i=_sinks.length; i-->0; ) 89 { 90 if( _sinks[i] == null ) 91 { 92 slotFree = true; 93 _sinks[i] = logSink; 94 break; 95 } 96 } 97 98 if( !slotFree ) 99 { 100 LogSink[] ns = new LogSink[_sinks.length+1]; 101 for (int i=_sinks.length;i-->0;) 102 ns[i]=_sinks[i]; 103 ns[_sinks.length]=logSink; 104 _sinks=ns; 105 } 106 } 107 _initialized = true; 108 109 info("added "+logSink); 110 } 111 112 113 114 117 public synchronized void add(String logSinkClass) 118 { 119 try 120 { 121 if (logSinkClass==null || logSinkClass.length()==0) 122 logSinkClass="org.mortbay.log.OutputStreamLogSink"; 123 Class sinkClass = Loader.loadClass(this.getClass(),logSinkClass); 124 LogSink sink=(LogSink)sinkClass.newInstance(); 125 add(sink); 126 } 127 catch(Exception e) 128 { 129 message(WARN,e,2); 130 throw new IllegalArgumentException (e.toString()); 131 } 132 133 } 134 135 138 public void debug(Object m) 139 { 140 if (_debugOn) 141 { 142 Frame frame = new Frame(1,true); 143 if (isDebugOnFor(frame)) 144 { 145 frame.complete(); 146 message(DEBUG,m,frame); 147 } 148 } 149 } 150 151 154 public void debug(Object m, Throwable ex) 155 { 156 if (_debugOn) 157 { 158 Frame frame = new Frame(1,true); 159 if (isDebugOnFor(frame)) 160 { 161 frame.complete(); 162 message(DEBUG,new Object []{m,ex},frame); 163 } 164 } 165 } 166 167 168 169 173 private synchronized void defaultInit() 174 { 175 if (!_initialized) 176 { 177 _initialized = true; 178 String sinkClasses = System.getProperty("LOG_SINKS","org.mortbay.log.OutputStreamLogSink"); 179 StringTokenizer sinkTokens = new StringTokenizer (sinkClasses, ";, "); 180 181 LogSink sink= null; 182 while (sinkTokens.hasMoreTokens()) 183 { 184 String sinkClassName = sinkTokens.nextToken(); 185 186 try 187 { 188 Class sinkClass = Loader.loadClass(this.getClass(),sinkClassName); 189 if (org.mortbay.log.LogSink.class.isAssignableFrom(sinkClass)) { 190 sink = (LogSink)sinkClass.newInstance(); 191 sink.start(); 192 add(sink); 193 } 194 else 195 System.err.println(sinkClass+" is not a org.mortbay.log.LogSink"); 197 } 198 catch (Exception e) { 199 e.printStackTrace(); 200 } 201 } 202 } 203 } 204 205 206 208 public synchronized void deleteStoppedLogSinks() 209 { 210 if (_sinks!=null) 211 { 212 for (int s=_sinks.length;s-->0;) 213 { 214 if (_sinks[s]==null) 215 continue; 216 if (!_sinks[s].isStarted()) 217 _sinks[s]=null; 218 } 219 } 220 } 221 222 223 226 public synchronized void reset() 227 { 228 info("reset"); 229 if (_sinks!=null) { 230 for (int s=_sinks.length;s-->0;) 231 { 232 try{ 233 if (_sinks[s]!=null) 234 _sinks[s].stop(); 235 _sinks[s]=null; 236 } 237 catch(InterruptedException e) 238 { 239 if (getDebug() && getVerbose()>0) 240 message("WARN",e); 241 } 242 } 243 _sinks=null; 244 } 245 _initialized=true; 246 } 247 248 249 252 public void error(Object arg0) 253 { 254 message(ERROR,arg0,new Frame(1)); 255 256 } 257 258 261 public void error(Object arg0, Throwable arg1) 262 { 263 message(ERROR,new Object []{arg0,arg1},new Frame(1)); 264 } 265 266 269 public void fatal(Object arg0) 270 { 271 message(FAIL,arg0,new Frame(1)); 272 273 } 274 275 278 public void fatal(Object arg0, Throwable arg1) 279 { 280 message(FAIL,new Object []{arg0,arg1},new Frame(1)); 281 } 282 283 284 285 288 public boolean getDebug() 289 { 290 return _debugOn; 291 } 292 293 294 297 public String getDebugPatterns() 298 { 299 return _patterns; 300 } 301 302 303 public LogSink[] getLogSinks() 304 { 305 return _sinks; 306 } 307 308 309 312 public boolean getSuppressWarnings() 313 { 314 return _suppressWarnings; 315 } 316 317 318 321 public int getVerbose() 322 { 323 return _verbose; 324 } 325 326 329 public void info(Object arg0) 330 { 331 if (isInfoEnabled()) 332 message(INFO,arg0,new Frame(1)); 333 334 } 335 336 339 public void info(Object arg0, Throwable arg1) 340 { 341 if (isInfoEnabled()) 342 message(INFO,new Object []{arg0,arg1},new Frame(1)); 343 } 344 345 348 public boolean isDebugEnabled() 349 { 350 return _debugOn; 351 } 352 353 356 public boolean isErrorEnabled() 357 { 358 return !_suppressWarnings; 359 } 360 361 364 public boolean isFatalEnabled() 365 { 366 return true; 367 } 368 369 372 public boolean isInfoEnabled() 373 { 374 return _verbose>=0; 375 } 376 377 380 public boolean isTraceEnabled() 381 { 382 return _verbose>0; 383 } 384 385 388 public boolean isWarnEnabled() 389 { 390 return !_suppressWarnings; 391 } 392 393 394 public void message(String tag, 395 Object msg, 396 Frame frame) 397 { 398 long time = System.currentTimeMillis(); 399 message(tag,msg,frame,time); 400 } 401 402 403 409 public synchronized void message(String tag, 410 Object msg, 411 Frame frame, 412 long time) 413 { 414 if (!_initialized) 415 defaultInit(); 416 417 boolean logged=false; 418 if (_sinks!=null) 419 { 420 for (int s=_sinks.length;s-->0;) 421 { 422 if (_sinks[s]==null) 423 continue; 424 425 if (_sinks[s].isStarted()) 426 { 427 logged=true; 428 _sinks[s].log(tag,msg,frame,time); 429 } 430 } 431 } 432 433 if (!logged) 434 System.err.println(time+": "+tag+":"+msg+" @ "+frame); 435 } 436 437 438 442 public synchronized void message(String tag, 443 Object msg) 444 { 445 message(tag,msg,new Frame(1),System.currentTimeMillis()); 446 } 447 448 449 453 public synchronized void message(String tag, 454 Object msg, 455 int depth) 456 { 457 message(tag,msg,new Frame(depth),System.currentTimeMillis()); 458 } 459 460 461 464 public synchronized void setDebug(boolean debug) 465 { 466 boolean oldDebug=_debugOn; 467 if (_debugOn && !debug) 468 this.message(DEBUG,"DEBUG OFF"); 469 _debugOn=debug; 470 if (!oldDebug && debug) 471 this.message(DEBUG,"DEBUG ON"); 472 } 473 474 475 478 public void setDebugPatterns(String patterns) 479 { 480 _patterns=patterns; 481 if (patterns!=null && patterns.length()>0) 482 { 483 _debugPatterns = new ArrayList (); 484 485 StringTokenizer tok = new StringTokenizer (patterns,", \t"); 486 while (tok.hasMoreTokens()) 487 { 488 String pattern = tok.nextToken(); 489 _debugPatterns.add(pattern); 490 } 491 } 492 else 493 _debugPatterns = null; 494 } 495 496 497 500 public void setSuppressWarnings(boolean warnings) 501 { 502 _suppressWarnings=warnings; 503 } 504 505 506 507 510 public void setVerbose(int verbose) 511 { 512 _verbose=verbose; 513 } 514 515 518 public void trace(Object arg0) 519 { 520 if (isTraceEnabled()) 521 message(TRACE,arg0,new Frame(1)); 522 } 523 524 527 public void trace(Object arg0, Throwable arg1) 528 { 529 if (isTraceEnabled()) 530 message(TRACE,new Object []{arg0,arg1},new Frame(1)); 531 } 532 533 536 public void warn(Object arg0) 537 { 538 if (!_suppressWarnings) 539 message(WARN,arg0,new Frame(1)); 540 } 541 542 545 public void warn(Object arg0, Throwable arg1) 546 { 547 if (!_suppressWarnings) 548 message(WARN,new Object []{arg0,arg1},new Frame(1)); 549 } 550 551 552 private boolean isDebugOnFor(Frame frame) 553 { 554 if (_debugOn) 555 { 556 if (_debugPatterns==null) 557 return true; 558 else 559 { 560 for (int i = _debugPatterns.size();--i>=0;) 561 { 562 if(frame.getWhere().indexOf((String )_debugPatterns 563 .get(i))>=0) 564 return true; 565 } 566 } 567 } 568 return false; 569 } 570 571 } 572 573 | Popular Tags |