Android ADB 命令之 Logcat 2015-03-15 / 4,570 次 / 快抢沙发 /

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

Runtime Environment
OS: Windows 8.1
Tools: ADB
最近在封装 Log 日志库的时候,发现需要用到 Android ADB 命令中的 Logcat 命令,整理如下。
如果你对Android ADB 命令中的某一条命令的用法不熟悉的话,我们可以通过 help 命令来查看其用法:
C:\>adb shell
root@android:/ $ logcat --help
logcat --help
Usage: logcat [options] [filterspecs]
options include:
  -s              Set default filter to silent.
                  Like specifying filterspec '*:s'
  -f <filename>   Log to file. Default to stdout
  -r [<kbytes>]   Rotate log every kbytes. (16 if unspecified). Requires -f
  -n <count>      Sets max number of rotated logs to <count>, default 4
  -v <format>     Sets the log print format, where <format> is one of:

                  brief process tag thread raw time threadtime long

  -c              clear (flush) the entire log and exit
  -d              dump the log and then exit (don't block)
  -t <count>      print only the most recent <count> lines (implies -d)
  -g              get the size of the log's ring buffer and exit
  -b <buffer>     Request alternate ring buffer, 'main', 'system', 'radio'
                  or 'events'. Multiple -b parameters are allowed and the
                  results are interleaved. The default is -b main -b system.
  -B              output the log in binary
filterspecs are a series of
  <tag>[:priority]

where <tag> is a log component tag (or * for all) and priority is:
  V    Verbose
  D    Debug
  I    Info
  W    Warn
  E    Error
  F    Fatal
  S    Silent (supress all output)

'*' means '*:d' and <tag> by itself means <tag>:v

If not specified on the commandline, filterspec is set from ANDROID_LOG_TAGS.
If no filterspec is found, filter defaults to '*:I'

If not specified with -v, format is set from ANDROID_PRINTF_LOG
or defaults to "brief"
C:\>adb shell
root@android:/ $ logcat --help
logcat --help
用法: logcat [选项] [过滤参数]
选项包括:
  -s              设置默认的过滤器为:silent.
                  如同指定过滤参数 '*:s'
  -f <文件名>      将 Log 日志保存到文件,默认为不保存,标准输出。
  -r [<kbytes>]   按照 * KB 分割日志 (16KB 为默认值) 需要配合 -f 一起使用
  -n <count>      设置最大的分割日志数:count 默认为 4
  -v <format>     设置日志打印格式,format 为其中之一:
                  brief process tag thread raw time threadtime long

  -c              清除整个日志并退出
  -d              载入整个日志并退出 (非阻塞)
  -t <count>      仅打印最近的 count 行 (载入日志后退出) 
  -g              获取日志 ring buffer 大小并退出
  -b <buffer>     请求  alternate ring buffer(此处不能理解 alternate这个词),
                  'main', 'system', 'radio' or 'events'. 允许多 -b 参数,
                  并且结果是交叉的。默认的是:-b main -b system.
  -B              二进制输出日志

过滤参数是一系列的:
<tag>[:priority]

<tag> 是日志中的 tag (* 标识所有 tag) 优先级如下:(备注:大小写不区分)
  V    Verbose
  D    Debug
  I    Info
  W    Warn
  E    Error
  F    Fatal
  S    Silent 禁止所有的输出 (说明:原资料中为 supress all output,
       其中 supress 这个单词感觉写错了,因为查了相关资料,没有发现
       有这个单词,应该是 suppress 这个单词)

'logcat *:*' 表示 'logcat *:d' 'logcat <tag>' 表示 'logcat <tag>:v'

如果未指定命令行,过滤参数由 ANDROID_LOG_TAGS 设置。
如果没有设置过滤参数,默认的过滤参数为 *:I
如果没有通过 -v 指定,那么格式由 ANDROID_PRINTF_LOG 设置,或者默认为 'brief'

逐条命令讲解:

logcat -s
和指定过滤参数 logcat *:s 或 logcat *:S 作用相同:禁止所有输出!
说明:这个命令不知道在实际的过程中有什么作用,后续再理解。
logcat -s 用法:

C:\>adb shell
root@android:/ $ logcat -s
logcat -s
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main

C:\>adb shell
root@android:/ $ logcat *:s
logcat *:s
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main

C:\>adb shell
root@android:/ $ logcat *:S
logcat *:S
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main
logcat -f filename
将日志保存到文件。
说明:filename 中的尖括号表示必须要有的参数,如果你使用 logcat -f 命令,那么后面必须带有参数。而且这个命令会将当前日志写入文件,后续产生的日志也会写入文件,这条命令不会自己退出。
logcat -f 用法:

C:\>adb shell
root@android:/ $ logcat -f /sdcard/logcat.txt
logcat -f /sdcard/logcat.txt
光标会停留在此处闪
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 一起使用。实际过程中,这个命令感觉没有什么太大的意义。这条命令不会自己退出。
logcat -r 用法:

C:\>adb shell
root@android:/ $ logcat -r -f /sdcard/logcat.txt
logcat -r -f /sdcard/logcat.txt
光标会停留在此处闪

C:\>adb shell
root@android:/ $ cd /sdcard
cd /sdcard
root@android:/sdcard $ ls -l
ls -l
drwxrwx--- root     sdcard_r          1970-07-18 10:54 Alarms
drwxrwx--x root     sdcard_r          1970-07-18 10:54 Android
drwxrwx--- root     sdcard_r          2015-03-04 15:53 DCIM
drwxrwx--- root     sdcard_r          2015-03-04 15:12 Download
drwxrwx--- root     sdcard_r          1970-07-18 10:54 Movies
drwxrwx--- root     sdcard_r          2015-03-03 15:06 Music
drwxrwx--- root     sdcard_r          1970-07-18 10:54 Notifications
drwxrwx--- root     sdcard_r          1970-07-18 10:54 Pictures
drwxrwx--- root     sdcard_r          1970-07-18 10:54 Podcasts
drwxrwx--- root     sdcard_r          1970-07-18 10:54 Ringtones
drwxrwx--- root     sdcard_r          2015-03-03 17:52 apt
-rw-rw---- root     sdcard_r    15409 2015-03-08 15:58 logcat.txt
-rw-rw---- root     sdcard_r    16457 2015-03-08 15:56 logcat.txt.1
-rw-rw---- root     sdcard_r    16423 2015-03-08 15:55 logcat.txt.2
-rw-rw---- root     sdcard_r    16404 2015-03-08 15:55 logcat.txt.3
-rw-rw---- root     sdcard_r    16534 2015-03-08 15:53 logcat.txt.4
logcat -n count
设置最大的分割日志数:count 默认为 4
说明:这个命令需要配合 -r -f 一起使用,不然没有效果。这个命令不会自己退出。
logcat -n 用法:

C:\>adb shell
root@android:/ $ logcat -n 7 -f /sdcard/logcat.txt
logcat -n 7 -f /sdcard/logcat.txt
光标会停留在此处闪
^C
C:\>adb shell
root@android:/ $ cd /sdcard
cd /sdcard
root@android:/ $ ls -l
ls -l
drwxrwx--- root     sdcard_r          1970-07-18 10:54 Alarms
drwxrwx--x root     sdcard_r          1970-07-18 10:54 Android
drwxrwx--- root     sdcard_r          2015-03-04 15:53 DCIM
drwxrwx--- root     sdcard_r          2015-03-04 15:12 Download
drwxrwx--- root     sdcard_r          1970-07-18 10:54 Movies
drwxrwx--- root     sdcard_r          2015-03-03 15:06 Music
drwxrwx--- root     sdcard_r          1970-07-18 10:54 Notifications
drwxrwx--- root     sdcard_r          1970-07-18 10:54 Pictures
drwxrwx--- root     sdcard_r          1970-07-18 10:54 Podcasts
-rw-rw---- root     sdcard_r   447787 2015-03-08 16:24 logcat.txt

root@android:/ $ logcat -r -n 7 -f /sdcard/logcat.txt
logcat -r -n 7 -f /sdcard/logcat.txt
光标会停留在此处闪
^C
C:\>adb shell
root@android:/ $ cd /sdcard
cd /sdcard
root@android:/sdcard $ ls -l
ls -l
drwxrwx--- root     sdcard_r          1970-07-18 10:54 Alarms
drwxrwx--x root     sdcard_r          1970-07-18 10:54 Android
drwxrwx--- root     sdcard_r          2015-03-04 15:53 DCIM
drwxrwx--- root     sdcard_r          2015-03-04 15:12 Download
drwxrwx--- root     sdcard_r          1970-07-18 10:54 Movies
drwxrwx--- root     sdcard_r          2015-03-03 15:06 Music
drwxrwx--- root     sdcard_r          1970-07-18 10:54 Notifications
drwxrwx--- root     sdcard_r          1970-07-18 10:54 Pictures
drwxrwx--- root     sdcard_r          1970-07-18 10:54 Podcasts
drwxrwx--- root     sdcard_r          1970-07-18 10:54 Ringtones
drwxrwx--- root     sdcard_r          2015-03-03 17:52 apt
-rw-rw---- root     sdcard_r    16319 2015-03-08 16:26 logcat.txt
-rw-rw---- root     sdcard_r    16458 2015-03-08 16:26 logcat.txt.1
-rw-rw---- root     sdcard_r    16468 2015-03-08 16:26 logcat.txt.2
-rw-rw---- root     sdcard_r    16414 2015-03-08 16:26 logcat.txt.3
-rw-rw---- root     sdcard_r    16454 2015-03-08 16:26 logcat.txt.4
-rw-rw---- root     sdcard_r    16633 2015-03-08 16:26 logcat.txt.5
-rw-rw---- root     sdcard_r    16412 2015-03-08 16:26 logcat.txt.6
-rw-rw---- root     sdcard_r    16429 2015-03-08 16:26 logcat.txt.7
logcat -v format
设置打印日志格式。
说明:一般采用logcat -v time 用的比较多。这个命令不会自己退出。
logcat -v 用法:

C:\>adb shell
root@android:/ $ logcat -v brief
W/EventLoggerService(10471): Unable to send logs Error code: 262146 | Unable to
resolve host "www.google.com": No address associated with hostname

root@android:/ $ logcat -v process
W(22678) Unable to start service Intent { cmp=com.thinksky.itools.markets/com.th
inksky.itools.download.DownloadService } U=0: not found  (ActivityManager)

root@android:/ $ logcat -v tag
W/ActivityManager: Unable to start service Intent { cmp=com.thinksky.itools.mark
ets/com.thinksky.itools.download.DownloadService } U=0: not found

root@android:/ $ logcat -v thread
I(22678:22689) START u0 {act=android.intent.action.MAIN cat=[android.intent.cate
gory.LAUNCHER] flg=0x10200000 cmp=***} from pid 22945

root@android:/ $ logcat -v raw
rmt_storage_connect_cb: clnt_h=0x48 conn_h=0xb82e4160

root@android:/ $ logcat -v time
03-08 16:45:57.837 I/rmt_storage(  173): rmt_storage_connect_cb: clnt_h=0x48 con
n_h=0xb82e4160

root@android:/ $ logcat -v threadtime
03-08 16:45:57.837   173   173 I rmt_storage: rmt_storage_connect_cb: clnt_h=0x4
8 conn_h=0xb82e4160

root@android:/ $ logcat -v long
[ 03-08 16:45:57.837   173:  173 I/rmt_storage ]
rmt_storage_connect_cb: clnt_h=0x48 conn_h=0xb82e4160
logcat -c
清除整个日志并退出。
说明:这条命令,适合我们准备要开始抓取日志之前,先前之前的日志清理掉。这条命令执行完成后,自己退出。
logcat -c 用法:

C:\>adb shell
shell@hammerhead:/ $ logcat -c
logcat -c
shell@hammerhead:/ $
logcat -d
载入整个日志并退出 (非阻塞)。
说明:这条命令,会载入当前所有日志,然后退出。
logcat -d 用法:

C:\>adb shell
root@android:/ $ logcat -d
logcat -d
--------- beginning of /dev/log/main
D/dalvikvm(22678): GC_CONCURRENT freed 11669K, 27% free 34609K/47380K, paused 3m
s+6ms, total 61ms
D/dalvikvm(22999): GC_CONCURRENT freed 365K, 4% free 18144K/18832K, paused 7ms+1
ms, total 31ms
--------- beginning of /dev/log/system
W/ProcessCpuTracker(22678): Skipping unknown process pid 26086
root@android:/ $
logcat -t count
仅打印最近的 count 行 (载入日志后退出) 。
说明:这条命令,会载入当前最近的 count 条命令,然后退出。
logcat -t 用法:

C:\>adb shell
root@android:/ $ logcat -t 3
logcat -t 3
--------- beginning of /dev/log/main
--------- beginning of /dev/log/system
D/com.qq.connect(31826): getLinkedFile:/storage/sdcard0-->/storage/emulated/legacy
D/com.qq.connect(31826): getLinkedFile:/storage/sdcard0-->/storage/emulated/legacy
D/dalvikvm(31864): GC_CONCURRENT freed 442K, 6% free 18393K/19388K, paused 9ms+1ms
root@android:/ $
logcat -g
获取日志 ring buffer 大小并退出。
说明:这条命令,会显示日志 ring buffer 大小,然后退出。
logcat -g 用法:

C:\>adb shell
root@android:/ $ logcat -g
logcat -g
/dev/log/main: ring buffer is 256Kb (21Kb consumed), max entry is 5120b, max payload is 4076b
/dev/log/system: ring buffer is 256Kb (4Kb consumed), max entry is 5120b, max payload is 4076b
root@android:/ $
logcat -b
说明:由于支持多 -b 命令,logcat -b main -b system -b radio -b events
这样的命令是可以被接受的。
logcat -b 用法:
说明:我在 logcat -b * 前面加上了 logcat -c 命令,是为了清除之前的日志,同时使用 &&
执行下一条命令。

C:\>adb shell
root@android:/ $ logcat -c && logcat -b main
logcat -c && logcat -b main
I/GEL     (22945): handleIntent(Intent { act=android.intent.action.MAIN cat=[and
roid.intent.category.HOME] flg=0x10200000 cmp=com.google.android.googlequicksear
chbox/com.google.android.launcher.GEL })
D/audio_hw_primary(22395): select_devices: out_snd_device(2: speaker) in_snd_dev
ice(0: )
D/dalvikvm(22746): GC_FOR_ALLOC freed 1925K, 50% free 23946K/47088K, paused 18ms
, total 18ms

C:\>adb shell
root@android:/ $ logcat -c && logcat -b system
logcat -c && logcat -b system
I/ActivityManager(22678): START u0 {act=android.intent.action.MAIN cat=[android.
intent.category.LAUNCHER] flg=0x10200000 cmp=*****/.activity.******} 
from pid 22945
I/ActivityManager(22678): START u0 {cmp=***********.MainActivity} from pid 21291
W/ActivityManager(22678): Unable to start service Intent { cmp=****Service } U=0: 
not found

C:\>adb shell
root@android:/ $ logcat -c && logcat -b radio
logcat -c && logcat -b radio
D/RILJ    (22903): [7355]> REQUEST_GET_NEIGHBORING_CELL_IDS
D/RILJ    (22903): [7355]< REQUEST_GET_NEIGHBORING_CELL_IDS
D/GsmSST  (22903): [GsmSST] SST.getAllCellInfo(): return last, back to back calls
D/GsmSST  (22903): [GsmSST] SST.getAllCellInfo(): X size=7 list=[CellInfoGsm:{mR
egistered=YES mTimeStampType=oem_ril mTimeStamp=384988951090840ns CellIdentityGs
m:{ mMcc=460 mMnc=0 mLac=9363 mCid=5092} CellSignalStrengthGsm: ss=18 ber=99}, C
ellInfoGsm:{mRegistered=NO mTimeStampType=oem_ril mTimeStamp=384988951090840ns C
ellIdentityGsm:{ mMcc=460 mMnc=0 mLac=9363 mCid=4262} CellSignalStrengthGsm: ss=
22 ber=99}, CellInfoGsm:{mRegistered=NO mTimeStampType=oem_ril mTimeStamp=384988
951090840ns CellIdentityGsm:{ mMcc=460 mMnc=0 mLac=9363 mCid=4261} CellSignalStr
engthGsm: ss=17 ber=99}, CellInfoGsm:{mRegistered=NO mTimeStampType=oem_ril mTim
eStamp=384988951090840ns CellIdentityGsm:{ mMcc=460 mMnc=0 mLac=9363 mCid=4263}
CellSignalStrengthGsm: ss=16 ber=99}, CellInfoGsm:{mRegistered=NO mTimeStampType
=oem_ril mTimeStamp=384988951090840ns CellIdentityGsm:{ mMcc=460 mMnc=0 mLac=936
3 mCid=4582} CellSignalStrengthGsm: ss=16 ber=99}, CellInfoGsm:{mRegistered=NO m
TimeStampType=oem_ril mTimeStamp=384988951090840ns CellIdentityGsm:{ mMcc=0 mMnc
=0 mLac=0 mCid=-1} CellSignalStrengthGsm: ss=16 ber=99}, CellInfoGsm:{mRegistere
d=NO mTimeStampType=oem_ril mTimeStamp=384988951090840ns CellIdentityGsm:{ mMcc=
0 mMnc=0 mLac=0 mCid=-1} CellSignalStrengthGsm: ss=15 ber=99}]

C:\>adb shell
root@android:/ $ logcat -c && logcat -b events
logcat -c && logcat -b events
I/am_kill (22678): [0,17133,com.android.providers.partnerbookmarks,15,empty for
1815s]
I/am_create_service(22678): [0,1120349904,.EventLogService,10007,13578]
I/aggregation(13578): 1425801938048
I/am_proc_died(22678): [0,17133,com.android.providers.partnerbookmarks]
I/am_destroy_service(22678): [0,1120349904,13578]
I/am_create_service(22678): [0,1120305256,.UploaderIntentService,10007,22885]
I/google_http_request(22885): [1,-1,Android/com.google.android.gms/4323038,0]
I/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 / 知识共享许可协议