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