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

Commit 4682c657 authored by Justin Weir's avatar Justin Weir Committed by Android (Google) Code Review
Browse files

Merge changes from topic "b221419865" into tm-dev

* changes:
  Change Logcat to only log in background thread in debug mode
  Move logging to background thread in LogBuffer
parents 123c1b09 1702ffa6
Loading
Loading
Loading
Loading
+35 −0
Original line number Diff line number Diff line
@@ -23,6 +23,9 @@ import java.io.PrintWriter
import java.text.SimpleDateFormat
import java.util.ArrayDeque
import java.util.Locale
import java.util.concurrent.ArrayBlockingQueue
import java.util.concurrent.BlockingQueue
import kotlin.concurrent.thread

/**
 * A simple ring buffer of recyclable log messages
@@ -81,6 +84,22 @@ class LogBuffer @JvmOverloads constructor(
    }

    private val buffer: ArrayDeque<LogMessageImpl> = ArrayDeque()
    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) {
                        echoToDesiredEndpoints(echoMessageQueue.take())
                    }
                } catch (e: InterruptedException) {
                    Thread.currentThread().interrupt()
                }
            }
        }
    }

    var frozen = false
        private set
@@ -176,6 +195,22 @@ class LogBuffer @JvmOverloads constructor(
            buffer.removeFirst()
        }
        buffer.add(message as LogMessageImpl)
        // 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)
        }
    }

    /** Sends message to echo after determining whether to use Logcat and/or systrace. */
    private fun echoToDesiredEndpoints(message: LogMessageImpl) {
        val includeInLogcat = logcatEchoTracker.isBufferLoggable(name, message.level) ||
                logcatEchoTracker.isTagLoggable(message.tag, message.level)
        echo(message, toLogcat = includeInLogcat, toSystrace = systrace)
+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
}