Android will use debug log information to save in SD card method _android

Source: Internet
Author: User
Tags date1

The

Writes the debug information that you applied to the SD card.

Package com.sdmc.hotel.util; 
Import Java.io.BufferedReader; 
Import Java.io.File; 
Import java.io.FileNotFoundException; 
Import Java.io.FileOutputStream; 
Import java.io.IOException; 
Import Java.io.InputStreamReader; 
Import Android.content.Context; 
Import android.os.Environment;
Import Android.util.Log; /** * Log Statistics Save * will print i,e,w, will not print d * each time the application, will be the last log information cover * @author WAY * * * * * * public class Logcathelper {private stat 
IC Logcathelper INSTANCE = null; 
private static String Path_logcat; 
Private Logdumper mlogdumper = null; 
private int mPId; /** * * * Initialization directory * */public void init (context context) {if (Environment.getexternalstoragestate (). Equals (environm Ent. media_mounted)) {///priority saved to SD card Path_logcat = Environment.getexternalstoragedirectory (). GetAbsolutePath () + 
File.separator + "Minigps"; 
else {//If the SD card does not exist, save it to the directory in this application Path_logcat = Context.getfilesdir (). GetAbsolutePath () + File.separator + "Minigps"; 
File File = new file (PATH_LOGCAT); if (!file.exists ()) {file.mkdirs (); }} public static Logcathelper getinstance {if (INSTANCE = = null) {INSTANCE = new Logcathelper (cont 
EXT); 
return INSTANCE; 
Private Logcathelper {init (context); 
MPId = Android.os.Process.myPid (); 
public void Start () {if (Mlogdumper = null) {Mlogdumper = new Logdumper (string.valueof (mPId), path_logcat); 
} log.i ("path", Path_logcat);///storage/sdcard0/minigps Mlogdumper.start (); 
public void Stop () {if (mlogdumper!= null) {mlogdumper.stoplogs (); 
Mlogdumper = null; 
} private class Logdumper extends Thread {private Process logcatproc; 
Private BufferedReader mreader = null; 
Private Boolean mrunning = true; 
String cmds = null; 
Private String MPID; 
Private FileOutputStream out = null; 
Public Logdumper (String pid, string dir) {MPID = pid; 
try {out = new FileOutputStream (New File (dir, "gps-" + mydate.getfilename () + ". Log")); catch (FileNotFoundException e) {//TODO auto-generated CatCH Block e.printstacktrace (); 
/** * * Log level: *:V, *:d, *:w, *:e, *:f, *:s * * Displays the log of E and W levels for the current MPID program. * * *//Cmds = "Logcat *:e *:w | 
grep \ "(" + MPID + ") \" "; Cmds = "Logcat | grep \ "(" + MPID + ") \" ";//Print all log information//Cmds =" logcat-s way ";//Print label filter information Cmds =" Logcat *:e *:i | 
grep \ "(" + MPID + ") \" ";//will print i,e,w and will not print D} public void Stoplogs () {mrunning = false; 
@Override public void Run () {try {Logcatproc = Runtime.getruntime (). exec (CMDS); 
Mreader = new BufferedReader (New InputStreamReader (Logcatproc.getinputstream ()), 1024); 
String line = null; 
while (mrunning && [line = Mreader.readline ())!= null] {if (!mrunning) {break; 
} if (line.length () = = 0) {continue; 
} if (out!= null && line.contains (MPID)) {Out.write (Mydate.getdateen () + "" + line + "\ n"). GetBytes ()); 
A catch (IOException e) {e.printstacktrace ()); 
finally {if (Logcatproc!= null) {Logcatproc.destroy (); 
Logcatproc = null; } if (Mreader!=NULL) {try {mreader.close (); 
Mreader = null; 
catch (IOException e) {e.printstacktrace (); 
} if (out!= null) {try {out.close (); 
catch (IOException e) {e.printstacktrace (); 
out = null;  } 
} 
} 
} 
}

System permissions

<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> 

Time Tool Class:

Package com.sdmc.hotel.util;
Import Java.text.SimpleDateFormat; 
Import java.util.Date; 
public class MyDate {public 
static String GetFileName () { 
SimpleDateFormat format = new SimpleDateFormat ("Yyyy-m M-dd "); 
String date = Format.format (new date (System.currenttimemillis ()); 
return date;//October 03, 2012 23:41:31 
} public 
static String Getdateen () { 
SimpleDateFormat format1 = new Si Mpledateformat ("Yyyy-mm-dd HH:mm:ss"); 
String date1 = Format1.format (New Date (System.currenttimemillis ())); 
return date1;//2012-10-03 23:41:31 
} 
}

Call to the method:

public class MyApplication extends application { 
@Override public 
void OnCreate () { 
Logcathelper.getinstance (This). Start (); 
 

Related Article

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.