91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android 之 LogDog

發布時間:2020-08-08 23:33:58 來源:網絡 閱讀:371 作者:panaimin 欄目:移動開發

在寫Android程序時,經常碰到在模擬器和調試器中無法捕捉的exception。有時自己運行好好的程序到了其他機器上就出現了問題。雖然Google Play有錯誤堆棧上傳功能,但是沒有辦法把整個運行的過程記錄下來。為此我寫了一個LogDog類來試圖解決這個問題。



import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.lang.Thread.UncaughtExceptionHandler; import java.sql.Date; import java.text.SimpleDateFormat; import java.util.HashSet; import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Build; import android.os.Environment; import android.os.Looper; //import android.util.Log; import android.util.Log; public class LogDog implements UncaughtExceptionHandler { public static String TAG = "LogDog"; private static HashSet _tags = new HashSet(); private static boolean _debug = false; static { // add tags to be captured _tags.add(PDFMaster.TAG); _tags.add(RecentlyUsedActivity.TAG); _tags.add(DB.TAG); } public static LogDog instance() { return _instance; } public static void i(String tag, String message) { if(_debug) { Log.i(tag, message); return; } PrintWriter writer = _instance.getWriter(); if(writer != null && _tags.contains(tag)) { _instance._writer.println("i:" + tag + ":" + message); } } public static void e(String tag, String message) { if(_debug) { Log.i(tag, message); return; } PrintWriter writer = _instance.getWriter(); if(writer != null && _tags.contains(tag)) { _instance._writer.println("e" + tag + ":" + message); } } public static void w(String tag, String message) { if(_debug) { Log.i(tag, message); return; } PrintWriter writer = _instance.getWriter(); if(writer != null && _tags.contains(tag)) { _instance._writer.println("w" + tag + ":" + message); } } public void init(Context context) { _context = context; _fileName = _context.getString(R.string.app_name) + ".log"; _defaultHandler = Thread.getDefaultUncaughtExceptionHandler(); Thread.setDefaultUncaughtExceptionHandler(this); } public void close() { if(_writer != null) { _writer.close(); _writer = null; } } // private private static LogDog _instance = new LogDog(); private static final String PATH = Environment.getExternalStorageDirectory().getPath() + "/log"; private Context _context; private String _fileName; private Thread.UncaughtExceptionHandler _defaultHandler; private PrintWriter _writer = null; private LogDog() { } private PrintWriter getWriter() { if(_writer != null) return _writer; // check if we have SD card if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { //Log.w(TAG, "sdcard unmounted,skip dump exception"); return null; } File dir = new File(PATH); if (!dir.exists()) { dir.mkdirs(); } File file = new File(PATH + "/" + _fileName); try { _writer = new PrintWriter(new BufferedWriter(new FileWriter(file))); } catch (IOException e) { //Log.e(TAG, "Failed to open " + PATH + "/" + FILE_NAME); _writer = null; } return _writer; } @Override public void uncaughtException(Thread thread, Throwable ex) { // first display a toast message new Thread() { @Override public void run() { Looper.prepare(); PDFMarkerApp.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; long current = System.currentTimeMillis(); String time = new SimpleDateFormat("yyyyMMdd.HHmmss").format(new Date(current)); _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.print("CPU ABI: "); _writer.println(Build.CPU_ABI); _writer.println(); ex.printStackTrace(_writer); close(); File file = new File(PATH + "/" + _fileName); File file2 = new File(PATH + "/" + _fileName + "." + time + ".txt"); file.renameTo(file2); } }

在使用時所有的日志就全記錄在一個文件中,直到最后的異常。每次異常都會單獨被記錄在一個文件中,連帶所有需要知道的信息。要是正常運行,這個文件下次就被覆蓋,不會造成空間問題。要是調試,只要把_debug改一下,所有日志就在LogCat中顯示出來,非常方便。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

新和县| 漳浦县| 登封市| 苗栗市| 绥芬河市| 嘉定区| 卢龙县| 宜都市| 临潭县| 丘北县| 白银市| 理塘县| 平阴县| 格尔木市| 义乌市| 新化县| 台南市| 甘泉县| 荥经县| 古蔺县| 贵定县| 溆浦县| 永登县| 巴林左旗| 博白县| 达孜县| 孟连| 邵武市| 淮安市| 青冈县| 临安市| 辉南县| 右玉县| 宣威市| 巴东县| 和林格尔县| 民丰县| 鲁山县| 页游| 河源市| 梨树县|