Improve logging infrastructure

This commit is contained in:
NerdNumber9 2019-04-13 23:47:57 -04:00
parent dce685e711
commit d4f1014df6
6 changed files with 113 additions and 3 deletions

View File

@ -269,6 +269,14 @@ dependencies {
// Firebase (EH)
implementation 'com.crashlytics.sdk.android:crashlytics:2.9.9'
// Better logging (EH)
implementation 'com.elvishew:xlog:1.6.1'
// Time utils (EH)
def typed_time_version = '1.0.2'
implementation "com.github.kizitonwose.time:time:$typed_time_version"
implementation "com.github.kizitonwose.time:time-android:$typed_time_version"
}
buildscript {

View File

@ -3,19 +3,35 @@ package eu.kanade.tachiyomi
import android.app.Application
import android.content.Context
import android.content.res.Configuration
import android.os.Environment
import android.support.multidex.MultiDex
import com.elvishew.xlog.LogConfiguration
import com.elvishew.xlog.LogLevel
import com.elvishew.xlog.XLog
import com.elvishew.xlog.printer.AndroidPrinter
import com.elvishew.xlog.printer.Printer
import com.elvishew.xlog.printer.file.FilePrinter
import com.elvishew.xlog.printer.file.clean.FileLastModifiedCleanStrategy
import com.elvishew.xlog.printer.file.naming.DateFileNameGenerator
import com.evernote.android.job.JobManager
import com.github.ajalt.reprint.core.Reprint
import com.kizitonwose.time.days
import eu.kanade.tachiyomi.data.backup.BackupCreatorJob
import eu.kanade.tachiyomi.data.library.LibraryUpdateJob
import eu.kanade.tachiyomi.data.notification.Notifications
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.data.updater.UpdaterJob
import eu.kanade.tachiyomi.util.LocaleHelper
import exh.log.CrashlyticsPrinter
import io.realm.Realm
import io.realm.RealmConfiguration
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import timber.log.Timber
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.InjektScope
import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.registry.default.DefaultRegistrar
import java.io.File
import kotlin.concurrent.thread
@ -31,8 +47,9 @@ open class App : Application() {
setupJobManager()
setupNotificationChannels()
setupRealm() //Setup metadata DB (EH)
GlobalScope.launch { deleteOldMetadataRealm() } // Delete old metadata DB (EH)
Reprint.initialize(this) //Setup fingerprint (EH)
setupExhLogging() // EXH logging
LocaleHelper.updateConfiguration(this, resources.configuration)
}
@ -67,14 +84,14 @@ open class App : Application() {
}
// EXH
private fun setupRealm() {
private fun deleteOldMetadataRealm() {
Realm.init(this)
val config = RealmConfiguration.Builder()
.name("gallery-metadata.realm")
.schemaVersion(3)
.deleteRealmIfMigrationNeeded()
.build()
Realm.setDefaultConfiguration(config)
Realm.deleteRealm(config)
//Delete old paper db files
listOf(
@ -89,4 +106,44 @@ open class App : Application() {
}
}
}
// EXH
private fun setupExhLogging() {
val logLevel = if(BuildConfig.DEBUG ||
Injekt.get<PreferencesHelper>().eh_detailedLogs().getOrDefault()) {
LogLevel.ALL
} else {
LogLevel.WARN
}
val logConfig = LogConfiguration.Builder()
.logLevel(logLevel)
.t()
.st(2)
.nb()
.build()
val printers = mutableListOf<Printer>(AndroidPrinter())
val logFolder = File(Environment.getExternalStorageDirectory().absolutePath + File.separator +
getString(R.string.app_name), "logs")
printers += FilePrinter
.Builder(logFolder.absolutePath)
.fileNameGenerator(DateFileNameGenerator())
.cleanStrategy(FileLastModifiedCleanStrategy(7.days.inMilliseconds.longValue))
.build()
// Install Crashlytics in prod
if(!BuildConfig.DEBUG) {
printers += CrashlyticsPrinter(LogLevel.ERROR)
}
XLog.init(
logConfig,
*printers.toTypedArray()
)
XLog.d("Application booting...")
}
}

View File

@ -182,4 +182,6 @@ object PreferenceKeys {
const val eh_delegateSources = "eh_delegate_sources"
const val eh_showTransitionPages = "eh_show_transition_pages"
const val eh_detailedLogs = "eh_detailed_logs"
}

View File

@ -255,4 +255,6 @@ class PreferencesHelper(val context: Context) {
fun eh_savedSearches() = rxPrefs.getStringSet("eh_saved_searches", emptySet())
fun eh_showTransitionPages() = rxPrefs.getBoolean(Keys.eh_showTransitionPages, true)
fun eh_detailedLogs() = rxPrefs.getBoolean(Keys.eh_detailedLogs, false)
}

View File

@ -8,6 +8,7 @@ import android.view.View
import com.afollestad.materialdialogs.MaterialDialog
import com.bluelinelabs.conductor.RouterTransaction
import com.bluelinelabs.conductor.changehandler.FadeChangeHandler
import eu.kanade.tachiyomi.BuildConfig
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.cache.ChapterCache
import eu.kanade.tachiyomi.data.database.DatabaseHelper
@ -118,6 +119,21 @@ class SettingsAdvancedController : SettingsController() {
summary = "Apply TachiyomiEH enhancements to the following sources if they are installed: ${DELEGATED_SOURCES.values.joinToString { it.sourceName }}"
}
switchPreference {
title = "Detailed logs"
if(BuildConfig.DEBUG) {
summary = "Force-enabled in this debug build."
isChecked = true
isPersistent = false
isEnabled = false
} else {
key = PreferenceKeys.eh_detailedLogs
defaultValue = false
summary = "Increase detail level of logs. May cause the app to become slightly slower."
}
}
preference {
title = "Open debug menu"
summary = Html.fromHtml("DO NOT TOUCH THIS MENU UNLESS YOU KNOW WHAT YOU ARE DOING! <font color='red'>IT CAN CORRUPT YOUR LIBRARY!</font>")

View File

@ -0,0 +1,25 @@
package exh.log
import com.crashlytics.android.Crashlytics
import com.elvishew.xlog.printer.Printer
import eu.kanade.tachiyomi.BuildConfig
class CrashlyticsPrinter(private val logLevel: Int) : Printer {
/**
* Print log in new line.
*
* @param logLevel the level of log
* @param tag the tag of log
* @param msg the msg of log
*/
override fun println(logLevel: Int, tag: String?, msg: String?) {
if(logLevel >= this.logLevel) {
try {
Crashlytics.log(logLevel, tag, msg)
} catch (t: Throwable) {
// Crash in debug if shit like this happens
if(BuildConfig.DEBUG) throw t
}
}
}
}