1 4 package com.tc.simulator.app; 5 6 import java.io.PrintStream ; 7 import java.io.PrintWriter ; 8 import java.io.StringWriter ; 9 10 public class ErrorContext { 11 private final String message; 12 private final Thread thread; 13 private Throwable throwable; 14 15 public ErrorContext(String message) { 16 this.message = message; 17 this.thread = Thread.currentThread(); 18 } 19 20 public ErrorContext(String message, Throwable throwable) { 21 this(message); 22 this.throwable = throwable; 23 } 24 25 public ErrorContext(Throwable t) { 26 this("", t); 27 } 28 29 public String getMessage() { 30 return message; 31 } 32 33 public Thread getThread() { 34 return thread; 35 } 36 37 public Throwable getThrowable() { 38 return throwable; 39 } 40 41 public String toString() { 42 43 StringWriter sw = new StringWriter (); 44 PrintWriter pw = new PrintWriter (sw); 45 dump(pw); 46 pw.flush(); 47 return sw.getBuffer().toString(); 48 } 49 50 54 public void dump(PrintWriter out) { 55 out.println(getClass().getName() + " [" + message + ", thread: " + thread + "]"); 56 if (throwable != null) { 57 throwable.printStackTrace(out); 58 } 59 } 60 61 public void dump(PrintStream out) { 62 out.println(getClass().getName() + " [" + message + ", thread: " + thread + "]"); 63 if (throwable != null) { 64 throwable.printStackTrace(out); 65 } 66 } 67 } | Popular Tags |