ADB,即 Android Debug Bridge,它是 Android 開發/測試人員不可替代的強大工具,也是 Android 設備玩家的好玩具。
注: 有部分命令的支持情況可能與 Android 系統版本及定製 ROM 的實現有關。
- [查看日誌]
- [Android 日誌]
- [按級別過濾日誌]
- [按 tag 和級別過濾日誌]
- [日誌格式]
- [清空日誌]
- [內核日誌]
- [Android 日誌]
查看日誌
Android 系統的日誌分爲兩部分,底層的 Linux 內核日誌輸出到 /proc/kmsg,Android 的日誌輸出到 /dev/log。
Android 日誌
命令格式:
[adb] logcat [<option>] ... [<filter-spec>] ...
常用用法列舉如下:
按級別過濾日誌
Android 的日誌分爲如下幾個優先級(priority):
- V —— Verbose(最低,輸出得最多)
- D —— Debug
- I —— Info
- W —— Warning
- E —— Error
- F —— Fatal
- S —— Silent(最高,啥也不輸出)
按某級別過濾日誌則會將該級別及以上的日誌輸出。
比如,命令:
adb logcat *:W
會將 Warning、Error、Fatal 和 Silent 日誌輸出。
(注: 在 macOS 下需要給 *:W
這樣以 *
作爲 tag 的參數加雙引號,如 adb logcat "*:W"
,不然會報錯 no matches found: *:W
。)
按 tag 和級別過濾日誌
<filter-spec>
可以由多個 <tag>[:priority]
組成。
比如,命令:
adb logcat ActivityManager:I MyApp:D *:S
表示輸出 tag ActivityManager
的 Info 以上級別日誌,輸出 tag MyApp
的 Debug 以上級別日誌,及其它 tag 的 Silent 級別日誌(即屏蔽其它 tag 日誌)。
日誌格式
可以用 adb logcat -v <format>
選項指定日誌輸出格式。
日誌支持按以下幾種 <format>
:
-
brief
默認格式。格式爲:
<priority>/<tag>(<pid>): <message>
示例:
D/HeadsetStateMachine( 1785): Disconnected process message: 10, size: 0
-
process
格式爲:
<priority>(<pid>) <message>
示例:
D( 1785) Disconnected process message: 10, size: 0 (HeadsetStateMachine)
-
tag
格式爲:
<priority>/<tag>: <message>
示例:
D/HeadsetStateMachine: Disconnected process message: 10, size: 0
-
raw
格式爲:
<message>
示例:
Disconnected process message: 10, size: 0
-
time
格式爲:
<datetime> <priority>/<tag>(<pid>): <message>
示例:
08-28 22:39:39.974 D/HeadsetStateMachine( 1785): Disconnected process message: 10, size: 0
-
threadtime
格式爲:
<datetime> <pid> <tid> <priority> <tag>: <message>
示例:
08-28 22:39:39.974 1785 1832 D HeadsetStateMachine: Disconnected process message: 10, size: 0
-
long
格式爲:
[ <datetime> <pid>:<tid> <priority>/<tag> ] <message>
示例:
[ 08-28 22:39:39.974 1785: 1832 D/HeadsetStateMachine ] Disconnected process message: 10, size: 0
指定格式可與上面的過濾同時使用。比如:
adb logcat -v long ActivityManager:I *:S
清空日誌
adb logcat -c
內核日誌
命令:
adb shell dmesg
輸出示例:
<6>[14201.684016] PM: noirq resume of devices complete after 0.982 msecs
<6>[14201.685525] PM: early resume of devices complete after 0.838 msecs
<6>[14201.753642] PM: resume of devices complete after 68.106 msecs
<4>[14201.755954] Restarting tasks ... done.
<6>[14201.771229] PM: suspend exit 2016-08-28 13:31:32.679217193 UTC
<6>[14201.872373] PM: suspend entry 2016-08-28 13:31:32.780363596 UTC
<6>[14201.872498] PM: Syncing filesystems ... done.
中括號裏的 [14201.684016]
代表內核開始啓動後的時間,單位爲秒。
通過內核日誌我們可以做一些事情,比如衡量內核啓動時間,在系統啓動完畢後的內核日誌裏找到 Freeing init memory
那一行前面的時間就是。