0

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);
        }
Community
  • 1
  • 1
ys126
  • 89
  • 1
  • 9

2 Answers2

3

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) {

    }
ys126
  • 89
  • 1
  • 9
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.

Bishnu
  • 853
  • 7
  • 11