So, I am attempting to use a bufferReader to read logcat from an android device. As "logcat -c" does not always work to clear the log, I want to start the reader and then drop the current output and start reading only new lines. How can I do this?
Each time I start the process the entire logcat output is captured, I only want the current lines being produced at the time of me starting the reader.
So when I do this:
ProcessBuilder pb = new ProcessBuilder("adb", "logcat", "-v", "threadtime", "-b", "all");
pb.redirectErrorStream(true);
Process p = pb.start();
BufferedReader reader =
new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ( (line = reader.readLine()) != null) {
result += line + "\n";
}
The entire log is captured, as adb logcat will give you the entire log, all previous lines are in the buffer and there are no options to clear the queue. I attempted to use mark and reset, but that does not seem to work. The result string always has the previous lines from logcat that occurred before I started the reader.
Anyone know how I can write this so only new lines are read, and previous are dropped?
EDIT: When I try this, with mark and reset, the output string still contains logcat lines from an hour ago, it needs to start from now. My understanding is that mark and set are for going back in the buffer, which I do not want to do. I want to read only new lines from logcat, over adb, not the previous lines.
ProcessBuilder pb = new ProcessBuilder("adb", "logcat", "-v", "threadtime", "-b", "all");
pb.redirectErrorStream(true);
Process p = pb.start();
BufferedReader reader =
new BufferedReader(new InputStreamReader(p.getInputStream()));
reader.mark(0);
reader.reset();
String line = null;
while ( (line = reader.readLine()) != null) {
result += line + "\n";
}
EDIT #2: I did come up with a "temp" solution, by using logcats -T option to grab only logs from a certain timestamp and on. However this solution requires that my PC and my android device times are in sync. It would be nice to have a better solution that did not have this time sync requirement.
So if anyone has one, that would be great. Here is my temp solution:
DateFormat dateFormat = new SimpleDateFormat("MM-dd HH:mm:ss.mmm");
Date date = new Date();
String dateString = dateFormat.format(date);
ProcessBuilder pb = new ProcessBuilder(
"adb", "-s", deviceID, "logcat", "-v", "threadtime", "-b", "all", "-T", dateString);
pb.redirectErrorStream(true);
p = pb.start();
BufferedReader reader =
new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ( (line = reader.readLine()) != null) {
result += line + "\n";
}