Skip to content

How do I properly capture the logcat using a process and filtering on PID?

It seems straightforward enough.

I’m using Runtime.getRuntime().exec(commandString);

I’ve tried

String commandString = "logcat -v raw --pid=" + PID);

I’ve tried it without the -v raw and have also tried (and need to) use multiple PIDs using |. Nothing seems to work. I have a new BufferedReader(new InputStreamReader(p.getInputStream())); that gets nothing. If I don’t filter on PID it works but prints out everything that hits the logcat which is not very useful.

What am I missing?

Here is the whole block of the important bit.

        try {
            Runtime.getRuntime().exec("logcat -c").waitFor();

            StringBuilder pids = new StringBuilder("");
            for (int i = 0; i < PIDs.size(); i++){
                if (i < PIDs.size() - 1){
            String commmandString = "logcat -v raw --pid=" + pids);
            p = Runtime.getRuntime().exec(commmandString);
            mBufferedReader = new BufferedReader(new InputStreamReader(p.getInputStream()));

        } catch (IOException e) {} catch (InterruptedException e) {}
        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {

        mThreadAlive = true;



This should help you redirect the output to an InputStream (which you could wrap in a BufferedReader ):

        String[] argv =
            new String[] { "logcat", "-v", "raw", "--pid=" + pid };
        Process process =
            new ProcessBuilder(argv)

        try (InputStream in = process.getInputStream()) {
            // Consume InputStream

            int status = process.waitFor();

            if (status != 0) {
                throw new IOException(argv + " returned exit status " + status);
5 People found this is helpful