0

I've added a method to my application that creates a Logcat file of that session.

public void LogcatToTextFile() {
    Date T = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    String szDateTime = sdf.format(T);
    SimpleDateFormat date = new SimpleDateFormat("yyyyMMdd");
    String szDate = date.format(T);

    //makes ApplicationLogFiles directory if there is none
    File logFileDirectory = new File(this.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "ApplicationLogFiles");
    logFileDirectory.mkdir();
    //makes a directory that is titled with today's date if there is none
    File logFileDateDirectory = new File(this.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) + "/ApplicationLogFiles/" + szDate);
    logFileDateDirectory.mkdir();
    //defines actual logFile in the nested directories
    File logFile = new File(this.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) + "/ApplicationLogFiles/" + szDate, "ApplicationLogCatFiles_" + szDateTime + ".txt");

    if (!logFile.exists()) {
        try {
            Process process = Runtime.getRuntime().exec("logcat -c"); 
            process = Runtime.getRuntime().exec("logcat -f " + logFile + " *:V");
        }
        catch (IOException e) {
           e.printStackTrace();
            Log.e("IOException", "exception in LogcatToTextFile() method");
        }
    }
}

This runs fine as long as regular events (Log.d) occur frequently. Some sessions have lasted more than ten hours (50,000 rows) until the application is exited by the user. However, without regular log events, Logcat stops after a short period of time (minutes to hours). This problem was also described by Sonja here: logcat stops writing to file on device . I would like for Logcat to continue writing to a file and document any errors that may occur in the application.

portsample
  • 1,986
  • 4
  • 19
  • 35

1 Answers1

0

This problem can by resolved by creating a log event at specified periods of time,

synchronized private void startLogCatTimer() {
    handlerLogCatTimer.postDelayed(getCountdownTask(), COUNTDOWN_TIME_MS);
}
private Runnable getCountdownTask() {
    String COUNTDOWN_TIME_MS = 300000; //5 mins
    Runnable task = new Runnable() {
        @Override
        public void run() {
            Log.d("Logcat", "Auto timer (No Problem). Time is: " + szDeviceTimeHHMM);
            handlerLogCatTimer.postDelayed(this, COUNTDOWN_TIME_MS);
        }
    };
    runnableLogCatDismissCountdown = task;
    return task;
}
portsample
  • 1,986
  • 4
  • 19
  • 35