Android ADB 命令之 Logcat 发布 2015-03-15 / 更新 2015-03-17 / 5,478 次 / 快抢沙发 /

摘要:Android ADB 命令中的 Logcat 命令是必须掌握的。

Runtime Environment
OS: Windows 8.1
Tools: ADB
最近在封装 Log 日志库的时候,发现需要用到 Android ADB 命令中的 Logcat 命令,整理如下。
如果你对Android ADB 命令中的某一条命令的用法不熟悉的话,我们可以通过 help 命令来查看其用法:
1C:\>adb shell
2root@android:/ $ logcat --help
3logcat --help
4Usage: logcat [options] [filterspecs]
5options include:
6  -s              Set default filter to silent.
7                  Like specifying filterspec '*:s'
8  -f <filename>   Log to file. Default to stdout
9  -r [<kbytes>]   Rotate log every kbytes. (16 if unspecified). Requires -f
10  -n <count>      Sets max number of rotated logs to <count>, default 4
11  -v <format>     Sets the log print format, where <format> is one of:
12 
13                  brief process tag thread raw time threadtime long
14 
15  -c              clear (flush) the entire log and exit
16  -d              dump the log and then exit (don't block)
17  -t <count>      print only the most recent <count> lines (implies -d)
18  -g              get the size of the log's ring buffer and exit
19  -b <buffer>     Request alternate ring buffer, 'main', 'system', 'radio'
20                  or 'events'. Multiple -b parameters are allowed and the
21                  results are interleaved. The default is -b main -b system.
22  -B              output the log in binary
23filterspecs are a series of
24  <tag>[:priority]
25 
26where <tag> is a log component tag (or * for all) and priority is:
27  V    Verbose
28  D    Debug
29  I    Info
30  W    Warn
31  E    Error
32  F    Fatal
33  S    Silent (supress all output)
34 
35'*' means '*:d' and <tag> by itself means <tag>:v
36 
37If not specified on the commandline, filterspec is set from ANDROID_LOG_TAGS.
38If no filterspec is found, filter defaults to '*:I'
39 
40If not specified with -v, format is set from ANDROID_PRINTF_LOG
41or defaults to "brief"
1C:\>adb shell
2root@android:/ $ logcat --help
3logcat --help
4用法: logcat [选项] [过滤参数]
5选项包括:
6  -s              设置默认的过滤器为:silent.
7                  如同指定过滤参数 '*:s'
8  -f <文件名>      将 Log 日志保存到文件,默认为不保存,标准输出。
9  -r [<kbytes>]   按照 * KB 分割日志 (16KB 为默认值) 需要配合 -f 一起使用
10  -n <count>      设置最大的分割日志数:count 默认为 4
11  -v <format>     设置日志打印格式,format 为其中之一:
12                  brief process tag thread raw time threadtime long
13 
14  -c              清除整个日志并退出
15  -d              载入整个日志并退出 (非阻塞)
16  -t <count>      仅打印最近的 count 行 (载入日志后退出)
17  -g              获取日志 ring buffer 大小并退出
18  -b <buffer>     请求  alternate ring buffer(此处不能理解 alternate这个词),
19                  'main''system''radio' or 'events'. 允许多 -b 参数,
20                  并且结果是交叉的。默认的是:-b main -b system.
21  -B              二进制输出日志
22 
23过滤参数是一系列的:
24<tag>[:priority]
25 
26<tag> 是日志中的 tag (* 标识所有 tag) 优先级如下:(备注:大小写不区分)
27  V    Verbose
28  D    Debug
29  I    Info
30  W    Warn
31  E    Error
32  F    Fatal
33  S    Silent 禁止所有的输出 (说明:原资料中为 supress all output,
34       其中 supress 这个单词感觉写错了,因为查了相关资料,没有发现
35       有这个单词,应该是 suppress 这个单词)
36 
37'logcat *:*' 表示 'logcat *:d' 'logcat <tag>' 表示 'logcat <tag>:v'
38 
39如果未指定命令行,过滤参数由 ANDROID_LOG_TAGS 设置。
40如果没有设置过滤参数,默认的过滤参数为 *:I
41如果没有通过 -v 指定,那么格式由 ANDROID_PRINTF_LOG 设置,或者默认为 'brief'

逐条命令讲解:

logcat -s
和指定过滤参数 logcat *:s 或 logcat *:S 作用相同:禁止所有输出!
说明:这个命令不知道在实际的过程中有什么作用,后续再理解。
1logcat -s 用法:
2 
3C:\>adb shell
4root@android:/ $ logcat -s
5logcat -s
6--------- beginning of /dev/log/system
7--------- beginning of /dev/log/main
8 
9C:\>adb shell
10root@android:/ $ logcat *:s
11logcat *:s
12--------- beginning of /dev/log/system
13--------- beginning of /dev/log/main
14 
15C:\>adb shell
16root@android:/ $ logcat *:S
17logcat *:S
18--------- beginning of /dev/log/system
19--------- beginning of /dev/log/main
logcat -f filename
将日志保存到文件。
说明:filename 中的尖括号表示必须要有的参数,如果你使用 logcat -f 命令,那么后面必须带有参数。而且这个命令会将当前日志写入文件,后续产生的日志也会写入文件,这条命令不会自己退出。
1logcat -f 用法:
2 
3C:\>adb shell
4root@android:/ $ logcat -f /sdcard/logcat.txt
5logcat -f /sdcard/logcat.txt
6光标会停留在此处闪
logcat -r [kbytes]
按照 * KB 分割日志 (16KB 为默认值) 需要配合 -f 一起使用。
说明:这条命令是需要这样来使用:
logcat -r -f /sdcard/logcat.txt 或
logcat -f /sdcard/logcat.txt -r 13
这条命令会将最近的日志,分割成约 16KB 大小的数据,写入 n (默认为 4) 个文件中 (logcat.txt.1/logcat.txt.2/logcat.txt.3/logcat.txt.4/***/logcat.txt.n),多余的数据写入 logcat.txt 文件中。按照时间先后,会依次写入 logcat.txt/logcat.txt.1/logcat.txt.2/logcat.txt.3/logcat.txt.4/***/logcat.txt.n 。这条命令不会退出,日志会隔一段时间更新写入文件。
说明:logcat -f /sdcard/logcat.txt -r 13 在实际使用过程中,指定分割数据库大小为 13,这条命令是不起作用的,实际还是默认值 16,而且
logcat -r 13 -f /sdcard/logcat.txt
这条命令会提示错误:需要配合 -f 一起使用。实际过程中,这个命令感觉没有什么太大的意义。这条命令不会自己退出。
1logcat -r 用法:
2 
3C:\>adb shell
4root@android:/ $ logcat -r -f /sdcard/logcat.txt
5logcat -r -f /sdcard/logcat.txt
6光标会停留在此处闪
7 
8C:\>adb shell
9root@android:/ $ cd /sdcard
10cd /sdcard
11root@android:/sdcard $ ls -l
12ls -l
13drwxrwx--- root     sdcard_r          1970-07-18 10:54 Alarms
14drwxrwx--x root     sdcard_r          1970-07-18 10:54 Android
15drwxrwx--- root     sdcard_r          2015-03-04 15:53 DCIM
16drwxrwx--- root     sdcard_r          2015-03-04 15:12 Download
17drwxrwx--- root     sdcard_r          1970-07-18 10:54 Movies
18drwxrwx--- root     sdcard_r          2015-03-03 15:06 Music
19drwxrwx--- root     sdcard_r          1970-07-18 10:54 Notifications
20drwxrwx--- root     sdcard_r          1970-07-18 10:54 Pictures
21drwxrwx--- root     sdcard_r          1970-07-18 10:54 Podcasts
22drwxrwx--- root     sdcard_r          1970-07-18 10:54 Ringtones
23drwxrwx--- root     sdcard_r          2015-03-03 17:52 apt
24-rw-rw---- root     sdcard_r    15409 2015-03-08 15:58 logcat.txt
25-rw-rw---- root     sdcard_r    16457 2015-03-08 15:56 logcat.txt.1
26-rw-rw---- root     sdcard_r    16423 2015-03-08 15:55 logcat.txt.2
27-rw-rw---- root     sdcard_r    16404 2015-03-08 15:55 logcat.txt.3
28-rw-rw---- root     sdcard_r    16534 2015-03-08 15:53 logcat.txt.4
logcat -n count
设置最大的分割日志数:count 默认为 4
说明:这个命令需要配合 -r -f 一起使用,不然没有效果。这个命令不会自己退出。
1logcat -n 用法:
2 
3C:\>adb shell
4root@android:/ $ logcat -n 7 -f /sdcard/logcat.txt
5logcat -n 7 -f /sdcard/logcat.txt
6光标会停留在此处闪
7^C
8C:\>adb shell
9root@android:/ $ cd /sdcard
10cd /sdcard
11root@android:/ $ ls -l
12ls -l
13drwxrwx--- root     sdcard_r          1970-07-18 10:54 Alarms
14drwxrwx--x root     sdcard_r          1970-07-18 10:54 Android
15drwxrwx--- root     sdcard_r          2015-03-04 15:53 DCIM
16drwxrwx--- root     sdcard_r          2015-03-04 15:12 Download
17drwxrwx--- root     sdcard_r          1970-07-18 10:54 Movies
18drwxrwx--- root     sdcard_r          2015-03-03 15:06 Music
19drwxrwx--- root     sdcard_r          1970-07-18 10:54 Notifications
20drwxrwx--- root     sdcard_r          1970-07-18 10:54 Pictures
21drwxrwx--- root     sdcard_r          1970-07-18 10:54 Podcasts
22-rw-rw---- root     sdcard_r   447787 2015-03-08 16:24 logcat.txt
23 
24root@android:/ $ logcat -r -n 7 -f /sdcard/logcat.txt
25logcat -r -n 7 -f /sdcard/logcat.txt
26光标会停留在此处闪
27^C
28C:\>adb shell
29root@android:/ $ cd /sdcard
30cd /sdcard
31root@android:/sdcard $ ls -l
32ls -l
33drwxrwx--- root     sdcard_r          1970-07-18 10:54 Alarms
34drwxrwx--x root     sdcard_r          1970-07-18 10:54 Android
35drwxrwx--- root     sdcard_r          2015-03-04 15:53 DCIM
36drwxrwx--- root     sdcard_r          2015-03-04 15:12 Download
37drwxrwx--- root     sdcard_r          1970-07-18 10:54 Movies
38drwxrwx--- root     sdcard_r          2015-03-03 15:06 Music
39drwxrwx--- root     sdcard_r          1970-07-18 10:54 Notifications
40drwxrwx--- root     sdcard_r          1970-07-18 10:54 Pictures
41drwxrwx--- root     sdcard_r          1970-07-18 10:54 Podcasts
42drwxrwx--- root     sdcard_r          1970-07-18 10:54 Ringtones
43drwxrwx--- root     sdcard_r          2015-03-03 17:52 apt
44-rw-rw---- root     sdcard_r    16319 2015-03-08 16:26 logcat.txt
45-rw-rw---- root     sdcard_r    16458 2015-03-08 16:26 logcat.txt.1
46-rw-rw---- root     sdcard_r    16468 2015-03-08 16:26 logcat.txt.2
47-rw-rw---- root     sdcard_r    16414 2015-03-08 16:26 logcat.txt.3
48-rw-rw---- root     sdcard_r    16454 2015-03-08 16:26 logcat.txt.4
49-rw-rw---- root     sdcard_r    16633 2015-03-08 16:26 logcat.txt.5
50-rw-rw---- root     sdcard_r    16412 2015-03-08 16:26 logcat.txt.6
51-rw-rw---- root     sdcard_r    16429 2015-03-08 16:26 logcat.txt.7
logcat -v format
设置打印日志格式。
说明:一般采用logcat -v time 用的比较多。这个命令不会自己退出。
1logcat -v 用法:
2 
3C:\>adb shell
4root@android:/ $ logcat -v brief
5W/EventLoggerService(10471): Unable to send logs Error code: 262146 | Unable to
6resolve host "www.google.com": No address associated with hostname
7 
8root@android:/ $ logcat -v process
9W(22678) Unable to start service Intent { cmp=com.thinksky.itools.markets/com.th
10inksky.itools.download.DownloadService } U=0: not found  (ActivityManager)
11 
12root@android:/ $ logcat -v tag
13W/ActivityManager: Unable to start service Intent { cmp=com.thinksky.itools.mark
14ets/com.thinksky.itools.download.DownloadService } U=0: not found
15 
16root@android:/ $ logcat -v thread
17I(22678:22689) START u0 {act=android.intent.action.MAIN cat=[android.intent.cate
18gory.LAUNCHER] flg=0x10200000 cmp=***} from pid 22945
19 
20root@android:/ $ logcat -v raw
21rmt_storage_connect_cb: clnt_h=0x48 conn_h=0xb82e4160
22 
23root@android:/ $ logcat -v time
2403-08 16:45:57.837 I/rmt_storage(  173): rmt_storage_connect_cb: clnt_h=0x48 con
25n_h=0xb82e4160
26 
27root@android:/ $ logcat -v threadtime
2803-08 16:45:57.837   173   173 I rmt_storage: rmt_storage_connect_cb: clnt_h=0x4
298 conn_h=0xb82e4160
30 
31root@android:/ $ logcat -v long
32[ 03-08 16:45:57.837   173:  173 I/rmt_storage ]
33rmt_storage_connect_cb: clnt_h=0x48 conn_h=0xb82e4160
logcat -c
清除整个日志并退出。
说明:这条命令,适合我们准备要开始抓取日志之前,先前之前的日志清理掉。这条命令执行完成后,自己退出。
1logcat -c 用法:
2 
3C:\>adb shell
4shell@hammerhead:/ $ logcat -c
5logcat -c
6shell@hammerhead:/ $
logcat -d
载入整个日志并退出 (非阻塞)。
说明:这条命令,会载入当前所有日志,然后退出。
1logcat -d 用法:
2 
3C:\>adb shell
4root@android:/ $ logcat -d
5logcat -d
6--------- beginning of /dev/log/main
7D/dalvikvm(22678): GC_CONCURRENT freed 11669K, 27% free 34609K/47380K, paused 3m
8s+6ms, total 61ms
9D/dalvikvm(22999): GC_CONCURRENT freed 365K, 4% free 18144K/18832K, paused 7ms+1
10ms, total 31ms
11--------- beginning of /dev/log/system
12W/ProcessCpuTracker(22678): Skipping unknown process pid 26086
13root@android:/ $
logcat -t count
仅打印最近的 count 行 (载入日志后退出) 。
说明:这条命令,会载入当前最近的 count 条命令,然后退出。
1logcat -t 用法:
2 
3C:\>adb shell
4root@android:/ $ logcat -t 3
5logcat -t 3
6--------- beginning of /dev/log/main
7--------- beginning of /dev/log/system
8D/com.qq.connect(31826): getLinkedFile:/storage/sdcard0-->/storage/emulated/legacy
9D/com.qq.connect(31826): getLinkedFile:/storage/sdcard0-->/storage/emulated/legacy
10D/dalvikvm(31864): GC_CONCURRENT freed 442K, 6% free 18393K/19388K, paused 9ms+1ms
11root@android:/ $
logcat -g
获取日志 ring buffer 大小并退出。
说明:这条命令,会显示日志 ring buffer 大小,然后退出。
1logcat -g 用法:
2 
3C:\>adb shell
4root@android:/ $ logcat -g
5logcat -g
6/dev/log/main: ring buffer is 256Kb (21Kb consumed), max entry is 5120b, max payload is 4076b
7/dev/log/system: ring buffer is 256Kb (4Kb consumed), max entry is 5120b, max payload is 4076b
8root@android:/ $
logcat -b
说明:由于支持多 -b 命令,logcat -b main -b system -b radio -b events
这样的命令是可以被接受的。
1logcat -b 用法:
2说明:我在 logcat -b * 前面加上了 logcat -c 命令,是为了清除之前的日志,同时使用 &&
3执行下一条命令。
4 
5C:\>adb shell
6root@android:/ $ logcat -c && logcat -b main
7logcat -c && logcat -b main
8I/GEL     (22945): handleIntent(Intent { act=android.intent.action.MAIN cat=[and
9roid.intent.category.HOME] flg=0x10200000 cmp=com.google.android.googlequicksear
10chbox/com.google.android.launcher.GEL })
11D/audio_hw_primary(22395): select_devices: out_snd_device(2: speaker) in_snd_dev
12ice(0: )
13D/dalvikvm(22746): GC_FOR_ALLOC freed 1925K, 50% free 23946K/47088K, paused 18ms
14, total 18ms
15 
16C:\>adb shell
17root@android:/ $ logcat -c && logcat -b system
18logcat -c && logcat -b system
19I/ActivityManager(22678): START u0 {act=android.intent.action.MAIN cat=[android.
20intent.category.LAUNCHER] flg=0x10200000 cmp=*****/.activity.******}
21from pid 22945
22I/ActivityManager(22678): START u0 {cmp=***********.MainActivity} from pid 21291
23W/ActivityManager(22678): Unable to start service Intent { cmp=****Service } U=0:
24not found
25 
26C:\>adb shell
27root@android:/ $ logcat -c && logcat -b radio
28logcat -c && logcat -b radio
29D/RILJ    (22903): [7355]> REQUEST_GET_NEIGHBORING_CELL_IDS
30D/RILJ    (22903): [7355]< REQUEST_GET_NEIGHBORING_CELL_IDS
31D/GsmSST  (22903): [GsmSST] SST.getAllCellInfo(): return last, back to back calls
32D/GsmSST  (22903): [GsmSST] SST.getAllCellInfo(): X size=7 list=[CellInfoGsm:{mR
33egistered=YES mTimeStampType=oem_ril mTimeStamp=384988951090840ns CellIdentityGs
34m:{ mMcc=460 mMnc=0 mLac=9363 mCid=5092} CellSignalStrengthGsm: ss=18 ber=99}, C
35ellInfoGsm:{mRegistered=NO mTimeStampType=oem_ril mTimeStamp=384988951090840ns C
36ellIdentityGsm:{ mMcc=460 mMnc=0 mLac=9363 mCid=4262} CellSignalStrengthGsm: ss=
3722 ber=99}, CellInfoGsm:{mRegistered=NO mTimeStampType=oem_ril mTimeStamp=384988
38951090840ns CellIdentityGsm:{ mMcc=460 mMnc=0 mLac=9363 mCid=4261} CellSignalStr
39engthGsm: ss=17 ber=99}, CellInfoGsm:{mRegistered=NO mTimeStampType=oem_ril mTim
40eStamp=384988951090840ns CellIdentityGsm:{ mMcc=460 mMnc=0 mLac=9363 mCid=4263}
41CellSignalStrengthGsm: ss=16 ber=99}, CellInfoGsm:{mRegistered=NO mTimeStampType
42=oem_ril mTimeStamp=384988951090840ns CellIdentityGsm:{ mMcc=460 mMnc=0 mLac=936
433 mCid=4582} CellSignalStrengthGsm: ss=16 ber=99}, CellInfoGsm:{mRegistered=NO m
44TimeStampType=oem_ril mTimeStamp=384988951090840ns CellIdentityGsm:{ mMcc=0 mMnc
45=0 mLac=0 mCid=-1} CellSignalStrengthGsm: ss=16 ber=99}, CellInfoGsm:{mRegistere
46d=NO mTimeStampType=oem_ril mTimeStamp=384988951090840ns CellIdentityGsm:{ mMcc=
470 mMnc=0 mLac=0 mCid=-1} CellSignalStrengthGsm: ss=15 ber=99}]
48 
49C:\>adb shell
50root@android:/ $ logcat -c && logcat -b events
51logcat -c && logcat -b events
52I/am_kill (22678): [0,17133,com.android.providers.partnerbookmarks,15,empty for
531815s]
54I/am_create_service(22678): [0,1120349904,.EventLogService,10007,13578]
55I/aggregation(13578): 1425801938048
56I/am_proc_died(22678): [0,17133,com.android.providers.partnerbookmarks]
57I/am_destroy_service(22678): [0,1120349904,13578]
58I/am_create_service(22678): [0,1120305256,.UploaderIntentService,10007,22885]
59I/google_http_request(22885): [1,-1,Android/com.google.android.gms/4323038,0]
60I/am_destroy_service(22678): [0,1120305256,22885]
logcat -B
二进制输出日志。
说明:这条命令,会以二进制格式显示日志,这条命令不会自己退出。

the-usage-of-android-adb-command-logcat-B

未完待续!
说明:logcat [:priority] 这个用法在实际测试过程中与描述的可能有些不符,待确认!
打赏
本博客所有文章如无特别注明均为原创。复制或转载请以超链接形式注明转自ifeegoo,原文地址《Android ADB 命令之 Logcat
上一篇: « 下一篇: »
暂无相关文章
Copyright © ifeegoo Time is limited, less is more! / 粤ICP备15109713号 / Theme by Hang & Ben / WordPress / 知识共享许可协议