从logcat读取最后N行?

时间:2012-05-07 15:24:38

标签: android

我想从logcat读取最后n行。这应该有效吗?:

Process process = Runtime.getRuntime().exec("logcat -t -500");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
...
  

-t number

使logcat仅从数字和向前指示的位置打印条目。正数表示该位置相对于日志文件的开头,负数表示该位置相对于日志文件的结尾。

由于

2 个答案:

答案 0 :(得分:5)

尝试这样的事情:

String[] command = { "logcat", "-t", "500" };
Process p = Runtime.getRuntime().exec(command);

有关完整示例,请检查Log Collector,它肯定有效:

http://code.google.com/p/android-log-collector/source/browse/trunk/android-log-collector/src/com/xtralogic/android/logcollector/SendLogActivity.java

解释为什么这适用于logcat man:

  -t <count>      print only the most recent <count> lines (implies -d)
  -t '<time>'     print most recent lines since specified time (implies -d)
  -T <count>      print only the most recent <count> lines (does not imply -d)
  -T '<time>'     print most recent lines since specified time (not imply -d)
                  count is pure numerical, time is 'MM-DD hh:mm:ss.mmm'

答案 1 :(得分:0)

如果您只想在发生异常后进行堆栈跟踪,那么可以在catch子句中使用getStackTraceString(Throwable tr)

try {

} catch(Exception e ) {
     String crashLog = Log.getStackTraceString(e); 
     // Save crashLog to file to do something...
} 

您可以在logcat命令中指定过滤器,以便至少获得完整日志的精简版本。

相关问题