1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.systemui.log 18 19 import android.util.Log 20 import com.android.systemui.dump.DumpManager 21 import com.android.systemui.log.dagger.LogModule 22 import java.io.PrintWriter 23 import java.text.SimpleDateFormat 24 import java.util.ArrayDeque 25 import java.util.Locale 26 27 /** 28 * A simple ring buffer of recyclable log messages 29 * 30 * The goal of this class is to enable logging that is both extremely chatty and extremely 31 * lightweight. If done properly, logging a message will not result in any heap allocations or 32 * string generation. Messages are only converted to strings if the log is actually dumped (usually 33 * as the result of taking a bug report). 34 * 35 * You can dump the entire buffer at any time by running: 36 * 37 * ``` 38 * $ adb shell dumpsys activity service com.android.systemui/.SystemUIService <bufferName> 39 * ``` 40 * 41 * ...where `bufferName` is the (case-sensitive) [name] passed to the constructor. 42 * 43 * By default, only messages of WARN level or higher are echoed to logcat, but this can be adjusted 44 * locally (usually for debugging purposes). 45 * 46 * To enable logcat echoing for an entire buffer: 47 * 48 * ``` 49 * $ adb shell settings put global systemui/buffer/<bufferName> <level> 50 * ``` 51 * 52 * To enable logcat echoing for a specific tag: 53 * 54 * ``` 55 * $ adb shell settings put global systemui/tag/<tag> <level> 56 * ``` 57 * 58 * In either case, `level` can be any of `verbose`, `debug`, `info`, `warn`, `error`, `assert`, or 59 * the first letter of any of the previous. 60 * 61 * Buffers are provided by [LogModule]. 62 * 63 * @param name The name of this buffer 64 * @param maxLogs The maximum number of messages to keep in memory at any one time, including the 65 * unused pool. 66 * @param poolSize The maximum amount that the size of the buffer is allowed to flex in response to 67 * sequential calls to [document] that aren't immediately followed by a matching call to [push]. 68 */ 69 class LogBuffer( 70 private val name: String, 71 private val maxLogs: Int, 72 private val poolSize: Int, 73 private val logcatEchoTracker: LogcatEchoTracker 74 ) { 75 private val buffer: ArrayDeque<LogMessageImpl> = ArrayDeque() 76 77 var frozen = false 78 private set 79 attachnull80 fun attach(dumpManager: DumpManager) { 81 dumpManager.registerBuffer(name, this) 82 } 83 84 /** 85 * Logs a message to the log buffer 86 * 87 * May also log the message to logcat if echoing is enabled for this buffer or tag. 88 * 89 * The actual string of the log message is not constructed until it is needed. To accomplish 90 * this, logging a message is a two-step process. First, a fresh instance of [LogMessage] is 91 * obtained and is passed to the [initializer]. The initializer stores any relevant data on the 92 * message's fields. The message is then inserted into the buffer where it waits until it is 93 * either pushed out by newer messages or it needs to printed. If and when this latter moment 94 * occurs, the [printer] function is called on the message. It reads whatever data the 95 * initializer stored and converts it to a human-readable log message. 96 * 97 * @param tag A string of at most 23 characters, used for grouping logs into categories or 98 * subjects. If this message is echoed to logcat, this will be the tag that is used. 99 * @param level Which level to log the message at, both to the buffer and to logcat if it's 100 * echoed. In general, a module should split most of its logs into either INFO or DEBUG level. 101 * INFO level should be reserved for information that other parts of the system might care 102 * about, leaving the specifics of code's day-to-day operations to DEBUG. 103 * @param initializer A function that will be called immediately to store relevant data on the 104 * log message. The value of `this` will be the LogMessage to be initialized. 105 * @param printer A function that will be called if and when the message needs to be dumped to 106 * logcat or a bug report. It should read the data stored by the initializer and convert it to 107 * a human-readable string. The value of `this` will be the LogMessage to be printed. 108 * **IMPORTANT:** The printer should ONLY ever reference fields on the LogMessage and NEVER any 109 * variables in its enclosing scope. Otherwise, the runtime will need to allocate a new instance 110 * of the printer for each call, thwarting our attempts at avoiding any sort of allocation. 111 */ lognull112 inline fun log( 113 tag: String, 114 level: LogLevel, 115 initializer: LogMessage.() -> Unit, 116 noinline printer: LogMessage.() -> String 117 ) { 118 if (!frozen) { 119 val message = obtain(tag, level, printer) 120 initializer(message) 121 push(message) 122 } 123 } 124 125 /** 126 * Same as [log], but doesn't push the message to the buffer. Useful if you need to supply a 127 * "reason" for doing something (the thing you supply the reason to will presumably call [push] 128 * on that message at some point). 129 */ documentnull130 inline fun document( 131 tag: String, 132 level: LogLevel, 133 initializer: LogMessage.() -> Unit, 134 noinline printer: LogMessage.() -> String 135 ): LogMessage { 136 val message = obtain(tag, level, printer) 137 initializer(message) 138 return message 139 } 140 141 /** 142 * Obtains an instance of [LogMessageImpl], usually from the object pool. If the pool has been 143 * exhausted, creates a new instance. 144 * 145 * In general, you should call [log] or [document] instead of this method. 146 */ 147 @Synchronized obtainnull148 fun obtain( 149 tag: String, 150 level: LogLevel, 151 printer: (LogMessage) -> String 152 ): LogMessageImpl { 153 val message = when { 154 frozen -> LogMessageImpl.create() 155 buffer.size > maxLogs - poolSize -> buffer.removeFirst() 156 else -> LogMessageImpl.create() 157 } 158 message.reset(tag, level, System.currentTimeMillis(), printer) 159 return message 160 } 161 162 /** 163 * Pushes a message into buffer, possibly evicting an older message if the buffer is full. 164 */ 165 @Synchronized pushnull166 fun push(message: LogMessage) { 167 if (frozen) { 168 return 169 } 170 if (buffer.size == maxLogs) { 171 Log.e(TAG, "LogBuffer $name has exceeded its pool size") 172 buffer.removeFirst() 173 } 174 buffer.add(message as LogMessageImpl) 175 if (logcatEchoTracker.isBufferLoggable(name, message.level) || 176 logcatEchoTracker.isTagLoggable(message.tag, message.level)) { 177 echoToLogcat(message) 178 } 179 } 180 181 /** Converts the entire buffer to a newline-delimited string */ 182 @Synchronized dumpnull183 fun dump(pw: PrintWriter, tailLength: Int) { 184 val start = if (tailLength <= 0) { 0 } else { buffer.size - tailLength } 185 186 for ((i, message) in buffer.withIndex()) { 187 if (i >= start) { 188 dumpMessage(message, pw) 189 } 190 } 191 } 192 193 /** 194 * "Freezes" the contents of the buffer, making them immutable until [unfreeze] is called. 195 * Calls to [log], [document], [obtain], and [push] will not affect the buffer and will return 196 * dummy values if necessary. 197 */ 198 @Synchronized freezenull199 fun freeze() { 200 if (!frozen) { 201 log(TAG, LogLevel.DEBUG, { str1 = name }, { "$str1 frozen" }) 202 frozen = true 203 } 204 } 205 206 /** 207 * Undoes the effects of calling [freeze]. 208 */ 209 @Synchronized unfreezenull210 fun unfreeze() { 211 if (frozen) { 212 log(TAG, LogLevel.DEBUG, { str1 = name }, { "$str1 unfrozen" }) 213 frozen = false 214 } 215 } 216 dumpMessagenull217 private fun dumpMessage(message: LogMessage, pw: PrintWriter) { 218 pw.print(DATE_FORMAT.format(message.timestamp)) 219 pw.print(" ") 220 pw.print(message.level) 221 pw.print(" ") 222 pw.print(message.tag) 223 pw.print(" ") 224 pw.println(message.printer(message)) 225 } 226 echoToLogcatnull227 private fun echoToLogcat(message: LogMessage) { 228 val strMessage = message.printer(message) 229 when (message.level) { 230 LogLevel.VERBOSE -> Log.v(message.tag, strMessage) 231 LogLevel.DEBUG -> Log.d(message.tag, strMessage) 232 LogLevel.INFO -> Log.i(message.tag, strMessage) 233 LogLevel.WARNING -> Log.w(message.tag, strMessage) 234 LogLevel.ERROR -> Log.e(message.tag, strMessage) 235 LogLevel.WTF -> Log.wtf(message.tag, strMessage) 236 } 237 } 238 } 239 240 private const val TAG = "LogBuffer" 241 private val DATE_FORMAT = SimpleDateFormat("MM-dd HH:mm:ss.SSS", Locale.US) 242