1 43 44 package org.jfree.util; 45 46 import java.util.ArrayList ; 47 import java.util.Arrays ; 48 import java.util.HashMap ; 49 50 56 public class Log { 57 58 61 public static class SimpleMessage { 62 63 66 private String message; 67 68 71 private Object [] param; 72 73 79 public SimpleMessage(final String message, final Object param1) { 80 this.message = message; 81 this.param = new Object []{param1}; 82 } 83 84 91 public SimpleMessage(final String message, final Object param1, 92 final Object param2) { 93 this.message = message; 94 this.param = new Object []{param1, param2}; 95 } 96 97 105 public SimpleMessage(final String message, final Object param1, 106 final Object param2, final Object param3) { 107 this.message = message; 108 this.param = new Object []{param1, param2, param3}; 109 } 110 111 120 public SimpleMessage(final String message, final Object param1, 121 final Object param2, final Object param3, 122 final Object param4) { 123 this.message = message; 124 this.param = new Object []{param1, param2, param3, param4}; 125 } 126 127 133 public SimpleMessage(final String message, final Object [] param) { 134 this.message = message; 135 this.param = param; 136 } 137 138 143 public String toString() { 144 final StringBuffer b = new StringBuffer (); 145 b.append(this.message); 146 if (this.param != null) { 147 for (int i = 0; i < this.param.length; i++) { 148 b.append(this.param[i]); 149 } 150 } 151 return b.toString(); 152 } 153 } 154 155 156 159 private int debuglevel; 160 161 164 private LogTarget[] logTargets; 165 166 167 private HashMap logContexts; 168 169 172 private static Log singleton; 173 174 177 protected Log() { 178 this.logContexts = new HashMap (); 179 this.logTargets = new LogTarget[0]; 180 this.debuglevel = 100; 181 } 182 183 188 public static synchronized Log getInstance() { 189 if (singleton == null) { 190 singleton = new Log(); 191 } 192 return singleton; 193 } 194 195 200 protected static synchronized void defineLog(final Log log) { 201 singleton = log; 202 } 203 204 210 public int getDebuglevel() { 211 return this.debuglevel; 212 } 213 214 220 protected void setDebuglevel(final int debuglevel) { 221 this.debuglevel = debuglevel; 222 } 223 224 230 public synchronized void addTarget(final LogTarget target) { 231 if (target == null) { 232 throw new NullPointerException (); 233 } 234 final LogTarget[] data = new LogTarget[this.logTargets.length + 1]; 235 System.arraycopy(this.logTargets, 0, data, 0, this.logTargets.length); 236 data[this.logTargets.length] = target; 237 this.logTargets = data; 238 } 239 240 245 public synchronized void removeTarget(final LogTarget target) { 246 if (target == null) { 247 throw new NullPointerException (); 248 } 249 final ArrayList l = new ArrayList (); 250 l.addAll(Arrays.asList(this.logTargets)); 251 l.remove(target); 252 253 final LogTarget[] targets = new LogTarget[l.size()]; 254 this.logTargets = (LogTarget[]) l.toArray(targets); 255 } 256 257 262 public LogTarget[] getTargets() { 263 return (LogTarget[]) this.logTargets.clone(); 264 } 265 266 271 public synchronized void replaceTargets(final LogTarget target) { 272 if (target == null) { 273 throw new NullPointerException (); 274 } 275 this.logTargets = new LogTarget[]{target}; 276 } 277 278 283 public static void debug(final Object message) { 284 log(LogTarget.DEBUG, message); 285 } 286 287 293 public static void debug(final Object message, final Exception e) { 294 log(LogTarget.DEBUG, message, e); 295 } 296 297 302 public static void info(final Object message) { 303 log(LogTarget.INFO, message); 304 } 305 306 312 public static void info(final Object message, final Exception e) { 313 log(LogTarget.INFO, message, e); 314 } 315 316 321 public static void warn(final Object message) { 322 log(LogTarget.WARN, message); 323 } 324 325 331 public static void warn(final Object message, final Exception e) { 332 log(LogTarget.WARN, message, e); 333 } 334 335 340 public static void error(final Object message) { 341 log(LogTarget.ERROR, message); 342 } 343 344 350 public static void error(final Object message, final Exception e) { 351 log(LogTarget.ERROR, message, e); 352 } 353 354 362 protected void doLog(int level, final Object message) { 363 if (level > 3) { 364 level = 3; 365 } 366 if (level <= this.debuglevel) { 367 for (int i = 0; i < this.logTargets.length; i++) { 368 final LogTarget t = this.logTargets[i]; 369 t.log(level, message); 370 } 371 } 372 } 373 374 382 public static void log(final int level, final Object message) { 383 getInstance().doLog(level, message); 384 } 385 386 397 public static void log(final int level, final Object message, final Exception e) { 398 getInstance().doLog(level, message, e); 399 } 400 401 412 protected void doLog(int level, final Object message, final Exception e) { 413 if (level > 3) { 414 level = 3; 415 } 416 417 if (level <= this.debuglevel) { 418 for (int i = 0; i < this.logTargets.length; i++) { 419 final LogTarget t = this.logTargets[i]; 420 t.log(level, message, e); 421 } 422 } 423 } 424 425 429 public void init() { 430 } 432 433 439 public static boolean isDebugEnabled() { 440 return getInstance().getDebuglevel() >= LogTarget.DEBUG; 441 } 442 443 449 public static boolean isInfoEnabled() { 450 return getInstance().getDebuglevel() >= LogTarget.INFO; 451 } 452 453 459 public static boolean isWarningEnabled() { 460 return getInstance().getDebuglevel() >= LogTarget.WARN; 461 } 462 463 469 public static boolean isErrorEnabled() { 470 return getInstance().getDebuglevel() >= LogTarget.ERROR; 471 } 472 473 480 public static LogContext createContext(final Class context) { 481 return createContext(context.getName()); 482 } 483 484 491 public static LogContext createContext(final String context) { 492 return getInstance().internalCreateContext(context); 493 } 494 495 502 protected LogContext internalCreateContext(final String context) { 503 synchronized (this) { 504 LogContext ctx = (LogContext) this.logContexts.get(context); 505 if (ctx == null) { 506 ctx = new LogContext(context); 507 this.logContexts.put(context, ctx); 508 } 509 return ctx; 510 } 511 } 512 513 } 514 | Popular Tags |