Browse Source

Print crashes to external storage on debug builds for in the field testing

master
Anthony Restaino 8 years ago
parent
commit
719b281bac
  1. 19
      app/src/main/java/acr/browser/lightning/app/BrowserApp.java
  2. 25
      app/src/main/java/acr/browser/lightning/utils/FileUtils.java

19
app/src/main/java/acr/browser/lightning/app/BrowserApp.java

@ -19,6 +19,7 @@ import javax.inject.Inject; @@ -19,6 +19,7 @@ import javax.inject.Inject;
import acr.browser.lightning.BuildConfig;
import acr.browser.lightning.preference.PreferenceManager;
import acr.browser.lightning.utils.FileUtils;
import acr.browser.lightning.utils.MemoryLeakUtils;
public class BrowserApp extends Application {
@ -46,6 +47,24 @@ public class BrowserApp extends Application { @@ -46,6 +47,24 @@ public class BrowserApp extends Application {
.build());
}
final Thread.UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
if (BuildConfig.DEBUG) {
FileUtils.writeCrashToStorage(ex);
}
if (defaultHandler != null) {
defaultHandler.uncaughtException(thread, ex);
} else {
System.exit(2);
}
}
});
mAppComponent = DaggerAppComponent.builder().appModule(new AppModule(this)).build();
mAppComponent.inject(this);

25
app/src/main/java/acr/browser/lightning/utils/FileUtils.java

@ -2,6 +2,7 @@ package acr.browser.lightning.utils; @@ -2,6 +2,7 @@ package acr.browser.lightning.utils;
import android.app.Application;
import android.os.Bundle;
import android.os.Environment;
import android.os.Parcel;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
@ -12,6 +13,7 @@ import java.io.FileInputStream; @@ -12,6 +13,7 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.constant.Constants;
@ -109,4 +111,27 @@ public class FileUtils { @@ -109,4 +111,27 @@ public class FileUtils {
return null;
}
/**
* Writes a stacktrace to the downloads folder with
* the following filename: [EXCEPTION]_[TIME OF CRASH IN MILLIS].txt
*
* @param throwable the Throwable to log to external storage
*/
public static void writeCrashToStorage(@NonNull Throwable throwable) {
String fileName = throwable.getClass().getSimpleName() + '_' + System.currentTimeMillis() + ".txt";
File outputFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fileName);
FileOutputStream outputStream = null;
try {
//noinspection IOResourceOpenedButNotSafelyClosed
outputStream = new FileOutputStream(outputFile);
throwable.printStackTrace(new PrintStream(outputStream));
outputStream.flush();
} catch (IOException e) {
Log.e(Constants.TAG, "Unable to write bundle to storage");
} finally {
Utils.close(outputStream);
}
}
}

Loading…
Cancel
Save