StdoutCapture

Process-wide manager for capturing System.out and System.err line-by-line into a caller-supplied sink.

Single-installer semantics: only one capture can be active per JVM at any time. install is idempotent — calling it while a capture is already active first uninstalls the previous one, then installs the new sink. uninstall is also idempotent — calling it while nothing is installed is a no-op.

A one-time JVM shutdown hook is registered on the first install so the original streams are restored if the process is killed without the host being able to call uninstall (e.g. force-quit, IDE Stop button, crash). Matters for IDE Run sessions where the JVM may persist across app launches.

The sink runs on whichever thread completed the captured line. Sinks targeting UI components must dispatch onto the right thread themselves.

Thread-safe: install and uninstall are synchronized; isInstalled reads a volatile flag.

Substrate-level JVM utility — usable by any host that wants line-by-line capture of process-wide standard streams.

Functions

Link copied to clipboard
fun install(sink: (text: String, fromErr: Boolean) -> Unit)

Install line-completing tees on System.out and System.err. The sink receives each completed line plus a flag indicating whether it came from stderr (fromErr = true) or stdout (fromErr = false).

Link copied to clipboard

Whether a capture is currently active.

Link copied to clipboard

Restore the original System.out and System.err. Safe to call when no capture is installed.