Read logcat from app not working correctly

自作多情 提交于 2019-12-13 04:06:25

问题


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

回答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.




回答2:


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



来源:https://stackoverflow.com/questions/7473469/read-logcat-from-app-not-working-correctly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!