努力攀登的程序猿

我是一直努力攀登的程序猿,这里是我攀登过的每一快岩石
正文

Google有只做日志的猫,我有一只做日志的狗

(2016-02-19 16:20:29) 下一个

安卓模拟器里做日志的是LogCat,我的是LogDog。

public class LogDog implements UncaughtExceptionHandler {

这只狗帮我做了好多事,最重要的是一旦遇到没有抓住的例外,它就帮我记录到文件里。

@Overridepublic void uncaughtException(Thread thread, Throwable ex) {   // first display a toast message   new Thread() {      @Override      public void run() {         Looper.prepare();         Utility.instance().showToast("Sorry for the trouble, dumping uncaught exeption to SD card");      }   }.start();   try {      dump(ex);   } catch (Exception e) {      Log.e(TAG, "Failed to dump because " + e.getMessage());      e.printStackTrace();   }   if (_defaultHandler != null) {      _defaultHandler.uncaughtException(thread, ex);   } else {      // sleep so the toast can have time to display      try {         Thread.sleep(3000);      } catch (InterruptedException e) {      }      android.os.Process.killProcess(android.os.Process.myPid());   }}

它给我记录这么多内容:

private void dump(Throwable ex) throws IOException, NameNotFoundException {   PrintWriter writer = _instance.getWriter();   if(writer == null)      return;   String time = Utility.instance().getTimeString();   _writer.println(time);   PackageManager pm = _context.getPackageManager();   PackageInfo pi = pm.getPackageInfo(_context.getPackageName(), PackageManager.GET_ACTIVITIES);   _writer.print("App Version: ");   _writer.print(pi.versionName);   _writer.print('_');   _writer.println(pi.versionCode);   _writer.print("OS Version: ");   _writer.print(Build.VERSION.RELEASE);   _writer.print("_");   _writer.println(Build.VERSION.SDK_INT);   _writer.print("Vendor: ");   _writer.println(Build.MANUFACTURER);   _writer.print("Model: ");   _writer.println(Build.MODEL);   _writer.println();   ex.printStackTrace(_writer);   _writer.flush();   close();   File file = new File(PATH + "/" + _fileName);   File file2 = new File(PATH + "/" + _fileName + "." + time + ".txt");   file.renameTo(file2);}

它替我抓住了很多我没有发现的臭虫,真的很管用!

 

 

[ 打印 ]
阅读 ()评论 (1)
评论
orikon 回复 悄悄话 能抓住运行时的exception.
登录后才可评论.