mermaid_cli/utils/
logger.rs

1use std::io;
2use tracing::{debug, error, info, warn};
3use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};
4
5/// Initialize the logging system with tracing
6pub fn init_logger(verbose: bool) {
7    // If --verbose flag is set, override to debug level
8    // Otherwise use RUST_LOG environment variable, default to warn level (quieter)
9    let filter = if verbose {
10        EnvFilter::new("debug,mermaid=debug")
11    } else {
12        EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("warn,mermaid=info"))
13    };
14
15    let fmt_layer = tracing_subscriber::fmt::layer()
16        .with_writer(io::stderr) // Write to stderr to not interfere with TUI
17        .with_target(false) // Don't show module paths in compact mode
18        .with_thread_ids(false)
19        .with_thread_names(false)
20        .with_file(false) // Don't show file locations
21        .with_line_number(false) // Don't show line numbers
22        .compact(); // Use compact format for cleaner output
23
24    tracing_subscriber::registry()
25        .with(filter)
26        .with(fmt_layer)
27        .init();
28}
29
30/// Log an info message with category prefix (backward compatible)
31pub fn log_info(category: &str, message: impl std::fmt::Display) {
32    info!(category = %category, "{}", message);
33}
34
35/// Log a warning message with category prefix (backward compatible)
36pub fn log_warn(category: &str, message: impl std::fmt::Display) {
37    warn!(category = %category, "{}", message);
38}
39
40/// Log an error message with category prefix (backward compatible)
41pub fn log_error(category: &str, message: impl std::fmt::Display) {
42    error!(category = %category, "{}", message);
43}
44
45/// Log a debug message (backward compatible)
46pub fn log_debug(message: impl std::fmt::Display) {
47    debug!("{}", message);
48}
49
50/// Status messages for the TUI (special handling)
51pub fn log_status(message: impl std::fmt::Display) {
52    // For now, still use eprintln for TUI status messages
53    // These will be handled differently when TUI is active
54    eprintln!("{}", message);
55}
56
57/// Progress indicator for startup sequence
58pub fn log_progress(step: usize, total: usize, message: impl std::fmt::Display) {
59    let progress = format!("[{}/{}]", step, total);
60    eprintln!("{} {} {}", progress, "->".to_string(), message);
61}