Próbuję odczytać logi systemowe w moim kodzie, aby wygenerować coś takiego jak raport o błędzie. Podobny do adb logcat, ale w sposób programistyczny.Jak odczytać plik dziennika systemu w systemie Android?
Jak mogę to osiągnąć?
Próbuję odczytać logi systemowe w moim kodzie, aby wygenerować coś takiego jak raport o błędzie. Podobny do adb logcat, ale w sposób programistyczny.Jak odczytać plik dziennika systemu w systemie Android?
Jak mogę to osiągnąć?
Log Collector ma swój kod źródłowy dostępny w Google Code. Właściwie po prostu wywołują logcat. Zobacz tutaj: android-log-collector - SendLogActivity.java
Oto klucz część:
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);
}
Można użyć wbudowanego systemu raportów błędów. Więcej informacji na moim blogu tutaj: 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);
[Oto dyskusja na temat tych postów na Meta] (http : //meta.stackexchange.com/q/153352/152134) –
EboMike za odsyłacz prowadzi teraz do 404, gdzie przeniósł się tutaj: http://code.google.com/p/android-log-collector/source/browse/trunk/ src/com/xtralogic/android/logcollector/SendLogActivity.java? r = 2 – Inoy
(Link w odpowiedzi jest zaktualizowany, również skopiowałem kluczową część do odpowiedzi) – EboMike