常州企業(yè)網(wǎng)站建設(shè)價(jià)格網(wǎng)站優(yōu)化關(guān)鍵詞排名
背景:
看SystemUI的鎖屏相關(guān)代碼時(shí)候發(fā)現(xiàn)SystemUI有一個(gè)日志打印相關(guān)的方法調(diào)用,相比于常規(guī)的Log.i直接可以logcat查看方式還是比較新穎。
具體日志打印代碼如下:
下面就來介紹一下這個(gè)ShadeLogger到底是如何打印的。
分析源碼:
源碼位置:
frameworks/base/packages/SystemUI/src/com/android/systemui/shade/ShadeLogger.kt
明顯是一個(gè)kt類,這里就只拿一個(gè)logEndMotionEvent方法來進(jìn)行源碼分析
fun logEndMotionEvent(msg: String,forceCancel: Boolean,expand: Boolean,
)
{buffer.log(TAG,LogLevel.VERBOSE,{str1 = msgbool1 = forceCancelbool2 = expand},{ "$str1; force=$bool1; expand=$bool2" })
}
可以看到這里看到實(shí)際是調(diào)用的buffer.log方法,也有對(duì)應(yīng)TAG和LogLevel等級(jí)。
那么下面來看看這個(gè)buffer.log中的buffer哪里來的,但是因?yàn)檫@構(gòu)造都是采用了很多注解drag2方式,所以不方便找,這里找到了一個(gè)NotificationPanelViewControllerBaseTest一個(gè)測(cè)試類有手動(dòng)進(jìn)行構(gòu)造,這里也可以看出相關(guān)過程
frameworks/base/packages/SystemUI/tests/src/com/android/systemui/shade/NotificationPanelViewControllerBaseTest.java
具體過程如下:
再看看mShadeLog構(gòu)造
再看看logcatLogBuffer方法
這里調(diào)用到了LogBuffer類,注意這里test類給的是50,實(shí)際的Shader類給的是500
frameworks/base/packages/SystemUI/log/src/com/android/systemui/log/LogBuffer.kt
看看log方法:
inline fun log(tag: String,level: LogLevel,messageInitializer: MessageInitializer,noinline messagePrinter: MessagePrinter,exception: Throwable? = null,
) {val message = obtain(tag, level, messagePrinter, exception)messageInitializer(message)commit(message)
}
看看obtain方法:
@Synchronizedoverride fun obtain(tag: String,level: LogLevel,messagePrinter: MessagePrinter,exception: Throwable?,): LogMessage {if (!mutable) {return FROZEN_MESSAGE}val message = buffer.advance()//可以看到這里是buffer中獲取,message.reset(tag, level, System.currentTimeMillis(), messagePrinter, exception)return message}
這里其實(shí)只是看出來了buffer中搞出了一個(gè)message,根據(jù)傳遞來的tag和msg
接下來重點(diǎn)看看commit方法
override fun commit(message: LogMessage) {if (echoMessageQueue != null && echoMessageQueue.remainingCapacity() > 0) {try {echoMessageQueue.put(message)//主要就是放入隊(duì)列} catch (e: InterruptedException) {// the background thread has been shut down, so just log on this oneechoToDesiredEndpoints(message)}} else {echoToDesiredEndpoints(message)}
}
commit主要就是實(shí)現(xiàn)對(duì)message放入到echoMessageQueue,那么什么時(shí)候取這個(gè)隊(duì)列呢?
這里要看最開始的init方法中有啟動(dòng)一個(gè)線程
init {if (logcatEchoTracker.logInBackgroundThread && echoMessageQueue != null) {thread(start = true, name = "LogBuffer-$name", priority = Thread.NORM_PRIORITY) {try {while (true) {//死循環(huán)的取出隊(duì)列echoToDesiredEndpoints(echoMessageQueue.take())//調(diào)用echoToDesiredEndpoints來處理消息}} catch (e: InterruptedException) {Thread.currentThread().interrupt()}}}
}
具體echoToDesiredEndpoints方法如下:
private fun echoToDesiredEndpoints(message: LogMessage) {//獲取log打印level,即其實(shí)可以通過命令來控制打印level,這里的level本質(zhì)是來自settings,具體啥settings值下面操作時(shí)候會(huì)講解val includeInLogcat =logcatEchoTracker.isBufferLoggable(name, message.level) ||logcatEchoTracker.isTagLoggable(message.tag, message.level)echo(message, toLogcat = includeInLogcat, toSystrace = systrace)//有了上面level后,echo開始處理}private fun echo(message: LogMessage, toLogcat: Boolean, toSystrace: Boolean) {if (toLogcat || toSystrace) {val strMessage = message.messagePrinter(message)if (toSystrace) {echoToSystrace(message, strMessage)//可以看這個(gè)日志還支持systrace相關(guān)}if (toLogcat) {echoToLogcat(message, strMessage)//這里就是最普通的logcat打印出來}}}
//具體的echoToLogcat其實(shí)就是根據(jù)傳遞進(jìn)來的等級(jí)進(jìn)行普通log打印private fun echoToLogcat(message: LogMessage, strMessage: String) {when (message.level) {LogLevel.VERBOSE -> Log.v(message.tag, strMessage, message.exception)LogLevel.DEBUG -> Log.d(message.tag, strMessage, message.exception)LogLevel.INFO -> Log.i(message.tag, strMessage, message.exception)LogLevel.WARNING -> Log.w(message.tag, strMessage, message.exception)LogLevel.ERROR -> Log.e(message.tag, strMessage, message.exception)LogLevel.WTF -> Log.wtf(message.tag, strMessage, message.exception)}}
到此LogBuffer源碼也就大概分析完成,可以得出以下幾個(gè)結(jié)論:
1、所有的埋點(diǎn)日志會(huì)保存到buffer中,這個(gè)buffer只是在內(nèi)存中的一個(gè)環(huán)形buffer,有固定大小
2、buffer中的日志是可以 實(shí)現(xiàn)輸出到logcat和systrace的功能
那么具體如何控制輸出到logcat,還有如何看buffer中的日志呢?接下來看看使用方法
使用方式:
在類的最開始部分有如下的使用注釋:
/*** A simple ring buffer of recyclable log messages** The goal of this class is to enable logging that is both extremely chatty and extremely* lightweight. If done properly, logging a message will not result in any heap allocations or* string generation. Messages are only converted to strings if the log is actually dumped (usually* as the result of taking a bug report).** You can dump the entire buffer at any time by running:* ```* $ adb shell dumpsys activity service com.android.systemui/.SystemUIService <bufferName>* ```** ...where `bufferName` is the (case-sensitive) [name] passed to the constructor.** By default, only messages of WARN level or higher are echoed to logcat, but this can be adjusted* locally (usually for debugging purposes).** To enable logcat echoing for an entire buffer:* ```* $ adb shell settings put global systemui/buffer/<bufferName> <level>* ```** To enable logcat echoing for a specific tag:* ```* $ adb shell settings put global systemui/tag/<tag> <level>* ```** In either case, `level` can be any of `verbose`, `debug`, `info`, `warn`, `error`, `assert`, or* the first letter of any of the previous.** In SystemUI, buffers are provided by LogModule. Instances should be created using a SysUI* LogBufferFactory.** @param name The name of this buffer, printed when the buffer is dumped and in some other* situations.* @param maxSize The maximum number of messages to keep in memory at any one time. Buffers start* out empty and grow up to [maxSize] as new messages are logged. Once the buffer's size reaches* the maximum, it behaves like a ring buffer.*/
其實(shí)上面已經(jīng)寫的很詳細(xì)了,主要就是2個(gè)核心點(diǎn),一個(gè)可以通過dumpsys看所有日志,一個(gè)是可以控制logcat輸出
控制dumpsys查看方法
adb shell dumpsys activity service com.android.systemui/.SystemUIService <bufferName>
比如這里的對(duì)ShadeLog
adb shell dumpsys activity service com.android.systemui/.SystemUIService ShadeLog
dumpsys后可以查看到相關(guān)的Log:
看到這個(gè)dump日志就感覺非常詳細(xì)的記錄了Shade鎖屏相關(guān)的操作,相關(guān)的tag等也是在ShadeLogger.kt定義的
如果想要普通logcat輸出呢?
adb shell settings put global systemui/tag/ShadeLog v
這里其實(shí)就是配置一個(gè)settings,然后上面的提到的echoToDesiredEndpoints的 logcatEchoTracker.isBufferLoggable就會(huì)去查詢這個(gè)settings值。
總結(jié)圖
更多framework詳細(xì)代碼和資料參考如下鏈接
投屏專題部分:
https://mp.weixin.qq.com/s/IGm6VHMiAOPejC_H3N_SNg
hal+perfetto+surfaceflinger
https://mp.weixin.qq.com/s/LbVLnu1udqExHVKxd74ILg
其他課程七件套專題:
點(diǎn)擊這里
https://mp.weixin.qq.com/s/Qv8zjgQ0CkalKmvi8tMGaw
視頻試看:
https://www.bilibili.com/video/BV1wc41117L4/
參考相關(guān)鏈接:
https://blog.csdn.net/zhimokf/article/details/137958615
更多framework假威風(fēng)耗:androidframework007