chie-core 0.2.0

Core protocol logic for CHIE Protocol
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
//! Logging configuration with configurable verbosity.
//!
//! This module provides a flexible logging system with configurable verbosity levels
//! and filtering capabilities.
//!
//! # Features
//!
//! - Multiple verbosity levels (Error, Warn, Info, Debug, Trace)
//! - Module-level filtering
//! - Structured logging support
//! - Performance metrics logging
//! - Conditional compilation for zero-cost when disabled
//!
//! # Example
//!
//! ```
//! use chie_core::logging::{LogConfig, LogLevel, Logger};
//!
//! // Create a logger with Info level
//! let config = LogConfig {
//!     level: LogLevel::Info,
//!     include_timestamps: true,
//!     include_module_path: true,
//!     include_line_numbers: false,
//!     filter_modules: vec![],
//! };
//!
//! let logger = Logger::new(config);
//!
//! // Log messages at different levels
//! logger.info("chie_core::storage", "Storage initialized");
//! logger.debug("chie_core::cache", "Cache size: 1024 entries");
//! logger.error("chie_core::network", "Connection failed");
//! ```

use std::collections::HashSet;
use std::fmt;
use std::time::SystemTime;

/// Log verbosity levels.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum LogLevel {
    /// Critical errors only.
    Error,
    /// Warnings and errors.
    Warn,
    /// Informational messages, warnings, and errors.
    Info,
    /// Debug messages and above.
    Debug,
    /// All messages including trace.
    Trace,
}

impl LogLevel {
    /// Get the string representation of the log level.
    #[must_use]
    #[inline]
    pub const fn as_str(&self) -> &'static str {
        match self {
            Self::Error => "ERROR",
            Self::Warn => "WARN",
            Self::Info => "INFO",
            Self::Debug => "DEBUG",
            Self::Trace => "TRACE",
        }
    }

    /// Get a colored version of the log level (ANSI codes).
    #[must_use]
    #[inline]
    pub const fn colored(&self) -> &'static str {
        match self {
            Self::Error => "\x1b[31mERROR\x1b[0m", // Red
            Self::Warn => "\x1b[33mWARN\x1b[0m",   // Yellow
            Self::Info => "\x1b[32mINFO\x1b[0m",   // Green
            Self::Debug => "\x1b[36mDEBUG\x1b[0m", // Cyan
            Self::Trace => "\x1b[90mTRACE\x1b[0m", // Gray
        }
    }

    /// Check if this level should be logged given the configured level.
    #[must_use]
    #[inline]
    pub const fn should_log(&self, configured_level: &Self) -> bool {
        (*self as u8) <= (*configured_level as u8)
    }
}

impl fmt::Display for LogLevel {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// Logging configuration.
#[derive(Debug, Clone)]
pub struct LogConfig {
    /// Minimum log level to display.
    pub level: LogLevel,
    /// Include timestamps in log messages.
    pub include_timestamps: bool,
    /// Include module path in log messages.
    pub include_module_path: bool,
    /// Include line numbers in log messages.
    pub include_line_numbers: bool,
    /// Modules to filter (only log these modules if non-empty).
    pub filter_modules: Vec<String>,
}

impl Default for LogConfig {
    fn default() -> Self {
        Self {
            level: LogLevel::Info,
            include_timestamps: true,
            include_module_path: true,
            include_line_numbers: false,
            filter_modules: Vec::new(),
        }
    }
}

impl LogConfig {
    /// Create a new configuration with the specified level.
    #[must_use]
    #[inline]
    pub const fn new(level: LogLevel) -> Self {
        Self {
            level,
            include_timestamps: true,
            include_module_path: true,
            include_line_numbers: false,
            filter_modules: Vec::new(),
        }
    }

    /// Create a minimal configuration (no timestamps, no module paths).
    #[must_use]
    #[inline]
    pub const fn minimal(level: LogLevel) -> Self {
        Self {
            level,
            include_timestamps: false,
            include_module_path: false,
            include_line_numbers: false,
            filter_modules: Vec::new(),
        }
    }

    /// Create a verbose configuration (all metadata included).
    #[must_use]
    #[inline]
    pub const fn verbose(level: LogLevel) -> Self {
        Self {
            level,
            include_timestamps: true,
            include_module_path: true,
            include_line_numbers: true,
            filter_modules: Vec::new(),
        }
    }

