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
//! ## 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);
//!
//! // Enable color display
//! config.color(true);
//!
//! // Change datetime format: [Fri Nov 27 15:56:08 2020]
//! config.date_format("%c").unwrap();
//!      
//! config.apply();
//!
//! 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().apply();
//! ```

mod config;
pub use config::{LogConfig, LogError, _LOG_CONFIG};
#[doc(hidden)]
pub use time;

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

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

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

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

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

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