Add hidden option to enable debug logs

This commit is contained in:
Moez Bhatti
2018-08-08 01:47:18 -04:00
parent f0862915c0
commit 26faf3c18f
7 changed files with 52 additions and 14 deletions
@@ -84,6 +84,7 @@ class Preferences @Inject constructor(private val rxPrefs: RxSharedPreferences)
val delivery = rxPrefs.getBoolean("delivery", false)
val unicode = rxPrefs.getBoolean("unicode", false)
val mmsSize = rxPrefs.getInteger("mmsSize", 300)
val logging = rxPrefs.getBoolean("logging", false)
fun theme(threadId: Long = 0): Preference<Int> {
val default = rxPrefs.getInteger("theme", 0xFF0097A7.toInt())
@@ -49,6 +49,7 @@ import com.bugsnag.android.Configuration
import com.moez.QKSMS.BuildConfig
import com.moez.QKSMS.R
import com.moez.QKSMS.common.util.BugsnagTree
import com.moez.QKSMS.common.util.FileLoggingTree
import com.moez.QKSMS.injection.AppComponentManager
import com.moez.QKSMS.injection.appComponent
import com.moez.QKSMS.manager.AnalyticsManager
@@ -75,6 +76,7 @@ class QKApplication : Application(), HasActivityInjector, HasBroadcastReceiverIn
@Inject lateinit var dispatchingActivityInjector: DispatchingAndroidInjector<Activity>
@Inject lateinit var dispatchingBroadcastReceiverInjector: DispatchingAndroidInjector<BroadcastReceiver>
@Inject lateinit var dispatchingServiceInjector: DispatchingAndroidInjector<Service>
@Inject lateinit var fileLoggingTree: FileLoggingTree
@Inject lateinit var nightModeManager: NightModeManager
private val packages = arrayOf("com.moez.QKSMS")
@@ -109,7 +111,7 @@ class QKApplication : Application(), HasActivityInjector, HasBroadcastReceiverIn
EmojiCompat.init(FontRequestEmojiCompatConfig(this, fontRequest))
Timber.plant(Timber.DebugTree(), BugsnagTree())
Timber.plant(Timber.DebugTree(), BugsnagTree(), fileLoggingTree)
}
override fun activityInjector(): AndroidInjector<Activity> {
@@ -20,24 +20,30 @@ package com.moez.QKSMS.common.util
import android.os.Environment
import android.util.Log
import com.moez.QKSMS.util.Preferences
import io.reactivex.schedulers.Schedulers
import timber.log.Timber
import java.io.File
import java.io.FileOutputStream
import java.text.SimpleDateFormat
import java.util.*
import javax.inject.Inject
import javax.inject.Singleton
/**
* Based off Vipin Kumar's FileLoggingTree: https://medium.com/@vicky7230/file-logging-with-timber-4e63a1b86a66
*/
class FileLoggingTree : Timber.DebugTree() {
@Singleton
class FileLoggingTree @Inject constructor(private val prefs: Preferences) : Timber.DebugTree() {
private val fileLock: Boolean = false
override fun log(priority: Int, tag: String, message: String, t: Throwable?) {
try {
val time = System.currentTimeMillis()
val dir = File(Environment.getExternalStorageDirectory(), "QKSMS/Logs").apply { mkdirs() }
val file = File(dir, "${SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(time)}.html")
if (!prefs.logging.get()) return
val priorityString = when(priority) {
Schedulers.io().scheduleDirect {
val timestamp = SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS", Locale.getDefault()).format(System.currentTimeMillis())
val priorityString = when (priority) {
Log.VERBOSE -> "V"
Log.DEBUG -> "D"
Log.INFO -> "I"
@@ -46,13 +52,24 @@ class FileLoggingTree : Timber.DebugTree() {
else -> "WTF"
}
FileOutputStream(file, true).use { fileOutputStream ->
val timestamp = SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS", Locale.getDefault()).format(time)
fileOutputStream.write("$timestamp $priorityString/$tag: $message ${Log.getStackTraceString(t)}<br>".toByteArray())
}
} catch (e: Exception) {
Log.e("FileLoggingTree", "Error while logging into file", e)
}
// Format the log to be written to the file
val log = "$timestamp $priorityString/$tag: $message ${Log.getStackTraceString(t)}<br>".toByteArray()
// Ensure that only one thread is writing to the file at a time
synchronized(fileLock) {
try {
// Create the directory
val dir = File(Environment.getExternalStorageDirectory(), "QKSMS/Logs").apply { mkdirs() }
// Create the file
val file = File(dir, "${SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(System.currentTimeMillis())}.html")
// Write the log to the file
FileOutputStream(file, true).use { fileOutputStream -> fileOutputStream.write(log) }
} catch (e: Exception) {
Log.e("FileLoggingTree", "Error while logging into file", e)
}
}
}
}
}
@@ -11,6 +11,7 @@ import com.bluelinelabs.conductor.changehandler.HorizontalChangeHandler
import com.bluelinelabs.conductor.changehandler.VerticalChangeHandler
import com.google.android.material.snackbar.Snackbar
import com.jakewharton.rxbinding2.view.clicks
import com.jakewharton.rxbinding2.view.longClicks
import com.moez.QKSMS.BuildConfig
import com.moez.QKSMS.R
import com.moez.QKSMS.common.QkChangeHandler
@@ -94,6 +95,8 @@ class SettingsController : QkController<SettingsView, SettingsState, SettingsPre
.map { preference -> preference.clicks().map { preference } }
.let { preferences -> Observable.merge(preferences) }
override fun aboutLongClicks(): Observable<*> = about.longClicks()
override fun viewQksmsPlusClicks(): Observable<*> = viewQksmsPlusSubject
override fun nightModeSelected(): Observable<Int> = nightModeDialog.adapter.menuItemClicks
@@ -1,6 +1,7 @@
package com.moez.QKSMS.feature.settings
import android.content.Context
import androidx.core.widget.toast
import com.moez.QKSMS.R
import com.moez.QKSMS.common.Navigator
import com.moez.QKSMS.common.base.QkPresenter
@@ -142,6 +143,17 @@ class SettingsPresenter @Inject constructor(
}
}
view.aboutLongClicks()
.map { !prefs.logging.get() }
.doOnNext { enabled -> prefs.logging.set(enabled) }
.autoDisposable(view.scope())
.subscribe { enabled ->
context.toast(when (enabled) {
true -> R.string.settings_logging_enabled
false -> R.string.settings_logging_disabled
})
}
view.nightModeSelected()
.withLatestFrom(billingManager.upgradeStatus) { mode, upgraded ->
if (!upgraded && mode == Preferences.NIGHT_MODE_AUTO) {
@@ -24,6 +24,7 @@ import io.reactivex.Observable
interface SettingsView : QkViewContract<SettingsState> {
fun preferenceClicks(): Observable<PreferenceView>
fun aboutLongClicks(): Observable<*>
fun viewQksmsPlusClicks(): Observable<*>
fun nightModeSelected(): Observable<Int>
fun nightStartSelected(): Observable<Pair<Int, Int>>
@@ -208,6 +208,8 @@
<string name="settings_sync_summary">Re-sync your messages with the native Android SMS database</string>
<string name="settings_about_title">About QKSMS</string>
<string name="settings_version">Version %s</string>
<string name="settings_logging_enabled">Debug logging enabled</string>
<string name="settings_logging_disabled">Debug logging disabled</string>
<string name="settings_delay_duration_hint">Enter duration (seconds)</string>