Limit crash logs, refactoring

This commit is contained in:
M66B 2018-09-14 08:31:49 +00:00
parent c9be3a3d6b
commit 63b4910f17
1 changed files with 46 additions and 22 deletions

View File

@ -42,34 +42,23 @@ public class ApplicationEx extends Application {
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
if (!(ex instanceof RemoteException) /* DeadSystemException */) {
if (ownFault(ex)) {
Log.e(Helper.TAG, ex + "\r\n" + Log.getStackTraceString(ex));
writeCrashLog(ex);
File file = new File(getCacheDir(), "crash.log");
Log.w(Helper.TAG, "Writing exception to " + file);
FileWriter out = null;
try {
out = new FileWriter(file);
out.write(ex.toString() + "\n" + Log.getStackTraceString(ex));
} catch (IOException e) {
Log.e(Helper.TAG, e + "\n" + Log.getStackTraceString(e));
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
Log.e(Helper.TAG, e + "\n" + Log.getStackTraceString(e));
}
}
}
if (prev != null)
prev.uncaughtException(thread, ex);
} else {
Log.w(Helper.TAG, ex + "\r\n" + Log.getStackTraceString(ex));
System.exit(1);
}
if (prev != null)
prev.uncaughtException(thread, ex);
}
});
createNotificationChannels();
}
private void createNotificationChannels() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationManager nm = getSystemService(NotificationManager.class);
@ -93,4 +82,39 @@ public class ApplicationEx extends Application {
nm.createNotificationChannel(error);
}
}
public boolean ownFault(Throwable ex) {
if (ex instanceof OutOfMemoryError)
return false;
if (ex instanceof RemoteException)
return false;
while (ex != null) {
for (StackTraceElement ste : ex.getStackTrace())
if (ste.getClassName().startsWith(getPackageName()))
return true;
ex = ex.getCause();
}
return false;
}
private void writeCrashLog(Throwable ex) {
File file = new File(getCacheDir(), "crash.log");
Log.w(Helper.TAG, "Writing exception to " + file);
FileWriter out = null;
try {
out = new FileWriter(file);
out.write(ex.toString() + "\n" + Log.getStackTraceString(ex));
} catch (IOException e) {
Log.e(Helper.TAG, e + "\n" + Log.getStackTraceString(e));
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
Log.e(Helper.TAG, e + "\n" + Log.getStackTraceString(e));
}
}
}
}
}