Android adb logcat使用

「这是我参与11月更文挑战的第18天,活动详情查看:2021最后一次更文挑战

解析adb logcat帮助信息

octopus@octopus:~$ adb 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"
复制代码

adb logcat 命令格式: adb logcat [选项] [过滤项], 其中 选项 和 过滤项 在 中括号 [] 中, 说明这是可选的;

选项解析:

-- "-s"选项 : 设置输出日志的标签, 只显示该标签的日志;

-- "-f"选项 : 将日志输出到文件, 默认输出到标准输出流中, -f 参数执行不成功;

-- "-r"选项 : 按照每千字节输出日志, 需要 -f 参数, 不过这个命令没有执行成功;

-- "-n"选项 : 设置日志输出的最大数目, 需要 -r 参数, 这个执行 感觉 跟 adb logcat 效果一样;

-- "-v"选项 : 设置日志的输出格式, 注意只能设置一项;

-- "-c"选项 : 清空所有的日志缓存信息;

-- "-d"选项 : 将缓存的日志输出到屏幕上, 并且不会阻塞;

-- "-t"选项 : 输出最近的几行日志, 输出完退出, 不阻塞;

-- "-g"选项 : 查看日志缓冲区信息;

-- "-b"选项 : 加载一个日志缓冲区, 默认是 main, 下面详解;

-- "-B"选项 : 以二进制形式输出日志;

输出指定标签内容:

-- "-s"选项 : 设置默认的过滤器, 如 我们想要输出 "System.out" 标签的信息, 就可以使用 adb logcat -s System.out 命令;

octopus@octopus:~$ adb logcat -s System.out
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main
I/System.out(22930): GSM -91
I/System.out(22930): SignalStrength issssssssss : -91
I/System.out(22930): GSM -91
I/System.out(22930): SignalStrength issssssssss : -91
I/System.out(22930): Supervisor Thread
I/System.out(22930): Got run mode
复制代码

输出日志信息到文件:

-- ">"输出 : ">" 后面跟着要输出的日志文件, 可以将 logcat 日志输出到文件中, 使用 adb logcat > log 命令, 使用 more log 命令查看日志信息;

octopus@octopus:~$ adb logcat > log
^C
octopus@octopus:~$ more log
--------- beginning of /dev/log/system
V/ActivityManager(  500): We have pending thumbnails: null
V/ActivityManager(  500): getTasks: max=1, flags=0, receiver=null
V/ActivityManager(  500): com.android.settings/.Settings: task=TaskRecord{42392278 #448 A com.android.settings U 0}
V/ActivityManager(  500): We have pending thumbnails: null
复制代码

指定logcat的日志输出格式:

-- "-v"选项 : 使用 adb logcat -v time 命令, 可以啥看日志的输出时间;

-- "brief"格式 : 这是默认的日志格式 " 优先级 / 标签 (进程ID) : 日志信息 " , 使用 adb logcat -v prief 命令;

octopus@octopus:~$ adb logcat -v brief
--------- beginning of /dev/log/system
D/PowerManagerService(  500): handleSandman: canDream=true, mWakefulness=Awake
D/PowerManagerService(  500): releaseWakeLockInternal: lock=1101267696, flags=0x0
复制代码

清空日志缓存信息:

使用 adb logcat -c 命令, 可以将之前的日志信息清空, 重新开始输出日志信息;

输出缓存日志:

使用 adb logcat -d 命令, 输出命令, 之后推出命令, 不会进行阻塞;

过滤项解析

[:priority] , 标签:日志等级, 默认的日志过滤项是 " *:I " ;

-- V : Verbose (明细);

-- D : Debug (调试);

-- I : Info (信息);

-- W : Warn (警告);

-- E : Error (错误);

-- F : Fatal (严重错误);

-- S : Silent(Super all output) (最高的优先级, 可能不会记载东西);

使用管道过滤日志

过滤固定字符串

只要命令行出现的日志都可以过滤, 不管是不是标签;

-- 命令 : adb logcat | grep Wifi ;

octopus@octopus:~$ adb logcat | grep Wifi
E/WifiHW  (  441): wifi_send_command : AP_SCAN 1 ; interface index=0;
E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;
E/WifiHW  (  441): wifi_send_command : SCAN ; interface index=0;
E/WifiHW  (  441): wifi_send_command : AP_SCAN 1 ; interface index=0;
E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;
E/WifiHW  (  441): wifi_send_command : AP_SCAN 1 ; interface index=0;
E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;
复制代码

过滤字符串忽略大小写 : adb logcat | grep -i wifi ;

使用正则表达式匹配

分析日志 : 该日志开头两个字符是 "V/", 后面开始就是标签, 写一个正则表达式 "^..ActivityManager", 就可以匹配日志中的 "V/ActivityManager" 字符串;

V/ActivityManager(  574): getTasks: max=1, flags=0, receiver=null
复制代码

正则表达式过滤日志 : 使用上面的正则表达式组成命令 adb logcat | grep "^..Activity" ;

猜你喜欢

转载自juejin.im/post/7032632637034496037