logcat命令
logcat介绍命令选项。
选项 | 说明 |
-s | 默认设置过滤器 |
- f 文件 | 输出到日志文件 |
-c | 清除日志 |
-d | 获取日志 |
-g | 获取日志的大小 |
- v 格式 |
设置日志(见下面的格式打印格式)
|
- v 格式 | 例 |
brief | W/tag ( 876): message |
process | W( 876) message (tag) |
tag | W/tag : message |
thread | W( 876:0x37c) message |
raw | message |
time | 09-08 05:40:26.729 W/tag ( 876): message |
threadtime | 09-08 05:40:26.729 876 892 W tag : message |
long | [ 09-08 05:40:26.729 876:0x37c W/tag ] message |
代码例子:
AndroidManifest.xml添加读取权限
<uses-permission android:name="android.permission.READ_LOGS" />
清除日志
try {
Runtime.getRuntime().exec("logcat -c");
} catch(Exception e) {
}
获取日志
try {
ArrayList<String> commandLine = new ArrayList<String>();
commandLine.add( "logcat");
commandLine.add( "-d");
commandLine.add( "-v");
commandLine.add( "time");
commandLine.add( "-s");
commandLine.add( "tag:W");
Process process = Runtime.getRuntime().exec( commandLine.toArray( new String[commandLine.size()]));
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(process.getInputStream()), 1024);
String line = bufferedReader.readLine();
while ( line != null) {
log.append(line);
log.append("\n")
}
} catch ( IOException e) {
}
结果:
09-08 09:44:42.267 W/tag ( 754): message1
09-08 09:44:42.709 W/tag ( 754): message2
09-08 09:44:43.187 W/tag ( 754): message3
09-08 09:44:45.295 E/tag ( 754): message8
java多线程设计wait/notify机制 (synchronized与对象锁)
android view onKeyDown not call 的原因?