中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

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

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

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

青阳县| 辽阳市| 内丘县| 河津市| 田林县| 济源市| 南昌县| 襄汾县| 宜君县| 娄底市| 朝阳县| 阳东县| 海城市| 贵州省| 平南县| 阜新| 青岛市| 吉林市| 登封市| 弥勒县| 元氏县| 眉山市| 简阳市| 额敏县| 和静县| 莎车县| 苗栗县| 布尔津县| 星座| 陇南市| 娄烦县| 安阳县| 仙游县| 双江| 太白县| 贵定县| 含山县| 宁晋县| 张家界市| 吕梁市| 广宁县|