您的位置:首页 > 移动开发 > Android开发

Android中log及logcat命令的使用

2013-06-21 14:25 267 查看

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"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: