pub mod config;
pub mod inspector;
pub mod metrics;
pub mod tracer;
pub use config::DebugConfig;
pub use inspector::Inspector;
pub use metrics::{FrameMetrics, PerformanceMetrics};
pub use tracer::{TraceEvent, TraceLevel, Tracer};
use std::sync::{Arc, Mutex};
#[derive(Clone)]
pub struct DebugContext {
tracer: Arc<Mutex<Tracer>>,
metrics: Arc<Mutex<PerformanceMetrics>>,
config: Arc<DebugConfig>,
}
impl DebugContext {
pub fn new() -> Self {
Self::with_config(DebugConfig::from_env())
}
pub fn with_config(config: DebugConfig) -> Self {
Self {
tracer: Arc::new(Mutex::new(Tracer::new(config.trace_level))),
metrics: Arc::new(Mutex::new(PerformanceMetrics::new())),
config: Arc::new(config),
}
}
pub fn is_enabled(&self) -> bool {
self.config.enabled
}
pub fn trace_level(&self) -> TraceLevel {
self.config.trace_level
}
pub fn trace_event(&self, event: TraceEvent) {
if self.is_enabled() {
if let Ok(mut tracer) = self.tracer.lock() {
tracer.trace(event);
}
}
}
pub fn record_frame(&self, metrics: FrameMetrics) {
if self.is_enabled() && self.config.collect_metrics {
if let Ok(mut perf) = self.metrics.lock() {
perf.record_frame(metrics);
}
}
}
pub fn get_metrics(&self) -> Option<PerformanceMetrics> {
if self.is_enabled() && self.config.collect_metrics {
self.metrics.lock().ok().map(|m| m.clone())
} else {
None
}
}
pub fn flush(&self) {
if let Ok(mut tracer) = self.tracer.lock() {
tracer.flush();
}
}
}
impl Default for DebugContext {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
}