blazon_core/
debug.rs

1//! Debug printer control for blazon.
2//!
3//! Provides a thread-safe atomic flag for debug logging via STDERR and a function
4//! to enable it programmatically (runs automatically if compiled in `cfg(test)`).
5
6use std::env;
7use std::sync::atomic::{AtomicBool, Ordering};
8
9/// Atomic flag indicating whether debug output is enabled.
10static DEBUG_ENABLED: AtomicBool = AtomicBool::new(false);
11
12/// Initialise the debug atomic from the `BLAZON_DEBUG` environment variable.
13///
14/// - Treats `"0"`, `"false"`, `"no"`, `"off"` as false.
15/// - Any other value is true.
16/// - If the variable is unset, defaults to true for tests, false otherwise.
17pub fn init_from_env() {
18    let enabled = match env::var("BLAZON_DEBUG") {
19        Ok(val) => {
20            let val = val.trim();
21            !(val == "0"
22                || val.eq_ignore_ascii_case("false")
23                || val.eq_ignore_ascii_case("no")
24                || val.eq_ignore_ascii_case("off"))
25        }
26        Err(_) => cfg!(test),
27    };
28    set_debug(enabled);
29}
30
31/// Enable or disable debug output programmatically.
32pub fn set_debug(enabled: bool) {
33    DEBUG_ENABLED.store(enabled, Ordering::Relaxed);
34}
35
36/// Check whether debug output is enabled.
37pub fn is_enabled() -> bool {
38    DEBUG_ENABLED.load(Ordering::Relaxed)
39}
40
41// /// Automatically enable debug output for tests, respecting the env var.
42// #[ctor::ctor]
43// fn init_debug() {
44//     init_from_env();
45// }