Skip to main content

Mountain/IPC/DevLog/
DebugOnce.rs

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