From eec2abf164f0b1f79ef5a7c58754e0e7e15bc5fe Mon Sep 17 00:00:00 2001 From: Valere Date: Mon, 2 Sep 2019 14:33:53 +0200 Subject: [PATCH] Reduce release build log level --- CHANGES.md | 2 +- .../java/im/vector/riotx/VectorApplication.kt | 9 ++- .../vector/riotx/core/di/VectorComponent.kt | 3 + .../riotx/features/rageshake/BugReporter.kt | 5 +- .../features/rageshake/VectorFileLogger.kt | 81 +++++++++++-------- .../features/settings/VectorPreferences.kt | 6 ++ vector/src/main/res/values/strings_riotX.xml | 1 + .../src/main/res/xml/vector_settings_labs.xml | 6 ++ 8 files changed, 73 insertions(+), 40 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index aa494b72..02c14b84 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -5,7 +5,7 @@ Features: - Improvements: - - + - Reduce default release build log level, and lab option to enable more logs. Other changes: - diff --git a/vector/src/main/java/im/vector/riotx/VectorApplication.kt b/vector/src/main/java/im/vector/riotx/VectorApplication.kt index b62c44f5..cd4a9732 100644 --- a/vector/src/main/java/im/vector/riotx/VectorApplication.kt +++ b/vector/src/main/java/im/vector/riotx/VectorApplication.kt @@ -86,9 +86,12 @@ class VectorApplication : Application(), HasVectorInjector, MatrixConfiguration. vectorComponent = DaggerVectorComponent.factory().create(this) vectorComponent.inject(this) vectorUncaughtExceptionHandler.activate(this) - // Log - VectorFileLogger.init(this) - Timber.plant(Timber.DebugTree(), VectorFileLogger) + + if (BuildConfig.DEBUG) { + Timber.plant(Timber.DebugTree()) + } + Timber.plant(vectorComponent.vectorFileLogger()) + if (BuildConfig.DEBUG) { Stetho.initializeWithDefaults(this) } diff --git a/vector/src/main/java/im/vector/riotx/core/di/VectorComponent.kt b/vector/src/main/java/im/vector/riotx/core/di/VectorComponent.kt index 7cbbc306..05492224 100644 --- a/vector/src/main/java/im/vector/riotx/core/di/VectorComponent.kt +++ b/vector/src/main/java/im/vector/riotx/core/di/VectorComponent.kt @@ -41,6 +41,7 @@ import im.vector.riotx.features.notifications.NotificationBroadcastReceiver import im.vector.riotx.features.notifications.NotificationDrawerManager import im.vector.riotx.features.notifications.PushRuleTriggerListener import im.vector.riotx.features.rageshake.BugReporter +import im.vector.riotx.features.rageshake.VectorFileLogger import im.vector.riotx.features.rageshake.VectorUncaughtExceptionHandler import im.vector.riotx.features.settings.VectorPreferences import javax.inject.Singleton @@ -101,6 +102,8 @@ interface VectorComponent { fun vectorPreferences(): VectorPreferences + fun vectorFileLogger(): VectorFileLogger + @Component.Factory interface Factory { fun create(@BindsInstance context: Context): VectorComponent diff --git a/vector/src/main/java/im/vector/riotx/features/rageshake/BugReporter.kt b/vector/src/main/java/im/vector/riotx/features/rageshake/BugReporter.kt index d21ad021..c678f47d 100755 --- a/vector/src/main/java/im/vector/riotx/features/rageshake/BugReporter.kt +++ b/vector/src/main/java/im/vector/riotx/features/rageshake/BugReporter.kt @@ -52,7 +52,8 @@ import javax.inject.Singleton */ @Singleton class BugReporter @Inject constructor(private val activeSessionHolder: ActiveSessionHolder, - private val versionProvider: VersionProvider) { + private val versionProvider: VersionProvider, + private val vectorFileLogger : VectorFileLogger) { var inMultiWindowMode = false companion object { @@ -162,7 +163,7 @@ class BugReporter @Inject constructor(private val activeSessionHolder: ActiveSes val gzippedFiles = ArrayList() if (withDevicesLogs) { - val files = VectorFileLogger.getLogFiles() + val files = vectorFileLogger.getLogFiles() for (f in files) { if (!mIsCancelled) { diff --git a/vector/src/main/java/im/vector/riotx/features/rageshake/VectorFileLogger.kt b/vector/src/main/java/im/vector/riotx/features/rageshake/VectorFileLogger.kt index cdea1f0d..3ef5e198 100644 --- a/vector/src/main/java/im/vector/riotx/features/rageshake/VectorFileLogger.kt +++ b/vector/src/main/java/im/vector/riotx/features/rageshake/VectorFileLogger.kt @@ -17,43 +17,74 @@ package im.vector.riotx.features.rageshake import android.content.Context -import android.text.TextUtils +import android.util.Log +import im.vector.riotx.features.settings.VectorPreferences import timber.log.Timber import java.io.File -import java.io.IOException import java.io.PrintWriter import java.io.StringWriter import java.text.SimpleDateFormat import java.util.* import java.util.logging.* import java.util.logging.Formatter +import javax.inject.Inject +import javax.inject.Singleton import kotlin.collections.ArrayList -object VectorFileLogger : Timber.DebugTree() { +private const val LOG_SIZE_BYTES = 20 * 1024 * 1024 // 20MB - private const val LOG_SIZE_BYTES = 50 * 1024 * 1024 // 50MB +private const val LOG_ROTATION_COUNT = 3 + + +@Singleton +class VectorFileLogger @Inject constructor(val context: Context, private val vectorPreferences: VectorPreferences) : Timber.DebugTree() { - // relatively large rotation count because closing > opening the app rotates the log (!) - private const val LOG_ROTATION_COUNT = 15 private val sLogger = Logger.getLogger("im.vector.riotx") - private lateinit var sFileHandler: FileHandler - private lateinit var sCacheDirectory: File - private var sFileName = "riotx" + private var sFileHandler: FileHandler? = null + private var sCacheDirectory: File? = null + private var sFileName = "riotxlogs" - fun init(context: Context) { + private val prioPrefixes = mapOf( + Log.VERBOSE to "V/ ", + Log.DEBUG to "D/ ", + Log.INFO to "I/ ", + Log.WARN to "W/ ", + Log.ERROR to "E/ ", + Log.ASSERT to "WTF/ " + ) + + init { val logsDirectoryFile = context.cacheDir.absolutePath + "/logs" - setLogDirectory(File(logsDirectoryFile)) - init("RiotXLog") + try { + if (sCacheDirectory != null) { + sFileHandler = FileHandler(sCacheDirectory!!.absolutePath + "/" + sFileName + ".%g.txt", LOG_SIZE_BYTES, LOG_ROTATION_COUNT) + sFileHandler?.formatter = LogFormatter() + sLogger.useParentHandlers = false + sLogger.level = Level.ALL + sLogger.addHandler(sFileHandler) + } + } catch (e: Throwable) { + Timber.e(e, "Failed to initialize FileLogger") + } } override fun log(priority: Int, tag: String?, message: String, t: Throwable?) { + if (sFileHandler == null) return + if (skipLog(priority)) return if (t != null) { logToFile(t) } + logToFile(prioPrefixes[priority] ?: "$priority ", tag ?: "Tag", message) + } - logToFile("$priority ", tag ?: "Tag", message) + private fun skipLog(priority: Int): Boolean { + return if (vectorPreferences.labAllowedExtendedLogging()) { + false + } else { + priority < Log.ERROR + } } /** @@ -68,24 +99,6 @@ object VectorFileLogger : Timber.DebugTree() { sCacheDirectory = cacheDir } - /** - * Initialises the logger. Should be called AFTER [Log.setLogDirectory]. - * - * @param fileName the base file name - */ - private fun init(fileName: String) { - try { - if (!TextUtils.isEmpty(fileName)) { - sFileName = fileName - } - sFileHandler = FileHandler(sCacheDirectory.absolutePath + "/" + sFileName + ".%g.txt", LOG_SIZE_BYTES, LOG_ROTATION_COUNT) - sFileHandler.formatter = LogFormatter() - sLogger.useParentHandlers = false - sLogger.level = Level.ALL - sLogger.addHandler(sFileHandler) - } catch (e: IOException) { - } - } /** * Adds our own log files to the provided list of files. @@ -99,8 +112,8 @@ object VectorFileLogger : Timber.DebugTree() { try { // reported by GA if (null != sFileHandler) { - sFileHandler.flush() - val absPath = sCacheDirectory.absolutePath + sFileHandler!!.flush() + val absPath = sCacheDirectory?.absolutePath ?: return emptyList() for (i in 0..LOG_ROTATION_COUNT) { val filepath = "$absPath/$sFileName.$i.txt" @@ -111,7 +124,7 @@ object VectorFileLogger : Timber.DebugTree() { } } } catch (e: Exception) { - Timber.e(e, "## addLogFiles() failed : " + e.message) + Timber.e(e, "## addLogFiles() failed : %s", e.message) } return files diff --git a/vector/src/main/java/im/vector/riotx/features/settings/VectorPreferences.kt b/vector/src/main/java/im/vector/riotx/features/settings/VectorPreferences.kt index 8ba6c7ed..7116f33a 100755 --- a/vector/src/main/java/im/vector/riotx/features/settings/VectorPreferences.kt +++ b/vector/src/main/java/im/vector/riotx/features/settings/VectorPreferences.kt @@ -149,6 +149,8 @@ class VectorPreferences @Inject constructor(private val context: Context) { private const val SETTINGS_USE_NATIVE_CAMERA_PREFERENCE_KEY = "SETTINGS_USE_NATIVE_CAMERA_PREFERENCE_KEY" private const val SETTINGS_ENABLE_SEND_VOICE_FEATURE_PREFERENCE_KEY = "SETTINGS_ENABLE_SEND_VOICE_FEATURE_PREFERENCE_KEY" + const val SETTINGS_LABS_ALLOW_EXTENDED_LOGS = "SETTINGS_LABS_ALLOW_EXTENDED_LOGS" + private const val SETTINGS_LABS_SHOW_HIDDEN_EVENTS_PREFERENCE_KEY = "SETTINGS_LABS_SHOW_HIDDEN_EVENTS_PREFERENCE_KEY" private const val SETTINGS_LABS_ENABLE_SWIPE_TO_REPLY = "SETTINGS_LABS_ENABLE_SWIPE_TO_REPLY" @@ -257,6 +259,10 @@ class VectorPreferences @Inject constructor(private val context: Context) { return defaultPrefs.getBoolean(SETTINGS_LABS_ENABLE_SWIPE_TO_REPLY, true) } + fun labAllowedExtendedLogging(): Boolean { + return defaultPrefs.getBoolean(SETTINGS_LABS_ALLOW_EXTENDED_LOGS, false) + } + /** * Tells if we have already asked the user to disable battery optimisations on android >= M devices. * diff --git a/vector/src/main/res/values/strings_riotX.xml b/vector/src/main/res/values/strings_riotX.xml index 118fde84..877512d7 100644 --- a/vector/src/main/res/values/strings_riotX.xml +++ b/vector/src/main/res/values/strings_riotX.xml @@ -2,5 +2,6 @@ + Help developers by allowing extended logging. \ No newline at end of file diff --git a/vector/src/main/res/xml/vector_settings_labs.xml b/vector/src/main/res/xml/vector_settings_labs.xml index 56205172..cb0cba33 100644 --- a/vector/src/main/res/xml/vector_settings_labs.xml +++ b/vector/src/main/res/xml/vector_settings_labs.xml @@ -45,6 +45,12 @@ android:key="SETTINGS_LABS_ENABLE_SWIPE_TO_REPLY" android:title="@string/labs_swipe_to_reply_in_timeline" /> + + + \ No newline at end of file