Skip to content

Instantly share code, notes, and snippets.

@Malinskiy
Created January 11, 2021 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Malinskiy/85c89097f13251bfc61527de181f1112 to your computer and use it in GitHub Desktop.
Save Malinskiy/85c89097f13251bfc61527de181f1112 to your computer and use it in GitHub Desktop.
private static String readLogcat(long startTimeMs) {
try {
...
// Start the process
final Process process = new ProcessBuilder()
.command("logcat", "-d", "-v threadtime,uid", "-T", timestamp)
.start();
// Nothing to write. Don't let the command accidentally block.
process.getOutputStream().close();
// Read the output
final StringBuilder str = new StringBuilder();
final InputStreamReader reader = new InputStreamReader(process.getInputStream());
char[] buffer = new char[4096];
int amt;
while ((amt = reader.read(buffer, 0, buffer.length)) >= 0) {
if (amt > 0) {
str.append(buffer, 0, amt);
}
}
try {
process.waitFor();
} catch (InterruptedException ex) {
// We already have the text, drop the exception.
}
return str.toString();
} catch (IOException ex) {
return "Error reading logcat command:\n" + ex.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment