src/share/classes/sun/util/logging/PlatformLogger.java

Print this page




 299      * Logs a FINEST message.
 300      */
 301     public void finest(String msg) {
 302         logger.doLog(FINEST, msg);
 303     }
 304 
 305     public void finest(String msg, Throwable t) {
 306         logger.doLog(FINEST, msg, t);
 307     }
 308 
 309     public void finest(String msg, Object... params) {
 310         logger.doLog(FINEST, msg, params);
 311     }
 312 
 313     /**
 314      * Default platform logging support - output messages to
 315      * System.err - equivalent to ConsoleHandler with SimpleFormatter.
 316      */
 317     static class LoggerProxy {
 318         private static final PrintStream defaultStream = System.err;
 319         private static final String lineSeparator = AccessController.doPrivileged(
 320             new PrivilegedAction<String>() {
 321                 public String run() {
 322                     return System.getProperty("line.separator");
 323                 }
 324             });
 325 
 326         final String name;
 327         volatile int levelValue;
 328         volatile int effectiveLevel = 0; // current effective level value
 329 
 330         LoggerProxy(String name) {
 331             this(name, defaultLevel);
 332         }
 333 
 334         LoggerProxy(String name, int level) {
 335             this.name = name;
 336             this.levelValue = level == 0 ? defaultLevel : level;
 337         }
 338 
 339         boolean isEnabled() {
 340             return levelValue != OFF;
 341         }
 342 
 343         int getLevel() {
 344             return effectiveLevel;
 345         }
 346 
 347         void setLevel(int newLevel) {
 348             levelValue = newLevel;
 349             effectiveLevel = newLevel;
 350         }
 351 
 352         void doLog(int level, String msg) {
 353             if (level < levelValue || levelValue == OFF) {
 354                 return;
 355             }
 356             defaultStream.println(format(level, msg, null));
 357         }
 358 
 359         void doLog(int level, String msg, Throwable thrown) {
 360             if (level < levelValue || levelValue == OFF) {
 361                 return;
 362             }
 363             defaultStream.println(format(level, msg, thrown));
 364         }
 365 
 366         void doLog(int level, String msg, Object... params) {
 367             if (level < levelValue || levelValue == OFF) {
 368                 return;
 369             }
 370             String newMsg = formatMessage(msg, params);
 371             defaultStream.println(format(level, newMsg, null));
 372         }
 373 
 374         public boolean isLoggable(int level) {
 375             if (level < levelValue || levelValue == OFF) {
 376                 return false;
 377             }
 378             return true;
 379         }
 380 
 381         private static final String format = "{0,date} {0,time}";
 382 
 383         private Object args[] = new Object[1];
 384         private MessageFormat formatter;
 385         private Date dat;
 386 
 387         // Copied from java.util.logging.Formatter.formatMessage
 388         private String formatMessage(String format, Object... parameters) {
 389             // Do the formatting.
 390             try {
 391                 if (parameters == null || parameters.length == 0) {
 392                     // No parameters.  Just return format string.
 393                     return format;
 394                 }
 395                 // Is it a java.text style format?
 396                 // Ideally we could match with
 397                 // Pattern.compile("\\{\\d").matcher(format).find())
 398                 // However the cost is 14% higher, so we cheaply check for
 399                 // 1 of the first 4 parameters
 400                 if (format.indexOf("{0") >= 0 || format.indexOf("{1") >=0 ||
 401                             format.indexOf("{2") >=0|| format.indexOf("{3") >=0) {
 402                     return java.text.MessageFormat.format(format, parameters);
 403                 }
 404                 return format;
 405             } catch (Exception ex) {
 406                 // Formatting failed: use format string.
 407                 return format;
 408             }
 409         }
 410 





 411         private synchronized String format(int level, String msg, Throwable thrown) {
 412             StringBuffer sb = new StringBuffer();
 413             // Minimize memory allocations here.
 414             if (dat == null) {
 415                 dat = new Date();
 416                 formatter = new MessageFormat(format);
 417             }
 418             dat.setTime(System.currentTimeMillis());
 419             args[0] = dat;
 420             StringBuffer text = new StringBuffer();
 421             formatter.format(args, text, null);
 422             sb.append(text);
 423             sb.append(" ");
 424             sb.append(getCallerInfo());
 425             sb.append(lineSeparator);
 426             sb.append(PlatformLogger.getLevelName(level));
 427             sb.append(": ");
 428             sb.append(msg);
 429             if (thrown != null) {
 430                 try {
 431                     StringWriter sw = new StringWriter();

 432                     PrintWriter pw = new PrintWriter(sw);
 433                     thrown.printStackTrace(pw);
 434                     pw.close();
 435                     sb.append(sw.toString());
 436                 } catch (Exception ex) {
 437                     throw new AssertionError(ex);
 438                 }
 439             }
 440 
 441             return sb.toString();






 442         }
 443 
 444         // Returns the caller's class and method's name; best effort
 445         // if cannot infer, return the logger's name.
 446         private String getCallerInfo() {
 447             String sourceClassName = null;
 448             String sourceMethodName = null;
 449 
 450             JavaLangAccess access = SharedSecrets.getJavaLangAccess();
 451             Throwable throwable = new Throwable();
 452             int depth = access.getStackTraceDepth(throwable);
 453 
 454             String logClassName = "sun.util.logging.PlatformLogger";
 455             boolean lookingForLogger = true;
 456             for (int ix = 0; ix < depth; ix++) {
 457                 // Calling getStackTraceElement directly prevents the VM
 458                 // from paying the cost of building the entire stack frame.
 459                 StackTraceElement frame =
 460                     access.getStackTraceElement(throwable, ix);
 461                 String cname = frame.getClassName();




 299      * Logs a FINEST message.
 300      */
 301     public void finest(String msg) {
 302         logger.doLog(FINEST, msg);
 303     }
 304 
 305     public void finest(String msg, Throwable t) {
 306         logger.doLog(FINEST, msg, t);
 307     }
 308 
 309     public void finest(String msg, Object... params) {
 310         logger.doLog(FINEST, msg, params);
 311     }
 312 
 313     /**
 314      * Default platform logging support - output messages to
 315      * System.err - equivalent to ConsoleHandler with SimpleFormatter.
 316      */
 317     static class LoggerProxy {
 318         private static final PrintStream defaultStream = System.err;






 319 
 320         final String name;
 321         volatile int levelValue;
 322         volatile int effectiveLevel = 0; // current effective level value
 323 
 324         LoggerProxy(String name) {
 325             this(name, defaultLevel);
 326         }
 327 
 328         LoggerProxy(String name, int level) {
 329             this.name = name;
 330             this.levelValue = level == 0 ? defaultLevel : level;
 331         }
 332 
 333         boolean isEnabled() {
 334             return levelValue != OFF;
 335         }
 336 
 337         int getLevel() {
 338             return effectiveLevel;
 339         }
 340 
 341         void setLevel(int newLevel) {
 342             levelValue = newLevel;
 343             effectiveLevel = newLevel;
 344         }
 345 
 346         void doLog(int level, String msg) {
 347             if (level < levelValue || levelValue == OFF) {
 348                 return;
 349             }
 350             defaultStream.print(format(level, msg, null));
 351         }
 352 
 353         void doLog(int level, String msg, Throwable thrown) {
 354             if (level < levelValue || levelValue == OFF) {
 355                 return;
 356             }
 357             defaultStream.print(format(level, msg, thrown));
 358         }
 359 
 360         void doLog(int level, String msg, Object... params) {
 361             if (level < levelValue || levelValue == OFF) {
 362                 return;
 363             }
 364             String newMsg = formatMessage(msg, params);
 365             defaultStream.print(format(level, newMsg, null));
 366         }
 367 
 368         public boolean isLoggable(int level) {
 369             if (level < levelValue || levelValue == OFF) {
 370                 return false;
 371             }
 372             return true;
 373         }
 374 






 375         // Copied from java.util.logging.Formatter.formatMessage
 376         private String formatMessage(String format, Object... parameters) {
 377             // Do the formatting.
 378             try {
 379                 if (parameters == null || parameters.length == 0) {
 380                     // No parameters.  Just return format string.
 381                     return format;
 382                 }
 383                 // Is it a java.text style format?
 384                 // Ideally we could match with
 385                 // Pattern.compile("\\{\\d").matcher(format).find())
 386                 // However the cost is 14% higher, so we cheaply check for
 387                 // 1 of the first 4 parameters
 388                 if (format.indexOf("{0") >= 0 || format.indexOf("{1") >=0 ||
 389                             format.indexOf("{2") >=0|| format.indexOf("{3") >=0) {
 390                     return java.text.MessageFormat.format(format, parameters);
 391                 }
 392                 return format;
 393             } catch (Exception ex) {
 394                 // Formatting failed: use format string.
 395                 return format;
 396             }
 397         }
 398 
 399         private static final String formatString =
 400             LoggingSupport.getSimpleFormat(false); // don't check logging.properties
 401 
 402         // minimize memory allocation
 403         private Date date = new Date();
 404         private synchronized String format(int level, String msg, Throwable thrown) {
 405             date.setTime(System.currentTimeMillis());
 406             String throwable = "";















 407             if (thrown != null) {

 408                 StringWriter sw = new StringWriter();
 409                 sw.append('\n');
 410                 PrintWriter pw = new PrintWriter(sw);
 411                 thrown.printStackTrace(pw);
 412                 pw.close();
 413                 throwable = sw.toString();


 414             }

 415 
 416             return String.format(formatString,
 417                                  date,
 418                                  getCallerInfo(),
 419                                  name,
 420                                  PlatformLogger.getLevelName(level),
 421                                  msg,
 422                                  throwable);
 423         }
 424 
 425         // Returns the caller's class and method's name; best effort
 426         // if cannot infer, return the logger's name.
 427         private String getCallerInfo() {
 428             String sourceClassName = null;
 429             String sourceMethodName = null;
 430 
 431             JavaLangAccess access = SharedSecrets.getJavaLangAccess();
 432             Throwable throwable = new Throwable();
 433             int depth = access.getStackTraceDepth(throwable);
 434 
 435             String logClassName = "sun.util.logging.PlatformLogger";
 436             boolean lookingForLogger = true;
 437             for (int ix = 0; ix < depth; ix++) {
 438                 // Calling getStackTraceElement directly prevents the VM
 439                 // from paying the cost of building the entire stack frame.
 440                 StackTraceElement frame =
 441                     access.getStackTraceElement(throwable, ix);
 442                 String cname = frame.getClassName();