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命令使用
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输出时就像一个“白名单”。
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"