Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read the last N lines from logcat?

Tags:

android

I'd like to read the last n lines from logcat. Should this be working?:

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

-t number

Makes logcat print only entries from the position indicated by number and onward. A positive number indicates that the position is relative to the beginning of the log file, and a negative number indicates that the position is relative to the end of the log file.

Thanks

like image 711
user291701 Avatar asked May 07 '12 15:05

user291701


1 Answers

Try something like this:

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

For a full example check Log Collector, it definitely works:

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

To explain why this works from 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'
like image 134
Nikolay Elenkov Avatar answered Oct 03 '22 03:10

Nikolay Elenkov