Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1702ffa6 authored by Justin Weir's avatar Justin Weir
Browse files

Change Logcat to only log in background thread in debug mode

The prod and always log trackers shoud just log syncrhonously, since they
do not do any jank-causing work to determine whether a message should be
logged.
Bug: 221419865
Test: ran 'atest SystemUITests SystemUIGoogleTests'

Change-Id: I2e673a480abbd5afda2d6ce3117df47e0415ea8a
parent 6c61f360
Loading
Loading
Loading
Loading
+19 −10
Original line number Diff line number Diff line
@@ -84,9 +84,11 @@ class LogBuffer @JvmOverloads constructor(
    }

    private val buffer: ArrayDeque<LogMessageImpl> = ArrayDeque()
    private val echoMessageQueue: BlockingQueue<LogMessageImpl> = ArrayBlockingQueue(poolSize)
    private val echoMessageQueue: BlockingQueue<LogMessageImpl>? =
            if (logcatEchoTracker.logInBackgroundThread) ArrayBlockingQueue(poolSize) else null

    init {
        if (logcatEchoTracker.logInBackgroundThread && echoMessageQueue != null) {
            thread(start = true, priority = Thread.NORM_PRIORITY) {
                try {
                    while (true) {
@@ -97,6 +99,7 @@ class LogBuffer @JvmOverloads constructor(
                }
            }
        }
    }

    var frozen = false
        private set
@@ -192,9 +195,15 @@ class LogBuffer @JvmOverloads constructor(
            buffer.removeFirst()
        }
        buffer.add(message as LogMessageImpl)
        // Log in the background thread only if it has capacity to avoid blocking this thread
        if (echoMessageQueue.remainingCapacity() > 0) {
        // Log in the background thread only if echoMessageQueue exists and has capacity (checking
        // capacity avoids the possibility of blocking this thread)
        if (echoMessageQueue != null && echoMessageQueue.remainingCapacity() > 0) {
            try {
                echoMessageQueue.put(message)
            } catch (e: InterruptedException) {
                // the background thread has been shut down, so just log on this one
                echoToDesiredEndpoints(message)
            }
        } else {
            echoToDesiredEndpoints(message)
        }
+5 −0
Original line number Diff line number Diff line
@@ -29,4 +29,9 @@ interface LogcatEchoTracker {
     * Whether [tagName] should echo messages of [level] or higher to logcat.
     */
    fun isTagLoggable(tagName: String, level: LogLevel): Boolean

    /**
     * Whether to log messages in a background thread.
     */
    val logInBackgroundThread: Boolean
}
+1 −0
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ class LogcatEchoTrackerDebug private constructor(
) : LogcatEchoTracker {
    private val cachedBufferLevels: MutableMap<String, LogLevel> = mutableMapOf()
    private val cachedTagLevels: MutableMap<String, LogLevel> = mutableMapOf()
    override val logInBackgroundThread = true

    companion object Factory {
        @JvmStatic
+2 −0
Original line number Diff line number Diff line
@@ -20,6 +20,8 @@ package com.android.systemui.log
 * Production version of [LogcatEchoTracker] that isn't configurable.
 */
class LogcatEchoTrackerProd : LogcatEchoTracker {
    override val logInBackgroundThread = false

    override fun isBufferLoggable(bufferName: String, level: LogLevel): Boolean {
        return level >= LogLevel.WARNING
    }
+1 −0
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ fun logcatLogBuffer(name: String = "EchoToLogcatLogBuffer") =
 * A [LogcatEchoTracker] that always allows echoing to the logcat.
 */
class LogcatEchoTrackerAlways : LogcatEchoTracker {
    override val logInBackgroundThread = false
    override fun isBufferLoggable(bufferName: String, level: LogLevel): Boolean = true
    override fun isTagLoggable(tagName: String, level: LogLevel): Boolean = true
}