Read the last N lines from the logarithm?

I would like to read the last n lines from logcat. Should this work ?:

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

-t number

Makes a log cut only for entries from the position indicated by the number and beyond. A positive number indicates that the position refers to the beginning of the log file, and a negative number indicates that the position is relative to the end of the log file.

thank

+3
source share
2 answers

Try something like this:

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

For a complete Log Collector validation example, 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'
+3
source

, getStackTraceString(Throwable tr) catch.

try {

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

logcat, .

0

All Articles