diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java index 4dff5599..187682ef 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java @@ -52,6 +52,8 @@ public class GBApplication extends Application { // don't do anything here before we set up logging, otherwise // slf4j may be implicitly initialized before we properly configured it. setupLogging(); + + setupExceptionHandler(); // For debugging problems with the logback configuration // LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); // print logback's internal status @@ -67,6 +69,11 @@ public class GBApplication extends Application { // db.close(); } + private void setupExceptionHandler() { + LoggingExceptionHandler handler = new LoggingExceptionHandler(Thread.getDefaultUncaughtExceptionHandler()); + Thread.setDefaultUncaughtExceptionHandler(handler); + } + public static boolean isFileLoggingEnabled() { return sharedPrefs.getBoolean("log_to_file", false); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/LoggingExceptionHandler.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/LoggingExceptionHandler.java new file mode 100644 index 00000000..02e8c943 --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/LoggingExceptionHandler.java @@ -0,0 +1,24 @@ +package nodomain.freeyourgadget.gadgetbridge; + + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class LoggingExceptionHandler implements Thread.UncaughtExceptionHandler { + private static final Logger LOG = LoggerFactory.getLogger(LoggingExceptionHandler.class); + private final Thread.UncaughtExceptionHandler mDelegate; + + public LoggingExceptionHandler(Thread.UncaughtExceptionHandler delegate) { + mDelegate = delegate; + } + + @Override + public void uncaughtException(Thread thread, Throwable ex) { + LOG.error("Uncaught exception: " + ex.getMessage(), ex); + if (mDelegate != null) { + mDelegate.uncaughtException(thread, ex); + } else { + System.exit(1); + } + } +}