Skip to main content

cpa_server/
logging.rs

1//! The process log (Go internal/logging/global_logger.go, log_dir_cleaner.go and
2//! `util.SetLogLevel`): Go's line format, stdout or a rotating `main.log`, the
3//! log-directory size cleaner and the `debug` level switch. The binary's `--log-file`
4//! replaces only stdout: `logging-to-file` still writes `main.log` and the cleaner
5//! still runs.
6//!
7//! [`init`] installs the subscriber once; [`configure`] applies a config snapshot
8//! and acts only on the settings that changed, as Go's reload does.
9
10use cpa_core::gostr::GoStr;
11use std::collections::VecDeque;
12use std::fs::{File, OpenOptions};
13use std::io::{self, Write};
14use std::path::{Path, PathBuf};
15use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
16use std::sync::{Arc, Mutex, MutexGuard, OnceLock, PoisonError};
17use std::time::Duration;
18
19use cpa_core::config::Config;
20use tracing::field::{Field, Visit};
21use tracing::{Event, Level, Subscriber};
22use tracing_subscriber::filter::LevelFilter;
23use tracing_subscriber::fmt::format::Writer;
24use tracing_subscriber::fmt::{FmtContext, FormatEvent, FormatFields, MakeWriter};
25use tracing_subscriber::layer::SubscriberExt;
26use tracing_subscriber::registry::LookupSpan;
27use tracing_subscriber::util::SubscriberInitExt;
28use tracing_subscriber::{EnvFilter, Layer, reload};
29
30/// lumberjack `MaxSize: 10` (megabytes of 1024 * 1024 bytes).
31const MAX_FILE_SIZE: u64 = 10 * 1024 * 1024;
32/// Only CLI process rotations are bounded when Go's directory budget is unlimited.
33const DEFAULT_PROCESS_ROTATIONS_SIZE: u64 = 32 * 1024 * 1024;
34const MAIN_LOG: &str = "main.log";
35const CLEANER_INTERVAL: Duration = Duration::from_secs(60);
36
37static OUTPUT: Mutex<Output> = Mutex::new(Output::Stdout);
38/// `--log-file`: where stdout output goes instead, kept across config reloads.
39static LOG_FILE: OnceLock<PathBuf> = OnceLock::new();
40static HOOK: Mutex<Option<Arc<LogHook>>> = Mutex::new(None);
41static STATE: Mutex<Option<Applied>> = Mutex::new(None);
42static LEVEL: OnceLock<reload::Handle<LevelFilter, tracing_subscriber::Registry>> = OnceLock::new();
43static CLEANER_GENERATION: AtomicU64 = AtomicU64::new(0);
44/// Set once [`init`] installed the subscriber; [`configure`] does nothing before.
45static INSTALLED: AtomicBool = AtomicBool::new(false);
46static HOOKS: Mutex<Vec<(u64, Hook)>> = Mutex::new(Vec::new());
47static NEXT_HOOK: AtomicU64 = AtomicU64::new(1);
48
49/// One process-log line as Go's logrus hooks see it (the Home app-log forwarder).
50pub struct Entry<'a> {
51    /// The formatted line, newline included.
52    pub line: &'a str,
53    /// logrus `Level.String()`: `warning` for WARN.
54    pub level: &'static str,
55    pub time: chrono::DateTime<chrono::FixedOffset>,
56    /// The raw `request_id` field, empty when absent.
57    pub request_id: &'a str,
58}
59
60pub type Hook = std::sync::Arc<dyn Fn(&Entry<'_>) + Send + Sync>;
61
62/// Go `log.AddHook`: `hook` sees every line written from now on, at the levels the
63/// logger writes. Returns the id for [`remove_hook`].
64pub fn add_hook(hook: Hook) -> u64 {
65    let id = NEXT_HOOK.fetch_add(1, Ordering::SeqCst);
66    HOOKS.lock().unwrap_or_else(PoisonError::into_inner).push((id, hook));
67    id
68}
69
70pub fn remove_hook(id: u64) {
71    HOOKS
72        .lock()
73        .unwrap_or_else(PoisonError::into_inner)
74        .retain(|(i, _)| *i != id);
75}
76
77fn fire_hooks(entry: &Entry<'_>) {
78    let hooks: Vec<Hook> = HOOKS
79        .lock()
80        .unwrap_or_else(PoisonError::into_inner)
81        .iter()
82        .map(|(_, hook)| hook.clone())
83        .collect();
84    for hook in hooks {
85        hook(entry);
86    }
87}
88
89/// The settings last applied, compared on every [`configure`].
90#[derive(Clone, Copy, PartialEq)]
91struct Applied {
92    logging_to_file: bool,
93    max_total_mb: i64,
94    debug: bool,
95}
96
97enum Output {
98    Stdout,
99    ProcessFile(RotatingFile),
100    File(RotatingFile),
101}
102
103/// Go `SetupBaseLogger`: Go's line format on stdout at info level. A set `RUST_LOG`
104/// replaces the level switch with its filter. Later calls are no-ops.
105pub fn init() {
106    let filter = match EnvFilter::try_from_default_env() {
107        Ok(env) => env.boxed(),
108        Err(_) => {
109            let (level, handle) = reload::Layer::new(LevelFilter::INFO);
110            let _ = LEVEL.set(handle);
111            level.boxed()
112        }
113    };
114    let format = tracing_subscriber::fmt::layer()
115        .event_format(GoFormat)
116        .with_writer(GlobalWriter);
117    if tracing_subscriber::registry()
118        .with(filter)
119        .with(format)
120        .try_init()
121        .is_ok()
122    {
123        INSTALLED.store(true, Ordering::SeqCst);
124    }
125}
126
127/// Go's TUI `LogHook` (internal/tui/loghook.go): every formatted log line, the
128/// newest kept when full (Go drops the oldest buffered line).
129pub struct LogHook {
130    lines: Mutex<VecDeque<String>>,
131    capacity: usize,
132    ready: tokio::sync::Notify,
133}
134
135impl LogHook {
136    fn push(&self, line: String) {
137        let mut lines = self.lines.lock().unwrap_or_else(PoisonError::into_inner);
138        if lines.len() >= self.capacity {
139            lines.pop_front();
140        }
141        lines.push_back(line);
142        drop(lines);
143        self.ready.notify_one();
144    }
145
146    /// The next line, waiting for one.
147    pub async fn next(&self) -> String {
148        loop {
149            if let Some(line) = self.lines.lock().unwrap_or_else(PoisonError::into_inner).pop_front() {
150                return line;
151            }
152            self.ready.notified().await;
153        }
154    }
155}
156
157/// The standalone TUI's log capture (Go `log.AddHook(hook)` with the logger output set
158/// to `io.Discard`): every line also goes to the returned hook, and stdout output is
159/// dropped until [`release`]. Output to `main.log` continues.
160pub fn capture(capacity: usize) -> Arc<LogHook> {
161    let hook = Arc::new(LogHook {
162        lines: Mutex::new(VecDeque::new()),
163        capacity: capacity.max(1),
164        ready: tokio::sync::Notify::new(),
165    });
166    *HOOK.lock().unwrap_or_else(PoisonError::into_inner) = Some(hook.clone());
167    hook
168}
169
170/// Ends [`capture`]: lines go to stdout again.
171pub fn release() {
172    *HOOK.lock().unwrap_or_else(PoisonError::into_inner) = None;
173}
174
175/// Go `ConfigureLogOutput` (when `logging-to-file` or `logs-max-total-size-mb`
176/// changed) and `SetLogLevel` (when `debug` changed), from the first call on.
177/// Without [`init`] (libraries, tests) the process log is not ours to redirect.
178pub fn configure(cfg: &Config) {
179    if !INSTALLED.load(Ordering::SeqCst) {
180        return;
181    }
182    let next = Applied {
183        logging_to_file: logs_setting(cfg, "logging-to-file").and_then(serde_yaml_ng::Value::as_bool) == Some(true),
184        max_total_mb: logs_setting(cfg, "logs-max-total-size-mb")
185            .and_then(serde_yaml_ng::Value::as_i64)
186            .unwrap_or(0),
187        debug: logs_setting(cfg, "debug").and_then(serde_yaml_ng::Value::as_bool) == Some(true),
188    };
189    let mut state = STATE.lock().unwrap_or_else(PoisonError::into_inner);
190    let previous = *state;
191    *state = Some(next);
192    drop(state);
193    let output_changed =
194        previous.is_none_or(|p| p.logging_to_file != next.logging_to_file || p.max_total_mb != next.max_total_mb);
195    if output_changed && let Err(error) = configure_output(&resolve_log_dir(cfg), next) {
196        tracing::error!("failed to reconfigure log output: {error}");
197    }
198    if previous.is_none_or(|p| p.debug != next.debug) {
199        set_level(next.debug);
200    }
201}
202
203fn logs_setting<'a>(cfg: &'a Config, key: &str) -> Option<&'a serde_yaml_ng::Value> {
204    cfg.document.get("observability")?.get("logs")?.get(key)
205}
206
207/// Opt-in replacement for stdout, kept across config reloads: lines that would go
208/// to stdout go to `path` instead, while `logging-to-file` still selects `main.log`
209/// and its cleaner. Uses the existing synchronous 10 MiB rotation: one fd, no new
210/// thread, timer or per-request work.
211pub fn set_log_file(path: PathBuf) -> io::Result<()> {
212    let path = std::path::absolute(path)?;
213    let mut file = RotatingFile {
214        path: path.clone(),
215        file: None,
216        size: 0,
217        // The loaded config chooses the budget, before any server/login runs.
218        rotation_budget: None,
219    };
220    file.open_existing_or_new(0, MAX_FILE_SIZE)?;
221    LOG_FILE
222        .set(path)
223        .map_err(|_| io::Error::other("log file already set"))?;
224    *OUTPUT.lock().unwrap_or_else(PoisonError::into_inner) = Output::ProcessFile(file);
225    Ok(())
226}
227
228fn configure_output(dir: &Path, applied: Applied) -> io::Result<()> {
229    let mut output = OUTPUT.lock().unwrap_or_else(PoisonError::into_inner);
230    let rotation_budget = (applied.max_total_mb <= 0).then_some(DEFAULT_PROCESS_ROTATIONS_SIZE);
231    if applied.logging_to_file
232        && let Some(path) = LOG_FILE.get()
233        && let Some(budget) = rotation_budget
234    {
235        prune_process_rotations(path, budget);
236    }
237    let protected = if applied.logging_to_file {
238        create_dir(dir)
239            .map_err(|e| io::Error::new(e.kind(), format!("logging: failed to create log directory: {e}")))?;
240        let path = dir.join(MAIN_LOG);
241        *output = Output::File(RotatingFile {
242            path: path.clone(),
243            file: None,
244            size: 0,
245            rotation_budget: None,
246        });
247        Some(std::path::absolute(path)?)
248    } else if let Some(path) = LOG_FILE.get() {
249        let mut file = RotatingFile {
250            path: path.clone(),
251            file: None,
252            size: 0,
253            rotation_budget,
254        };
255        file.open_existing_or_new(0, MAX_FILE_SIZE)?;
256        *output = Output::ProcessFile(file);
257        Some(path.clone())
258    } else {
259        *output = Output::Stdout;
260        None
261    };
262    drop(output);
263    start_cleaner(dir, applied.max_total_mb, protected);
264    Ok(())
265}
266
267/// Go `SetLogLevel`: debug or info, announced when it changes.
268fn set_level(enabled: bool) {
269    let Some(handle) = LEVEL.get() else { return };
270    let next = if enabled { LevelFilter::DEBUG } else { LevelFilter::INFO };
271    let Some(current) = handle.clone_current() else { return };
272    if current == next {
273        return;
274    }
275    let _ = handle.modify(|level| *level = next);
276    let name = |l: LevelFilter| l.to_string().go_lower();
277    tracing::info!(
278        "log level changed from {} to {} (debug={})",
279        name(current),
280        name(next),
281        enabled
282    );
283}
284
285/// Go `ResolveLogDirectory`: `$WRITABLE_PATH/logs`, else `logs` when it is a writable
286/// directory, else `<auth-dir>/logs`.
287pub fn resolve_log_dir(cfg: &Config) -> PathBuf {
288    for key in ["WRITABLE_PATH", "writable_path"] {
289        if let Ok(v) = std::env::var(key)
290            && !v.trim().is_empty()
291        {
292            return Path::new(v.trim()).join("logs");
293        }
294    }
295    let local = Path::new("logs");
296    let writable = local.is_dir() && {
297        let probe = local.join(".perm_test");
298        let ok = File::create(&probe).is_ok();
299        let _ = std::fs::remove_file(&probe);
300        ok
301    };
302    if writable || cfg.auth_dir.as_os_str().is_empty() {
303        return local.to_owned();
304    }
305    cfg.auth_dir.join("logs")
306}
307
308/// Go `configureLogDirCleanerLocked`: replaces the running cleaner. A positive
309/// limit starts one that runs now and every minute.
310fn start_cleaner(dir: &Path, max_total_mb: i64, protected: Option<PathBuf>) {
311    let generation = CLEANER_GENERATION.fetch_add(1, Ordering::SeqCst) + 1;
312    let Some(max_bytes) = u64::try_from(max_total_mb)
313        .ok()
314        .filter(|&mb| mb > 0)
315        .map(|mb| mb * 1024 * 1024)
316    else {
317        return;
318    };
319    let dir = dir.to_owned();
320    // ponytail: a stopped cleaner notices on its next wake-up, up to a minute later.
321    let spawned = std::thread::Builder::new()
322        .name("log-dir-cleaner".into())
323        .spawn(move || {
324            while CLEANER_GENERATION.load(Ordering::SeqCst) == generation {
325                match enforce_size_limit(
326                    &dir,
327                    max_bytes,
328                    protected.as_deref(),
329                    LOG_FILE.get().map(PathBuf::as_path),
330                ) {
331                    Ok(0) => {}
332                    Ok(deleted) => tracing::debug!(
333                        "logging: removed {deleted} old log file(s) to enforce log directory size limit"
334                    ),
335                    Err(error) => tracing::warn!("logging: failed to enforce log directory size limit: {error}"),
336                }
337                std::thread::sleep(CLEANER_INTERVAL);
338            }
339        });
340    if let Err(error) = spawned {
341        tracing::warn!("logging: failed to start the log directory cleaner: {error}");
342    }
343}
344
345fn canonical_log_path(path: &Path) -> io::Result<PathBuf> {
346    match std::fs::canonicalize(path) {
347        Err(error) if error.kind() == io::ErrorKind::NotFound => std::path::absolute(path),
348        result => result,
349    }
350}
351
352/// Go `enforceLogDirSizeLimit`: deletes the oldest `*.log` / `*.log.gz` files (by
353/// modification time, never `protected`) and the selected process-log family until
354/// their combined size is within `max_bytes`. No other files outside `dir` qualify.
355pub(crate) fn enforce_size_limit(
356    dir: &Path,
357    max_bytes: u64,
358    protected: Option<&Path>,
359    process_log: Option<&Path>,
360) -> io::Result<usize> {
361    let protected = protected.map(canonical_log_path).transpose()?;
362    let dir = canonical_log_path(dir)?;
363    let process_log = process_log.map(canonical_log_path).transpose()?;
364    let process_dir = process_log
365        .as_deref()
366        .and_then(Path::parent)
367        .map(canonical_log_path)
368        .transpose()?;
369    let process_dir = process_dir.as_deref();
370    let mut dirs = vec![dir.clone()];
371    if let Some(parent) = process_dir
372        && parent != dir
373    {
374        dirs.push(parent.to_owned());
375    }
376    let mut files = Vec::new();
377    let mut total = 0u64;
378    for scan_dir in dirs {
379        let entries = match std::fs::read_dir(&scan_dir) {
380            Ok(entries) => entries,
381            Err(e) if e.kind() == io::ErrorKind::NotFound => continue,
382            Err(e) => return Err(e),
383        };
384        for entry in entries.flatten() {
385            let filename = entry.file_name();
386            let name = filename.to_string_lossy();
387            let normal_log = scan_dir == dir && {
388                let name = name.trim().go_lower();
389                name.ends_with(".log") || name.ends_with(".log.gz")
390            };
391            let process_file = process_log.as_ref().is_some_and(|path| {
392                if Some(scan_dir.as_path()) != process_dir {
393                    return false;
394                }
395                let base = path.file_name().unwrap_or_default().to_string_lossy();
396                name == base || is_process_rotation(&name, &base)
397            });
398            if !normal_log && !process_file {
399                continue;
400            }
401            let Ok(info) = entry.metadata() else { continue };
402            if !info.is_file() {
403                continue;
404            }
405            total += info.len();
406            files.push((info.modified().ok(), info.len(), entry.path()));
407        }
408    }
409    if total <= max_bytes {
410        return Ok(0);
411    }
412    files.sort_by_key(|(modified, _, _)| *modified);
413    let mut deleted = 0;
414    for (_, size, path) in files {
415        if total <= max_bytes {
416            break;
417        }
418        let Ok(canonical) = canonical_log_path(&path) else {
419            continue;
420        };
421        if protected.as_ref().is_some_and(|p| *p == canonical) {
422            continue;
423        }
424        // Serialize deletion with rotation/reconfiguration only for --log-file.
425        // A previous cleaner generation must not unlink the newly active output.
426        let output = process_log
427            .as_ref()
428            .map(|_| OUTPUT.lock().unwrap_or_else(PoisonError::into_inner));
429        if output.as_ref().is_some_and(|output| match &**output {
430            Output::ProcessFile(file) | Output::File(file) => {
431                canonical_log_path(&file.path).is_ok_and(|active| active == canonical)
432            }
433            Output::Stdout => false,
434        }) {
435            continue;
436        }
437        let removed = std::fs::remove_file(&path);
438        drop(output);
439        if let Err(error) = removed {
440            let name = path.file_name().unwrap_or_default().to_string_lossy();
441            tracing::warn!("logging: failed to remove old log file: {name}: {error}");
442            continue;
443        }
444        total -= size;
445        deleted += 1;
446    }
447    Ok(deleted)
448}
449
450/// lumberjack.Logger with Go's settings, except the CLI process output can cap
451/// its rotations synchronously without starting another cleaner timer.
452pub(crate) struct RotatingFile {
453    path: PathBuf,
454    file: Option<File>,
455    size: u64,
456    rotation_budget: Option<u64>,
457}
458
459impl RotatingFile {
460    #[cfg(test)]
461    fn new(path: PathBuf) -> Self {
462        Self {
463            path,
464            file: None,
465            size: 0,
466            rotation_budget: None,
467        }
468    }
469
470    fn write(&mut self, p: &[u8], max: u64) -> io::Result<usize> {
471        let len = p.len() as u64;
472        if len > max {
473            return Err(io::Error::other(format!(
474                "write length {len} exceeds maximum file size {max}"
475            )));
476        }
477        if self.file.is_none() {
478            self.open_existing_or_new(len, max)?;
479        }
480        if self.size + len > max {
481            self.rotate()?;
482        }
483        let file = self.file.as_mut().ok_or_else(|| io::Error::other("log file closed"))?;
484        let n = file.write(p)?;
485        self.size += n as u64;
486        Ok(n)
487    }
488
489    fn open_existing_or_new(&mut self, len: u64, max: u64) -> io::Result<()> {
490        let info = match std::fs::metadata(&self.path) {
491            Ok(info) => info,
492            Err(e) if e.kind() == io::ErrorKind::NotFound => return self.open_new(),
493            Err(e) => return Err(io::Error::new(e.kind(), format!("error getting log file info: {e}"))),
494        };
495        if info.len() + len >= max {
496            return self.rotate();
497        }
498        match OpenOptions::new().append(true).open(&self.path) {
499            Ok(file) => {
500                self.file = Some(file);
501                self.size = info.len();
502                if let Some(budget) = self.rotation_budget {
503                    prune_process_rotations(&self.path, budget);
504                }
505                Ok(())
506            }
507            // lumberjack: an existing file that cannot be opened is replaced.
508            Err(_) => self.open_new(),
509        }
510    }
511
512    fn rotate(&mut self) -> io::Result<()> {
513        self.file = None;
514        self.open_new()
515    }
516
517    fn open_new(&mut self) -> io::Result<()> {
518        let dir = self.path.parent().unwrap_or(Path::new("."));
519        create_dir(dir)
520            .map_err(|e| io::Error::new(e.kind(), format!("can't make directories for new logfile: {e}")))?;
521        let previous = std::fs::metadata(&self.path).ok();
522        if previous.is_some() {
523            std::fs::rename(&self.path, backup_name(&self.path, chrono::Utc::now()))
524                .map_err(|e| io::Error::new(e.kind(), format!("can't rename log file: {e}")))?;
525        }
526        let mut options = OpenOptions::new();
527        options.create(true).write(true).truncate(true);
528        // lumberjack: 0600, or the mode of the file being rotated. Windows has no
529        // mode bits (Go's mode is ignored there).
530        #[cfg(unix)]
531        {
532            use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
533            options.mode(previous.map_or(0o600, |info| info.permissions().mode() & 0o7777));
534        }
535        let file = options
536            .open(&self.path)
537            .map_err(|e| io::Error::new(e.kind(), format!("can't open new logfile: {e}")))?;
538        self.file = Some(file);
539        self.size = 0;
540        if let Some(budget) = self.rotation_budget {
541            prune_process_rotations(&self.path, budget);
542        }
543        Ok(())
544    }
545}
546
547fn is_process_rotation(name: &str, base: &str) -> bool {
548    let (stem, ext) = base.rfind('.').map_or((base, ""), |i| (&base[..i], &base[i..]));
549    name.strip_prefix(stem)
550        .and_then(|v| v.strip_prefix('-'))
551        .and_then(|v| v.strip_suffix(ext))
552        .is_some_and(|timestamp| chrono::NaiveDateTime::parse_from_str(timestamp, "%Y-%m-%dT%H-%M-%S%.3f").is_ok())
553}
554
555/// Runs only at startup/reconfiguration and rotation, under the writer's lock.
556/// Excluding the base filename keeps the open file out of both deletion and budget.
557/// Cleanup is best-effort: it must not fail log writes or log reconfiguration.
558fn prune_process_rotations(path: &Path, max_bytes: u64) {
559    let Ok(entries) = std::fs::read_dir(path.parent().unwrap_or(Path::new("."))) else {
560        return;
561    };
562    let base = path.file_name().unwrap_or_default().to_string_lossy();
563    let mut files = Vec::new();
564    let mut total = 0u64;
565    for entry in entries.flatten() {
566        if !is_process_rotation(&entry.file_name().to_string_lossy(), &base) {
567            continue;
568        }
569        let Ok(info) = entry.metadata() else { continue };
570        if !info.is_file() {
571            continue;
572        }
573        total += info.len();
574        files.push((info.modified().ok(), info.len(), entry.path()));
575    }
576    files.sort_by_key(|(modified, _, _)| *modified);
577    for (_, size, path) in files {
578        if total <= max_bytes {
579            break;
580        }
581        match std::fs::remove_file(path) {
582            Ok(()) => total -= size,
583            Err(e) if e.kind() == io::ErrorKind::NotFound => total -= size,
584            Err(_) => continue,
585        }
586    }
587}
588
589/// `os.MkdirAll(dir, 0755)`.
590fn create_dir(dir: &Path) -> io::Result<()> {
591    let mut builder = std::fs::DirBuilder::new();
592    builder.recursive(true);
593    #[cfg(unix)]
594    std::os::unix::fs::DirBuilderExt::mode(&mut builder, 0o755);
595    builder.create(dir)
596}
597
598/// lumberjack `backupName`: `main.log` -> `main-2006-01-02T15-04-05.000.log`.
599fn backup_name(path: &Path, now: chrono::DateTime<chrono::Utc>) -> PathBuf {
600    let name = path.file_name().unwrap_or_default().to_string_lossy();
601    let (stem, ext) = match name.rfind('.') {
602        Some(i) => (&name[..i], &name[i..]),
603        None => (&name[..], ""),
604    };
605    path.with_file_name(format!("{stem}-{}{ext}", now.format("%Y-%m-%dT%H-%M-%S%.3f")))
606}
607
608/// Writes each formatted event, whole, to the current output.
609struct GlobalWriter;
610
611struct OutputGuard(MutexGuard<'static, Output>);
612
613impl<'a> MakeWriter<'a> for GlobalWriter {
614    type Writer = OutputGuard;
615
616    fn make_writer(&'a self) -> OutputGuard {
617        OutputGuard(OUTPUT.lock().unwrap_or_else(PoisonError::into_inner))
618    }
619}
620
621impl Write for OutputGuard {
622    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
623        // tracing writes each formatted event in one call.
624        let hooked = HOOK.lock().unwrap_or_else(PoisonError::into_inner).clone();
625        if let Some(hook) = &hooked {
626            hook.push(String::from_utf8_lossy(buf).trim_end_matches(['\n', '\r']).to_owned());
627        }
628        match &mut *self.0 {
629            Output::Stdout | Output::ProcessFile(_) if hooked.is_some() => Ok(buf.len()),
630            Output::Stdout => io::stdout().write(buf),
631            Output::ProcessFile(file) | Output::File(file) => file.write(buf, MAX_FILE_SIZE),
632        }
633    }
634
635    fn flush(&mut self) -> io::Result<()> {
636        match &mut *self.0 {
637            Output::Stdout => io::stdout().flush(),
638            Output::ProcessFile(file) | Output::File(file) => file.file.as_mut().map_or(Ok(()), Write::flush),
639        }
640    }
641}
642
643/// Go `logFieldOrder`: the fields Go prints, in order.
644const FIELD_ORDER: &[&str] = &[
645    "provider",
646    "model",
647    "plugin_id",
648    "plugin_name",
649    "source_id",
650    "version",
651    "active_version",
652    "retired_version",
653    "overwritten",
654    "mode",
655    "budget",
656    "level",
657    "original_mode",
658    "original_value",
659    "min",
660    "max",
661    "clamped_to",
662    "error",
663    "credential",
664    "auth_id",
665    "connection",
666    "proxy_scheme",
667    "remote_transport",
668    "media_session_id",
669    "call_id",
670    "peer",
671    "state",
672    "reason",
673];
674
675/// Go `quotedLogFields`: string values printed with `strconv.Quote`.
676const QUOTED_FIELDS: &[&str] = &[
677    "credential",
678    "auth_id",
679    "connection",
680    "proxy_scheme",
681    "remote_transport",
682    "media_session_id",
683    "call_id",
684    "peer",
685    "state",
686    "reason",
687];
688
689/// Go `LogFormatter`:
690/// `[2006-01-02 15:04:05] [reqid8ch] [info ] [file.rs:12] message key=value`.
691struct GoFormat;
692
693impl<S, N> FormatEvent<S, N> for GoFormat
694where
695    S: Subscriber + for<'a> LookupSpan<'a>,
696    N: for<'a> FormatFields<'a> + 'static,
697{
698    fn format_event(&self, _: &FmtContext<'_, S, N>, mut writer: Writer<'_>, event: &Event<'_>) -> std::fmt::Result {
699        let mut fields = Fields::default();
700        event.record(&mut fields);
701        let now = chrono::Local::now();
702        // ponytail: Go's hook drops a non-string request_id; Rust records every
703        // request_id value as text.
704        let request_id = fields.request_id.clone();
705        let level = event.metadata().level();
706        let line = format_line(
707            now.naive_local(),
708            level,
709            event.metadata().file().zip(event.metadata().line()),
710            fields,
711        );
712        fire_hooks(&Entry {
713            line: &line,
714            level: logrus_level(level),
715            time: now.fixed_offset(),
716            request_id: &request_id,
717        });
718        writer.write_str(&line)
719    }
720}
721
722#[derive(Default)]
723struct Fields {
724    message: String,
725    request_id: String,
726    /// Name, value and whether the value was a string, in recording order.
727    rest: Vec<(&'static str, String, bool)>,
728}
729
730impl Visit for Fields {
731    fn record_str(&mut self, field: &Field, value: &str) {
732        match field.name() {
733            "message" => self.message = value.to_owned(),
734            "request_id" => self.request_id = value.to_owned(),
735            name => self.rest.push((name, value.to_owned(), true)),
736        }
737    }
738
739    fn record_debug(&mut self, field: &Field, value: &dyn std::fmt::Debug) {
740        match field.name() {
741            "message" => self.message = format!("{value:?}"),
742            "request_id" => self.request_id = format!("{value:?}"),
743            name => self.rest.push((name, format!("{value:?}"), false)),
744        }
745    }
746}
747
748/// logrus `Level.String()`.
749fn logrus_level(level: &Level) -> &'static str {
750    match *level {
751        Level::ERROR => "error",
752        Level::WARN => "warning",
753        Level::INFO => "info",
754        Level::DEBUG => "debug",
755        Level::TRACE => "trace",
756    }
757}
758
759fn format_line(now: chrono::NaiveDateTime, level: &Level, caller: Option<(&str, u32)>, fields: Fields) -> String {
760    let request_id = match fields.request_id.trim() {
761        "" => "--------",
762        // Go `ShortRequestID`: the trailing 8 bytes.
763        id => id.get(id.len().saturating_sub(8)..).unwrap_or(id),
764    };
765    let level = match *level {
766        Level::ERROR => "error",
767        Level::WARN => "warn",
768        Level::INFO => "info",
769        Level::DEBUG => "debug",
770        Level::TRACE => "trace",
771    };
772    let mut line = format!("[{}] [{request_id}] [{level:<5}] ", now.format("%Y-%m-%d %H:%M:%S"));
773    if let Some((file, number)) = caller {
774        let base = file.rsplit(['/', '\\']).next().unwrap_or(file);
775        line.push_str(&format!("[{base}:{number}] "));
776    }
777    line.push_str(fields.message.trim_end_matches(['\r', '\n']));
778    let mut rest = fields.rest;
779    // ponytail: Go prints only the known fields; Rust call sites also log structured
780    // context Go writes into the message, so the other fields follow in call order.
781    rest.sort_by_key(|(name, _, _)| FIELD_ORDER.iter().position(|k| k == name).unwrap_or(FIELD_ORDER.len()));
782    for (name, value, is_str) in rest {
783        if is_str && QUOTED_FIELDS.contains(&name) {
784            // ponytail: Rust's string escaping stands in for strconv.Quote; they agree
785            // on printable ASCII, quotes, backslashes and \n \r \t.
786            line.push_str(&format!(" {name}={value:?}"));
787        } else {
788            line.push_str(&format!(" {name}={value}"));
789        }
790    }
791    line.push('\n');
792    line
793}
794
795#[cfg(test)]
796mod tests {
797    use super::*;
798
799    /// A fresh directory under the system temp dir, unique per test and process.
800    fn scratch(name: &str) -> PathBuf {
801        let dir = std::env::temp_dir().join(format!("cpa-logging-{name}-{}", std::process::id()));
802        let _ = std::fs::remove_dir_all(&dir);
803        std::fs::create_dir_all(&dir).unwrap();
804        dir
805    }
806
807    fn fields(message: &str, request_id: &str, rest: &[(&'static str, &str, bool)]) -> Fields {
808        Fields {
809            message: message.into(),
810            request_id: request_id.into(),
811            rest: rest.iter().map(|(n, v, s)| (*n, (*v).to_owned(), *s)).collect(),
812        }
813    }
814
815    #[test]
816    fn lines_follow_go_log_formatter() {
817        let now = chrono::NaiveDate::from_ymd_opt(2025, 12, 23)
818            .unwrap()
819            .and_hms_opt(20, 14, 4)
820            .unwrap();
821        // Go: [2025-12-23 20:14:04] [debug] [manager.go:524] ... with request id and
822        // padded level; warning prints as "warn ".
823        assert_eq!(
824            format_line(
825                now,
826                &Level::WARN,
827                Some(("crates/x/src/manager.rs", 524)),
828                fields("Use API key\r\n", "0198-aaaa-a1b2c3d4", &[])
829            ),
830            "[2025-12-23 20:14:04] [a1b2c3d4] [warn ] [manager.rs:524] Use API key\n"
831        );
832        // Known fields in Go's order (model before auth_id; quoted auth_id), others after.
833        assert_eq!(
834            format_line(
835                now,
836                &Level::INFO,
837                None,
838                fields(
839                    "done",
840                    "",
841                    &[
842                        ("credentials", "3", false),
843                        ("auth_id", "a b", true),
844                        ("model", "m", true)
845                    ]
846                )
847            ),
848            "[2025-12-23 20:14:04] [--------] [info ] done model=m auth_id=\"a b\" credentials=3\n"
849        );
850    }
851
852    #[test]
853    fn rotating_file_follows_lumberjack() {
854        let dir = scratch("rotate");
855        let path = dir.join("logs").join(MAIN_LOG);
856        let mut file = RotatingFile::new(path.clone());
857        // New file: directory created, mode 0600.
858        assert_eq!(file.write(b"12345", 10).unwrap(), 5);
859        #[cfg(unix)]
860        {
861            use std::os::unix::fs::PermissionsExt;
862            assert_eq!(std::fs::metadata(&path).unwrap().permissions().mode() & 0o777, 0o600);
863        }
864        // size + len == max fits; one more byte rotates.
865        file.write(b"67890", 10).unwrap();
866        assert_eq!(std::fs::read(&path).unwrap(), b"1234567890");
867        file.write(b"x", 10).unwrap();
868        assert_eq!(std::fs::read(&path).unwrap(), b"x");
869        let rotated: Vec<String> = std::fs::read_dir(path.parent().unwrap())
870            .unwrap()
871            .map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
872            .filter(|n| n != MAIN_LOG)
873            .collect();
874        assert_eq!(rotated.len(), 1);
875        let name = &rotated[0];
876        assert!(
877            name.starts_with("main-")
878                && name.ends_with(".log")
879                && name.len() == "main-2006-01-02T15-04-05.000.log".len(),
880            "{name}"
881        );
882        assert_eq!(std::fs::read(path.with_file_name(name)).unwrap(), b"1234567890");
883        // Oversized writes fail without touching the file.
884        assert!(file.write(b"0123456789ab", 10).is_err());
885        // Reopening: an existing file where size + len >= max rotates first.
886        let mut reopened = RotatingFile::new(path.clone());
887        std::fs::write(&path, b"123456789").unwrap();
888        reopened.write(b"z", 10).unwrap();
889        assert_eq!(std::fs::read(&path).unwrap(), b"z");
890        // Below the limit it appends.
891        let mut appended = RotatingFile::new(path.clone());
892        appended.write(b"w", 10).unwrap();
893        assert_eq!(std::fs::read(&path).unwrap(), b"zw");
894    }
895
896    #[test]
897    fn process_rotations_prune_on_open_and_rotation_without_touching_active_or_other_logs() {
898        let dir = scratch("process-rotation-default");
899        create_dir(&dir).unwrap();
900        let path = dir.join("process.txt");
901        let older = dir.join("process-2001-09-09T01-46-40.000.txt");
902        let newer = dir.join("process-2001-09-09T01-46-41.000.txt");
903        for (file, contents, seconds) in [(&older, "123", 1_000_000_000), (&newer, "45", 1_000_000_001)] {
904            std::fs::write(file, contents).unwrap();
905            File::options()
906                .write(true)
907                .open(file)
908                .unwrap()
909                .set_modified(std::time::UNIX_EPOCH + Duration::from_secs(seconds))
910                .unwrap();
911        }
912        let unrelated = dir.join("main.log");
913        let malformed = dir.join("process-not-a-timestamp.txt");
914        std::fs::write(&unrelated, b"unlimited Go logs").unwrap();
915        std::fs::write(&malformed, b"not a rotation").unwrap();
916        std::fs::write(&path, b"a").unwrap();
917        let mut file = RotatingFile::new(path.clone());
918        file.rotation_budget = Some(4);
919        file.write(b"b", 10).unwrap();
920        assert!(
921            !older.exists() && newer.exists(),
922            "startup must remove oldest rotations only"
923        );
924        // Filesystem mtimes can share a tick; give rotations distinct ages.
925        File::options()
926            .write(true)
927            .open(&path)
928            .unwrap()
929            .set_modified(std::time::UNIX_EPOCH + Duration::from_secs(1_000_000_002))
930            .unwrap();
931        file.write(b"123456789", 10).unwrap();
932        assert!(newer.exists(), "two 2-byte rotations fit the 4-byte budget exactly");
933        assert_eq!(
934            std::fs::read(&path).unwrap(),
935            b"123456789",
936            "active file can exceed rotation budget"
937        );
938        File::options()
939            .write(true)
940            .open(&path)
941            .unwrap()
942            .set_modified(std::time::UNIX_EPOCH + Duration::from_secs(1_000_000_003))
943            .unwrap();
944        // Distinct millisecond backup names, as in production's 10 MiB rotations.
945        std::thread::sleep(Duration::from_millis(2));
946        file.write(b"xy", 10).unwrap();
947        assert!(!newer.exists());
948        assert_eq!(
949            std::fs::read(&path).unwrap(),
950            b"xy",
951            "the active writer must stay linked"
952        );
953        assert!(
954            std::fs::read_dir(&dir)
955                .unwrap()
956                .all(|entry| { !is_process_rotation(&entry.unwrap().file_name().to_string_lossy(), "process.txt") })
957        );
958        assert_eq!(std::fs::read(&unrelated).unwrap(), b"unlimited Go logs");
959        assert_eq!(std::fs::read(&malformed).unwrap(), b"not a rotation");
960        drop(file);
961        std::fs::remove_dir_all(dir).unwrap();
962    }
963
964    #[cfg(unix)]
965    #[test]
966    fn process_pruning_does_not_drop_a_line_when_directory_scanning_fails() {
967        use std::os::unix::fs::PermissionsExt;
968
969        let dir = scratch("process-prune-denied");
970        let path = dir.join("process.log");
971        let mut file = RotatingFile::new(path.clone());
972        file.rotation_budget = Some(4);
973        file.write(b"a", 1).unwrap();
974        // Write + search permits rotation but not enumeration by the cleaner.
975        std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o300)).unwrap();
976        let denied = std::fs::read_dir(&dir).is_err();
977        let result = file.write(b"b", 1);
978        std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o700)).unwrap();
979        // Root can bypass mode bits; ordinary users exercise the failure path.
980        if denied {
981            result.unwrap();
982            assert_eq!(std::fs::read(&path).unwrap(), b"b");
983        }
984        drop(file);
985        std::fs::remove_dir_all(dir).unwrap();
986    }
987
988    #[test]
989    fn backup_names_use_utc_milliseconds() {
990        let at = chrono::DateTime::parse_from_rfc3339("2026-10-03T04:05:06.789Z")
991            .unwrap()
992            .to_utc();
993        assert_eq!(
994            backup_name(Path::new("/l/main.log"), at),
995            PathBuf::from("/l/main-2026-10-03T04-05-06.789.log")
996        );
997    }
998
999    #[test]
1000    fn cleaner_removes_oldest_logs_but_keeps_main_log() {
1001        let dir = scratch("clean");
1002        let dir = dir.as_path();
1003        let at = |secs: u64| std::time::UNIX_EPOCH + Duration::from_secs(1_700_000_000 + secs);
1004        let put = |name: &str, len: usize, secs: u64| {
1005            let path = dir.join(name);
1006            std::fs::write(&path, vec![b'x'; len]).unwrap();
1007            File::options()
1008                .write(true)
1009                .open(&path)
1010                .unwrap()
1011                .set_modified(at(secs))
1012                .unwrap();
1013        };
1014        put(MAIN_LOG, 400, 0); // oldest, but protected
1015        put("main-2026-01-01T00-00-00.000.log", 300, 1);
1016        put("error-a.log.gz", 300, 2);
1017        put("v1-responses-b.LOG", 300, 3);
1018        put("notes.txt", 5000, 0); // not a log
1019        // Total 1300 > 800: drop the two oldest unprotected logs.
1020        let protected = dir.join(MAIN_LOG);
1021        assert_eq!(enforce_size_limit(dir, 800, Some(&protected), None).unwrap(), 2);
1022        let mut left: Vec<String> = std::fs::read_dir(dir)
1023            .unwrap()
1024            .map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
1025            .collect();
1026        left.sort();
1027        assert_eq!(left, ["main.log", "notes.txt", "v1-responses-b.LOG"]);
1028        // Within the limit nothing happens; a missing directory is not an error.
1029        assert_eq!(enforce_size_limit(dir, 800, Some(&protected), None).unwrap(), 0);
1030        assert_eq!(enforce_size_limit(&dir.join("none"), 1, None, None).unwrap(), 0);
1031    }
1032
1033    #[test]
1034    fn cleaner_shares_budget_with_only_the_selected_process_log_family() {
1035        let root = scratch("process-budget");
1036        let logs = root.join("logs");
1037        create_dir(&logs).unwrap();
1038        let cli = root.join("process output.txt");
1039        let put = |path: &Path, size: usize, seconds: u64| {
1040            std::fs::write(path, vec![b'x'; size]).unwrap();
1041            File::options()
1042                .write(true)
1043                .open(path)
1044                .unwrap()
1045                .set_modified(std::time::UNIX_EPOCH + Duration::from_secs(1_700_000_000 + seconds))
1046                .unwrap();
1047        };
1048        put(&cli, 200, 0); // oldest, protected despite exceeding a smaller budget
1049        let old_cli = root.join("process output-2026-01-01T00-00-00.001.txt");
1050        let old_main = logs.join("main-2026-01-02T00-00-00.002.log");
1051        let new_cli = root.join("process output-2026-01-03T00-00-00.003.txt");
1052        put(&old_cli, 300, 1);
1053        put(&old_main, 300, 2);
1054        put(&logs.join(MAIN_LOG), 400, 3);
1055        put(&new_cli, 200, 4);
1056        let unrelated = root.join("unrelated.log");
1057        let near_match = root.join("process output-not-a-timestamp.txt");
1058        put(&unrelated, 5000, 0);
1059        put(&near_match, 5000, 0);
1060        // Combined 1400 > 800, though each directory separately fits 800.
1061        assert_eq!(enforce_size_limit(&logs, 800, Some(&cli), Some(&cli)).unwrap(), 2);
1062        assert!(!old_cli.exists() && !old_main.exists());
1063        assert!(new_cli.exists() && logs.join(MAIN_LOG).exists() && cli.exists());
1064        assert!(unrelated.exists() && near_match.exists());
1065        assert_eq!(enforce_size_limit(&logs, 800, Some(&cli), Some(&cli)).unwrap(), 0);
1066        // A missing normal log directory must not disable external rotations.
1067        assert_eq!(
1068            enforce_size_limit(&root.join("missing"), 200, Some(&cli), Some(&cli)).unwrap(),
1069            1
1070        );
1071        assert!(cli.exists() && !new_cli.exists());
1072        let _ = std::fs::remove_dir_all(root);
1073    }
1074
1075    /// `--log-file` stands in for stdout only: `logging-to-file: true` still writes
1076    /// `main.log` with the directory cleaner, and switching it off goes back to the
1077    /// CLI file while the cleaner keeps its budget. The only test that touches the
1078    /// process-wide output.
1079    #[test]
1080    fn log_file_replaces_only_stdout() {
1081        let dir = scratch("cli-file");
1082        let logs = dir.join("logs");
1083        let cli = dir.join("process output.txt");
1084        set_log_file(cli.clone()).unwrap();
1085        let emit = |line: &str| GlobalWriter.make_writer().write_all(line.as_bytes()).unwrap();
1086        let read = |path: &Path| std::fs::read_to_string(path).unwrap_or_default();
1087        // An old rotation over the 1 MiB budget, older than anything written here.
1088        let stale = |name: &str| {
1089            let path = logs.join(name);
1090            std::fs::write(&path, vec![b'x'; 2 * 1024 * 1024]).unwrap();
1091            File::options()
1092                .write(true)
1093                .open(&path)
1094                .unwrap()
1095                .set_modified(std::time::UNIX_EPOCH + Duration::from_secs(1_000_000_000))
1096                .unwrap();
1097            path
1098        };
1099        let gone = |path: &Path| {
1100            let deadline = std::time::Instant::now() + Duration::from_secs(10);
1101            while path.exists() && std::time::Instant::now() < deadline {
1102                std::thread::sleep(Duration::from_millis(20));
1103            }
1104            !path.exists()
1105        };
1106        emit("before-config\n");
1107        assert_eq!(read(&cli), "before-config\n");
1108
1109        configure_output(
1110            &logs,
1111            Applied {
1112                logging_to_file: false,
1113                max_total_mb: 0,
1114                debug: false,
1115            },
1116        )
1117        .unwrap();
1118        assert!(matches!(&*OUTPUT.lock().unwrap(), Output::ProcessFile(file)
1119            if file.rotation_budget == Some(32 * 1024 * 1024)));
1120        create_dir(&logs).unwrap();
1121        let old = stale("main-2001-09-09T01-46-40.000.log");
1122        let on = Applied {
1123            logging_to_file: true,
1124            max_total_mb: 1,
1125            debug: false,
1126        };
1127        configure_output(&logs, on).unwrap();
1128        emit("to-main\n");
1129        assert_eq!(read(&logs.join(MAIN_LOG)), "to-main\n");
1130        assert_eq!(read(&cli), "before-config\n");
1131        assert!(gone(&old), "the cleaner runs with logging-to-file and --log-file");
1132
1133        let old = stale("error-old.log");
1134        configure_output(
1135            &logs,
1136            Applied {
1137                logging_to_file: false,
1138                ..on
1139            },
1140        )
1141        .unwrap();
1142        assert!(
1143            matches!(&*OUTPUT.lock().unwrap(), Output::ProcessFile(file) if file.rotation_budget.is_none()),
1144            "a configured positive budget must replace the rotation-only default"
1145        );
1146        emit("to-cli\n");
1147        assert_eq!(read(&cli), "before-config\nto-cli\n");
1148        assert_eq!(read(&logs.join(MAIN_LOG)), "to-main\n");
1149        assert!(gone(&old), "the cleaner keeps running with only --log-file");
1150
1151        // Even when the active CLI file alone exceeds the budget, keep it linked.
1152        let large = "x".repeat(2 * 1024 * 1024);
1153        emit(&large);
1154        // Delete the CLI file first if configure_output forgets its protected path.
1155        File::options()
1156            .write(true)
1157            .open(&cli)
1158            .unwrap()
1159            .set_modified(std::time::UNIX_EPOCH + Duration::from_secs(1_000_000_000))
1160            .unwrap();
1161        configure_output(
1162            &logs,
1163            Applied {
1164                logging_to_file: false,
1165                ..on
1166            },
1167        )
1168        .unwrap();
1169        assert!(
1170            gone(&logs.join(MAIN_LOG)),
1171            "the active cleaner must run with the CLI file protected"
1172        );
1173        assert!(cli.exists(), "configure_output must protect the active process log");
1174        // A relative/dotted directory spelling must match the absolute protected path.
1175        enforce_size_limit(
1176            &logs.join("."),
1177            1,
1178            LOG_FILE.get().map(PathBuf::as_path),
1179            LOG_FILE.get().map(PathBuf::as_path),
1180        )
1181        .unwrap();
1182        assert!(cli.exists(), "the cleaner must never unlink the active process log");
1183        emit("still-linked\n");
1184        assert_eq!(read(&cli), format!("before-config\nto-cli\n{large}still-linked\n"));
1185
1186        // No stale protected path: even a previous generation must respect the
1187        // output currently open, while removing its rotations outside logs/.
1188        let old = backup_name(&cli, chrono::Utc::now());
1189        std::fs::write(&old, b"rotation").unwrap();
1190        enforce_size_limit(&logs, 1, Some(&logs.join(MAIN_LOG)), Some(&cli)).unwrap();
1191        assert!(cli.exists() && !old.exists());
1192        emit("after-clean\n");
1193        assert!(read(&cli).ends_with("after-clean\n"));
1194
1195        #[cfg(unix)]
1196        {
1197            let alias = dir.join("alias");
1198            std::os::unix::fs::symlink(&dir, &alias).unwrap();
1199            enforce_size_limit(
1200                &logs,
1201                1,
1202                Some(&logs.join(MAIN_LOG)),
1203                Some(&alias.join("process output.txt")),
1204            )
1205            .unwrap();
1206            assert!(
1207                cli.exists(),
1208                "directory aliases must not hide the file open for writing"
1209            );
1210            emit("after-alias-clean\n");
1211            assert!(read(&cli).ends_with("after-alias-clean\n"));
1212            std::fs::remove_file(alias).unwrap();
1213        }
1214
1215        // Stop the asynchronous cleaner before checking exact deletion counts.
1216        configure_output(&logs, Applied { max_total_mb: 0, ..on }).unwrap();
1217        let inside = logs.join("process.log");
1218        let spelling = logs.join("..").join("logs").join("process.log");
1219        *OUTPUT.lock().unwrap() = Output::ProcessFile(RotatingFile::new(spelling.clone()));
1220        emit(&"a".repeat(600));
1221        let older = logs.join("error-dedupe.log");
1222        let rotation = backup_name(&inside, chrono::Utc::now());
1223        std::fs::write(&older, vec![b'b'; 400]).unwrap();
1224        std::fs::write(&rotation, vec![b'c'; 200]).unwrap();
1225        for (path, seconds) in [
1226            (&inside, 1_000_000_000),
1227            (&older, 1_000_000_001),
1228            (&rotation, 1_000_000_002),
1229        ] {
1230            File::options()
1231                .write(true)
1232                .open(path)
1233                .unwrap()
1234                .set_modified(std::time::UNIX_EPOCH + Duration::from_secs(seconds))
1235                .unwrap();
1236        }
1237        // 1,200 bytes, not 2,000: the alias must not count the process family twice.
1238        assert_eq!(
1239            enforce_size_limit(&logs, 1_000, Some(&spelling), Some(&spelling)).unwrap(),
1240            1
1241        );
1242        assert!(inside.exists() && rotation.exists() && !older.exists());
1243        assert_eq!(
1244            enforce_size_limit(&logs, 1, Some(&logs.join(MAIN_LOG)), Some(&spelling)).unwrap(),
1245            1
1246        );
1247        emit("still-linked-inside\n");
1248        assert!(read(&inside).ends_with("still-linked-inside\n"));
1249        #[cfg(unix)]
1250        {
1251            let alias = dir.join("alias-logs");
1252            std::os::unix::fs::symlink(&logs, &alias).unwrap();
1253            assert_eq!(
1254                enforce_size_limit(&alias, 1, Some(&alias.join("process.log")), Some(&spelling)).unwrap(),
1255                0
1256            );
1257            std::fs::remove_file(alias).unwrap();
1258        }
1259        #[cfg(windows)]
1260        assert_eq!(
1261            enforce_size_limit(&logs, 1, Some(&logs.join("PROCESS.LOG")), Some(&spelling)).unwrap(),
1262            0
1263        );
1264
1265        // Restore stdout for the rest of the process.
1266        *OUTPUT.lock().unwrap() = Output::Stdout;
1267        let _ = std::fs::remove_dir_all(&dir);
1268    }
1269
1270    /// Hooks see the written line itself, logrus level names, the local time and
1271    /// the raw request ID; removed hooks see nothing more.
1272    #[test]
1273    fn hooks_see_each_written_line() {
1274        use std::sync::Arc;
1275        type Seen = Vec<(String, &'static str, String)>;
1276        let seen: Arc<Mutex<Seen>> = Arc::default();
1277        let id = add_hook({
1278            let seen = seen.clone();
1279            Arc::new(move |e: &Entry<'_>| {
1280                if e.line.contains("hook-marker") {
1281                    seen.lock()
1282                        .unwrap()
1283                        .push((e.line.to_owned(), e.level, e.request_id.to_owned()));
1284                }
1285            })
1286        });
1287        let written = Arc::new(Mutex::new(Vec::<u8>::new()));
1288        let writer = {
1289            let written = written.clone();
1290            move || SharedWriter(written.clone())
1291        };
1292        let subscriber = tracing_subscriber::registry().with(
1293            tracing_subscriber::fmt::layer()
1294                .event_format(GoFormat)
1295                .with_writer(writer),
1296        );
1297        tracing::subscriber::with_default(subscriber, || {
1298            tracing::warn!(request_id = " req-123456789 ", "hook-marker one");
1299            tracing::error!("hook-marker two");
1300            remove_hook(id);
1301            tracing::info!("hook-marker three");
1302        });
1303        let seen = seen.lock().unwrap().clone();
1304        let written = String::from_utf8(written.lock().unwrap().clone()).unwrap();
1305        let lines: Vec<String> = written.split_inclusive('\n').take(2).map(str::to_owned).collect();
1306        assert_eq!(
1307            seen,
1308            [
1309                (lines[0].clone(), "warning", " req-123456789 ".to_owned()),
1310                (lines[1].clone(), "error", String::new()),
1311            ]
1312        );
1313        assert!(lines[0].contains("] [23456789] [warn ] ["), "{written}");
1314    }
1315
1316    struct SharedWriter(std::sync::Arc<Mutex<Vec<u8>>>);
1317
1318    impl Write for SharedWriter {
1319        fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
1320            self.0.lock().unwrap().extend_from_slice(buf);
1321            Ok(buf.len())
1322        }
1323
1324        fn flush(&mut self) -> io::Result<()> {
1325            Ok(())
1326        }
1327    }
1328}