Search code examples
androidlogcatandroid-logcat

applying logcat filter programmatically


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

Solution

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