rustorch 0.6.29

Production-ready PyTorch-compatible deep learning library in Rust with special mathematical functions (gamma, Bessel, error functions), statistical distributions, Fourier transforms (FFT/RFFT), matrix decomposition (SVD/QR/LU/eigenvalue), automatic differentiation, neural networks, computer vision transforms, complete GPU acceleration (CUDA/Metal/OpenCL), SIMD optimizations, parallel processing, WebAssembly browser support, comprehensive distributed learning support, and performance validation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
//! RusTorch Debug & Logging Framework
//!
//! Comprehensive logging and debugging system with structured logging,
//! performance profiling, memory tracking, and advanced debug utilities.
//!
//! # Architecture
//! - Core Logger: Structured logging with multiple levels and outputs
//! - Debug Profiler: Performance monitoring and bottleneck detection  
//! - Memory Tracker: Allocation monitoring and leak detection
//! - Log Analyzer: Pattern recognition and alert generation
//!
//! # Usage Examples
//! ```rust
//! use rustorch::debug::{Logger, DebugFramework, LogLevel};
//!
//! // Initialize framework
//! let mut debug_framework = DebugFramework::new();
//!
//! // Structured logging
//! debug_framework.log(LogLevel::Info, "operation_complete",
//!     &[("duration_ms", "150"), ("tensors_processed", "1000")]);
//!
//! // Performance profiling
//! let _guard = debug_framework.profile("matrix_multiply");
//! // ... operation code ...
//! // Profile automatically captured on drop
//!
//! // Memory tracking
//! debug_framework.track_allocation("tensor_data", 1024 * 1024);
//! ```

use serde_json::{json, Value};
use std::collections::HashMap;
use std::fmt;
use std::fs::{File, OpenOptions};
use std::io::{BufWriter, Write};
use std::path::PathBuf;
use std::sync::{Arc, Mutex, RwLock};
use std::thread;
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};

use crate::error::{RusTorchError, RusTorchResult};

pub mod debug_utils;
pub mod log_analyzer;
pub mod logger;
pub mod memory_tracker;
pub mod profiler;

pub use debug_utils::{DebugUtils, StackTrace, SystemInfo};
pub use log_analyzer::{AlertRule, LogAnalyzer, LogPattern};
pub use logger::{LogEntry, LogLevel, LogOutput, Logger};
pub use memory_tracker::{AllocationInfo, MemoryReport, MemoryTracker};
pub use profiler::{DebugProfiler, PerformanceMetrics, ProfileEntry};

/// Unified Debug Framework
///
/// Central coordinator for all debugging and logging operations.
/// Integrates logging, profiling, memory tracking, and analysis.
#[derive(Debug)]
pub struct DebugFramework {
    /// Core logging system
    logger: Arc<Mutex<Logger>>,
    /// Performance profiler
    profiler: Arc<Mutex<DebugProfiler>>,
    /// Memory allocation tracker
    memory_tracker: Arc<Mutex<MemoryTracker>>,
    /// Log pattern analyzer
    log_analyzer: Arc<Mutex<LogAnalyzer>>,
    /// Framework configuration
    config: FrameworkConfig,
    /// Session metadata
    session_id: String,
    /// Framework start time
    start_time: Instant,
}

/// Framework Configuration
#[derive(Debug, Clone)]
pub struct FrameworkConfig {
    /// Logging configuration
    pub log_level: LogLevel,
    pub log_to_console: bool,
    pub log_to_file: bool,
    pub log_file_path: Option<PathBuf>,

    /// Profiling configuration
    pub enable_profiling: bool,
    pub profile_threshold_ms: u64,
    pub max_profile_entries: usize,

    /// Memory tracking configuration
    pub enable_memory_tracking: bool,
    pub memory_threshold_mb: usize,
    pub track_allocations: bool,

    /// Analysis configuration
    pub enable_log_analysis: bool,
    pub analysis_window_size: usize,
    pub alert_threshold: usize,
}

/// Debug Session Information
#[derive(Debug, Clone)]
pub struct DebugSession {
    pub session_id: String,
    pub start_time: SystemTime,
    pub duration: Duration,
    pub total_logs: usize,
    pub total_profiles: usize,
    pub memory_peak_mb: f64,
    pub alerts_triggered: usize,
}

