Android Development Debug Log Tool class [support save to SD card]

Source: Internet
Author: User
Tags try catch

Directly on the code:


Package Com.example.callstatus;import Java.io.file;import Java.io.filewriter;import java.io.ioexception;import Java.io.printwriter;import Java.io.stringwriter;import Java.net.unknownhostexception;import Java.text.simpledateformat;import Java.util.date;import Android.annotation.suppresslint;import Android.os.environment;import android.util.log;/** * Android Development Debug Log Tool class [support save to SD card]<br> * <br> * * requires some permissions: < br> * <uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> <br> * < Uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/><br> * * @author Pmtoam * */@ Suppresslint ("SimpleDateFormat") public class Mylog{public static final String cache_dir_name = "Dphonelog";p ublic static Boolean Isdebugmodel = true;//whether to output log public static Boolean issavedebuginfo = true;//Save debug Log public static Boolean Issavecrashinfo = true;//Whether to save the error log public static void V (final string tag, final string msg) {if (Isdebugmodel) {LOG.V (tag, "--" + msg);}} public static void D (final string tag, final string msg) {if (Isdebugmodel) {LOG.D (tag, "--" + msg)}} public static void I (final string tag, final string msg) {if (Isdebugmodel) {LOG.I (tag, "--" + msg)}} public static void W (final string tag, final string msg) {if (Isdebugmodel) {LOG.W (tag, "--" + msg)}} /** * Debug Log for easy development tracking.

* @param tag * @param msg */public static void E (final string tag, final string msg) {if (Isdebugmodel) {LOG.E (tag, "- "+ msg);} if (issavedebuginfo) {new Thread () {public void run () {Write (time () + tag + "+" + msg + "\ n");};}. Start ();}} /** * Try catch is used. On-line products can upload feedback.

* @param tag * @param tr */public static void E (Final String tag, final throwable tr) {if (issavecrashinfo) {new Thread () {p ublic void Run () {Write (time () + tag + "[CRASH]-" + getstacktracestring (TR) + "\ n");}. Start ();}} /** * Gets the string that catches the exception * @param tr * @return */public static string getstacktracestring (Throwable tr) {if (tr = = null) {return ' ";} Throwable t = tr;while (t! = null) {if (T instanceof unknownhostexception) {return "";} t = T.getcause ();} StringWriter SW = new StringWriter (); PrintWriter pw = new PrintWriter (SW); Tr.printstacktrace (PW); return sw.tostring ();} /** * Identifies when each log was generated * @return */private static String time () {return "[" + New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format ( New Date (System.currenttimemillis ())) + "]";} /** * Date as log file name * @return */private static String date () {return new SimpleDateFormat ("Yyyy-mm-dd"). Format (new date (Syst Em.currenttimemillis ()));} /** * Save to log file * @param content */public static synchronized void write (String content) {Try{filewriter writer = nEW FileWriter (GetFile (), true); Writer.write (content); Writer.close ();} catch (IOException e) {e.printstacktrace ();}} /** * Get log file path * @return */public static String getFile () {File Sddir = null;if (Environment.getexternalstoragestate (). Equa LS (android.os.Environment.MEDIA_MOUNTED)) Sddir = Environment.getexternalstoragedirectory (); File Cachedir = new file (Sddir + file.separator + cache_dir_name), if (!cachedir.exists ()) Cachedir.mkdir (); File FilePath = new file (Cachedir + file.separator + date () + ". txt"); return filepath.tostring ();}}




Android Development Debug Log Tool class [support save to SD card]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.