0

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";
        }
DAC
  • 231
  • 4
  • 12
  • Mark and reset don't work, I tired this and it still gets all the previous logcat lines. – DAC Nov 02 '15 at 19:57
  • Read the whole answer, not just the first line. – Daniel Zolnai Nov 02 '15 at 21:29
  • I did "read the whole answer". From my understanding that solution is for going back and starting at the beginning of the buffer again. This is not what I want, I don't want to go back in the buffer, as my question stated. – DAC Nov 02 '15 at 21:37
  • Is there a reason you can't just read everything and discard the parts you don't want? I don't really understand why `logcat -c` wouldn't work -- I've never seen it fail. – fadden Nov 02 '15 at 23:53
  • It fails consistently, as described here: https://code.google.com/p/android/issues/detail?id=78916 – DAC Nov 03 '15 at 15:03

0 Answers0