impl Default for FrameworkConfig {
    fn default() -> Self {
        Self {
            log_level: LogLevel::Info,
            log_to_console: true,
            log_to_file: true,
            log_file_path: Some(PathBuf::from("rustorch_debug.log")),

            enable_profiling: true,
            profile_threshold_ms: 1,
            max_profile_entries: 10000,

            enable_memory_tracking: true,
            memory_threshold_mb: 1024,
            track_allocations: true,

            enable_log_analysis: true,
            analysis_window_size: 1000,
            alert_threshold: 10,
        }
    }
}

impl DebugFramework {
    /// Create new debug framework with default configuration
    pub fn new() -> Self {
        Self::with_config(FrameworkConfig::default())
    }

    /// Create debug framework with custom configuration
    pub fn with_config(config: FrameworkConfig) -> Self {
        let session_id = format!(
            "debug_session_{}",
            SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs()
        );

        let logger = Arc::new(Mutex::new(Logger::new(
            config.log_level,
            config.log_to_console,
            config.log_to_file,
        )));

        let profiler = Arc::new(Mutex::new(DebugProfiler::new(
            config.enable_profiling,
            config.max_profile_entries,
        )));

        let memory_tracker = Arc::new(Mutex::new(MemoryTracker::new(
            config.enable_memory_tracking,
            config.memory_threshold_mb,
        )));

        let log_analyzer = Arc::new(Mutex::new(LogAnalyzer::new(
            config.enable_log_analysis,
            config.analysis_window_size,
        )));

        Self {
            logger,
            profiler,
            memory_tracker,
            log_analyzer,
            config,
            session_id,
            start_time: Instant::now(),
        }
    }

    /// Log structured message with metadata
    pub fn log(
        &self,
        level: LogLevel,
        message: &str,
        metadata: &[(&str, &str)],
    ) -> RusTorchResult<()> {
        let mut logger = self.logger.lock().map_err(|_| RusTorchError::Debug {
            message: "Failed to acquire logger lock".to_string(),
        })?;

        let mut meta_map = HashMap::new();
        for (key, value) in metadata {
            meta_map.insert(key.to_string(), value.to_string());
        }
        meta_map.insert("session_id".to_string(), self.session_id.clone());

        logger.log(level, message, meta_map.clone())?;

        // Feed to log analyzer if enabled
        if self.config.enable_log_analysis {
            if let Ok(mut analyzer) = self.log_analyzer.lock() {
                let _ = analyzer.analyze_log_entry(level, message, &meta_map);
            }
        }

        Ok(())
    }

    /// Create profiling guard for automatic timing
    pub fn profile(&self, operation_name: &str) -> RusTorchResult<ProfileGuard> {
        if !self.config.enable_profiling {
            return Ok(ProfileGuard::disabled());
        }

        Ok(ProfileGuard::new(
            operation_name.to_string(),
            Arc::clone(&self.profiler),
            self.config.profile_threshold_ms,
        ))
    }

    /// Track memory allocation
    pub fn track_allocation(&self, component: &str, size_bytes: usize) -> RusTorchResult<()> {
        if !self.config.enable_memory_tracking {
            return Ok(());
        }

        let mut tracker = self
            .memory_tracker
            .lock()
            .map_err(|_| RusTorchError::Debug {
                message: "Failed to acquire memory tracker lock".to_string(),
            })?;

        tracker.track_allocation(component, size_bytes)?;

        // Check for memory threshold alerts
        let current_usage = tracker.get_current_usage_mb();
        if current_usage > self.config.memory_threshold_mb as f64 {
            self.log(
                LogLevel::Warning,
                "Memory threshold exceeded",
                &[
                    ("current_mb", &format!("{:.2}", current_usage)),
                    ("threshold_mb", &self.config.memory_threshold_mb.to_string()),
                    ("component", component),
                ],
            )?;
        }

        Ok(())
    }

    /// Track memory deallocation
    pub fn track_deallocation(&self, component: &str, size_bytes: usize) -> RusTorchResult<()> {
        if !self.config.enable_memory_tracking {
            return Ok(());
        }

        let mut tracker = self
            .memory_tracker
            .lock()
            .map_err(|_| RusTorchError::Debug {
                message: "Failed to acquire memory tracker lock".to_string(),
            })?;

        tracker.track_deallocation(component, size_bytes)
    }

