spikard_core/
debug.rs

1//! Debug logging utilities for spikard-http
2//!
3//! This module provides debug logging that can be enabled via:
4//! - Building in debug mode (cfg(debug_assertions))
5//! - Setting SPIKARD_DEBUG=1 environment variable
6
7use std::sync::atomic::{AtomicBool, Ordering};
8
9static DEBUG_ENABLED: AtomicBool = AtomicBool::new(false);
10
11/// Initialize debug logging based on environment and build mode
12pub fn init() {
13    let enabled = cfg!(debug_assertions) || std::env::var("SPIKARD_DEBUG").is_ok() || std::env::var("DEBUG").is_ok();
14
15    eprintln!(
16        "[spikard-http::debug] init() called, cfg!(debug_assertions)={}, DEBUG={}, enabled={}",
17        cfg!(debug_assertions),
18        std::env::var("DEBUG").is_ok(),
19        enabled
20    );
21
22    DEBUG_ENABLED.store(enabled, Ordering::Relaxed);
23
24    if enabled {
25        eprintln!("[spikard-http] Debug logging enabled");
26    }
27}
28
29/// Check if debug logging is enabled
30#[inline]
31pub fn is_enabled() -> bool {
32    DEBUG_ENABLED.load(Ordering::Relaxed)
33}
34
35/// Log a debug message if debugging is enabled
36#[macro_export]
37macro_rules! debug_log {
38    ($($arg:tt)*) => {
39        if $crate::debug::is_enabled() {
40            eprintln!("[spikard-http] {}", format!($($arg)*));
41        }
42    };
43}
44
45/// Log a debug message with a specific module/component name
46#[macro_export]
47macro_rules! debug_log_module {
48    ($module:expr, $($arg:tt)*) => {
49        if $crate::debug::is_enabled() {
50            eprintln!("[spikard-http::{}] {}", $module, format!($($arg)*));
51        }
52    };
53}
54
55/// Log a debug value with pretty-printing
56#[macro_export]
57macro_rules! debug_log_value {
58    ($name:expr, $value:expr) => {
59        if $crate::debug::is_enabled() {
60            eprintln!("[spikard-http] {} = {:?}", $name, $value);
61        }
62    };
63}