When writing Android programs, you often encounter exception that cannot be captured in the simulator or debugger. Sometimes it's a problem to run a good program on the other machine. While Google Play has the wrong stack upload feature, there's no way to keep track of the entire running process. For this I have written a Logdog class to try to solve this problem.
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";p rivate static hashset_tags = new HashSet ();p rivate static B Oolean_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;}} Privateprivate static logdog_instance = new Logdog ();p rivate static final String PATH =environment.getexternalstoragedirectory (). GetPath () + "/log";p rivate 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 cardif (! 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));} CA TCH (IOException e) {//log.e (TAG, "Failed to open" + PATH + "/" + file_name); _writer = null;} return _writer;} @Overridepublic void uncaughtexception (thread thread, Throwable ex) {//First display a toast messagenew Thread () {@Overri depublic 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 has time to displaytry {thread.sleep;} catch (Interruptedexception e) {}ANDROID.O S.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); _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);}}
When used, all logs are recorded in one file until the last exception. Each exception is recorded in a single file, with all the information that needs to be known. If it works, the file will be overwritten next time, without causing any space problems. If debugging, as long as the _DEBUG change, all the logs are displayed in the Logcat, very convenient.
Android's Logdog