1 23 package com.sun.enterprise.util; 24 25 import java.util.StringTokenizer ; 26 import java.io.ByteArrayOutputStream ; 27 import java.io.File ; 28 import java.io.PrintStream ; 29 import java.io.FileOutputStream ; 30 import java.io.IOException ; 31 import java.io.StringWriter ; 32 import java.io.Writer ; 33 import java.io.PrintWriter ; 34 35 86 87 public class Print 88 { 89 90 92 93 94 public static final String SYSTEM_DEBUG_MODE = "ri.debugMode"; 95 public static final String SYSTEM_STACK_FRAME = "ri.print.showStackFrame"; 96 97 98 private static Throwable stackTrace = null; 99 100 101 private static boolean showStackFrameSource = false; 102 103 105 106 109 public static void setDebugMode(boolean state) 110 { 111 System.setProperty(SYSTEM_DEBUG_MODE, String.valueOf(state)); 112 } 113 114 117 public static boolean isDebugMode() 118 { 119 return Boolean.getBoolean(SYSTEM_DEBUG_MODE); 120 } 121 122 124 125 128 public static void setPrintStackFrame(boolean state) 129 { 130 System.setProperty(SYSTEM_STACK_FRAME, String.valueOf(state)); 131 } 132 133 136 public static boolean printStackFrame() 137 { 138 return Boolean.getBoolean(SYSTEM_STACK_FRAME); 139 } 140 141 143 144 147 public static void setShowStackFrameSource(boolean flag) 148 { 149 showStackFrameSource = flag; 150 } 151 152 154 155 159 public static StackTraceElement getStackFrame(int frameNum) 160 { 161 if (stackTrace == null) { stackTrace = new Throwable (); } 162 stackTrace.fillInStackTrace(); 163 return stackTrace.getStackTrace()[frameNum + 1]; 164 } 165 166 170 public static String getStackFrameString(int frameNum) 171 { 172 return getStackFrameString(frameNum + 1, showStackFrameSource); 173 } 174 175 179 public static String getStackFrameString(StackTraceElement stackFrame) 180 { 181 return getStackFrameString(stackFrame, showStackFrameSource); 182 } 183 184 189 public static String getStackFrameString(int frameNum, boolean showSrc) 190 { 191 return getStackFrameString(getStackFrame(frameNum + 1), showSrc); 192 } 193 194 199 public static String getStackFrameString(StackTraceElement stackFrame, boolean showSrc) 200 { 201 StringBuffer sb = new StringBuffer (); 202 if (showSrc) { 203 String s = stackFrame.getFileName(); 204 if (s != null) { 205 int p = s.lastIndexOf(File.separator); 206 sb.append((p >= 0)? s.substring(p + 1) : s); 207 } else { 208 String c = stackFrame.getClassName(); 209 int p = c.lastIndexOf("."); 210 sb.append("<").append((p >= 0)? c.substring(p + 1) : c).append(">"); 211 } 212 } else { 213 String c = stackFrame.getClassName(); 214 int p = c.lastIndexOf("."); 215 sb.append((p >= 0)? c.substring(p + 1) : c); 216 } 217 sb.append(".").append(stackFrame.getMethodName()); 218 if (stackFrame.getLineNumber() >= 0) { 219 sb.append(":").append(stackFrame.getLineNumber()); 220 } 221 return sb.toString(); 222 } 223 224 226 227 232 protected static void _println(PrintStream out, int frameNum, String msg) 233 { 234 String m = (frameNum >= 0)? "[" + getStackFrameString(frameNum + 1) + "] " + msg : msg; 235 print(out, m + "\n"); 236 } 237 238 240 241 246 public static void print(PrintStream out, String msg) 247 { 248 ((out != null)? out : System.out).print(msg); 249 } 250 251 257 public static void dprint(PrintStream out, String msg) 258 { 259 if (isDebugMode()) { 260 print(out, msg); 261 } 262 } 263 264 268 public static void print(String msg) 269 { 270 print(null, msg); 271 } 272 273 278 public static void dprint(String msg) 279 { 280 if (isDebugMode()) { 281 print(null, msg); 282 } 283 } 284 285 294 public static void println(PrintStream out, int frameNum, String msg) 295 { 296 int f = (frameNum >= 0)? (frameNum + 1) : (printStackFrame()? Math.abs(frameNum) : -1); 297 _println(out, f, msg); 298 } 299 300 308 public static void dprintln(PrintStream out, int frameNum, String msg) 309 { 310 if (isDebugMode()) { 311 int f = (frameNum >= 0)? (frameNum + 1) : Math.abs(frameNum); 312 _println(out, f, msg); 313 } 314 } 315 316 324 public static void println(int frameNum, String msg) 325 { 326 int f = (frameNum >= 0)? (frameNum + 1) : (printStackFrame()? Math.abs(frameNum) : -1); 327 _println(null, f, msg); 328 } 329 330 337 public static void dprintln(int frameNum, String msg) 338 { 339 if (isDebugMode()) { 340 int f = (frameNum >= 0)? (frameNum + 1) : Math.abs(frameNum); 341 _println(null, f, msg); 342 } 343 } 344 345 351 public static void println(PrintStream out, String msg) 352 { 353 _println(out, (printStackFrame()? 1 : -1), msg); 354 } 355 356 363 public static void dprintln(PrintStream out, String msg) 364 { 365 if (isDebugMode()) { 366 _println(out, 1, msg); 367 } 368 } 369 370 375 public static void println(String msg) 376 { 377 _println(null, (printStackFrame()? 1 : -1), msg); 378 } 379 380 386 public static void dprintln(String msg) 387 { 388 if (isDebugMode()) { 389 _println(null, 1, msg); 390 } 391 } 392 393 395 396 private static PrintStream printStackTrace_Stream = null; 397 private static File printStackTrace_LogFile = null; 398 399 402 public static void setStackTraceLogFile(File logFile) 403 { 404 405 406 if (printStackTrace_Stream != null) { 407 printStackTrace_Stream.close(); 408 printStackTrace_Stream = null; 409 printStackTrace_LogFile = null; 410 } 411 412 413 if ((logFile != null) && logFile.isAbsolute()) { 414 try { 415 printStackTrace_Stream = new PrintStream (new FileOutputStream (logFile), true); 416 printStackTrace_LogFile = logFile; 417 } catch (IOException ioe) { 418 Print.println(0, "Unable to open StackTrace log file: " + logFile); 419 } 420 } 421 422 } 423 424 431 private static void _printStackTrace(PrintStream out, int frame, String title, String msg, Throwable excp) 432 { 433 434 435 final String _dash = "----------------------------------------"; final String dashLine = _dash + _dash + "\n"; final String _ttl = " " + title + " "; 438 final String errTitle = dashLine.substring(0, 16) + _ttl + dashLine.substring(16 + _ttl.length()); 439 440 441 if (out == null) { 442 out = System.out; 443 } 444 445 446 Print.print(out, "\n"); 447 Print.print(out, dashLine + errTitle); 448 Print.print(out, "[" + Print.getStackFrameString(frame + 1) + "]\n"); 449 if ((msg != null) && !msg.equals("")) { Print.print(out, msg + "\n"); } 450 451 452 if (excp != null) { 453 Print.print(out, excp.toString() + "\n"); 454 Print.print(out, dashLine); 455 excp.printStackTrace(out); 456 } else { 457 Print.print(out, dashLine); 458 Print.print(out, "Stack Trace:\n"); 459 Throwable t = new Throwable (); 460 t.fillInStackTrace(); 461 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 462 PrintStream ps = new PrintStream (bos); 463 t.printStackTrace(ps); 464 StringTokenizer st = new StringTokenizer (bos.toString(), "\n"); 465 st.nextToken(); for (int i = 0; i < frame + 1; i++) { st.nextToken(); } for (;st.hasMoreTokens();) { Print.println(out, st.nextToken()); } 468 } 469 470 471 Print.print(out, dashLine); 472 Print.print(out, "\n"); 473 474 } 475 476 483 public static void printStackTrace(PrintStream out, int frame, String title, String msg, Throwable excp) 484 { 485 if (out == null) { out = printStackTrace_Stream; } 486 if ((out != null) && (out != System.out) && (out != System.err)) { 487 Print.print("\n"); 488 if (excp != null) { 489 String m = ((msg != null) && !msg.equals(""))? (msg + " - ") : ""; 490 Print.println(null, frame + 1, "Error: " + m + excp + ""); 491 StackTraceElement ste[] = excp.getStackTrace(); 492 if (ste.length > 0) { 493 Print.print(" ==> at " + getStackFrameString(ste[0]) + "\n"); 494 } 495 } else { 496 Print.println(null, frame + 1, msg); 497 } 498 if (printStackTrace_LogFile != null) { 499 Print.print("(Stack trace logged to '" + printStackTrace_LogFile + "')\n"); 500 } 501 Print.print("\n"); 502 } 503 _printStackTrace(out, frame + 1, title, msg, excp); 504 } 505 506 514 public static void dprintStackTrace(PrintStream out, int frame, String title, String msg, Throwable excp) 515 { 516 if (isDebugMode()) { 517 if (out == null) { out = printStackTrace_Stream; } 518 if ((out != null) && (out != System.out) && (out != System.err)) { 519 String m = (excp != null)? (msg + " (" + excp + ")") : msg; 520 Print.println(null, frame + 1, "{log} " + m); 521 } 522 _printStackTrace(out, frame + 1, title, msg, excp); 523 } 524 } 525 526 531 public static void printStackTrace(String title, String msg, Throwable excp) 532 { 533 printStackTrace(null, 1, title, msg, excp); 534 } 535 536 542 public static void dprintStackTrace(String title, String msg, Throwable excp) 543 { 544 if (isDebugMode()) { 545 dprintStackTrace(null, 1, title, msg, excp); 546 } 547 } 548 549 554 public static void printStackTrace(String msg, Throwable excp) 555 { 556 String title = (excp != null)? "Exception" : "StackTrace"; 557 printStackTrace(null, 1, title, msg, excp); 558 } 559 560 566 public static void dprintStackTrace(String msg, Throwable excp) 567 { 568 if (isDebugMode()) { 569 String title = (excp != null)? "(DEBUG) Exception" : "(DEBUG) StackTrace"; 570 dprintStackTrace(null, 1, title, msg, excp); 571 } 572 } 573 574 578 public static void printStackTrace(String msg) 579 { 580 printStackTrace(null, 1, "StackTrace", msg, null); 581 } 582 583 588 public static void dprintStackTrace(String msg) 589 { 590 if (isDebugMode()) { 591 dprintStackTrace(null, 1, "(DEBUG) StackTrace", msg, null); 592 } 593 } 594 595 600 public static void printStackTrace(int frame, String msg) 601 { 602 printStackTrace(null, frame + 1, "StackTrace", msg, null); 603 } 604 605 611 public static void dprintStackTrace(int frame, String msg) 612 { 613 if (isDebugMode()) { 614 dprintStackTrace(null, frame + 1, "(DEBUG) StackTrace", msg, null); 615 } 616 } 617 618 public static String printStackTraceToString() 619 { 620 Throwable t = new Throwable ("printStackTraceToString"); 621 t.fillInStackTrace(); 622 623 StringWriter stringWriter = new StringWriter (); 624 PrintWriter printWriter = new PrintWriter (stringWriter); 625 t.printStackTrace(printWriter); 626 String stackTrace = stringWriter.toString(); 627 628 printWriter.close(); 629 630 return stackTrace; 631 } 632 } 633 | Popular Tags |