文档库 最新最全的文档下载
当前位置:文档库 › logcat用法、实例

logcat用法、实例

Android日志系统提供了记录和查看系统调试信息的功能。日志都是从各种软件和一些系统的缓冲区中记录下来的,缓冲区可以通过 logcat 命令来查看和使用.

一、使用logcat命令的目的:
1、你可以用 logcat 命令来查看系统日志缓冲区的内容:
[adb] logcat [


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 选 项来指定输出格式:
[adb] logcat [-v ]
下面是用 thread 来产生的日志格式:
adb logcat -v thread
需要注意的是你只能-v 选项来规定输出格式 option.

三、“查看”可用日志缓冲区:

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

-b 选项使用方法:
[adb] logcat [-b ]
下面的例子表示怎么查看日志缓冲区包含radio 和 telephony信息:
adb logcat -b radio

四、查看 stdout 和stderr
在默认状态下,Android系统有stdout 和 stderr (System.out和System.err ) 输出到/dev/null , 在运行Dalvik VM的进程中,有一个系统可以备份日志文件。在这种情况下,系统会 用stdout 和stderr 和 优先级 I.来记录日志信息
通过这种方法指定输出的路径,停止运行的模拟器/设备,然后通过用setprop 命 令远程输入日志
$ adb shell stop
$ adb shell setprop log.redirect-stdio true
$ adb shell start
系统直到你关闭模拟器/设备前设置会一直保留,可以通过添加/data/local.prop 可 以使用模拟器/设备上的默认设置

五、Logcat命令列表:

Option Description
-b 加载一个可使用的日志缓冲区供查看,比如event 和radio . 默认值是main 。具体查看Viewing Alternative Log Buffers.
-c 清楚屏幕上的日志.
-d 输出日志到屏幕上.
-f 指定输出日志信息的 , 默认是stdout .
-g 输出指定的日志缓冲区,输出后退出.
-n 设置日志的最大数目 .,

默认值是4,需要和 -r 选 项一起使用。
-r 时 输出日志,默认值为16,需要和-f 选 项一起使用.
-s 设置默认的过滤级别为silent.
-v 设置日志输入格式,默认的是brief 格 式,
要知道更多的支持的格式,参看Controlling Log Output Format.

相关文档