applying logcat filter programmatically

依然范特西╮ 提交于 2019-12-08 14:46:00

问题


I need to dump logcat with filter programmatically. I applied adb logcat -s "TAGNAME" but the app just "hung". there is a similar thread but there is no solution: Read filtered log cat(Programmatically)?

        try {
            Process process = Runtime.getRuntime().exec("logcat -s XXX");
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));

            StringBuilder log = new StringBuilder();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                log.append(line);
                log.append(FragmentLog.LINE_SEPARATOR);
            }

            ((TextView) tv).setText(log.toString());

        } catch (IOException e) {
            ((TextView) tv).setText(R.string.default_blank);
        }

回答1:


I am using another way to get my app work. Here is the code, in case anyone wants to know,.

    try {               
        Process process = Runtime.getRuntime().exec("logcat -d");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

        String line;

        Pattern pattern = Pattern.compile("XXX", 0);

        while ((line = bufferedReader.readLine()) != null) {
            if (patternu != null
                    && !pattern.matcher(line).find()) {
                continue;
            }
            log.append(line);
            log.append('\n');
        }

    } catch (IOException e) {

    }



回答2:


Well first off this command will stream logcat and won't dump it. Hence it will never quit running. Also, if there is no content to report the BufferedReader is probably waiting for a full line to come in, so you are blocking.



来源:https://stackoverflow.com/questions/9294820/applying-logcat-filter-programmatically

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