|
| 1 | +//! Non-blocking file logger backed by a dedicated OS thread. |
| 2 | +//! |
| 3 | +//! Design: a bounded `mpsc::sync_channel` feeds a background thread that owns |
| 4 | +//! the file descriptor and writes through a 64 KB `BufWriter`. The hot path |
| 5 | +//! (`emit`) does a single `try_send` — if the channel is full the message is |
| 6 | +//! silently dropped, so logging never blocks the proxy. |
| 7 | +//! |
| 8 | +//! When no log file is configured only stderr is written (zero overhead from |
| 9 | +//! the file path — no channel, no allocation, no thread). |
| 10 | +
|
| 11 | +use std::fmt; |
| 12 | +use std::fs::OpenOptions; |
| 13 | +use std::io::{BufWriter, Write}; |
| 14 | +use std::sync::OnceLock; |
| 15 | +use std::sync::mpsc; |
| 16 | + |
| 17 | +/// Channel capacity — number of formatted log lines buffered before drops. |
| 18 | +const CHANNEL_CAP: usize = 4096; |
| 19 | + |
| 20 | +/// BufWriter capacity — bytes buffered before a syscall write. |
| 21 | +const BUF_CAP: usize = 64 * 1024; |
| 22 | + |
| 23 | +static FILE_TX: OnceLock<mpsc::SyncSender<String>> = OnceLock::new(); |
| 24 | + |
| 25 | +/// Initialise file logging. Call once at startup. |
| 26 | +/// If `path` is `None`, only stderr logging is active (the default). |
| 27 | +pub fn init(path: Option<&str>) { |
| 28 | + let Some(p) = path else { return }; |
| 29 | + |
| 30 | + let file = OpenOptions::new() |
| 31 | + .create(true) |
| 32 | + .append(true) |
| 33 | + .open(p) |
| 34 | + .unwrap_or_else(|e| panic!("[spiceio] failed to open log file {p}: {e}")); |
| 35 | + |
| 36 | + let (tx, rx) = mpsc::sync_channel::<String>(CHANNEL_CAP); |
| 37 | + |
| 38 | + std::thread::Builder::new() |
| 39 | + .name("spiceio-log".into()) |
| 40 | + .spawn(move || writer_loop(rx, file)) |
| 41 | + .expect("[spiceio] failed to spawn log thread"); |
| 42 | + |
| 43 | + FILE_TX.set(tx).ok(); |
| 44 | +} |
| 45 | + |
| 46 | +/// Background writer — drains the channel and flushes in batches. |
| 47 | +fn writer_loop(rx: mpsc::Receiver<String>, file: std::fs::File) { |
| 48 | + let mut w = BufWriter::with_capacity(BUF_CAP, file); |
| 49 | + while let Ok(line) = rx.recv() { |
| 50 | + let _ = w.write_all(line.as_bytes()); |
| 51 | + let _ = w.write_all(b"\n"); |
| 52 | + // Drain any queued messages before issuing the syscall flush. |
| 53 | + while let Ok(line) = rx.try_recv() { |
| 54 | + let _ = w.write_all(line.as_bytes()); |
| 55 | + let _ = w.write_all(b"\n"); |
| 56 | + } |
| 57 | + let _ = w.flush(); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/// Write a formatted message to stderr and (if configured) to the log file. |
| 62 | +/// The file send is non-blocking — messages are dropped if the channel is full. |
| 63 | +#[inline] |
| 64 | +pub fn emit(args: fmt::Arguments<'_>) { |
| 65 | + eprintln!("{args}"); |
| 66 | + if let Some(tx) = FILE_TX.get() { |
| 67 | + // Allocate only when file logging is active. |
| 68 | + let _ = tx.try_send(args.to_string()); |
| 69 | + } |
| 70 | +} |
0 commit comments