From 1105cd9d1b6c93351cd361cc4329a04c8581d50b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Lenclud?= Date: Thu, 21 Sep 2023 17:56:38 +0200 Subject: [PATCH] Add log level to debug settings Sort out our Timber configuration accordingly. --- app/proguard-project.txt | 1 + .../fragment/DebugSettingsFragment.kt | 11 +---- .../settings/preferences/UserPreferences.kt | 6 +++ app/src/main/java/fulguris/App.kt | 40 ++++++++++++++--- app/src/main/java/fulguris/LogLevel.kt | 43 +++++++++++++++++++ ...imberReleaseTree.kt => TimberLevelTree.kt} | 22 +++------- app/src/main/res/drawable/ic_short_text.xml | 34 ++------------- app/src/main/res/values/arrays.xml | 9 ++++ app/src/main/res/values/donottranslate.xml | 1 + app/src/main/res/values/strings.xml | 12 +++++- app/src/main/res/xml/preference_debug.xml | 39 ++++++++++++++++- .../res/xml/preference_domain_default.xml | 2 +- 12 files changed, 152 insertions(+), 68 deletions(-) create mode 100644 app/src/main/java/fulguris/LogLevel.kt rename app/src/main/java/fulguris/{TimberReleaseTree.kt => TimberLevelTree.kt} (65%) diff --git a/app/proguard-project.txt b/app/proguard-project.txt index 2e2d6ef41..e024b5f51 100644 --- a/app/proguard-project.txt +++ b/app/proguard-project.txt @@ -14,6 +14,7 @@ -keep public class acr.browser.lightning.reading.* -keep public class acr.browser.lightning.settings.fragment.* -keep public class acr.browser.lightning.settings.NoYesAsk +-keep public class fulguris.LogLevel # Needed for now to fix our bottom sheet issue from com.google.android.material:material:1.4.0-alpha02 -keep class com.google.android.material.bottomsheet.BottomSheetDialog$EdgeToEdgeCallback { diff --git a/app/src/main/java/acr/browser/lightning/settings/fragment/DebugSettingsFragment.kt b/app/src/main/java/acr/browser/lightning/settings/fragment/DebugSettingsFragment.kt index b1f232b3c..d6769320f 100644 --- a/app/src/main/java/acr/browser/lightning/settings/fragment/DebugSettingsFragment.kt +++ b/app/src/main/java/acr/browser/lightning/settings/fragment/DebugSettingsFragment.kt @@ -50,18 +50,9 @@ class DebugSettingsFragment : AbstractSettingsFragment() { //activity?.snackbar(R.string.pref_summary_crash_log) userPreferences.crashLogs = change } - // We will use this the find our view later on + // We will use this to find our view later on ).setViewId(R.id.pref_id_crash_logs) - // - switchPreference( - preference = getString(R.string.pref_key_logs), - isChecked = userPreferences.logs, - onCheckChange = { change -> - userPreferences.logs = change - app.plantTimberLogs() - } - ) } private fun openCrashLogsFolder() { diff --git a/app/src/main/java/acr/browser/lightning/settings/preferences/UserPreferences.kt b/app/src/main/java/acr/browser/lightning/settings/preferences/UserPreferences.kt index 6ad6b5ef4..236b446b0 100644 --- a/app/src/main/java/acr/browser/lightning/settings/preferences/UserPreferences.kt +++ b/app/src/main/java/acr/browser/lightning/settings/preferences/UserPreferences.kt @@ -17,6 +17,7 @@ import acr.browser.lightning.settings.NewTabPosition import acr.browser.lightning.utils.FileUtils import acr.browser.lightning.view.RenderingMode import android.content.SharedPreferences +import fulguris.LogLevel import javax.inject.Inject import javax.inject.Singleton @@ -328,6 +329,11 @@ class UserPreferences @Inject constructor( */ var logs by preferences.booleanPreference(R.string.pref_key_logs, R.bool.pref_default_logs) + /** + * Specify from which log level we should feed logcat. + */ + var logLevel by preferences.enumPreference(R.string.pref_key_log_level, LogLevel.DEBUG) + /** * Toggle visibility of close tab button on drawer tab list items. */ diff --git a/app/src/main/java/fulguris/App.kt b/app/src/main/java/fulguris/App.kt index d40a1edc0..1485aa738 100644 --- a/app/src/main/java/fulguris/App.kt +++ b/app/src/main/java/fulguris/App.kt @@ -23,6 +23,7 @@ package fulguris import acr.browser.lightning.BuildConfig +import acr.browser.lightning.R import acr.browser.lightning.database.bookmark.BookmarkExporter import acr.browser.lightning.database.bookmark.BookmarkRepository import acr.browser.lightning.di.DatabaseScheduler @@ -39,9 +40,9 @@ import android.annotation.SuppressLint import android.app.Activity import android.app.Application import android.content.Context +import android.content.SharedPreferences import android.os.Build import android.os.Bundle -import android.os.StrictMode import android.webkit.WebView import androidx.appcompat.app.AppCompatDelegate import com.jakewharton.threetenabp.AndroidThreeTen @@ -56,7 +57,7 @@ import kotlin.system.exitProcess lateinit var app: App @HiltAndroidApp -class App : Application() { +class App : Application(), SharedPreferences.OnSharedPreferenceChangeListener { @Inject internal lateinit var developerPreferences: DeveloperPreferences @Inject internal lateinit var userPreferences: UserPreferences @@ -84,13 +85,28 @@ class App : Application() { } - fun plantTimberLogs() { - // Setup our log engine according to user preferences + /** + * Setup Timber log engine according to user preferences + */ + private fun plantTimberLogs() { + + // Update Timber if (userPreferences.logs) { - Timber.plant(Timber.DebugTree()) + Timber.uprootAll() + Timber.plant(TimberLevelTree(userPreferences.logLevel.value)) } else { - Timber.plant(TimberReleaseTree()) + Timber.uprootAll() } + + // Test our logs + Timber.v("Log verbose") + Timber.d("Log debug") + Timber.i("Log info") + Timber.w("Log warn") + Timber.e("Log error") + // We disabled that as we don't want our process to terminate + // Though it did not terminate the app in debug configuration on Huawei P30 Pro - Android 10 + //Timber.wtf("Log assert") } @@ -99,6 +115,8 @@ class App : Application() { // SL: Use this to debug when launched from another app for instance //Debug.waitForDebugger() super.onCreate() + // No need to unregister I suppose cause this is for the life time of the application anyway + userPreferences.preferences.registerOnSharedPreferenceChangeListener(this) plantTimberLogs() @@ -221,4 +239,14 @@ class App : Application() { } } + /** + * + */ + override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) { + if (key == getString(R.string.pref_key_log_level) || key == getString(R.string.pref_key_logs)) { + // Update Timber according to changed preferences + plantTimberLogs() + } + } + } diff --git a/app/src/main/java/fulguris/LogLevel.kt b/app/src/main/java/fulguris/LogLevel.kt new file mode 100644 index 000000000..ce76fa082 --- /dev/null +++ b/app/src/main/java/fulguris/LogLevel.kt @@ -0,0 +1,43 @@ +package fulguris + +/* + * The contents of this file are subject to the Common Public Attribution License Version 1.0. + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * https://github.com/Slion/Fulguris/blob/main/LICENSE.CPAL-1.0. + * The License is based on the Mozilla Public License Version 1.1, but Sections 14 and 15 have been + * added to cover use of software over a computer network and provide for limited attribution for + * the Original Developer. In addition, Exhibit A has been modified to be consistent with Exhibit B. + * + * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF + * ANY KIND, either express or implied. See the License for the specific language governing rights + * and limitations under the License. + * + * The Original Code is Fulguris. + * + * The Original Developer is the Initial Developer. + * The Initial Developer of the Original Code is Stéphane Lenclud. + * + * All portions of the code written by Stéphane Lenclud are Copyright © 2023 Stéphane Lenclud. + * All Rights Reserved. + */ + + +import acr.browser.lightning.settings.preferences.IntEnum +import android.util.Log + +/** + * Notably used to define what to do when there is a third-party associated with a web site. + * + * NOTE: Class name is referenced as strings in our resources. + */ +enum class LogLevel(override val value: Int) : IntEnum { + VERBOSE(Log.VERBOSE), + DEBUG(Log.DEBUG), + INFO(Log.INFO), + WARN(Log.WARN), + ERROR(Log.ERROR), + ASSERT(Log.ASSERT) +} + + diff --git a/app/src/main/java/fulguris/TimberReleaseTree.kt b/app/src/main/java/fulguris/TimberLevelTree.kt similarity index 65% rename from app/src/main/java/fulguris/TimberReleaseTree.kt rename to app/src/main/java/fulguris/TimberLevelTree.kt index 454696201..3e14f28f2 100644 --- a/app/src/main/java/fulguris/TimberReleaseTree.kt +++ b/app/src/main/java/fulguris/TimberLevelTree.kt @@ -22,27 +22,15 @@ package fulguris -import android.util.Log import timber.log.Timber /** - * Could be used for crash reporting somehow - * For we just suppress all logs + * Timber tree which logs messages from the specified priority. */ -class TimberReleaseTree : Timber.Tree() { +class TimberLevelTree(private val iPriority: Int) : Timber.DebugTree() { - override fun log(priority: Int, tag: String?, message: String, t: Throwable?) { - if (priority == Log.VERBOSE || priority == Log.DEBUG) { - return - } - /* - FakeCrashLibrary.log(priority, tag, message) - if (t != null) { - if (priority == Log.ERROR) { - FakeCrashLibrary.logError(t) - } else if (priority == Log.WARN) { - FakeCrashLibrary.logWarning(t) - } - }*/ + override fun isLoggable(tag: String?, priority: Int): Boolean { + return priority >= iPriority } + } \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_short_text.xml b/app/src/main/res/drawable/ic_short_text.xml index 5f669695d..aeaca3333 100644 --- a/app/src/main/res/drawable/ic_short_text.xml +++ b/app/src/main/res/drawable/ic_short_text.xml @@ -1,31 +1,5 @@ - - - + + diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index b3c1065c5..8aef70fd2 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -59,4 +59,13 @@ @string/action_ask + + @string/log_level_verbose + @string/log_level_debug + @string/log_level_info + @string/log_level_warn + @string/log_level_error + @string/log_level_assert + + diff --git a/app/src/main/res/values/donottranslate.xml b/app/src/main/res/values/donottranslate.xml index 3bb080749..dc9140334 100644 --- a/app/src/main/res/values/donottranslate.xml +++ b/app/src/main/res/values/donottranslate.xml @@ -68,6 +68,7 @@ pref_key_analytics pref_key_crash_logs pref_key_logs + pref_key_log_level pref_key_rendering_mode pref_key_tool_bar_text_display pref_key_restore_tabs_on_startup diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index fbf71d3f4..7e78509b0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -433,8 +433,9 @@ Crash logs Write crash logs to file system.\nTap to open folder. Please provide relevant file when reporting a bug. Logs - Output all logs over logcat - Filter out debug and verbose logs + Enabled + Disabled + Log level Open in new tab @@ -674,4 +675,11 @@ Switch to last tab Switch to last session + Verbose + Debug + Info + Warn + Error + Assert + diff --git a/app/src/main/res/xml/preference_debug.xml b/app/src/main/res/xml/preference_debug.xml index d5fc65435..a100d06bc 100644 --- a/app/src/main/res/xml/preference_debug.xml +++ b/app/src/main/res/xml/preference_debug.xml @@ -1,6 +1,29 @@ - + + + + -