From 909e946e58f41065d600de728e36680850177cdf Mon Sep 17 00:00:00 2001 From: akwizgran <michael@briarproject.org> Date: Mon, 1 Jul 2019 14:34:51 +0100 Subject: [PATCH] Enable debug logging for debug and beta builds. --- .../briar/android/BriarApplicationImpl.java | 19 ++++++---- .../briar/android/LevelRaisingHandler.java | 36 +++++++++++++++++++ 2 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 briar-android/src/main/java/org/briarproject/briar/android/LevelRaisingHandler.java diff --git a/briar-android/src/main/java/org/briarproject/briar/android/BriarApplicationImpl.java b/briar-android/src/main/java/org/briarproject/briar/android/BriarApplicationImpl.java index c0b3c3da7b..a7de9fb1c5 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/BriarApplicationImpl.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/BriarApplicationImpl.java @@ -37,6 +37,7 @@ import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREG import static android.os.Build.VERSION.SDK_INT; import static java.util.logging.Level.FINE; import static java.util.logging.Level.INFO; +import static java.util.logging.Logger.getLogger; import static org.acra.ReportField.ANDROID_VERSION; import static org.acra.ReportField.APP_VERSION_CODE; import static org.acra.ReportField.APP_VERSION_NAME; @@ -82,7 +83,7 @@ public class BriarApplicationImpl extends Application implements BriarApplication { private static final Logger LOG = - Logger.getLogger(BriarApplicationImpl.class.getName()); + getLogger(BriarApplicationImpl.class.getName()); private final CachingLogHandler logHandler = new CachingLogHandler(); private final BackgroundMonitor backgroundMonitor = new BackgroundMonitor(); @@ -108,12 +109,16 @@ public class BriarApplicationImpl extends Application if (IS_DEBUG_BUILD) enableStrictMode(); - Logger rootLogger = Logger.getLogger(""); - if (!IS_DEBUG_BUILD && !IS_BETA_BUILD) { - // Remove default log handlers so system log is not used - for (Handler handler : rootLogger.getHandlers()) { - rootLogger.removeHandler(handler); - } + Logger rootLogger = getLogger(""); + Handler[] handlers = rootLogger.getHandlers(); + // Disable the Android logger for release builds + for (Handler handler : handlers) rootLogger.removeHandler(handler); + if (IS_DEBUG_BUILD || IS_BETA_BUILD) { + // We can't set the level of the Android logger at runtime, so + // raise records to the logger's default level + rootLogger.addHandler(new LevelRaisingHandler(FINE, INFO)); + // Restore the default handlers after the level raising handler + for (Handler handler : handlers) rootLogger.addHandler(handler); } rootLogger.addHandler(logHandler); rootLogger.setLevel(IS_DEBUG_BUILD || IS_BETA_BUILD ? FINE : INFO); diff --git a/briar-android/src/main/java/org/briarproject/briar/android/LevelRaisingHandler.java b/briar-android/src/main/java/org/briarproject/briar/android/LevelRaisingHandler.java new file mode 100644 index 0000000000..347ae17495 --- /dev/null +++ b/briar-android/src/main/java/org/briarproject/briar/android/LevelRaisingHandler.java @@ -0,0 +1,36 @@ +package org.briarproject.briar.android; + +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.LogRecord; + +/** + * Log handler that raises all records at or above a given source level to a + * given destination level. This affects the level seen by subsequent handlers. + */ +class LevelRaisingHandler extends Handler { + + private final Level dest; + private final int srcInt, destInt; + + LevelRaisingHandler(Level src, Level dest) { + this.dest = dest; + srcInt = src.intValue(); + destInt = dest.intValue(); + if (srcInt > destInt) throw new IllegalArgumentException(); + } + + @Override + public void publish(LogRecord record) { + int recordInt = record.getLevel().intValue(); + if (recordInt >= srcInt && recordInt < destInt) record.setLevel(dest); + } + + @Override + public void flush() { + } + + @Override + public void close() { + } +} -- GitLab