Read logcat from app not working correctly
Asked Answered
D

2

0

I am trying to read from the logcat output in my app. I am able to read in correctly, but it goes on reading it in endless loop. Somehow there seems no way to detect the end of stream.

Not sure what I am doing wrong.

Here is my code:

    String baseCommand = "logcat -v time MyTag:D *:S";

    Process process = null;
    try {
        process = Runtime.getRuntime().exec(baseCommand);
        InputStreamReader reader = new InputStreamReader(process.getInputStream());
        BufferedReader bufferedReader = new BufferedReader(reader);
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            Log.d("SomeOtherTag", line); //This line executes endlessly
        }
    } catch (IOException e) {
        Log.e(DEBUG_TAG, "error in logging");
        e.printStackTrace();
    }
Dalenedalenna answered 19/9, 2011 at 15:37 Comment(0)
I
1

Not positive but I believe you need to pass the logcat call if it has args in a String[] so it would be something like

String[] baseCommand = {"logcat", "-v", "time", "MyTag:D", "*:S"};

then the rest of your code.

The single string call is just the program name, not the args.

Idealism answered 19/9, 2011 at 15:50 Comment(3)
Thanks for letting me know about the overloaded method. However, even with this, the program just seems to hang after the lines are written out.Dalenedalenna
The only other thing I can think of is maybe store the logs in a list until you reach the end, close the stream and print out the list? I have pretty similar code and it works for me. Could you somehow be adding more logs to print while printing them out to Log.d?Idealism
I don't think so. The tag is different. It seems to me , after printing what it's got, the readline() just sits there listening to logcat...Dalenedalenna
B
3

Logcat doesn't exit so the buffer is blocked.

Use 'logcat -d' in order to dump the log and then exit.

Hope this still helps, Yaron

Bushcraft answered 4/5, 2013 at 17:42 Comment(0)
I
1

Not positive but I believe you need to pass the logcat call if it has args in a String[] so it would be something like

String[] baseCommand = {"logcat", "-v", "time", "MyTag:D", "*:S"};

then the rest of your code.

The single string call is just the program name, not the args.

Idealism answered 19/9, 2011 at 15:50 Comment(3)
Thanks for letting me know about the overloaded method. However, even with this, the program just seems to hang after the lines are written out.Dalenedalenna
The only other thing I can think of is maybe store the logs in a list until you reach the end, close the stream and print out the list? I have pretty similar code and it works for me. Could you somehow be adding more logs to print while printing them out to Log.d?Idealism
I don't think so. The tag is different. It seems to me , after printing what it's got, the readline() just sits there listening to logcat...Dalenedalenna

© 2022 - 2024 — McMap. All rights reserved.