diff --git a/app/src/main/java/eu/faircode/email/ApplicationEx.java b/app/src/main/java/eu/faircode/email/ApplicationEx.java index 2457e20777..f10dba55fa 100644 --- a/app/src/main/java/eu/faircode/email/ApplicationEx.java +++ b/app/src/main/java/eu/faircode/email/ApplicationEx.java @@ -67,7 +67,7 @@ public class ApplicationEx extends Application @Override protected void attachBaseContext(Context base) { - TinyLogConfigurationLoader.setup(base); + FairEmailLoggingProvider.setup(base); Runtime.getRuntime().addShutdownHook(new Thread() { @Override @@ -413,6 +413,10 @@ public class ApplicationEx extends Application // Should be excluded for import restart(this, key); break; + case "debug": + case "log_level": + FairEmailLoggingProvider.setLevel(this); + break; } } catch (Throwable ex) { Log.e(ex); diff --git a/app/src/main/java/eu/faircode/email/DebugHelper.java b/app/src/main/java/eu/faircode/email/DebugHelper.java index 1e5e818d6c..5c31790e1d 100644 --- a/app/src/main/java/eu/faircode/email/DebugHelper.java +++ b/app/src/main/java/eu/faircode/email/DebugHelper.java @@ -1308,7 +1308,7 @@ public class DebugHelper { attachment.progress = 0; attachment.id = db.attachment().insertAttachment(attachment); - attachment.zip(context, TinyLogConfigurationLoader.getFiles(context)); + attachment.zip(context, FairEmailLoggingProvider.getLogFiles(context)); /* // https://cheatsheetseries.owasp.org/cheatsheets/OS_Command_Injection_Defense_Cheat_Sheet.html#java ProcessBuilder pb = new ProcessBuilder("/system/bin/logcat", diff --git a/app/src/main/java/eu/faircode/email/TinyLogConfigurationLoader.java b/app/src/main/java/eu/faircode/email/FairEmailLoggingProvider.java similarity index 57% rename from app/src/main/java/eu/faircode/email/TinyLogConfigurationLoader.java rename to app/src/main/java/eu/faircode/email/FairEmailLoggingProvider.java index d21a38773f..020564a05f 100644 --- a/app/src/main/java/eu/faircode/email/TinyLogConfigurationLoader.java +++ b/app/src/main/java/eu/faircode/email/FairEmailLoggingProvider.java @@ -25,55 +25,74 @@ import android.content.SharedPreferences; import androidx.preference.PreferenceManager; import org.tinylog.Level; -import org.tinylog.configuration.PropertiesConfigurationLoader; +import org.tinylog.core.TinylogLoggingProvider; +import org.tinylog.format.MessageFormatter; +import org.tinylog.provider.ProviderRegistry; import java.io.File; import java.util.Arrays; import java.util.Comparator; -import java.util.Properties; // https://tinylog.org/v2/configuration/ // https://github.com/tinylog-org/tinylog-android-example/blob/v2/app/src/main/resources/tinylog.properties -public class TinyLogConfigurationLoader extends PropertiesConfigurationLoader { - private static Level level = Level.TRACE; +public class FairEmailLoggingProvider extends TinylogLoggingProvider { + private Level activeLevel = Level.WARN; + + public void setLevel(Level level) { + activeLevel = level; + } + + @Override + public boolean isEnabled(int depth, String tag, Level level) { + return (activeLevel.ordinal() <= level.ordinal() && + super.isEnabled(depth + 1, tag, level)); + } @Override - public Properties load() { - Properties props = super.load(); - props.setProperty("level", level.name()); - return props; + public void log(int depth, String tag, Level level, Throwable exception, MessageFormatter formatter, Object obj, Object... arguments) { + if (activeLevel.ordinal() <= level.ordinal()) + super.log(depth, tag, level, exception, formatter, obj, arguments); + } + + @Override + public void log(String loggerClassName, String tag, Level level, Throwable exception, MessageFormatter formatter, Object obj, Object... arguments) { + if (activeLevel.ordinal() <= level.ordinal()) + super.log(loggerClassName, tag, level, exception, formatter, obj, arguments); } static void setup(Context context) { + System.setProperty("tinylog.directory", + new File(context.getFilesDir(), "logs").getAbsolutePath()); + + setLevel(context); + } + + static void setLevel(Context context) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - boolean debug = prefs.getBoolean("debug", false); // Changing requires force stop + boolean debug = prefs.getBoolean("debug", false); + + FairEmailLoggingProvider provider = (FairEmailLoggingProvider) ProviderRegistry.getLoggingProvider(); if (debug) - level = Level.DEBUG; + provider.activeLevel = Level.DEBUG; else { int def = (BuildConfig.DEBUG || BuildConfig.TEST_RELEASE ? android.util.Log.INFO : android.util.Log.WARN); int _level = prefs.getInt("log_level", def); if (_level == android.util.Log.VERBOSE) - level = Level.TRACE; + provider.activeLevel = Level.TRACE; else if (_level == android.util.Log.DEBUG) - level = Level.DEBUG; + provider.activeLevel = Level.DEBUG; else if (_level == android.util.Log.INFO) - level = Level.INFO; + provider.activeLevel = Level.INFO; else if (_level == android.util.Log.WARN) - level = Level.WARN; + provider.activeLevel = Level.WARN; else if (_level == android.util.Log.ERROR) - level = Level.ERROR; + provider.activeLevel = Level.ERROR; } - - System.setProperty("tinylog.configurationloader", - TinyLogConfigurationLoader.class.getName()); - - System.setProperty("tinylog.directory", - new File(context.getFilesDir(), "logs").getAbsolutePath()); } - static File[] getFiles(Context context) { + static File[] getLogFiles(Context context) { File[] files = new File(context.getFilesDir(), "logs").listFiles(); if (files == null) diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java b/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java index 5edb0805cc..af1df1a43f 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java @@ -901,8 +901,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc swLogInfo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton v, boolean checked) { - prefs.edit().putInt("log_level", checked ? android.util.Log.INFO : android.util.Log.WARN).commit(); - ApplicationEx.restart(v.getContext(), "log_level"); + prefs.edit().putInt("log_level", checked ? android.util.Log.INFO : android.util.Log.WARN).apply(); } }); diff --git a/app/src/main/res/layout/fragment_options_misc.xml b/app/src/main/res/layout/fragment_options_misc.xml index 82d21d0950..ab6da1e614 100644 --- a/app/src/main/res/layout/fragment_options_misc.xml +++ b/app/src/main/res/layout/fragment_options_misc.xml @@ -784,19 +784,6 @@ app:layout_constraintTop_toBottomOf="@id/tvProtocolHint" app:switchPadding="12dp" /> - -