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
This commit is contained in:
Justin Weir
2022-04-14 11:34:59 +00:00
committed by Android (Google) Code Review
5 changed files with 44 additions and 0 deletions

View File

@@ -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)

View File

@@ -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
}

View File

@@ -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

View File

@@ -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
}

View File

@@ -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
}