如何在Android中读取系统日志文件?文件、如何在、系统、日志

2023-09-05 04:17:14 作者:薄情痞子 @

我想读系统日志在我的code,产生类似的错误报告。 类似的亚行logcat,但在节目的方式。

I am trying to read system logs in my code to generate something like an error report. Similar to adb logcat, but in a programming way.

我怎样才能做到这一点?

How can I achieve this?

推荐答案

日志收集有其源$ C ​​$ C,适用于谷歌code。他们实际上只需要调用logcat的。在这里看到: Android的日志收集 - SendLogActivity.java

Log Collector has their source code available on Google Code. They actually just call logcat. See here: android-log-collector - SendLogActivity.java

下面是关键的部分:

ArrayList<String> commandLine = new ArrayList<String>();
commandLine.add("logcat");//$NON-NLS-1$
commandLine.add("-d");//$NON-NLS-1$
ArrayList<String> arguments = ((params != null) && (params.length > 0)) ? params[0] : null;

if (null != arguments){
    commandLine.addAll(arguments);
}

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

String line;
while ((line = bufferedReader.readLine()) != null){ 
     log.append(line);
     log.append(App.LINE_SEPARATOR); 
}