I want to save all the contents of log cat into specific file in Android. I used Eclipse IDE to develop the android application.
How i can achieve this ?
Thanks.
My method allows to get log in testing situation when the device is not connected to ADB
Just create the clone of LogWrapper and in constructor create the file in external memory. I selected DIRECTORY_PICTURES because it is present on all devices. Constructor public LogWrapperExt(){ sdf = new SimpleDateFormat("\n hh:mm:ss.SSS"); String path; isValid = true; boolean isPresent; try { topDirPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); if(topDirPath != null ) path = topDirPath.getCanonicalPath();
} catch (java.io.IOException e) {
isValid = false;
} catch (java.lang.NoSuchFieldError e) {
isValid = false;
}
if(isValid){
logsFolder = new File(topDirPath + File.separator + "MyLogs");// put your name
if (!logsFolder.exists()) {
isPresent = logsFolder.mkdir();
}
try {
String formattedDate = new Date().toString() ;
formattedDate = formattedDate.replaceAll(" ", "_");
String fileName = "test_"+ formattedDate + ".txt";
logFile = new File(logsFolder.getAbsolutePath(),fileName);
outputStreamLogFile = new FileOutputStream(logFile);
} catch (java.io.FileNotFoundException e) {
}
}
}
then in public void println make your print also.
Log.println(priority, tag, useMsg);
String myp = dateStr + " " + level_name[priority] + " " + tag + " " + useMsg;
try{
outputStreamLogFile.write(myp.getBytes());
} catch (java.io.IOException e) {
}
Some overhead - true, but data yours in all situations
Dont forget to replace LogWrapper name to yours