0

It seems straightforward enough.

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

I've tried

String commandString = "logcat -v raw --pid=" + PID);
Runtime.getRuntime().exec(commandString);

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++){
                pids.append(Integer.toString(PIDs.get(i)));
                if (i < PIDs.size() - 1){
                    pids.append("|");
                }
            }
            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() {
                p.destroy();
            };
        });

        mThreadAlive = true;
        mLoggingThread.start();
Burnrate
  • 13
  • 5

1 Answers1

0

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)
            .inheritIO()
            .redirectOutput(ProcessBuilder.Redirect.PIPE)
            .start();

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

            int status = process.waitFor();

            if (status != 0) {
                throw new IOException(argv + " returned exit status " + status);
            }
        }
Allen D. Ball
  • 1,916
  • 1
  • 9
  • 16
  • Getting the logcat this way seems to be no longer possible due to the restrictions put in place in J. I can also filter on PID and logcat directly to a file but not get the info in the program. Maybe this is how it is supposed to work and the restrictions are in place on purpose. The annoying part is that when I do it the way I have above and I do not filter on PID I can get all the messages. – Burnrate Aug 20 '20 at 15:24
  • I think I was running into some weird problems. We are currently using custom Android builds with 4 different apps working together on different devices. What I'm doing is going to require a different approach but your comment would work if we made the app a system app. – Burnrate Aug 25 '20 at 16:20