SHARE
TWEET

Untitled

a guest Apr 23rd, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class LoggingExceptionHandler implements Thread.UncaughtExceptionHandler {
  2.     private final static String TAG = LoggingExceptionHandler.class.getSimpleName();
  3.     private final static String ERROR_FILE = MyAuthException.class.getSimpleName() + ".error";
  4.     private final Context context;
  5.     private final Thread.UncaughtExceptionHandler rootHandler;
  6.     public LoggingExceptionHandler(Context context) {
  7.         this.context = context;
  8.         // we should store the current exception handler -- to invoke it for all not handled
  9.     exceptions ...
  10.         rootHandler = Thread.getDefaultUncaughtExceptionHandler();
  11.         // we replace the exception handler now with us -- we will properly dispatch the
  12.     exceptions ...
  13.         Thread.setDefaultUncaughtExceptionHandler(this);
  14.      }
  15.     @Override
  16.     public void uncaughtException(final Thread thread, final Throwable ex) {
  17.          try {
  18.              Log.d(TAG, "called for " + ex.getClass());
  19.              // assume we would write each error in one file ...
  20.              File f = new File(context.getFilesDir(), ERROR_FILE);
  21.              // log this exception ...
  22.              FileUtils.writeStringToFile(f, ex.getClass().getSimpleName() + " " + System.currentTimeMillis() + "\n", true);
  23.             } catch (Exception e) {
  24.                  Log.e(TAG, "Exception Logger failed!", e);
  25.             }
  26.     }
  27.     public static final List<String> readExceptions(Context context) {
  28.         List<String> exceptions = new ArrayList<>();
  29.         File f = new File(context.getFilesDir(), ERROR_FILE);
  30.         if (f.exists()) {
  31.             try {
  32.                 exceptions = FileUtils.readLines(f);
  33.                 } catch (IOException e) {
  34.                     Log.e(TAG, "readExceptions failed!", e);
  35.                 }
  36.         }
  37.         return exceptions;
  38.     }
  39. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top