如何正确使用进程并筛选PID来捕获logcat日志?

huangapple go评论87阅读模式
英文:

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

问题

好的,以下是翻译好的部分:

似乎足够简单。

我正在使用 Runtime.getRuntime().exec(commandString);

我尝试过

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

我尝试过删除 -v raw,并且还尝试过(也需要)使用多个 PID,使用 |
没有任何结果。我有一个 new BufferedReader(new InputStreamReader(p.getInputStream()));,但什么都没有得到。如果我不过滤 PID,它可以工作,但会打印出命中 logcat 的所有内容,这并不是很有用。

我错过了什么吗?

下面是关键部分的完整代码块。

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

请注意,由于代码部分包含特殊字符,如 &quot;&lt;,我已经将它们还原为正常的代码语法。如果您需要将其粘贴回代码中,请将其替换为适当的字符。

英文:

It seems straightforward enough.

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

I've tried

String commandString = &quot;logcat -v raw --pid=&quot; + 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(&quot;logcat -c&quot;).waitFor();

            StringBuilder pids = new StringBuilder(&quot;&quot;);
            for (int i = 0; i &lt; PIDs.size(); i++){
                pids.append(Integer.toString(PIDs.get(i)));
                if (i &lt; PIDs.size() - 1){
                    pids.append(&quot;|&quot;);
                }
            }
            String commmandString = &quot;logcat -v raw --pid=&quot; + 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();

答案1

得分: 0

这将帮助您将输出重定向到一个InputStream(您可以在其中包装一个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()) {
            // 消耗InputStream流

            int status = process.waitFor();

            if (status != 0) {
                throw new IOException(argv + " 返回退出状态 " + status);
            }
        }
英文:

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

        String[] argv =
            new String[] { &quot;logcat&quot;, &quot;-v&quot;, &quot;raw&quot;, &quot;--pid=&quot; + 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 + &quot; returned exit status &quot; + status);
            }
        }

huangapple
  • 本文由 发表于 2020年8月20日 05:59:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63495514.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定