Skip to main content

vtcode_commons/
trace_flush.rs

1#![expect(
2    clippy::let_underscore_must_use,
3    reason = "The trace flush hook is registered once and duplicate registration is intentionally ignored."
4)]
5
6//! Global trace log flush hook.
7//!
8//! Allows any crate (including `vtcode-ui`) to trigger a trace log flush
9//! without depending on `vtcode-core`. The flush callback is registered once
10//! during tracing initialization and can be invoked from signal handlers or
11//! shutdown sequences.
12
13use std::sync::OnceLock;
14
15static FLUSH_HOOK: OnceLock<fn()> = OnceLock::new();
16
17/// Register a flush callback. Called once during tracing initialization.
18pub fn register_trace_flush_hook(f: fn()) {
19    let _ = FLUSH_HOOK.set(f);
20}
21
22/// Flush the global trace log writer.
23///
24/// Safe to call from signal handlers, shutdown hooks, or `Drop` implementations.
25/// No-op if no flush hook has been registered.
26pub fn flush_trace_log() {
27    if let Some(hook) = FLUSH_HOOK.get() {
28        hook();
29    }
30}