logcat 命令小结

文章出处:https://blog.youkuaiyun.com/shift_wwx/article/details/11133787


logcat 是android系统中的一个命令,是一个可执行的bin文件,放置在/system/bin目录下。

root@android:/system/bin # ls -l logcat                                        
-rwxr-xr-x root     shell       13680 2008-08-01 12:00 logcat


logcat的源代码在/system/core下面,根据Android.mk:

# Copyright 2006 The Android Open Source Project

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_SRC_FILES:= logcat.cpp event.logtags

LOCAL_SHARED_LIBRARIES := liblog

LOCAL_MODULE:= logcat

include $(BUILD_EXECUTABLE 

可以看出编译出来是可执行文件。

要是感兴趣可以研究一下。

 

看一下logcat的注释:

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"


解释一下options:

-s 设置默认的过滤级别为silent

-f <filename> 指定输出日志信息的<filename> ,默认是stdout

-r [<kbytes>] 每<kbytes> 时输出日志,默认值为16,需要和-f 选项一起使用.

-n <count> 设置日志的最大数目<count> .,默认值是4,需要和 -r 选项一起使用。

-v <format> 设置日志输入格式,默认的是brief 格式, <format> 是下面的一种: brief process tag thread raw time threadtime long.

-c 清楚屏幕上的日志.

-d 得到所有log并退出 (不阻塞).

-g 得到环形缓冲区的大小并退出.

-b <buffer> 加载一个可使用的日志缓冲区供查看,比如eventradio . ('main' (默认), 'radio', 'events').

-B 输出log到二进制中.

 

过滤日志输出

每一个输出的Android日志信息都有一个标签和它的优先级.

  • 日志的标签是系统部件原始信息的一个简要的标志。(比如:“View”就是查看系统的标签).
  • 优先级有下列集中,是按照从低到高顺利排列的:
    • V — Verbose (lowest priority)
    • D — Debug
    • I — Info
    • W — Warning
    • E — Error
    • F — Fatal
    • S — Silent (highest priority, on which nothing is ever printed)

在运行logcat的时候在前两列的信息中你就可以看到 logcat 的标签列表和优先级别,它是这样标出的:<priority>/<tag> .

下面是一个logcat输出的例子,它的优先级就似乎I,标签就是ActivityManage:

I/ActivityManager(  585): Starting activity: Intent { action=android.intent.action...}

为了让日志输出能体现管理的级别,你还可以用过滤器来控制日志输出,过滤器可以帮助你描述系统的标签等级.

过滤器语句按照下面的格式描tag:priority ... , tag 表示是标签, priority 是表示标签的报告的最低等级. 从上面的tag的中可以得到日志的优先级. 你可以在过滤器中多次写tag:priority .

这些说明都只到空白结束。下面有一个列子,例子表示支持所有的日志信息,除了那些标签为”ActivityManager”和优先级为”Info”以上的和标签为” MyApp”和优先级为” Debug”以上的。小等级,优先权报告为tag.

logcat ActivityManager:I MyApp:D *:S

上面表达式的最后的元素 *:S ,,是设置所有的标签为"silent",所有日志只显示有"ActivityManager" and "MyApp"的,用*:S 的另一个用处是能够确保日志输出的时候是按照过滤器的说明限制的,也让过滤器也作为一项输出到日志中.

下面的过滤语句指显示优先级为warning或更高的日志信息:

adb logcat *:W

如果你电脑上运行logcat ,相比在远程adb shell端,你还可以为环境变量ANDROID_LOG_TAGS :输入一个参数来设置默认的过滤

export ANDROID_LOG_TAGS="ActivityManager:I MyApp:D *:S"

需要注意的是ANDROID_LOG_TAGS 过滤器如果通过远程shell运行logcat 或用adb shell logcat 来运行模拟器/设备不能输出日志.

 

控制日志输出格式

日志信息包括了许多元数据域包括标签和优先级。可以修改日志的输出格式,所以可以显示出特定的元数据域。可以通过 -v 选项得到格式化输出日志的相关信息.

  • brief — Display priority/tag and PID of originating process (the default format).
  • process — Display PID only.
  • tag — Display the priority/tag only.
  • thread — Display process:thread and priority/tag only.
  • raw — Display the raw log message, with no other metadata fields.
  • time — Display the date, invocation time, priority/tag, and PID of the originating process.
  • long — Display all metadata fields and separate messages with a blank lines.

当启动了logcat ,你可以通过-v 选项来指定输出格式:

logcat [-v <format>]

下面是用 thread 来产生的日志格式:

logcat -v thread

需要注意的是你只能-v 选项来规定输出格式 option.

 logcat -v time

显示时间

查看可用日志缓冲区

Android日志系统有循环缓冲区,并不是所有的日志系统都有默认循环缓冲区。为了得到日志信息,你需要通过-b 选项来启动logcat 。如果要使用循环缓冲区,你需要查看剩余的循环缓冲期:

  • radio — 查看缓冲区的相关的信息.
  • events — 查看和事件相关的的缓冲区.
  • main — 查看主要的日志缓冲区

-b 选项使用方法:

logcat [-b <buffer>]

下面的例子表示怎么查看日志缓冲区包含radio 和 telephony信息:

logcat -b radio

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

私房菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值