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
//! ## Example
//!  
//! ```rust
//! use logs::{debug, error, info, trace, warn};
//!
//! debug!("This is a debug log");
//! trace!("This is a trace log");
//! info!("This is a info log");
//! warn!("This is a warn log");
//! error!("This is a error log");
//! ```
//!
//! ## Config
//!
//! ```
//! use logs::{LogConfig, debug, error};
//!
//! let mut config = LogConfig::disable_all();
//!
//! // Disable debug! output
//! config.debug(false);
//!
//! // Allow error! output
//! config.error(true);
//!
//! // The output of `trace!` is only displayed in debug mode
//! #[cfg(debug_assertions)]
//! config.trace(true);
//!
//! // Change datetime format: [Fri Nov 27 15:56:08 2020]
//! config.date_format("%c").unwrap();
//!      
//! config.init();
//!
//! debug!("This is a debug log");
//! error!("This is a error log");
//!
//! ```
//!
//! ### env
//!
//! This can be configured by reading the `LOG` environment variable, which disables all output by default
//!
//! ```bash
//! # Enable all output
//! # Disable debug output
//! # ...
//! export LOG='all,!debug,info,!error'
//! ```
//!
//! ```
//! use logs::LogConfig;
//!
//! LogConfig::from_env().unwrap_or_default().init();
//! ```

mod config;
pub use config::{LogConfig, DATE_FORMAT, LOG_CONFIG};
pub use time;

#[doc(hidden)]
#[macro_export]
macro_rules! _time {
    () => {
        $crate::time::now()
            .strftime(unsafe { $crate::DATE_FORMAT })
            .unwrap()
    };
}

#[macro_export]
macro_rules! trace {
    ($($arg:tt)*) => {
        if unsafe { $crate::LOG_CONFIG.trace } {
            println!("[{}] \x1B[2;3m[{}]\x1B[0m {}", $crate::_time!(), "TRACE", format!($($arg)*));
        }
    };
}

#[macro_export]
macro_rules! debug {
    () => {
        if unsafe { $crate::LOG_CONFIG.debug } {
            println!("[{}] \x1B[36m[{}]\x1B[0m [{}:{}]", $crate::_time!(), "DEBUG", file!(), line!());
        }
    };
    ($val:expr) => {
        if unsafe { $crate::LOG_CONFIG.debug } {
            println!("[{}] \x1B[36m[{}]\x1B[0m [{}:{}]\n{:#?}", $crate::_time!(), "DEBUG", file!(), line!(), $val);
        }
    };
    ($($arg:expr), *) => {
        if unsafe { $crate::LOG_CONFIG.debug } {
            print!("[{}] \x1B[36m[{}]\x1B[0m [{}:{}]\n{}", $crate::_time!(), "DEBUG", file!(), line!(), {
                let mut content = String::new();
                $(content += &format!("{:#?}\n", $arg);)*
                content
            });
        }
    };
}

#[macro_export]
macro_rules! info {
    ($($arg:tt)*) => {
        if unsafe { $crate::LOG_CONFIG.info } {
            println!("[{}] \x1B[32m[{}]\x1B[0m {}", $crate::_time!(), "INFO ", format!($($arg)*));
        }
    };
}

#[macro_export]
macro_rules! warn {
    ($($arg:tt)*) => {
        if unsafe { $crate::LOG_CONFIG.warn } {
            println!("[{}] \x1B[4;33m[{}]\x1B[0m {}", $crate::_time!(), "WARN ", format!($($arg)*));
        }
    };
}

#[macro_export]
macro_rules! error {
    ($($arg:tt)*) => {
        if unsafe { $crate::LOG_CONFIG.error } {
            eprintln!("[{}] \x1B[1;31m[{}]\x1B[0m {}", $crate::_time!(), "ERROR", format!($($arg)*));
        }
    };
}