001package arcommon.exception;
002
003import java.text.SimpleDateFormat;
004import java.util.Date;
005
006public class ExceptionData {
007    private Thread thread;
008    private Throwable throwable;
009    private String timeString;
010    
011    public ExceptionData(Throwable pthrowable) {
012        this(Thread.currentThread(), pthrowable, 0);
013    }
014    
015    public ExceptionData(Thread pthread, Throwable pthrowable) {
016        this(pthread, pthrowable, 0);
017    }
018    
019    public ExceptionData(Thread pthread, Throwable pthrowable, long time) {
020        thread = pthread;
021        throwable = pthrowable;
022        
023        if (time == 0) {
024            time = new Date().getTime();
025        }
026        timeString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);
027    }
028    
029    public String getTimeString() {
030        return timeString;
031    }
032    
033    public String exceptionStackToString(boolean includeTime) {
034        String text = includeTime ? timeString + " " : "";
035        if (thread != null) {
036            text += "Exception occurred in thread: ";
037            String simpleName = thread.getClass().getName();
038            text += simpleName;
039            
040            String threadName = thread.getName();
041            if (threadName != null && threadName.length() > 0) {
042                text += " [" + threadName + "]";
043            }
044            
045            if (thread.isDaemon()) {
046                text += " (Daemon)";
047            }
048            
049            text += System.getProperty("line.separator");
050        }
051        
052        String exceptionStackToString = ExceptionUtil.exceptionToString(throwable);
053        exceptionStackToString = exceptionStackToString.replace("\t", "    ");
054        
055        return text + exceptionStackToString;
056    }
057}