5

我想从 logcat 中读取最后 n 行。这应该工作吗?:

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

-t 数字

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

谢谢

4

2 回答 2

6

尝试这样的事情:

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

对于完整的示例检查日志收集器,它肯定有效:

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'
于 2012-05-08T06:12:55.097 回答
0

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

try {

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

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

于 2012-05-07T15:38:58.863 回答