Skip to main content

init_tracing_with_file_log

Function init_tracing_with_file_log 

Source
pub fn init_tracing_with_file_log() -> Option<WorkerGuard>
Expand description

Initialise tracing for tcode serve: stderr (unchanged, MCP-framing-safe) PLUS a daily-rotating file layer under file_log_dir (#6537).

Why: only the long-running serve daemon accumulates enough log volume to be worth draining — mirrors trusty-mpm’s own daemon/CLI split (bin/tm/tracing_setup.rs), where a short-lived CLI invocation gets the lighter init_tracing instead. A file-directory creation failure (a read-only home, a full disk) degrades to stderr-only rather than failing daemon startup. A prior version created the directory with a raw create_dir_all, which applies the process umask and commonly leaves 0755/0644 — code-review fix (#6537): this now routes through the same private-state hardening every other writer under ~/.trusty-code uses (#6999), so the directory is 0700 and each rolled file 0600. What: builds a tracing_subscriber::registry() with a stderr fmt layer and, when the log directory is creatable, a non-blocking daily file fmt layer (with_ansi(false), since a rotated file is read by tooling, not a terminal). The directory is created and tightened via crate::paths::private_state::ensure_private_state_dir (the ~/.trusty-code root) then [crate::paths::private_state::ensure_dir] (the logs subdirectory itself); [chmod_log_files_owner_only] then tightens whichever file tracing_appender::rolling::daily just opened for today. Returns the file layer’s WorkerGuard — the caller MUST hold it for the process lifetime; dropping it early silently discards buffered log records. Test: tests::file_log_dir_is_under_private_state covers the path computation this function depends on; installing a second GLOBAL subscriber in-process (this function’s own behavior) cannot itself run in the shared --lib test binary alongside begin_capture’s try_init — see that test’s own doc comment. The directory/file mode guarantee is covered by tests/logging_e2e.rs::file_log_dir_and_current_file_are_owner_only (a subprocess with HOME pointed at a tempdir, for the same reason).