    /// Get comprehensive debug report
    pub fn generate_debug_report(&self) -> RusTorchResult<DebugReport> {
        let session = self.get_session_info()?;

        let log_summary = self
            .logger
            .lock()
            .map_err(|_| RusTorchError::Debug {
                message: "Failed to acquire logger lock".to_string(),
            })?
            .get_summary();

        let performance_metrics = self
            .profiler
            .lock()
            .map_err(|_| RusTorchError::Debug {
                message: "Failed to acquire profiler lock".to_string(),
            })?
            .get_performance_metrics();

        let memory_report = self
            .memory_tracker
            .lock()
            .map_err(|_| RusTorchError::Debug {
                message: "Failed to acquire memory tracker lock".to_string(),
            })?
            .generate_memory_report()?;

        let analysis_summary = self
            .log_analyzer
            .lock()
            .map_err(|_| RusTorchError::Debug {
                message: "Failed to acquire log analyzer lock".to_string(),
            })?
            .get_analysis_summary();

        Ok(DebugReport {
            session,
            log_summary,
            performance_metrics,
            memory_report,
            analysis_summary,
            config: self.config.clone(),
        })
    }

    /// Get current session information
    pub fn get_session_info(&self) -> RusTorchResult<DebugSession> {
        let duration = self.start_time.elapsed();

        let total_logs = self
            .logger
            .lock()
            .map_err(|_| RusTorchError::Debug {
                message: "Failed to acquire logger lock".to_string(),
            })?
            .get_total_logs();

        let total_profiles = self
            .profiler
            .lock()
            .map_err(|_| RusTorchError::Debug {
                message: "Failed to acquire profiler lock".to_string(),
            })?
            .get_total_profiles();

        let memory_peak_mb = self
            .memory_tracker
            .lock()
            .map_err(|_| RusTorchError::Debug {
                message: "Failed to acquire memory tracker lock".to_string(),
            })?
            .get_peak_usage_mb();

        let alerts_triggered = self
            .log_analyzer
            .lock()
            .map_err(|_| RusTorchError::Debug {
                message: "Failed to acquire log analyzer lock".to_string(),
            })?
            .get_total_alerts();

        Ok(DebugSession {
            session_id: self.session_id.clone(),
            start_time: SystemTime::now() - duration,
            duration,
            total_logs,
            total_profiles,
            memory_peak_mb,
            alerts_triggered,
        })
    }

    /// Flush all pending data
    pub fn flush(&self) -> RusTorchResult<()> {
        // Flush logger
        self.logger
            .lock()
            .map_err(|_| RusTorchError::Debug {
                message: "Failed to acquire logger lock".to_string(),
            })?
            .flush()?;

        Ok(())
    }
}

/// RAII Profile Guard for automatic timing
pub struct ProfileGuard {
    operation_name: Option<String>,
    profiler: Option<Arc<Mutex<DebugProfiler>>>,
    start_time: Instant,
    threshold_ms: u64,
}

impl ProfileGuard {
    fn new(operation_name: String, profiler: Arc<Mutex<DebugProfiler>>, threshold_ms: u64) -> Self {
        Self {
            operation_name: Some(operation_name),
            profiler: Some(profiler),
            start_time: Instant::now(),
            threshold_ms,
        }
    }

    fn disabled() -> Self {
        Self {
            operation_name: None,
            profiler: None,
            start_time: Instant::now(),
            threshold_ms: 0,
        }
    }
}

impl Drop for ProfileGuard {
    fn drop(&mut self) {
        if let (Some(operation_name), Some(profiler)) = (&self.operation_name, &self.profiler) {
            let duration = self.start_time.elapsed();

            if duration.as_millis() as u64 >= self.threshold_ms {
                if let Ok(mut prof) = profiler.lock() {
                    let _ = prof.record_operation(operation_name, duration);
                }
            }
        }
    }
}

/// Comprehensive Debug Report
#[derive(Debug)]
pub struct DebugReport {
    pub session: DebugSession,
    pub log_summary: LogSummary,
    pub performance_metrics: PerformanceMetrics,
    pub memory_report: MemoryReport,
    pub analysis_summary: AnalysisSummary,
    pub config: FrameworkConfig,
}

/// Log Summary Statistics
#[derive(Debug, Clone)]
pub struct LogSummary {
    pub total_logs: usize,
    pub logs_by_level: HashMap<String, usize>,
    pub recent_errors: Vec<String>,
    pub log_rate_per_second: f64,
}

/// Analysis Summary
#[derive(Debug, Clone)]
pub struct AnalysisSummary {
    pub patterns_detected: usize,
    pub alerts_triggered: usize,
    pub most_common_errors: Vec<(String, usize)>,
    pub performance_bottlenecks: Vec<String>,
}

