Skip to main content

hojicha_core/debug/
mod.rs

1//! Debugging and developer ergonomics tools
2//!
3//! This module provides comprehensive debugging utilities for hojicha applications,
4//! including command tracing, message inspection, and performance metrics.
5
6pub mod config;
7pub mod inspector;
8pub mod metrics;
9pub mod tracer;
10
11pub use config::DebugConfig;
12pub use inspector::Inspector;
13pub use metrics::{FrameMetrics, PerformanceMetrics};
14pub use tracer::{TraceEvent, TraceLevel, Tracer};
15
16use std::sync::{Arc, Mutex};
17
18/// Global debug context that can be shared across the application
19#[derive(Clone)]
20pub struct DebugContext {
21    tracer: Arc<Mutex<Tracer>>,
22    metrics: Arc<Mutex<PerformanceMetrics>>,
23    config: Arc<DebugConfig>,
24}
25
26impl DebugContext {
27    /// Create a new debug context with default configuration
28    pub fn new() -> Self {
29        Self::with_config(DebugConfig::from_env())
30    }
31
32    /// Create a new debug context with specific configuration
33    pub fn with_config(config: DebugConfig) -> Self {
34        Self {
35            tracer: Arc::new(Mutex::new(Tracer::new(config.trace_level))),
36            metrics: Arc::new(Mutex::new(PerformanceMetrics::new())),
37            config: Arc::new(config),
38        }
39    }
40
41    /// Check if debugging is enabled
42    pub fn is_enabled(&self) -> bool {
43        self.config.enabled
44    }
45
46    /// Get the current trace level
47    pub fn trace_level(&self) -> TraceLevel {
48        self.config.trace_level
49    }
50
51    /// Trace an event
52    pub fn trace_event(&self, event: TraceEvent) {
53        if self.is_enabled() {
54            if let Ok(mut tracer) = self.tracer.lock() {
55                tracer.trace(event);
56            }
57        }
58    }
59
60    /// Record frame metrics
61    pub fn record_frame(&self, metrics: FrameMetrics) {
62        if self.is_enabled() && self.config.collect_metrics {
63            if let Ok(mut perf) = self.metrics.lock() {
64                perf.record_frame(metrics);
65            }
66        }
67    }
68
69    /// Get current performance metrics
70    pub fn get_metrics(&self) -> Option<PerformanceMetrics> {
71        if self.is_enabled() && self.config.collect_metrics {
72            self.metrics.lock().ok().map(|m| m.clone())
73        } else {
74            None
75        }
76    }
77
78    /// Flush all debug output
79    pub fn flush(&self) {
80        if let Ok(mut tracer) = self.tracer.lock() {
81            tracer.flush();
82        }
83    }
84}
85
86impl Default for DebugContext {
87    fn default() -> Self {
88        Self::new()
89    }
90}
91
92#[cfg(test)]
93mod tests {
94    // Tests removed - they were shallow instantiation tests
95}