diff --git a/app/src/main/java/com/droiddeck/launcher/core/SessionLogCapture.kt b/app/src/main/java/com/droiddeck/launcher/core/SessionLogCapture.kt index 965a4950d8..9ba5c2f6ff 100644 --- a/app/src/main/java/com/droiddeck/launcher/core/SessionLogCapture.kt +++ b/app/src/main/java/com/droiddeck/launcher/core/SessionLogCapture.kt @@ -4,6 +4,7 @@ import android.os.Process import android.util.Log import java.io.BufferedWriter import java.io.File +import java.io.FileOutputStream object SessionLogCapture { private const val TAG = "SessionLogCapture" @@ -11,6 +12,7 @@ object SessionLogCapture { private var pid = -1 private var writer: BufferedWriter? = null private var target: File? = null + private var syncer: Thread? = null @Synchronized fun start(target: File) { @@ -37,6 +39,7 @@ object SessionLogCapture { out.write("logcat could not be started; this ROM may not hand an app its own entries.\n") out.flush() } + target.parentFile?.let { syncer = startSyncing(it) } Log.i(TAG, "app log -> $target (logcat pid $pid)") } catch (e: Exception) { Log.w(TAG, "could not start the app log", e) @@ -49,8 +52,19 @@ object SessionLogCapture { if (t.parentFile?.absolutePath == dir.absolutePath) stop() } + /** flush() only reaches the page cache, and a hard reset on f2fs lost the last minutes of every + * log. Every 2 s the session folder's files (ours and the native parts') are synced to storage. */ + private fun startSyncing(dir: File): Thread = Thread({ + while (true) { + try { Thread.sleep(2000) } catch (e: InterruptedException) { break } + dir.listFiles()?.forEach { runCatching { FileOutputStream(it, true).use { s -> s.fd.sync() } } } + } + }, "session-log-sync").apply { isDaemon = true; start() } + @Synchronized fun stop() { + syncer?.interrupt() + syncer = null target = null if (pid != -1) { try {