Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LoggingExceptionHandler implements Thread.UncaughtExceptionHandler {
- private final static String TAG = LoggingExceptionHandler.class.getSimpleName();
- private final static String ERROR_FILE = MyAuthException.class.getSimpleName() + ".error";
- private final Context context;
- private final Thread.UncaughtExceptionHandler rootHandler;
- public LoggingExceptionHandler(Context context) {
- this.context = context;
- // we should store the current exception handler -- to invoke it for all not handled
- exceptions ...
- rootHandler = Thread.getDefaultUncaughtExceptionHandler();
- // we replace the exception handler now with us -- we will properly dispatch the
- exceptions ...
- Thread.setDefaultUncaughtExceptionHandler(this);
- }
- @Override
- public void uncaughtException(final Thread thread, final Throwable ex) {
- try {
- Log.d(TAG, "called for " + ex.getClass());
- // assume we would write each error in one file ...
- File f = new File(context.getFilesDir(), ERROR_FILE);
- // log this exception ...
- FileUtils.writeStringToFile(f, ex.getClass().getSimpleName() + " " + System.currentTimeMillis() + "\n", true);
- } catch (Exception e) {
- Log.e(TAG, "Exception Logger failed!", e);
- }
- }
- public static final List<String> readExceptions(Context context) {
- List<String> exceptions = new ArrayList<>();
- File f = new File(context.getFilesDir(), ERROR_FILE);
- if (f.exists()) {
- try {
- exceptions = FileUtils.readLines(f);
- } catch (IOException e) {
- Log.e(TAG, "readExceptions failed!", e);
- }
- }
- return exceptions;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement