Tôi đang cố gắng đọc nhật ký hệ thống trong mã của mình để tạo một cái gì đó giống như báo cáo lỗi. Tương tự như adb logcat, nhưng theo cách lập trình.Cách đọc tệp nhật ký hệ thống trong Android?
Tôi làm cách nào để đạt được điều này?
Tôi đang cố gắng đọc nhật ký hệ thống trong mã của mình để tạo một cái gì đó giống như báo cáo lỗi. Tương tự như adb logcat, nhưng theo cách lập trình.Cách đọc tệp nhật ký hệ thống trong Android?
Tôi làm cách nào để đạt được điều này?
Nhật ký thu thập có sẵn mã nguồn của họ trên Google Code. Họ thực sự chỉ cần gọi logcat. Xem ở đây: android-log-collector - SendLogActivity.java
Đây là phần quan trọng:
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);
}
Bạn có thể sử dụng được xây dựng trong hệ thống báo cáo lỗi. Thông tin thêm về blog của tôi ở đây: link 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);
[Đây là cuộc thảo luận về các bài đăng này trên Meta] (http : //meta.stackexchange.com/q/153352/152134) –
EboMike bây giờ dẫn đến 404, ở đây, nơi nó di chuyển: http://code.google.com/p/android-log-collector/source/browse/trunk/ src/com/xtralogic/android/logcollector/SendLogActivity.java? r = 2 – Inoy
(Liên kết trong câu trả lời được cập nhật, tôi cũng đã sao chép phần chính vào câu trả lời) – EboMike