gpt4 book ai didi

android - 清除 logcat 时出现问题

转载 作者:行者123 更新时间:2023-11-30 03:20:58 26 4
gpt4 key购买 nike

我正在使用 Android 的日志来监督应用程序的使用,所以我在手机中而不是在 Eclipse 中使用 logcat。

好吧,如果我只是在日志中写入并向我发送所有信息,那没问题,但我会收到以前执行的信息。我决定在我的应用程序每次启动时清除日志,但现在我通常会丢失第一条日志消息。也许 logcat 需要一些时间才能清除?因为当我尝试调试一切时,一切正常。

例子:

clear log, message 1, message 2, message 3, ...

有时候收不到消息1,有时候收不到1和2...

我已经检查了我所有的代码是否有可能被意外清除,但我没有发现任何东西......

我一开始就调用了这个函数(在onCreate()中)

public static void clearLog(){
ArrayList<String> commandLine = new ArrayList<String>();
commandLine.add("logcat");
commandLine.add("-c");//CLEAR

Runtime process = Runtime.getRuntime();
process.exec(commandLine.toArray(new String[0]));

TAG = "";
}

然后我添加日志

log.i(TAG, "message1");
..
log.i(TAG, "messageN");

这就是我收集日志的方式:

ArrayList<String> commandLine = new ArrayList<String>();
commandLine.add("logcat");
commandLine.add("-d");//dump and exit
commandLine.add("-v");//especify verbose mode
commandLine.add("raw");//raw show only the message, brief for show all
commandLine.add(TAG + ":V");//show TAG's log
commandLine.add("*:S");//hide others

Process process = Runtime.getRuntime().exec(
commandLine.toArray(new String[0]));
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));

String line, log = "";
while ((line = bufferedReader.readLine()) != null) {
log += line + LINE_SEPARATOR;
}

最佳答案

来自 the docs对于 exec():

Executes the specified command and its arguments in a separate native process.

所以它在一个单独的进程中运行。在开始记录之前,并没有真正的好方法来判断它是否已经完成。

您可以在每次运行时更改日志标签,而不是清除日志。只需使用常规标签并附加一些标识运行的数字,甚至只是一个随机的数字。然后当你收集你的日志时,你可以过滤它,只收集你想要的。

关于android - 清除 logcat 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19136382/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com