| 1 | #[cfg(feature = "logging")] |
| 2 | pub fn init_logger( |
| 3 | min_level: log::LevelFilter, debug_file_name: Option<&std::ffi::OsStr>, |
| 4 | ) -> anyhow::Result<()> { |
| 5 | let dispatch = fern::Dispatch::new() |
| 6 | .format(|out, message, record| { |
| 7 | out.finish(format_args!( |
| 8 | "[{}][{}][{}] {}", |
| 9 | humantime::format_rfc3339_nanos(std::time::SystemTime::now()), |
| 10 | record.target(), |
| 11 | record.level(), |
| 12 | message |
| 13 | )) |
| 14 | }) |
| 15 | .level(min_level); |
| 16 | |
| 17 | if let Some(debug_file_name) = debug_file_name { |
| 18 | dispatch.chain(fern::log_file(debug_file_name)?).apply()?; |
| 19 | } else { |
| 20 | dispatch.chain(std::io::stdout()).apply()?; |
| 21 | } |
| 22 | |
| 23 | Ok(()) |
| 24 | } |
| 25 | |
| 26 | #[macro_export] |
| 27 | macro_rules! error { |
| 28 | ($($x:tt)*) => { |
| 29 | #[cfg(feature = "logging")] |
| 30 | { |
| 31 | log::error!($($x)*); |
| 32 | } |
| 33 | }; |
| 34 | } |
| 35 | |
| 36 | #[macro_export] |
| 37 | macro_rules! warn { |
| 38 | ($($x:tt)*) => { |
| 39 | #[cfg(feature = "logging")] |
| 40 | { |
| 41 | log::warn!($($x)*); |
| 42 | } |
| 43 | }; |
| 44 | } |
| 45 | |
| 46 | #[macro_export] |
| 47 | macro_rules! info { |
| 48 | ($($x:tt)*) => { |
| 49 | #[cfg(feature = "logging")] |
| 50 | { |
| 51 | log::info!($($x)*); |
| 52 | } |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | #[macro_export] |
| 57 | macro_rules! debug { |
| 58 | ($($x:tt)*) => { |
| 59 | #[cfg(feature = "logging")] |
| 60 | { |
| 61 | log::debug!($($x)*); |
| 62 | } |
| 63 | }; |
| 64 | } |
| 65 | |
| 66 | #[macro_export] |
| 67 | macro_rules! trace { |
| 68 | ($($x:tt)*) => { |
| 69 | #[cfg(feature = "logging")] |
| 70 | { |
| 71 | log::trace!($($x)*); |
| 72 | } |
| 73 | }; |
| 74 | } |
| 75 | |
| 76 | #[macro_export] |
| 77 | macro_rules! log { |
| 78 | ($($x:tt)*) => { |
| 79 | #[cfg(feature = "logging")] |
| 80 | { |
| 81 | log::log!(log::Level::Trace, $($x)*); |
| 82 | } |
| 83 | }; |
| 84 | ($level:expr, $($x:tt)*) => { |
| 85 | #[cfg(feature = "logging")] |
| 86 | { |
| 87 | log::log!($level, $($x)*); |
| 88 | } |
| 89 | }; |
| 90 | } |
| 91 | |
| 92 | #[macro_export] |
| 93 | macro_rules! info_every_n_secs { |
| 94 | ($n:expr, $($x:tt)*) => { |
| 95 | #[cfg(feature = "logging")] |
| 96 | { |
| 97 | $crate::log_every_n_secs!(log::Level::Info, $n, $($x)*); |
| 98 | } |
| 99 | }; |
| 100 | } |
| 101 | |
| 102 | #[macro_export] |
| 103 | macro_rules! log_every_n_secs { |
| 104 | ($level:expr, $n:expr, $($x:tt)*) => { |
| 105 | #[cfg(feature = "logging")] |
| 106 | { |
| 107 | use std::sync::atomic::{AtomicU64, Ordering}; |
| 108 | static LAST_LOG: AtomicU64 = AtomicU64::new(0); |
| 109 | let since_last_log = LAST_LOG.load(Ordering::Relaxed); |
| 110 | let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).expect("should be valid").as_secs(); |
| 111 | |
| 112 | if now - since_last_log > $n { |
| 113 | LAST_LOG.store(now, Ordering::Relaxed); |
| 114 | log::log!($level, $($x)*); |
| 115 | } |
| 116 | } |
| 117 | }; |
| 118 | } |
| 119 | |
| 120 | #[cfg(test)] |
| 121 | mod test { |
| 122 | #[cfg(feature = "logging")] |
| 123 | /// We do this to ensure that the test logger is only initialized _once_ for |
| 124 | /// things like the default test runner that run tests in the same process. |
| 125 | /// |
| 126 | /// This doesn't do anything if you use something like nextest, which runs |
| 127 | /// a test-per-process, but that's fine. |
| 128 | fn init_test_logger() { |
| 129 | use std::sync::Once; |
| 130 | |
| 131 | static INIT: Once = Once::new(); |
| 132 | |
| 133 | INIT.call_once(|| { |
| 134 | super::init_logger(log::LevelFilter::Trace, None) |
| 135 | .expect("initializing the logger should succeed"); |
| 136 | }); |
| 137 | } |
| 138 | |
| 139 | #[cfg(feature = "logging")] |
| 140 | #[test] |
| 141 | fn test_logging_macros() { |
| 142 | init_test_logger(); |
| 143 | |
| 144 | error!("This is an error."); |
| 145 | warn!("This is a warning."); |
| 146 | info!("This is an info."); |
| 147 | debug!("This is a debug."); |
| 148 | info!("This is a trace."); |
| 149 | } |
| 150 | |
| 151 | #[cfg(feature = "logging")] |
| 152 | #[test] |
| 153 | fn test_log_every_macros() { |
| 154 | init_test_logger(); |
| 155 | |
| 156 | info_every_n_secs!(10, "This is an info every 10 seconds."); |
| 157 | } |
| 158 | } |