如何持续监控LogCat文件?
我需要以某种方式监视 LogCat 日志,这意味着当我的服务运行时,我需要读取 LogCat 中的新条目。 目前我只知道如何检索一次日志:
Process mLogcatProc = null;
BufferedReader reader = null;
try
{
mLogcatProc = Runtime.getRuntime().exec(new String[]
{"logcat", "-d", "ActivityManager:I *:S" });
reader = new BufferedReader(new InputStreamReader
(mLogcatProc.getInputStream()));
String line;
final StringBuilder log = new StringBuilder();
String separator = System.getProperty("line.separator");
while ((line = reader.readLine()) != null)
{
log.append(line);
log.append(separator);
}
如果我删除 -d 选项,它将不会退出,但也不会工作。 那么如何修改下面的代码以便不断从 LogCat 读取新条目呢?
I need to somehow monitor the LogCat log, meaning that while my service is running I need to read the LogCat for new entries.
At this moment I know only how to retrieve once the Log:
Process mLogcatProc = null;
BufferedReader reader = null;
try
{
mLogcatProc = Runtime.getRuntime().exec(new String[]
{"logcat", "-d", "ActivityManager:I *:S" });
reader = new BufferedReader(new InputStreamReader
(mLogcatProc.getInputStream()));
String line;
final StringBuilder log = new StringBuilder();
String separator = System.getProperty("line.separator");
while ((line = reader.readLine()) != null)
{
log.append(line);
log.append(separator);
}
If I remove the -d option it will not exit but also it will not either work.
So how can I modify the bellow code in order to continuously read new entries from LogCat?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我是按照刘嘉豪的建议这样做的:
This is how I did it, using Jiahao Liu's suggestion:
您可以创建一个新线程来在后台运行 logcat(不带选项 -d)。
注意:使用缓冲事件而不是流程ActivityManager,更准确。
“logcat -b 事件”
You can create a new thread to running the logcat(without the option -d) in the background.
Note: Use the buffer events instead of the process ActivityManager, it is more accurate.
"logcat -b events"