Log otherwise uncaught exceptions (#134)

here
cpfeiffer 2015-10-07 23:32:58 +02:00
parent 600e7d59b5
commit 4533ae22ee
2 changed files with 31 additions and 0 deletions

View File

@ -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);
}

View File

@ -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);
}
}
}