From 2ed7afd672df16ad8a19b22886ecdc7881916b3d Mon Sep 17 00:00:00 2001 From: The412Banner <205237651+The412Banner@users.noreply.github.com> Date: Wed, 25 Nov 2026 12:11:47 +0000 Subject: [PATCH] fix(logs): cap app.log and audio.log at 64 MB per session A guest client reconnecting to PulseAudio thousands of times a second filled app.log to 1.4 GB and audio.log to 875 MB in 15 minutes. Both files now stop at 64 MB with a marker, keep their last 400 lines for the end of the file, and the audio daemon's lines stop going to logcat once audio.log is full. --- .../audio/DirectAudioRelayComponent.java | 18 +++-- .../launcher/audio/PulseAudioComponent.java | 31 +++++--- .../launcher/core/CappedLogWriter.kt | 59 ++++++++++++++ .../launcher/core/SessionLogCapture.kt | 26 ++---- .../launcher/core/CappedLogWriterTest.kt | 79 +++++++++++++++++++ 5 files changed, 176 insertions(+), 37 deletions(-) create mode 100644 app/src/main/java/com/droiddeck/launcher/core/CappedLogWriter.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/core/CappedLogWriterTest.kt diff --git a/app/src/main/java/com/droiddeck/launcher/audio/DirectAudioRelayComponent.java b/app/src/main/java/com/droiddeck/launcher/audio/DirectAudioRelayComponent.java index 8d9ddcb1f0..af9df262bf 100644 --- a/app/src/main/java/com/droiddeck/launcher/audio/DirectAudioRelayComponent.java +++ b/app/src/main/java/com/droiddeck/launcher/audio/DirectAudioRelayComponent.java @@ -3,6 +3,7 @@ package com.droiddeck.launcher.audio; import android.os.Process; import android.util.Log; +import com.droiddeck.launcher.core.CappedLogWriter; import com.droiddeck.launcher.core.FileUtils; import com.droiddeck.launcher.core.SessionPart; import com.droiddeck.launcher.core.HostProcess; @@ -16,6 +17,7 @@ public class DirectAudioRelayComponent extends SessionPart { private final File socketPath; private File logFile; + private volatile CappedLogWriter log; private final File micFifoPath; private volatile int pid = -1; private final Object micLock = new Object(); @@ -77,24 +79,23 @@ public class DirectAudioRelayComponent extends SessionPart { } ArrayList env = new ArrayList<>(); env.add("HOME=" + app().getFilesDir()); - java.io.PrintWriter out = null; + CappedLogWriter out = null; if (logFile != null) { try { - out = new java.io.PrintWriter(new java.io.FileWriter(logFile, true)); - out.println("== DirectAudio relay helper starting" + out = new CappedLogWriter(logFile, true); + out.line("== DirectAudio relay helper starting" + (withMic ? " with the microphone" : " (no microphone)")); - out.flush(); } catch (Exception e) { Log.w(TAG, "could not open " + logFile, e); } } - final java.io.PrintWriter log = out; + final CappedLogWriter helperLog = out; + log = out; pid = HostProcess.start(command.toString(), env.toArray(new String[0]), app().getFilesDir(), null, line -> { - Log.i(TAG, line); + if (helperLog == null || helperLog.line(line)) Log.i(TAG, line); if (withMic) trackMic(line); - if (log != null) synchronized (log) { log.println(line); log.flush(); } }); Log.i(TAG, "started pid=" + pid + " socket=" + socketPath + (withMic ? " mic=" + micFifoPath : " (no microphone)")); @@ -141,6 +142,9 @@ public class DirectAudioRelayComponent extends SessionPart { Process.killProcess(pid); pid = -1; } + CappedLogWriter current = log; + log = null; + if (current != null) current.close(); } @Override diff --git a/app/src/main/java/com/droiddeck/launcher/audio/PulseAudioComponent.java b/app/src/main/java/com/droiddeck/launcher/audio/PulseAudioComponent.java index 53a0a6aacf..c82e879980 100644 --- a/app/src/main/java/com/droiddeck/launcher/audio/PulseAudioComponent.java +++ b/app/src/main/java/com/droiddeck/launcher/audio/PulseAudioComponent.java @@ -4,6 +4,7 @@ import android.content.Context; import android.os.Process; import android.util.Log; +import com.droiddeck.launcher.core.CappedLogWriter; import com.droiddeck.launcher.core.FileUtils; import com.droiddeck.launcher.core.SessionPart; import com.droiddeck.launcher.core.HostProcess; @@ -32,6 +33,7 @@ public class PulseAudioComponent extends SessionPart { private final File workingDir; private final File deviceControls; private File logFile; + private CappedLogWriter log; private final String micFifoPath; private final HostLifecycle host = new HostLifecycle(Process::killProcess); @@ -191,7 +193,7 @@ public class PulseAudioComponent extends SessionPart { + " --system=false --disable-shm=true --fail=false" + " -n --file=default.pa --daemonize=false --use-pid-file=false --exit-idle-time=-1" + " --log-level=info"; - final java.io.PrintWriter out = openLog(); + final CappedLogWriter out = openLog(); if (!host.launch(generation, () -> HostProcess.start(command, env.toArray(new String[0]), workingDir, status -> { if (host.exited(generation)) { Log.w(TAG, "audio host exited with status " + status + "; restarting"); @@ -199,8 +201,7 @@ public class PulseAudioComponent extends SessionPart { } }, line -> { - Log.i(TAG, line); - if (out != null) synchronized (out) { out.println(line); out.flush(); } + if (out == null || out.line(line)) Log.i(TAG, line); }))) return; long token = host.token(); Thread guard = new Thread(() -> ensureClientSink(out, token), "pulse-sink-guard"); @@ -208,7 +209,7 @@ public class PulseAudioComponent extends SessionPart { guard.start(); } - private void ensureClientSink(java.io.PrintWriter out, long token) { + private void ensureClientSink(CappedLogWriter out, long token) { try { // Nothing that may block (a sink whose AAudio open hangs: a Galaxy S24 FE (US) hung the // daemon's start-up loading one from default.pa) is loaded by the daemon itself; the @@ -253,17 +254,17 @@ public class PulseAudioComponent extends SessionPart { Log.w(TAG, line); } Log.i(TAG, line); - if (out != null) synchronized (out) { out.println(line); out.flush(); } + if (out != null) out.line(line); } catch (Throwable t) { Log.w(TAG, "ensureClientSink failed", t); } } /** A timestamped line in the session's audio log. */ - private void note(java.io.PrintWriter out, String what) { + private void note(CappedLogWriter out, String what) { String line = "== " + new java.text.SimpleDateFormat("HH:mm:ss.SSS", java.util.Locale.US).format(new java.util.Date()) + " " + what; Log.i(TAG, line); - if (out != null) synchronized (out) { out.println(line); out.flush(); } + if (out != null) out.line(line); } private String pactlFor(long token, String... args) { @@ -330,6 +331,7 @@ public class PulseAudioComponent extends SessionPart { @Override public void stop() { host.stop(); + closeLog(); } public boolean setSinkSuspended(boolean suspended) { @@ -380,19 +382,24 @@ public class PulseAudioComponent extends SessionPart { } } - private java.io.PrintWriter openLog() { + private synchronized CappedLogWriter openLog() { + closeLog(); if (logFile == null) return null; try { - java.io.PrintWriter w = new java.io.PrintWriter(new java.io.FileWriter(logFile, true)); - w.println("== PulseAudio daemon starting" + (micFifoPath != null ? " with a microphone source" : "")); - w.flush(); - return w; + log = new CappedLogWriter(logFile, true); + log.line("== PulseAudio daemon starting" + (micFifoPath != null ? " with a microphone source" : "")); + return log; } catch (Exception e) { Log.w(TAG, "could not open " + logFile, e); return null; } } + private synchronized void closeLog() { + if (log != null) log.close(); + log = null; + } + private void copyFromLibraryDir() { String[] libs = {"libltdl.so", "libpulseaudio.so", "libpulse.so", "libpulsecommon-13.0.so", "libpulsecore-13.0.so", "libsndfile.so", "libffi.so"}; diff --git a/app/src/main/java/com/droiddeck/launcher/core/CappedLogWriter.kt b/app/src/main/java/com/droiddeck/launcher/core/CappedLogWriter.kt new file mode 100644 index 0000000000..b63e0a0164 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/core/CappedLogWriter.kt @@ -0,0 +1,59 @@ +package com.droiddeck.launcher.core + +import java.io.BufferedWriter +import java.io.Closeable +import java.io.File +import java.io.FileOutputStream +import java.io.OutputStreamWriter + +class CappedLogWriter @JvmOverloads constructor( + private val file: File, + append: Boolean, + private val limitBytes: Long = SESSION_LOG_LIMIT_BYTES, + private val tailLines: Int = TAIL_LINES, +) : Closeable { + private val out = BufferedWriter(OutputStreamWriter(FileOutputStream(file, append), Charsets.UTF_8)) + private val tail = ArrayDeque() + private var dropped = 0L + private var closed = false + + @Synchronized + fun line(text: String): Boolean { + if (closed) return false + if (dropped == 0L && file.length() + text.toByteArray(Charsets.UTF_8).size + 1 <= limitBytes) { + runCatching { out.write(text); out.newLine(); out.flush() } + return true + } + if (dropped == 0L) { + runCatching { + out.write("== this log reached its ${limitBytes / (1024 * 1024)} MB limit; later lines are dropped and only the last $tailLines are kept at the end") + out.newLine() + out.flush() + } + } + dropped++ + tail.addLast(text) + if (tail.size > tailLines) tail.removeFirst() + return false + } + + @Synchronized + override fun close() { + if (closed) return + closed = true + runCatching { + if (dropped > 0) { + out.write("== ${dropped - tail.size} lines dropped after the limit; the last ${tail.size} follow") + out.newLine() + tail.forEach { out.write(it); out.newLine() } + } + out.flush() + } + runCatching { out.close() } + } + + companion object { + const val SESSION_LOG_LIMIT_BYTES = 64L * 1024 * 1024 + const val TAIL_LINES = 400 + } +} 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 650a6b8d4d..5715377981 100644 --- a/app/src/main/java/com/droiddeck/launcher/core/SessionLogCapture.kt +++ b/app/src/main/java/com/droiddeck/launcher/core/SessionLogCapture.kt @@ -3,7 +3,6 @@ package com.droiddeck.launcher.core import android.content.Context import android.os.Process import android.util.Log -import java.io.BufferedWriter import java.io.File import java.io.FileOutputStream import java.nio.file.Files @@ -14,7 +13,7 @@ object SessionLogCapture { private const val TAG = "SessionLogCapture" private var pid = -1 - private var writer: BufferedWriter? = null + private var writer: CappedLogWriter? = null private var target: File? = null private var syncer: Thread? = null @@ -22,26 +21,20 @@ object SessionLogCapture { fun start(target: File, sinceMillis: Long) { stop() try { - val out = target.bufferedWriter() - out.write("The app's own log for this session (logcat, this process only).\n") - out.write("Lines the app wrote before the session folder existed are in logcat only.\n\n") - out.flush() + val out = CappedLogWriter(target, false) + out.line("The app's own log for this session (logcat, this process only).") + out.line("Lines the app wrote before the session folder existed are in logcat only.") + out.line("") writer = out this.target = target pid = HostProcess.start( command(Process.myPid(), sinceMillis), null, target.parentFile, null, ) { line -> - synchronized(this) { - try { - writer?.apply { write(line); newLine(); flush() } - } catch (e: Exception) { - } - } + synchronized(this) { writer?.line(line) } } if (pid == -1) { - out.write("logcat could not be started; this ROM may not hand an app its own entries.\n") - out.flush() + out.line("logcat could not be started; this ROM may not hand an app its own entries.") } target.parentFile?.let { syncer = startSyncing(it) } Log.i(TAG, "app log -> $target (logcat pid $pid)") @@ -81,10 +74,7 @@ object SessionLogCapture { } pid = -1 } - try { - writer?.close() - } catch (e: Exception) { - } + writer?.close() writer = null } diff --git a/app/src/test/java/com/droiddeck/launcher/core/CappedLogWriterTest.kt b/app/src/test/java/com/droiddeck/launcher/core/CappedLogWriterTest.kt new file mode 100644 index 0000000000..a2084875d1 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/core/CappedLogWriterTest.kt @@ -0,0 +1,79 @@ +package com.droiddeck.launcher.core + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder + +class CappedLogWriterTest { + @get:Rule val folder = TemporaryFolder() + + @Test fun aLogUnderTheLimitIsWrittenAsIs() { + val file = folder.newFile("app.log") + CappedLogWriter(file, false, 1024, 3).use { log -> + assertTrue(log.line("first")) + assertTrue(log.line("second")) + } + assertEquals(listOf("first", "second"), file.readLines()) + } + + @Test fun aReconnectStormStopsAtTheLimitAndKeepsTheLastLines() { + val file = folder.newFile("audio.log") + val limit = 4096L + val log = CappedLogWriter(file, true, limit, 3) + var written = 0 + for (n in 0 until 10_000) { + if (log.line("I: [linker64] client.c: Created $n \"Native client (UNIX socket client)\"")) written++ + } + val capped = file.length() + assertTrue("file grew to $capped bytes", capped <= limit + 200) + assertFalse(log.line("after the limit")) + log.close() + val lines = file.readLines() + assertEquals(written, lines.count { it.startsWith("I: [linker64] client.c: Created ") } - 2) + assertTrue(lines.any { it.startsWith("== this log reached its") }) + assertEquals("== ${10_000 + 1 - written - 3} lines dropped after the limit; the last 3 follow", lines[lines.size - 4]) + assertEquals( + listOf( + "I: [linker64] client.c: Created 9998 \"Native client (UNIX socket client)\"", + "I: [linker64] client.c: Created 9999 \"Native client (UNIX socket client)\"", + "after the limit", + ), + lines.takeLast(3), + ) + assertTrue(file.length() < limit + 1024) + } + + @Test fun twoWritersOnOneFileShareItsLimit() { + val file = folder.newFile("audio.log") + val daemon = CappedLogWriter(file, true, 2048, 2) + val relay = CappedLogWriter(file, true, 2048, 2) + repeat(2_000) { daemon.line("I: [linker64] protocol-native.c: Connection died.") } + assertFalse(relay.line("DA-Relay: mic enabled=1")) + assertTrue(file.length() <= 2048 + 200) + daemon.close() + relay.close() + assertEquals("DA-Relay: mic enabled=1", file.readLines().last()) + } + + @Test fun appendKeepsWhatTheFileHeldAndOverwriteStartsFresh() { + val file = folder.newFile("audio.log") + file.writeText("== PulseAudio daemon starting\n") + CappedLogWriter(file, true, 1024, 2).use { it.line("restarted") } + assertEquals(listOf("== PulseAudio daemon starting", "restarted"), file.readLines()) + CappedLogWriter(file, false, 1024, 2).use { it.line("new session") } + assertEquals(listOf("new session"), file.readLines()) + } + + @Test fun linesAfterCloseAreIgnored() { + val file = folder.newFile("app.log") + val log = CappedLogWriter(file, false, 1024, 2) + log.line("kept") + log.close() + assertFalse(log.line("late")) + log.close() + assertEquals(listOf("kept"), file.readLines()) + } +}