package cgmblelib.utils;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;

/**
 * Created by minyuchun on 2019/4/18.
 * 全局异常捕获
 * @author minyuchun
 */
public class CrashHandler implements Thread.UncaughtExceptionHandler{

    @Override
    public void uncaughtException(Thread t, Throwable e) {
        String errorMessage = getStackTraceInfo(e);
        MyLog.eLog("程序出现异常了"+ "Thread = " + t.getName()
                + "\nThrowable = " + e.getMessage()
                + "\nThrowableMessage = "+ errorMessage);
    }


    private String getStackTraceInfo(final Throwable throwable) {
        PrintWriter pw = null;
        Writer writer = new StringWriter();
        try {
            pw = new PrintWriter(writer);
            throwable.printStackTrace(pw);
        } catch (Exception e) {
            return "";
        } finally {
            if (pw != null) {
                pw.close();
            }
        }
        return writer.toString();
    }

}
