Last Updated: February 25, 2016
·
12.72K
· danielerigo

Android: read the logcat programmatically

This is how you can read the logcat:

Process logcat;
final StringBuilder log = new StringBuilder();
try {
logcat = Runtime.getRuntime().exec(new String[]{"logcat", "-d"});
BufferedReader br = new BufferedReader(new InputStreamReader(logcat.getInputStream()),4*1024);
String line;
String separator = System.getProperty("line.separator"); 
    while ((line = br.readLine()) != null) {
        log.append(line);
        log.append(separator);
    }
} catch (Exception e) {
    e.printStackTrace();
}

And this is how you can clear it:

try {
    Runtime.getRuntime().exec(new String[]{"logcat", "-c"});
} catch (Exception e1) {
    e1.printStackTrace();
}