Skip to content

Commit

Permalink
Add log level to debug settings
Browse files Browse the repository at this point in the history
Sort out our Timber configuration accordingly.
  • Loading branch information
Slion committed Sep 21, 2023
1 parent 62fd276 commit 1105cd9
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 68 deletions.
1 change: 1 addition & 0 deletions app/proguard-project.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
*/
Expand Down
40 changes: 34 additions & 6 deletions app/src/main/java/fulguris/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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")
}


Expand All @@ -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()

Expand Down Expand Up @@ -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()
}
}

}
43 changes: 43 additions & 0 deletions app/src/main/java/fulguris/LogLevel.kt
Original file line number Diff line number Diff line change
@@ -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)
}


Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}
34 changes: 4 additions & 30 deletions app/src/main/res/drawable/ic_short_text.xml
Original file line number Diff line number Diff line change
@@ -1,31 +1,5 @@
<!--
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 © 2020 Stéphane Lenclud.
All Rights Reserved.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M4,9h16v2H4V9zM4,13h10v2H4V13z"/>
<vector android:autoMirrored="true" android:height="24dp"
android:tint="?attr/colorControlNormal" android:viewportHeight="24"
android:viewportWidth="24" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M4,9h16v2H4V9zM4,13h10v2H4V13z"/>
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,13 @@
<item>@string/action_ask</item>
</string-array>

<string-array name="LogLevel">
<item>@string/log_level_verbose</item>
<item>@string/log_level_debug</item>
<item>@string/log_level_info</item>
<item>@string/log_level_warn</item>
<item>@string/log_level_error</item>
<item>@string/log_level_assert</item>
</string-array>

</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/donottranslate.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<string name="pref_key_analytics">pref_key_analytics</string>
<string name="pref_key_crash_logs">pref_key_crash_logs</string>
<string name="pref_key_logs">pref_key_logs</string>
<string name="pref_key_log_level">pref_key_log_level</string>
<string name="pref_key_rendering_mode">pref_key_rendering_mode</string>
<string name="pref_key_tool_bar_text_display">pref_key_tool_bar_text_display</string>
<string name="pref_key_restore_tabs_on_startup">pref_key_restore_tabs_on_startup</string>
Expand Down
12 changes: 10 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,9 @@
<string name="pref_title_crash_logs">Crash logs</string>
<string name="pref_summary_crash_logs">Write crash logs to file system.\nTap to open folder. Please provide relevant file when reporting a bug.</string>
<string name="pref_title_logs">Logs</string>
<string name="pref_summary_on_logs">Output all logs over logcat</string>
<string name="pref_summary_off_logs">Filter out debug and verbose logs</string>
<string name="pref_summary_on_logs">Enabled</string>
<string name="pref_summary_off_logs">Disabled</string>
<string name="pref_title_log_level">Log level</string>

<!-- Dialogs -->
<string name="dialog_open_new_tab">Open in new tab</string>
Expand Down Expand Up @@ -674,4 +675,11 @@
<string name="action_switch_to_last_tab">Switch to last tab</string>
<string name="action_switch_to_last_session">Switch to last session</string>

<string name="log_level_verbose">Verbose</string>
<string name="log_level_debug">Debug</string>
<string name="log_level_info">Info</string>
<string name="log_level_warn">Warn</string>
<string name="log_level_error">Error</string>
<string name="log_level_assert">Assert</string>

</resources>
39 changes: 37 additions & 2 deletions app/src/main/res/xml/preference_debug.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:a="http://schemas.android.com/apk/res/android">
<!--
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.
-->
<PreferenceScreen
xmlns:a="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
>

<SwitchPreferenceCompat
a:key="@string/pref_key_logs"
Expand All @@ -12,10 +35,22 @@
a:singleLineTitle="false"
/>

<fulguris.preference.EnumListPreference
a:key="@string/pref_key_log_level"
a:dependency="@string/pref_key_logs"
a:title="@string/pref_title_log_level"
a:useSimpleSummaryProvider="true"
a:defaultValue="DEBUG"
a:entries="@array/LogLevel"
a:icon="@drawable/ic_short_text"
a:singleLineTitle="false"
a:enumClassName="fulguris.LogLevel"
/>

<SwitchPreferenceCompat
a:key="@string/pref_key_crash_logs"
a:title="@string/pref_title_crash_logs"
a:id="@+id/pref_id_crash_logs"
android:id="@+id/pref_id_crash_logs"
a:icon="@drawable/ic_folder_open"
a:defaultValue="@bool/pref_default_crash_logs"
a:summary="@string/pref_summary_crash_logs"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/xml/preference_domain_default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
All portions of the code written by Stéphane Lenclud are Copyright © 2023 Stéphane Lenclud.
All Rights Reserved.
-->
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.preference.PreferenceScreen
xmlns:a="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

Expand Down

0 comments on commit 1105cd9

Please sign in to comment.