From ac5f9286523f8cb3398145935855f3f0c234feb9 Mon Sep 17 00:00:00 2001 From: vanous Date: Fri, 5 Aug 2022 20:26:54 +0200 Subject: [PATCH] Add another safeguard to prevent unintended slf4j setup --- .../freeyourgadget/gadgetbridge/Logging.java | 4 ++++ .../freeyourgadget/gadgetbridge/util/GB.java | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/Logging.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/Logging.java index 8cd86c7e1..e18069ff4 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/Logging.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/Logging.java @@ -61,6 +61,10 @@ public abstract class Logging { return null; } + public boolean isFileLoggerInitialized() { + return fileLogger != null; + } + public void debugLoggingConfiguration() { // For debugging problems with the logback configuration LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/GB.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/GB.java index e55f0ee0b..a06a7c485 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/GB.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/GB.java @@ -31,6 +31,7 @@ import android.os.Handler; import android.os.Looper; import android.text.Html; import android.text.SpannableString; +import android.util.Log; import android.widget.Toast; import androidx.annotation.NonNull; @@ -98,6 +99,8 @@ public class GB { private static boolean notificationChannelsCreated; + private static final String TAG = "GB"; + public static void createNotificationChannels(Context context) { if (notificationChannelsCreated) return; @@ -459,6 +462,18 @@ public class GB { } public static void log(String message, int severity, Throwable ex) { + + // Handle if slf4j is not setup yet as this causes this issue: + // https://codeberg.org/Freeyourgadget/Gadgetbridge/issues/2394 + // and similar, as reported by users via matrix chat, because + // under some conditions the FileUtils.getWritableExternalFilesDirs + // can break the slf4j rule again, but this method is used while bootstrapping + // slf4j, so catch22... and it is useful to have proper logging when slf4f is ready. + if (!GBApplication.getLogging().isFileLoggerInitialized()) { + Log.i(TAG, message); + return; + } + switch (severity) { case INFO: LOG.info(message, ex);