Android中log及logcat命令的使用

本文介绍了Android中log的使用方法,包括在程序文件中包含log头文件,以及logcat命令的详细使用,如不同优先级的标识。logcat可以通过过滤器进行信息筛选,例如使用adb logcat命令指定特定标签和优先级。示例过滤表达式展示了如何仅显示'ActivityManager'和'MyApp'的相关日志。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.log使用

对于Android HAL/Framework可在Android系统源代码下通过以下方式使用log

1.在程序文件中包含头文件

#include <cutils/log.h> 
2.定义log的标签宏

#define LOG_TAG "YOUR LOG TAG"
3.修改对应.mk文件链接log库

LOCAL_SHARED_LIBRARIES := liblog libcutils
通过以上设置后即可使用LOGD/LOGE等等打印log信息了

2.logcat命令使用

Android的log都有一个标签和优先级信息
标志就是上面的宏:#define LOG_TAG "YOUR LOG TAG"确定的,表示这组log信息是属于哪个文件的。
优先级就类似于Linux kernel里面的信息优先级,Android log信息优先级有如下:

o    V — Verbose (最低优先级)

o    D — Debug

o    I — Info

o    W — Warning

o    E — Error

o    F — Fatal

o    S — Silent (最高优先级,没有任何输出)

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

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

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

为了让LOG输出达到可管理的级别,还可以用过滤器来限制LOG输出,过滤器可以帮助只是显示感兴趣的tag-priority组合的信息,而隐藏其他信息。过滤器语句按tag:priority …格式描述。这里,tag 表示是感兴趣的标签,priority 是表示报告指定标签的最低等级。 包含上面tag的信息或者指定优先级之上的信息被写入LOG。你可以在一过滤表达式中提供多个tag:priority 声明,这些声明之间用空白符间隔。

下面有一个过滤表达式的例子,例子表示不显示其他所有的LOG信息,除了那些标签为“ActivityManager”且优先级为“Info”以上的和标签为“ MyApp”且优先级为“ Debug”以上的信息。

adb logcat ActivityManager:I MyApp:D *:S

上面表达式的最后的元素 *:S,,是设置所有标签的优先级为“silent”,这样保证只有“View”和“MyApp”的LOG才会显示。用 *:S 的是确保日志输出被限制在你明确指定的过滤表达式的极好方法——它使的你的过滤器在LOG输出时就像一个“白名单”。


关于logcat命令的更改使用,可以通过logcat -v 查看其useage:
logcat -v
option requires an argument -- vUnrecognized Option
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"






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值