Skip to content

Commit e921412

Browse files
committed
Log uncatched exceptions
1 parent 64282c2 commit e921412

File tree

1 file changed

+16
-2
lines changed
  • sg2-app/src/cz/hartrik/sg2/app/sandbox

1 file changed

+16
-2
lines changed

sg2-app/src/cz/hartrik/sg2/app/sandbox/Main.java

+16-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import cz.hartrik.sg2.engine.Platform;
1212
import java.io.IOException;
1313
import java.io.InputStream;
14+
import java.io.PrintWriter;
15+
import java.io.StringWriter;
1416
import java.nio.file.Path;
1517
import java.util.Locale;
1618
import java.util.Map;
@@ -42,7 +44,8 @@ public class Main extends javafx.application.Application {
4244

4345
@Override
4446
public void start(Stage stage) throws Exception {
45-
initLogging();
47+
loadLoggingConfig();
48+
initGlobalExceptionHandler();
4649
processParameters(getParameters());
4750

4851
setUserAgentStylesheet(STYLESHEET_MODENA);
@@ -61,7 +64,7 @@ public void start(Stage stage) throws Exception {
6164
frame.show();
6265
}
6366

64-
private void initLogging() {
67+
private void loadLoggingConfig() {
6568
InputStream inputStream = getClass().getResourceAsStream(LOGGING_CONFIG);
6669

6770
try {
@@ -72,6 +75,17 @@ private void initLogging() {
7275
}
7376
}
7477

78+
private void initGlobalExceptionHandler() {
79+
Thread.setDefaultUncaughtExceptionHandler((t, e) -> {
80+
StringWriter sw = new StringWriter();
81+
PrintWriter pw = new PrintWriter(sw);
82+
e.printStackTrace(pw);
83+
String stackTrace = sw.toString(); // stack trace as a string
84+
85+
Logger.getGlobal().severe("Uncatched exception:\n" + stackTrace);
86+
});
87+
}
88+
7589
private void processParameters(Parameters parameters) {
7690
Map<String, String> map = parameters.getNamed();
7791

0 commit comments

Comments
 (0)