SoFunction
Updated on 2025-03-02

How to save application debug log information on SD card

Write the debugging information you applied to the SD card.

package ;
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ;
/**
 * Log log statistics saving
 * Can print i, e, w, don't print d
 * Every time the application is opened, the last log information will be overwritten
 * @author way
 *
 */ 
public class LogcatHelper { 
private static LogcatHelper INSTANCE = null; 
private static String PATH_LOGCAT; 
private LogDumper mLogDumper = null; 
private int mPId; 
/**
 *
 * Initialize directory
 *
 * */ 
public void init(Context context) { 
if (().equals( 
Environment.MEDIA_MOUNTED)) {// Priority save to SD cardPATH_LOGCAT = () 
.getAbsolutePath() +  + "miniGPS"; 
} else {// If the SD card does not exist, save it to the directory of this applicationPATH_LOGCAT = ().getAbsolutePath() 
+  + "miniGPS"; 
} 
File file = new File(PATH_LOGCAT); 
if (!()) { 
(); 
} 
} 
public static LogcatHelper getInstance(Context context) { 
if (INSTANCE == null) { 
INSTANCE = new LogcatHelper(context); 
} 
return INSTANCE; 
} 
private LogcatHelper(Context context) { 
init(context); 
mPId = (); 
} 
public void start() { 
if (mLogDumper == null){ 
mLogDumper = new LogDumper((mPId), PATH_LOGCAT); 
}
("path", PATH_LOGCAT);///storage/sdcard0/miniGPS
(); 
} 
public void stop() { 
if (mLogDumper != null) { 
(); 
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-" 
+ () + ".log")); 
} catch (FileNotFoundException e) { 
// TODO Auto-generated catch block 
(); 
} 
/**
 *
 * Log level: *:v , *:d , *:w , *:e , *:f , *:s
 *
 * Displays the logs of the E and W levels of the current mPID program.
 *
 * */ 
// cmds = "logcat *:e *:w | grep \"(" + mPID + ")\""; 
// cmds = "logcat | grep \"(" + mPID + ")\"";//Print all log information// cmds = "logcat -s way";//Print label filtering informationcmds = "logcat *:e *:i | grep \"(" + mPID + ")\"";//Can print i, e, w, and will not print d} 
public void stopLogs() { 
mRunning = false; 
} 
@Override 
public void run() { 
try { 
logcatProc = ().exec(cmds); 
mReader = new BufferedReader(new InputStreamReader( 
()), 1024); 
String line = null; 
while (mRunning && (line = ()) != null) { 
if (!mRunning) { 
break; 
} 
if (() == 0) { 
continue; 
} 
if (out != null && (mPID)) { 
((() + " " + line + "\n") 
.getBytes()); 
} 
} 
} catch (IOException e) { 
(); 
} finally { 
if (logcatProc != null) { 
(); 
logcatProc = null; 
} 
if (mReader != null) { 
try { 
(); 
mReader = null; 
} catch (IOException e) { 
(); 
} 
} 
if (out != null) { 
try { 
(); 
} catch (IOException e) { 
(); 
} 
out = null; 
} 
} 
} 
} 
} 

System permissions

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

Time tool class:

package ;
import ; 
import ; 
public class MyDate { 
public static String getFileName() { 
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
String date = (new Date(())); 
return date;// October 03, 2012 23:41:31} 
public static String getDateEN() { 
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
String date1 = (new Date(())); 
return date1;// 2012-10-03 23:41:31 
} 
}

Method calls:

public class MyApplication extends Application { 
@Override 
public void onCreate() { 
(this).start(); 
} 
}