英文:
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();
请注意,由于代码部分包含特殊字符,如 "
和 <
,我已经将它们还原为正常的代码语法。如果您需要将其粘贴回代码中,请将其替换为适当的字符。
英文:
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();
答案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[] { "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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论