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
//!  ```
//!  use logs::{debug, error, info, trace, warn};
//!  
//!  fn main() {
//!      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");
//!  }
//!  ```
//! ## Features
//!
//! By default, these logs will not be displayed in `--release` mode, if you need to open please add the logs features.
//!
//! ```no-run
//! [dependencies]
//! logs = { version = "*", features = ["warn", "error" ...] }
//! ```
//!
//! By default, the log does not include the recording time, to display please add logs features.
//!
//! ```no-run
//! [dependencies]
//! logs = { version = "*", features = ["time"] }
//! ```
//!
//! ```no-run
//! ...
//! [2020-07-06 15:56:08] [WARN ] This is a warn log
//! [2020-07-06 15:56:08] [ERROR] This is a error log
//! ```

#[cfg(feature = "time")]
pub use time;

use cfg_if::cfg_if;

cfg_if! {
    if #[cfg(feature = "time")] {
        #[doc(hidden)]
        #[macro_export]
        macro_rules! _time {
            ($print:expr) => {
                if $print {
                    print!("[{}] ", $crate::time::now().strftime("%Y-%m-%d %H:%M:%S").unwrap());
                }else {
                    eprint!("[{}] ", $crate::time::now().strftime("%Y-%m-%d %H:%M:%S").unwrap());
                }
            };
        }
    } else {
        #[doc(hidden)]
        #[macro_export]
        macro_rules! _time {
            ($_:expr) => {};
        }
    }
}

#[doc(hidden)]
#[macro_export]
macro_rules! _print {
    ($($arg:tt)*) => {
        $crate::_time!(true);
        print!($($arg)*);
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! _println {
    ($($arg:tt)*) => {
        $crate::_time!(true);
        println!($($arg)*);
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! _eprint {
    ($($arg:tt)*) => {
        $crate::_time!(false);
        eprint!($($arg)*);
    };
}

cfg_if! {
    if #[cfg(any(debug_assertions, feature = "debug"))] {
        #[macro_export]
        macro_rules! debug {
            () => {
                $crate::_println!("\x1B[36m{}\x1B[0m [{}:{}]", "[DEBUG]", file!(), line!());
            };
            ($val:expr) => {
                $crate::_println!("\x1B[36m{}\x1B[0m [{}:{}]", "[DEBUG]", file!(), line!());
                println!("{:#?}", $val);
            };
            ($($arg:tt)*) => {
                $crate::_println!("\x1B[36m{}\x1B[0m [{}:{}]", "[DEBUG]", file!(), line!());
                println!($($arg)*);
             };
        }
    } else {
        #[macro_export]
        macro_rules! debug {
            ($($arg:tt)*) => {};
        }
    }
}

cfg_if! {
    if #[cfg(any(debug_assertions, feature = "trace"))] {
        #[macro_export]
        macro_rules! trace {
            ($($arg:tt)*) => {
                $crate::_print!("\x1B[2;3m{}\x1B[0m ", "[TRACE]");
                println!($($arg)*);
            };
        }
    }else {
        #[macro_export]
        macro_rules! trace {
            ($($arg:tt)*) => { };
        }
    }
}

cfg_if! {
    if #[cfg(any(debug_assertions, feature = "info"))] {
        #[macro_export]
        macro_rules! info {
            ($($arg:tt)*) => {
                $crate::_print!("\x1B[32m{}\x1B[0m ", "[INFO ]");
                println!($($arg)*);
            };
        }
    }else {
        #[macro_export]
        macro_rules! info {
            ($($arg:tt)*) => { };
        }
    }
}

cfg_if! {
    if #[cfg(any(debug_assertions, feature = "warn"))] {
        #[macro_export]
        macro_rules! warn {
            ($($arg:tt)*) => {
                $crate::_print!("\x1B[4;33m{}\x1B[0m ", "[WARN ]");
                println!($($arg)*);
            };
        }
    }else {
        #[macro_export]
        macro_rules! warn {
            ($($arg:tt)*) => { };
        }
    }
}

cfg_if! {
    if #[cfg(any(debug_assertions, feature = "error"))] {
        #[macro_export]
        macro_rules! error {
            ($($arg:tt)*) => {
                $crate::_eprint!("\x1B[1;31m{}\x1B[0m ", "[ERROR]");
                eprintln!($($arg)*);
            };
        }
    }else {
        #[macro_export]
        macro_rules! error {
            ($($arg:tt)*) => { };
        }
    }
}