真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Android之LogDog

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

創(chuàng)新互聯(lián)建站專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站建設(shè)、成都網(wǎng)站制作、柳江網(wǎng)絡(luò)推廣、小程序設(shè)計、柳江網(wǎng)絡(luò)營銷、柳江企業(yè)策劃、柳江品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供柳江建站搭建服務(wù),24小時服務(wù)熱線:18980820575,官方網(wǎng)址:www.cdcxhl.com


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); } }

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


當(dāng)前題目:Android之LogDog
轉(zhuǎn)載注明:http://weahome.cn/article/gspdpg.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部