    /// Add a module filter.
    #[must_use]
    pub fn with_module_filter(mut self, module: String) -> Self {
        self.filter_modules.push(module);
        self
    }
}

/// Logger instance with configuration.
pub struct Logger {
    config: LogConfig,
    filter_set: HashSet<String>,
    use_color: bool,
}

impl Logger {
    /// Create a new logger with the given configuration.
    #[must_use]
    pub fn new(config: LogConfig) -> Self {
        let filter_set: HashSet<String> = config.filter_modules.iter().cloned().collect();

        Self {
            config,
            filter_set,
            use_color: is_terminal(),
        }
    }

    /// Create a logger with default configuration.
    #[must_use]
    #[inline]
    pub fn default_config() -> Self {
        Self::new(LogConfig::default())
    }

    /// Check if a module should be logged.
    #[must_use]
    #[inline]
    fn should_log_module(&self, module: &str) -> bool {
        if self.filter_set.is_empty() {
            return true;
        }

        self.filter_set
            .iter()
            .any(|filter| module.starts_with(filter) || filter.starts_with(module))
    }

    /// Log a message at the specified level.
    pub fn log(&self, level: LogLevel, module: &str, message: &str, line: Option<u32>) {
        if !level.should_log(&self.config.level) {
            return;
        }

        if !self.should_log_module(module) {
            return;
        }

        let mut parts = Vec::new();

        // Timestamp
        if self.config.include_timestamps {
            let timestamp = format_timestamp();
            parts.push(timestamp);
        }

        // Level
        let level_str = if self.use_color {
            level.colored().to_string()
        } else {
            level.as_str().to_string()
        };
        parts.push(format!("[{}]", level_str));

        // Module path
        if self.config.include_module_path {
            parts.push(format!("[{}]", module));
        }

        // Line number
        if self.config.include_line_numbers {
            if let Some(line_num) = line {
                parts.push(format!("[L{}]", line_num));
            }
        }

        // Message
        parts.push(message.to_string());

        println!("{}", parts.join(" "));
    }

    /// Log an error message.
    #[inline]
    pub fn error(&self, module: &str, message: &str) {
        self.log(LogLevel::Error, module, message, None);
    }

    /// Log a warning message.
    #[inline]
    pub fn warn(&self, module: &str, message: &str) {
        self.log(LogLevel::Warn, module, message, None);
    }

    /// Log an info message.
    #[inline]
    pub fn info(&self, module: &str, message: &str) {
        self.log(LogLevel::Info, module, message, None);
    }

    /// Log a debug message.
    #[inline]
    pub fn debug(&self, module: &str, message: &str) {
        self.log(LogLevel::Debug, module, message, None);
    }

    /// Log a trace message.
    #[inline]
    pub fn trace(&self, module: &str, message: &str) {
        self.log(LogLevel::Trace, module, message, None);
    }

    /// Log an error message with line number.
    #[inline]
    pub fn error_at(&self, module: &str, message: &str, line: u32) {
        self.log(LogLevel::Error, module, message, Some(line));
    }

    /// Log a warning message with line number.
    #[inline]
    pub fn warn_at(&self, module: &str, message: &str, line: u32) {
        self.log(LogLevel::Warn, module, message, Some(line));
    }

    /// Log a structured message with key-value pairs.
    pub fn structured(
        &self,
        level: LogLevel,
        module: &str,
        message: &str,
        fields: &[(&str, &str)],
    ) {
        if !level.should_log(&self.config.level) {
            return;
        }

        if !self.should_log_module(module) {
            return;
        }

        let fields_str: Vec<String> = fields.iter().map(|(k, v)| format!("{}={}", k, v)).collect();

        let full_message = if fields_str.is_empty() {
            message.to_string()
        } else {
            format!("{} {}", message, fields_str.join(" "))
        };

        self.log(level, module, &full_message, None);
    }

    /// Log performance metrics.
    #[inline]
    pub fn perf(&self, module: &str, operation: &str, duration_ms: u64) {
        let message = format!("{} completed in {}ms", operation, duration_ms);
        self.structured(
            LogLevel::Debug,
            module,
            &message,
            &[
                ("operation", operation),
                ("duration_ms", &duration_ms.to_string()),
            ],
        );
    }

    /// Get the current log level.
    #[must_use]
    #[inline]
    pub const fn level(&self) -> LogLevel {
        self.config.level
    }

    /// Set the log level.
    pub fn set_level(&mut self, level: LogLevel) {
        self.config.level = level;
    }

