Skip to main content

Mountain/IPC/DevLog/
DebugOnce.rs

1
2//! Emit a tagged dev-log line exactly once per process, keyed
3//! on `Key`. Subsequent calls with the same key are dropped
4//! from the console; the file sink still records the first
5//! occurrence so post-mortems show every probe path that
6//! fired.
7
8use std::{
9	collections::HashSet,
10	sync::{Mutex, OnceLock},
11};
12
13use crate::IPC::DevLog::{IsEnabled, WriteToFile};
14
15static DEBUG_ONCE_KEYS:OnceLock<Mutex<HashSet<String>>> = OnceLock::new();
16
17fn DebugOnceKeys() -> &'static Mutex<HashSet<String>> { DEBUG_ONCE_KEYS.get_or_init(|| Mutex::new(HashSet::new())) }
18
19pub fn Fn(Tag:&str, Key:&str, Line:&str) {
20	if let Ok(mut Keys) = DebugOnceKeys().lock() {
21		if !Keys.insert(Key.to_string()) {
22			return;
23		}
24	}
25
26	if IsEnabled::Fn(Tag) || IsEnabled::Fn("all") {
27		let Formatted = format!("[DEV:{}] {}", Tag.to_uppercase(), Line);
28
29		eprintln!("{}", Formatted);
30
31		WriteToFile::Fn(&Formatted);
32	} else {
33		let Formatted = format!("[DEV:{}/once] {}", Tag.to_uppercase(), Line);
34
35		WriteToFile::Fn(&Formatted);
36	}
37}