diff --git a/systemUI/log/Android.bp b/systemUI/log/Android.bp index afdcae481d..3cb2b4c3b1 100644 --- a/systemUI/log/Android.bp +++ b/systemUI/log/Android.bp @@ -22,15 +22,35 @@ package { default_applicable_licenses: ["frameworks_base_packages_SystemUI_license"], } +java_library { + sdk_version: "current", + name: "SystemUILogCoreLib", + device_supported: true, + srcs: [ + "src/**/core/*.java", + "src/**/core/*.kt", + ], + static_libs: [ + "error_prone_annotations", + ], + + // Enforce compat with PluginCoreLib + java_version: "1.8", +} + java_library { name: "SystemUILogLib", srcs: [ "src/**/*.java", "src/**/*.kt", ], + exclude_srcs: [ + "src/**/core/*.java", + "src/**/core/*.kt", + ], static_libs: [ - "error_prone_annotations", "SystemUICommon", + "SystemUILogCoreLib", ], kotlincflags: ["-Xjvm-default=all"], } diff --git a/systemUI/log/src/com/android/systemui/Dumpable.java b/systemUI/log/src/com/android/systemui/Dumpable.java new file mode 100644 index 0000000000..2941ddcd15 --- /dev/null +++ b/systemUI/log/src/com/android/systemui/Dumpable.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui; + +import androidx.annotation.NonNull; + +import java.io.PrintWriter; + +/** + * Implemented by classes who want to be in: + * {@code adb shell dumpsys activity service com.android.systemui} + * + * @see com.android.systemui.dump.DumpManager + */ +public interface Dumpable { + + /** + * Called when it's time to dump the internal state + * @param pw Where to write your dump to. + * @param args Arguments. + */ + void dump(@NonNull PrintWriter pw, @NonNull String[] args); +} diff --git a/systemUI/log/src/com/android/systemui/log/LogMessageImpl.kt b/systemUI/log/src/com/android/systemui/log/LogMessageImpl.kt index 33cc199e71..d8adcb0303 100644 --- a/systemUI/log/src/com/android/systemui/log/LogMessageImpl.kt +++ b/systemUI/log/src/com/android/systemui/log/LogMessageImpl.kt @@ -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, ) } } diff --git a/systemUI/log/src/com/android/systemui/log/LogcatOnlyMessageBuffer.kt b/systemUI/log/src/com/android/systemui/log/LogcatOnlyMessageBuffer.kt new file mode 100644 index 0000000000..dbee7eb071 --- /dev/null +++ b/systemUI/log/src/com/android/systemui/log/LogcatOnlyMessageBuffer.kt @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui.log + +import com.android.systemui.log.core.LogLevel +import com.android.systemui.log.core.LogMessage +import com.android.systemui.log.core.MessageBuffer +import com.android.systemui.log.core.MessagePrinter +import kotlin.collections.ArrayDeque + +/** + * A simple implementation of [MessageBuffer] that forwards messages to [android.util.Log] + * immediately. This defeats the intention behind [LogBuffer] and should only be used when + * [LogBuffer]s are unavailable in a certain context. + */ +class LogcatOnlyMessageBuffer( + private val targetLogLevel: LogLevel, + private val maxMessageCount: Int, +): MessageBuffer { + private val messages = ArrayDeque(maxMessageCount) + + constructor(targetLogLevel: LogLevel) : this(targetLogLevel, DEFAULT_MESSAGE_MAX_COUNT) + + override fun obtain( + tag: String, + level: LogLevel, + messagePrinter: MessagePrinter, + exception: Throwable?, + ): LogMessage { + val message = + synchronized(messages) { messages.removeFirstOrNull() } + ?: LogMessageImpl.Factory.create() + message.reset(tag, level, System.currentTimeMillis(), messagePrinter, exception) + return message + } + + override fun commit(message: LogMessage) { + if (message.level >= targetLogLevel) { + val strMessage = message.messagePrinter(message) + message.level.logcatFunc(message.tag, strMessage, message.exception) + } + + if (message is LogMessageImpl) { + message.clear() + synchronized(messages) { + if (messages.size < maxMessageCount) { + messages.addLast(message) + } + } + } + } + + companion object { + private const val DEFAULT_MESSAGE_MAX_COUNT = 4 + } +} diff --git a/systemUI/log/src/com/android/systemui/log/core/LogLevel.kt b/systemUI/log/src/com/android/systemui/log/core/LogLevel.kt index a30d099066..769e93f416 100644 --- a/systemUI/log/src/com/android/systemui/log/core/LogLevel.kt +++ b/systemUI/log/src/com/android/systemui/log/core/LogLevel.kt @@ -19,11 +19,15 @@ package com.android.systemui.log.core import android.util.Log /** Enum version of @Log.Level */ -enum class LogLevel(val nativeLevel: Int, val shortString: String) { - VERBOSE(Log.VERBOSE, "V"), - DEBUG(Log.DEBUG, "D"), - INFO(Log.INFO, "I"), - WARNING(Log.WARN, "W"), - ERROR(Log.ERROR, "E"), - WTF(Log.ASSERT, "WTF") +enum class LogLevel( + val nativeLevel: Int, + val shortString: String, + val logcatFunc: (String, String, Throwable?) -> Unit, +) { + VERBOSE(Log.VERBOSE, "V", Log::v), + DEBUG(Log.DEBUG, "D", Log::d), + INFO(Log.INFO, "I", Log::i), + WARNING(Log.WARN, "W", Log::w), + ERROR(Log.ERROR, "E", Log::e), + WTF(Log.ASSERT, "WTF", Log::wtf), }