Read logcat from app not working correctly

2019-08-20 08:44发布

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();
    }

2条回答
疯言疯语
2楼-- · 2019-08-20 09:11

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.

查看更多
在下西门庆
3楼-- · 2019-08-20 09:20

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

查看更多
登录 后发表回答