    /// Enable or disable colored output.
    #[inline]
    pub fn set_color(&mut self, use_color: bool) {
        self.use_color = use_color;
    }
}

/// Format a timestamp for logging.
#[must_use]
fn format_timestamp() -> String {
    let now = SystemTime::now()
        .duration_since(SystemTime::UNIX_EPOCH)
        .unwrap_or_default();

    let secs = now.as_secs();
    let millis = now.subsec_millis();

    // Simple ISO-like format: HH:MM:SS.mmm
    let hours = (secs / 3600) % 24;
    let minutes = (secs / 60) % 60;
    let seconds = secs % 60;

    format!("{:02}:{:02}:{:02}.{:03}", hours, minutes, seconds, millis)
}

/// Check if stdout is a terminal (for color support).
#[must_use]
#[inline]
fn is_terminal() -> bool {
    // Simple heuristic: check if TERM environment variable is set
    std::env::var("TERM").is_ok()
}

/// Macro for logging with automatic module path.
#[macro_export]
macro_rules! log_error {
    ($logger:expr, $($arg:tt)*) => {
        $logger.error(module_path!(), &format!($($arg)*))
    };
}

/// Macro for logging warnings with automatic module path.
#[macro_export]
macro_rules! log_warn {
    ($logger:expr, $($arg:tt)*) => {
        $logger.warn(module_path!(), &format!($($arg)*))
    };
}

/// Macro for logging info with automatic module path.
#[macro_export]
macro_rules! log_info {
    ($logger:expr, $($arg:tt)*) => {
        $logger.info(module_path!(), &format!($($arg)*))
    };
}

/// Macro for logging debug with automatic module path.
#[macro_export]
macro_rules! log_debug {
    ($logger:expr, $($arg:tt)*) => {
        $logger.debug(module_path!(), &format!($($arg)*))
    };
}

/// Macro for logging trace with automatic module path.
#[macro_export]
macro_rules! log_trace {
    ($logger:expr, $($arg:tt)*) => {
        $logger.trace(module_path!(), &format!($($arg)*))
    };
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_log_level_ordering() {
        assert!(LogLevel::Error < LogLevel::Warn);
        assert!(LogLevel::Warn < LogLevel::Info);
        assert!(LogLevel::Info < LogLevel::Debug);
        assert!(LogLevel::Debug < LogLevel::Trace);
    }

    #[test]
    fn test_should_log() {
        let configured_level = LogLevel::Info;

        assert!(LogLevel::Error.should_log(&configured_level));
        assert!(LogLevel::Warn.should_log(&configured_level));
        assert!(LogLevel::Info.should_log(&configured_level));
        assert!(!LogLevel::Debug.should_log(&configured_level));
        assert!(!LogLevel::Trace.should_log(&configured_level));
    }

    #[test]
    fn test_log_config_default() {
        let config = LogConfig::default();
        assert_eq!(config.level, LogLevel::Info);
        assert!(config.include_timestamps);
        assert!(config.include_module_path);
        assert!(!config.include_line_numbers);
    }

    #[test]
    fn test_log_config_minimal() {
        let config = LogConfig::minimal(LogLevel::Debug);
        assert_eq!(config.level, LogLevel::Debug);
        assert!(!config.include_timestamps);
        assert!(!config.include_module_path);
        assert!(!config.include_line_numbers);
    }

    #[test]
    fn test_log_config_verbose() {
        let config = LogConfig::verbose(LogLevel::Trace);
        assert_eq!(config.level, LogLevel::Trace);
        assert!(config.include_timestamps);
        assert!(config.include_module_path);
        assert!(config.include_line_numbers);
    }

    #[test]
    fn test_logger_creation() {
        let config = LogConfig::default();
        let logger = Logger::new(config);
        assert_eq!(logger.level(), LogLevel::Info);
    }

    #[test]
    fn test_module_filtering() {
        let config = LogConfig::default().with_module_filter("chie_core::storage".to_string());
        let logger = Logger::new(config);

        assert!(logger.should_log_module("chie_core::storage"));
        assert!(logger.should_log_module("chie_core::storage::chunk"));
        assert!(!logger.should_log_module("chie_core::network"));
    }

    #[test]
    fn test_logger_level_change() {
        let mut logger = Logger::default_config();
        assert_eq!(logger.level(), LogLevel::Info);

        logger.set_level(LogLevel::Debug);
        assert_eq!(logger.level(), LogLevel::Debug);
    }

    #[test]
    fn test_log_level_display() {
        assert_eq!(LogLevel::Error.to_string(), "ERROR");
        assert_eq!(LogLevel::Warn.to_string(), "WARN");
        assert_eq!(LogLevel::Info.to_string(), "INFO");
        assert_eq!(LogLevel::Debug.to_string(), "DEBUG");
        assert_eq!(LogLevel::Trace.to_string(), "TRACE");
    }
}