Created
June 8, 2022 23:02
-
-
Save stroucki/35265d8f0d9ff9cd8223db22b2676fb0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
in the activity group constructor: | |
/* | |
* ensure default exception handler is set | |
*/ | |
if (!(Thread.getDefaultUncaughtExceptionHandler() instanceof FoobarDefaultExceptionHandler)) { | |
Thread.setDefaultUncaughtExceptionHandler(new FoobarDefaultExceptionHandler(exnLogDir)); | |
} | |
FoobarDefaultExceptionHandler.java: | |
package foobar.core.utils; | |
import foobar.core.debug.DEBUG; | |
import foobar.core.debug.InternalException; | |
import java.io.BufferedWriter; | |
import java.io.File; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.io.StringWriter; | |
import java.io.Writer; | |
import java.lang.Thread.UncaughtExceptionHandler; | |
/** | |
* The exception handler of last resort | |
* | |
* @author stroucki | |
* @version 1.0 | |
* | |
*/ | |
public class FoobarDefaultExceptionHandler implements UncaughtExceptionHandler { | |
private final UncaughtExceptionHandler defaultUEH; | |
private final String dir; | |
public static final String DEHFileName = "DEHstacktrace"; | |
public FoobarDefaultExceptionHandler(String dir) { | |
File test = new File(dir); | |
boolean ok = test.exists() && test.isDirectory() && test.canWrite(); | |
if (!ok) { | |
throw new InternalException("[FoobarDefaultExceptionHandler] not a writable directory: "+dir); | |
} | |
this.dir = dir; | |
this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler(); | |
DEBUG.println("[FoobarDefaultExceptionHandler] initialized. dir: "+dir); | |
} | |
private void writeToFile(String stacktrace, String timestamp) { | |
BufferedWriter bos = null; | |
try { | |
bos = new BufferedWriter(new FileWriter( | |
dir + "/"+DEHFileName, true)); | |
bos.write("Default Exception Handler stacktrace: "+timestamp+"\n"); | |
bos.write(stacktrace); | |
bos.flush(); | |
bos.close(); | |
} catch (Exception e) { | |
DEBUG.printStackTrace(e);; | |
} | |
finally { | |
if (bos != null) { | |
try { | |
bos.close(); | |
} catch (IOException e) { | |
; | |
} | |
} | |
} | |
} | |
@Override | |
public void uncaughtException(Thread t, Throwable e) { | |
// trying to save a file first, then use the Foobar logging facility | |
// see whether the Foobar debugging gets written reliably. | |
String timestamp = Long.toString(System.currentTimeMillis()); | |
final Writer result = new StringWriter(); | |
final PrintWriter printWriter = new PrintWriter(result); | |
e.printStackTrace(printWriter); | |
String stacktrace = result.toString(); | |
printWriter.close(); | |
if (dir != null) { | |
writeToFile(stacktrace, timestamp); | |
} | |
DEBUG.printStackTrace(e); | |
DEBUG.error("[FoobarDefaultExceptionHandler] default exception handler called"); | |
// send the exception to the original handler | |
defaultUEH.uncaughtException(t, e); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment