1 7 8 package java.util.logging; 9 import java.util.*; 10 import java.io.*; 11 12 48 49 public class LogRecord implements java.io.Serializable { 50 private static long globalSequenceNumber; 51 private static int nextThreadId=10; 52 private static ThreadLocal threadIds = new ThreadLocal (); 53 54 57 private Level level; 58 59 62 private long sequenceNumber; 63 64 67 private String sourceClassName; 68 69 72 private String sourceMethodName; 73 74 77 private String message; 78 79 82 private int threadID; 83 84 87 private long millis; 88 89 92 private Throwable thrown; 93 94 97 private String loggerName; 98 99 102 private String resourceBundleName; 103 104 private transient boolean needToInferCaller; 105 private transient Object parameters[]; 106 private transient ResourceBundle resourceBundle; 107 108 124 public LogRecord(Level level, String msg) { 125 level.getClass(); 127 this.level = level; 128 message = msg; 129 synchronized (LogRecord .class) { 131 sequenceNumber = globalSequenceNumber++; 132 Integer id = (Integer )threadIds.get(); 133 if (id == null) { 134 id = new Integer (nextThreadId++); 135 threadIds.set(id); 136 } 137 threadID = id.intValue(); 138 } 139 millis = System.currentTimeMillis(); 140 needToInferCaller = true; 141 } 142 143 148 public String getLoggerName() { 149 return loggerName; 150 } 151 152 157 public void setLoggerName(String name) { 158 loggerName = name; 159 } 160 161 169 public ResourceBundle getResourceBundle() { 170 return resourceBundle; 171 } 172 173 178 public void setResourceBundle(ResourceBundle bundle) { 179 resourceBundle = bundle; 180 } 181 182 189 public String getResourceBundleName() { 190 return resourceBundleName; 191 } 192 193 198 public void setResourceBundleName(String name) { 199 resourceBundleName = name; 200 } 201 202 206 public Level getLevel() { 207 return level; 208 } 209 210 214 public void setLevel(Level level) { 215 if (level == null) { 216 throw new NullPointerException (); 217 } 218 this.level = level; 219 } 220 221 229 public long getSequenceNumber() { 230 return sequenceNumber; 231 } 232 233 239 public void setSequenceNumber(long seq) { 240 sequenceNumber = seq; 241 } 242 243 257 public String getSourceClassName() { 258 if (needToInferCaller) { 259 inferCaller(); 260 } 261 return sourceClassName; 262 } 263 264 269 public void setSourceClassName(String sourceClassName) { 270 this.sourceClassName = sourceClassName; 271 needToInferCaller = false; 272 } 273 274 288 public String getSourceMethodName() { 289 if (needToInferCaller) { 290 inferCaller(); 291 } 292 return sourceMethodName; 293 } 294 295 300 public void setSourceMethodName(String sourceMethodName) { 301 this.sourceMethodName = sourceMethodName; 302 needToInferCaller = false; 303 } 304 305 319 public String getMessage() { 320 return message; 321 } 322 323 328 public void setMessage(String message) { 329 this.message = message; 330 } 331 332 338 public Object [] getParameters() { 339 return parameters; 340 } 341 342 347 public void setParameters(Object parameters[]) { 348 this.parameters = parameters; 349 } 350 351 359 public int getThreadID() { 360 return threadID; 361 } 362 363 367 public void setThreadID(int threadID) { 368 this.threadID = threadID; 369 } 370 371 376 public long getMillis() { 377 return millis; 378 } 379 380 385 public void setMillis(long millis) { 386 this.millis = millis; 387 } 388 389 397 public Throwable getThrown() { 398 return thrown; 399 } 400 401 406 public void setThrown(Throwable thrown) { 407 this.thrown = thrown; 408 } 409 410 private static final long serialVersionUID = 5372048053134512534L; 411 412 422 private void writeObject(ObjectOutputStream out) throws IOException { 423 out.defaultWriteObject(); 425 426 out.writeByte(1); 428 out.writeByte(0); 429 if (parameters == null) { 430 out.writeInt(-1); 431 return; 432 } 433 out.writeInt(parameters.length); 434 for (int i = 0; i < parameters.length; i++) { 436 if (parameters[i] == null) { 437 out.writeObject(null); 438 } else { 439 out.writeObject(parameters[i].toString()); 440 } 441 } 442 } 443 444 private void readObject(ObjectInputStream in) 445 throws IOException, ClassNotFoundException { 446 in.defaultReadObject(); 448 449 byte major = in.readByte(); 451 byte minor = in.readByte(); 452 if (major != 1) { 453 throw new IOException("LogRecord: bad version: " + major + "." + minor); 454 } 455 int len = in.readInt(); 456 if (len == -1) { 457 parameters = null; 458 } else { 459 parameters = new Object [len]; 460 for (int i = 0; i < parameters.length; i++) { 461 parameters[i] = in.readObject(); 462 } 463 } 464 if (resourceBundleName != null) { 466 try { 467 resourceBundle = ResourceBundle.getBundle(resourceBundleName); 468 } catch (MissingResourceException ex) { 469 resourceBundle = null; 472 } 473 } 474 475 needToInferCaller = false; 476 } 477 478 private void inferCaller() { 480 needToInferCaller = false; 481 StackTraceElement stack[] = (new Throwable ()).getStackTrace(); 483 int ix = 0; 485 while (ix < stack.length) { 486 StackTraceElement frame = stack[ix]; 487 String cname = frame.getClassName(); 488 if (cname.equals("java.util.logging.Logger")) { 489 break; 490 } 491 ix++; 492 } 493 while (ix < stack.length) { 495 StackTraceElement frame = stack[ix]; 496 String cname = frame.getClassName(); 497 if (!cname.equals("java.util.logging.Logger")) { 498 setSourceClassName(cname); 500 setSourceMethodName(frame.getMethodName()); 501 return; 502 } 503 ix++; 504 } 505 } 508 } 509 | Popular Tags |