logcat stops writing to file on device

761 Views Asked by At

For an android app I'm saving logs on the device itself so that when there are issues we can find out what they are. This device runs in a no internet environment so it's not an option to write the logs remotely.

The below code first clears the buffer, then continuously writes whatever is logged to 'logFile'.

    try {
        Process process = Runtime.getRuntime().exec("logcat -c"); // -c clear buffer.
        process = Runtime.getRuntime().exec("logcat -f " + logFile + " -r 5120 -n 30"); // -r amount of bits to write, -n amount of logs to rotate
    } catch ( IOException e ) {
        e.printStackTrace();
    }

This code works fine, except after some time, hours or days it randomly stops saving the log.

What might cause this and how can I fix this?

2

There are 2 best solutions below

2
Sonja On BEST ANSWER

The problem was the app used a browser which did javascript requests. Occassionally these requests failed, but they were never cleaned up because no response came. This specific app is on 24/7 meaning it doesn't get to clear these requests in the normal way. Because this browser is using it's own sandbox no errors are thrown when this happens but at some point the memory fills up causing it to have no memory left to continue logging. Hence the problem of no memory is not logged. The fix in this case was to clear and restart the in-app browser every 6 hours, which fixed the problem.

This only happened on a specific Siemens tablet with a specific Android build.

1
Anupreet Kaur On

I think you should check permissions first. Might you are testing the application in marshmallow and above. Please try the below code and let me know is it work for you:

    public class MyProjectApp extends Application {

    /**
     * Called when the application is starting, before any activity, service, or receiver objects (excluding content providers) have been created.
     */
    public void onCreate() {
        super.onCreate();

        if ( isExternalStorageWritable() ) {

            File appDirectory = new File( Environment.getExternalStorageDirectory() + "/MyProjectAppFolder" );
            File logDirectory = new File( appDirectory + "/log" );
            File logFile = new File( logDirectory, "logcat" + System.currentTimeMillis() + ".txt" );

            // create app folder
            if ( !appDirectory.exists() ) {
                appDirectory.mkdir();
            }

            // create log folder
            if ( !logDirectory.exists() ) {
                logDirectory.mkdir();
            }

            // clear the previous logcat and then write the new one to the file
            try {
                Process process = Runtime.getRuntime().exec("logcat -c");
                process = Runtime.getRuntime().exec("logcat -f " + logFile);
            } catch ( IOException e ) {
                e.printStackTrace();
            }

        } else if ( isExternalStorageReadable() ) {
            // only readable
        } else {
            // not accessible
        }
    }

    /* Checks if external storage is available for read and write */
    public boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        if ( Environment.MEDIA_MOUNTED.equals( state ) ) {
            return true;
        }
        return false;
    }

    /* Checks if external storage is available to at least read */
    public boolean isExternalStorageReadable() {
        String state = Environment.getExternalStorageState();
        if ( Environment.MEDIA_MOUNTED.equals( state ) ||
                Environment.MEDIA_MOUNTED_READ_ONLY.equals( state ) ) {
            return true;
        }
        return false;
    }
}

you need the correct permissions in your .manifest file:

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

Source source: http://www.journal.deviantdev.com/android-log-logcat-to-file-while-runtime/