开发者

How to read system log file in Android?

I am trying to read system logs in my code to generate something like an e开发者_JAVA百科rror report. Similar to adb logcat, but in a programming way.

How can I achieve this?


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

Here is the key part:

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


You could use the built-in bug report system. More information on my blog here: http://blog.tomtasche.at/2012/10/use-built-in-feedback-mechanism-on.html

ApplicationErrorReport report = new ApplicationErrorReport();
report.packageName = report.processName = getApplication()
    .getPackageName();
report.time = System.currentTimeMillis();
report.type = ApplicationErrorReport.TYPE_CRASH;
report.systemApp = false;

ApplicationErrorReport.CrashInfo crash = new ApplicationErrorReport.CrashInfo();
crash.exceptionClassName = e.getClass().getSimpleName();
crash.exceptionMessage = e.getMessage();

StringWriter writer = new StringWriter();
PrintWriter printer = new PrintWriter(writer);
e.printStackTrace(printer);

crash.stackTrace = writer.toString();

StackTraceElement stack = e.getStackTrace()[0];
crash.throwClassName = stack.getClassName();
crash.throwFileName = stack.getFileName();
crash.throwLineNumber = stack.getLineNumber();
crash.throwMethodName = stack.getMethodName();

report.crashInfo = crash;

Intent intent = new Intent(Intent.ACTION_APP_ERROR);
intent.putExtra(Intent.EXTRA_BUG_REPORT, report);
startActivity(intent);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