impl fmt::Display for DebugReport {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "🔧 RusTorch Debug Report")?;
        writeln!(f, "========================")?;
        writeln!(f, "Session ID: {}", self.session.session_id)?;
        writeln!(f, "Duration: {:.2}s", self.session.duration.as_secs_f64())?;
        writeln!(f, "Total Logs: {}", self.session.total_logs)?;
        writeln!(f, "Total Profiles: {}", self.session.total_profiles)?;
        writeln!(f, "Memory Peak: {:.2} MB", self.session.memory_peak_mb)?;
        writeln!(f, "Alerts: {}", self.session.alerts_triggered)?;
        writeln!(f)?;
        writeln!(f, "📊 Performance Summary:")?;
        writeln!(
            f,
            "  Average Operation Time: {:.2}ms",
            self.performance_metrics.average_duration_ms
        )?;
        writeln!(
            f,
            "  Slowest Operation: {}",
            self.performance_metrics.slowest_operation
        )?;
        writeln!(
            f,
            "  Operations > 100ms: {}",
            self.performance_metrics.slow_operations_count
        )?;
        writeln!(f)?;
        writeln!(f, "🧠 Memory Summary:")?;
        writeln!(
            f,
            "  Current Usage: {:.2} MB",
            self.memory_report.current_usage_mb
        )?;
        writeln!(
            f,
            "  Peak Usage: {:.2} MB",
            self.memory_report.peak_usage_mb
        )?;
        writeln!(
            f,
            "  Total Allocations: {}",
            self.memory_report.total_allocations
        )?;
        Ok(())
    }
}

// Convenience functions for quick debugging
impl DebugFramework {
    /// Quick error log
    pub fn error(&self, message: &str) -> RusTorchResult<()> {
        self.log(LogLevel::Error, message, &[])
    }

    /// Quick warning log
    pub fn warn(&self, message: &str) -> RusTorchResult<()> {
        self.log(LogLevel::Warning, message, &[])
    }

    /// Quick info log
    pub fn info(&self, message: &str) -> RusTorchResult<()> {
        self.log(LogLevel::Info, message, &[])
    }

    /// Quick debug log
    pub fn debug(&self, message: &str) -> RusTorchResult<()> {
        self.log(LogLevel::Debug, message, &[])
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::thread;
    use std::time::Duration as StdDuration;

    #[test]
    fn test_debug_framework_creation() {
        let framework = DebugFramework::new();
        assert!(framework.session_id.contains("debug_session_"));
    }

    #[test]
    fn test_logging_functionality() {
        let framework = DebugFramework::new();

        // Test different log levels
        assert!(framework.info("Test info message").is_ok());
        assert!(framework.warn("Test warning message").is_ok());
        assert!(framework.error("Test error message").is_ok());
        assert!(framework.debug("Test debug message").is_ok());

        // Test structured logging
        assert!(framework
            .log(
                LogLevel::Info,
                "Structured log",
                &[("key1", "value1"), ("key2", "value2"),]
            )
            .is_ok());
    }

    #[test]
    fn test_profiling_functionality() {
        let framework = DebugFramework::new();

        // Test profiling guard
        {
            let _guard = framework.profile("test_operation").unwrap();
            thread::sleep(StdDuration::from_millis(10));
        } // Guard drops here, recording the profile

        let report = framework.generate_debug_report().unwrap();
        assert!(report.performance_metrics.total_operations > 0);
    }

    #[test]
    fn test_memory_tracking() {
        let framework = DebugFramework::new();

        // Track allocations
        assert!(framework.track_allocation("test_component", 1024).is_ok());
        assert!(framework.track_allocation("test_component", 2048).is_ok());

        // Track deallocation
        assert!(framework.track_deallocation("test_component", 1024).is_ok());

        let report = framework.generate_debug_report().unwrap();
        assert!(report.memory_report.total_allocations > 0);
    }

    #[test]
    fn test_debug_report_generation() {
        let framework = DebugFramework::new();

        // Generate some activity
        let _ = framework.info("Test message");
        let _ = framework.track_allocation("test", 100);

        let report = framework.generate_debug_report();
        assert!(report.is_ok());

        let report = report.unwrap();
        assert!(!report.session.session_id.is_empty());
        assert!(report.session.total_logs > 0);
    }
}