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

Commit aadbf51a authored by Hawkwood Glazier's avatar Hawkwood Glazier Committed by Android (Google) Code Review
Browse files

Merge "Fix threading problem with LogcatOnlyMessageBuffer" into main

parents 41c1e2ba d33928b3
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -41,6 +41,10 @@ data class LogMessageImpl(
    override var bool4: Boolean,
) : LogMessage {

    fun clear() {
        reset(DEFAULT_TAG, LogLevel.DEBUG, 0, DEFAULT_PRINTER)
    }

    fun reset(
        tag: String,
        level: LogLevel,
@@ -86,7 +90,7 @@ data class LogMessageImpl(
                false,
                false,
                false,
                false
                false,
            )
        }
    }
+20 −23
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.systemui.log.core

import android.util.Log
import com.android.systemui.log.LogMessageImpl
import kotlin.collections.ArrayDeque

/**
 * A simple implementation of [MessageBuffer] that forwards messages to [android.util.Log]
@@ -26,39 +27,24 @@ import com.android.systemui.log.LogMessageImpl
 */
class LogcatOnlyMessageBuffer(
    val targetLogLevel: LogLevel,
    val maxMessageCount: Int = DEFAULT_MESSAGE_MAX_COUNT,
) : MessageBuffer {
    private val singleMessage = LogMessageImpl.Factory.create()
    private var isObtained: Boolean = false
    private val messages = ArrayDeque<LogMessageImpl>(maxMessageCount)

    @Synchronized
    override fun obtain(
        tag: String,
        level: LogLevel,
        messagePrinter: MessagePrinter,
        exception: Throwable?,
    ): LogMessage {
        if (isObtained) {
            throw UnsupportedOperationException(
                "Message has already been obtained. Call order is incorrect."
            )
        val message =
            synchronized(messages) { messages.removeFirstOrNull() }
                ?: LogMessageImpl.Factory.create()
        message.reset(tag, level, System.currentTimeMillis(), messagePrinter, exception)
        return message
    }

        singleMessage.reset(tag, level, System.currentTimeMillis(), messagePrinter, exception)
        isObtained = true
        return singleMessage
    }

    @Synchronized
    override fun commit(message: LogMessage) {
        if (singleMessage != message) {
            throw IllegalArgumentException("Message argument is not the expected message.")
        }
        if (!isObtained) {
            throw UnsupportedOperationException(
                "Message has not been obtained. Call order is incorrect."
            )
        }

        if (message.level >= targetLogLevel) {
            val strMessage = message.messagePrinter(message)
            when (message.level) {
@@ -71,6 +57,17 @@ class LogcatOnlyMessageBuffer(
            }
        }

        isObtained = false
        if (message is LogMessageImpl) {
            message.clear()
            synchronized(messages) {
                if (messages.size < maxMessageCount) {
                    messages.addLast(message)
                }
            }
        }
    }

    companion object {
        val DEFAULT_MESSAGE_MAX_COUNT = 4
    }
}