From bee33fcf9f282b129b28893870f027c9256bf84a Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 27 Feb 2021 17:45:08 +0100 Subject: [PATCH] Added log level debug option --- .../java/eu/faircode/email/ApplicationEx.java | 4 ++ .../java/eu/faircode/email/EmailService.java | 6 ++- .../faircode/email/FragmentOptionsMisc.java | 13 ++++++- app/src/main/java/eu/faircode/email/Log.java | 38 +++++++++++++++---- .../main/res/layout/fragment_options_misc.xml | 15 +++++++- app/src/main/res/values/strings.xml | 1 + 6 files changed, 63 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ApplicationEx.java b/app/src/main/java/eu/faircode/email/ApplicationEx.java index 417bdd0bd4..aceddd8679 100644 --- a/app/src/main/java/eu/faircode/email/ApplicationEx.java +++ b/app/src/main/java/eu/faircode/email/ApplicationEx.java @@ -200,6 +200,10 @@ public class ApplicationEx extends Application // Should be excluded for import restart(); break; + case "debug": + case "log_level": + Log.setLevel(this); + break; } } diff --git a/app/src/main/java/eu/faircode/email/EmailService.java b/app/src/main/java/eu/faircode/email/EmailService.java index a77fb16a94..45874bd031 100644 --- a/app/src/main/java/eu/faircode/email/EmailService.java +++ b/app/src/main/java/eu/faircode/email/EmailService.java @@ -577,8 +577,10 @@ public class EmailService implements AutoCloseable { if (!line.endsWith("ignoring socket timeout")) if (log) EntityLog.log(context, user + " " + line); - else - android.util.Log.i("javamail", user + " " + line); + else { + if (BuildConfig.DEBUG) + Log.i("javamail", user + " " + line); + } bos.reset(); } else bos.write(b); diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java b/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java index 4b802d03ea..ff7a30cd4f 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java @@ -110,6 +110,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc private SwitchCompat swProtocol; private SwitchCompat swDebug; + private SwitchCompat swInfo; private SwitchCompat swExpunge; private SwitchCompat swAuthPlain; private SwitchCompat swAuthLogin; @@ -135,7 +136,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc "classification", "class_min_probability", "class_min_difference", "language", "watchdog", "updates", "experiments", "wal", "query_threads", "crash_reports", "cleanup_attachments", - "protocol", "debug", "perform_expunge", "auth_plain", "auth_login", "auth_ntlm", "auth_sasl" + "protocol", "debug", "log_level", "perform_expunge", "auth_plain", "auth_login", "auth_ntlm", "auth_sasl" }; private final static String[] RESET_QUESTIONS = new String[]{ @@ -206,6 +207,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc swProtocol = view.findViewById(R.id.swProtocol); swDebug = view.findViewById(R.id.swDebug); + swInfo = view.findViewById(R.id.swInfo); swExpunge = view.findViewById(R.id.swExpunge); swAuthPlain = view.findViewById(R.id.swAuthPlain); swAuthLogin = view.findViewById(R.id.swAuthLogin); @@ -471,11 +473,17 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc @Override public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { prefs.edit().putBoolean("debug", checked).apply(); - Log.setDebug(checked); grpDebug.setVisibility(checked || BuildConfig.DEBUG ? View.VISIBLE : View.GONE); } }); + swInfo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { + prefs.edit().putInt("log_level", checked ? android.util.Log.INFO : android.util.Log.WARN).apply(); + } + }); + swProtocol.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { @@ -868,6 +876,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc swProtocol.setChecked(prefs.getBoolean("protocol", false)); swDebug.setChecked(prefs.getBoolean("debug", false)); + swInfo.setChecked(prefs.getInt("log_level", Log.getDefaultLogLevel()) <= android.util.Log.INFO); swExpunge.setChecked(prefs.getBoolean("perform_expunge", true)); swAuthPlain.setChecked(prefs.getBoolean("auth_plain", true)); swAuthLogin.setChecked(prefs.getBoolean("auth_login", true)); diff --git a/app/src/main/java/eu/faircode/email/Log.java b/app/src/main/java/eu/faircode/email/Log.java index a16011d87d..7c52520499 100644 --- a/app/src/main/java/eu/faircode/email/Log.java +++ b/app/src/main/java/eu/faircode/email/Log.java @@ -123,28 +123,52 @@ import javax.net.ssl.SSLPeerUnverifiedException; import io.requery.android.database.CursorWindowAllocationException; public class Log { - private static boolean debug = false; + private static int level = android.util.Log.INFO; private static final int MAX_CRASH_REPORTS = 5; private static final String TAG = "fairemail"; - public static void setDebug(boolean value) { - debug = value; + public static void setLevel(Context context) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + boolean debug = prefs.getBoolean("debug", false); + if (debug) + level = android.util.Log.DEBUG; + else + level = prefs.getInt("log_level", getDefaultLogLevel()); + android.util.Log.d(TAG, "Log level=" + level); + } + + public static int getDefaultLogLevel() { + return (BuildConfig.DEBUG ? android.util.Log.INFO : android.util.Log.WARN); } public static int d(String msg) { - if (debug) + if (level <= android.util.Log.DEBUG) return android.util.Log.d(TAG, msg); else return 0; } + public static int d(String tag, String msg) { + if (level <= android.util.Log.DEBUG) + return android.util.Log.d(tag, msg); + else + return 0; + } + public static int i(String msg) { - if (BuildConfig.BETA_RELEASE) + if (level <= android.util.Log.INFO) return android.util.Log.i(TAG, msg); else return 0; } + public static int i(String tag, String msg) { + if (level <= android.util.Log.INFO) + return android.util.Log.i(tag, msg); + else + return 0; + } + public static int w(String msg) { return android.util.Log.w(TAG, msg); } @@ -262,9 +286,7 @@ public class Log { } static void setup(Context context) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - debug = prefs.getBoolean("debug", false); - + setLevel(context); setupBugsnag(context); } diff --git a/app/src/main/res/layout/fragment_options_misc.xml b/app/src/main/res/layout/fragment_options_misc.xml index 174348aa05..5f5e80d9e3 100644 --- a/app/src/main/res/layout/fragment_options_misc.xml +++ b/app/src/main/res/layout/fragment_options_misc.xml @@ -506,6 +506,17 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/swDebug" /> + + + app:constraint_referenced_ids="swInfo,swExpunge,swAuthPlain,swAuthLogin,swAuthNtlm,swAuthSasl,tvProcessors,tvMemoryClass,tvMemoryUsage,tvStorageUsage,tvFingerprint,btnCharsets,btnCiphers,btnFiles" /> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5956e12b9a..10409c6fff 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -537,6 +537,7 @@ Send error reports Protocol logging Debug mode + Log info (debug only) Delete attachments of old messages Cleanup Last cleanup: %1$s