| 1 | #[cfg(feature = "logging")] |
| 2 | pub fn init_logger( |
| 3 | min_level: log::LevelFilter, debug_file_name: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#>, |
| 4 | ) -> anyhowrust-analyzer cargo anyhow 1.0.104 crate/::Resultrust-analyzer cargo anyhow 1.0.104 Result#<()> { |
| 5 | let dispatch = fern::Dispatch::new() |
| 6 | .format(|out, message, record| { |
| 7 | out.finish(format_argsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/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 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(debug_file_name) = debug_file_name { |
| 18 | dispatch.chain(fern::log_file(debug_file_name)?).apply()?; |
| 19 | } else { |
| 20 | dispatch.chain(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::stdoutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/stdout().()).apply()?; |
| 21 | } |
| 22 | |
| 23 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 24 | } |
| 25 | |
| 26 | #[macro_export] |
| 27 | macro_rules! errorrust-analyzer cargo bottom 0.14.9 utils/logging/error! { |
| 28 | ($($x:tt)*) => { |
| 29 | #[cfg(feature = "logging")] |
| 30 | { |
| 31 | log::error!($($x)*); |
| 32 | } |
| 33 | }; |
| 34 | } |
| 35 | |
| 36 | #[macro_export] |
| 37 | macro_rules! warnrust-analyzer cargo bottom 0.14.9 utils/logging/warn! { |
| 38 | ($($x:tt)*) => { |
| 39 | #[cfg(feature = "logging")] |
| 40 | { |
| 41 | log::warn!($($x)*); |
| 42 | } |
| 43 | }; |
| 44 | } |
| 45 | |
| 46 | #[macro_export] |
| 47 | macro_rules! inforust-analyzer cargo bottom 0.14.9 utils/logging/info! { |
| 48 | ($($x:tt)*) => { |
| 49 | #[cfg(feature = "logging")] |
| 50 | { |
| 51 | log::info!($($x)*); |
| 52 | } |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | #[macro_export] |
| 57 | macro_rules! debugrust-analyzer cargo bottom 0.14.9 utils/logging/debug! { |
| 58 | ($($x:tt)*) => { |
| 59 | #[cfg(feature = "logging")] |
| 60 | { |
| 61 | log::debug!($($x)*); |
| 62 | } |
| 63 | }; |
| 64 | } |
| 65 | |
| 66 | #[macro_export] |
| 67 | macro_rules! tracerust-analyzer cargo bottom 0.14.9 utils/logging/trace! { |
| 68 | ($($x:tt)*) => { |
| 69 | #[cfg(feature = "logging")] |
| 70 | { |
| 71 | log::trace!($($x)*); |
| 72 | } |
| 73 | }; |
| 74 | } |
| 75 | |
| 76 | #[macro_export] |
| 77 | macro_rules! logrust-analyzer cargo bottom 0.14.9 utils/logging/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_secsrust-analyzer cargo bottom 0.14.9 utils/logging/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_secsrust-analyzer cargo bottom 0.14.9 utils/logging/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 testrust-analyzer cargo bottom 0.14.9 utils/logging/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 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::Oncerust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/once/Once#; |
| 130 | |
| 131 | static INIT: Once = Once::new(); |
| 132 | |
| 133 | INIT.call_once(|| { |
| 134 | superrust-analyzer cargo bottom 0.14.9 utils/logging/::init_logger(log::LevelFilter::Trace, Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#) |
| 135 | .expect("initializing the logger should succeed"); |
| 136 | }); |
| 137 | } |
| 138 | |
| 139 | #[cfg(feature = "logging")] |
| 140 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 141 | fn test_logging_macros() { |
| 142 | init_test_logger(); |
| 143 | |
| 144 | errorrust-analyzer cargo bottom 0.14.9 utils/logging/error!!("This is an error."); |
| 145 | warnrust-analyzer cargo bottom 0.14.9 utils/logging/warn!!("This is a warning."); |
| 146 | inforust-analyzer cargo bottom 0.14.9 utils/logging/info!!("This is an info."); |
| 147 | debugrust-analyzer cargo bottom 0.14.9 utils/logging/debug!!("This is a debug."); |
| 148 | inforust-analyzer cargo bottom 0.14.9 utils/logging/info!!("This is a trace."); |
| 149 | } |
| 150 | |
| 151 | #[cfg(feature = "logging")] |
| 152 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 153 | fn test_log_every_macros() { |
| 154 | init_test_logger(); |
| 155 | |
| 156 | info_every_n_secsrust-analyzer cargo bottom 0.14.9 utils/logging/info_every_n_secs!!(10, "This is an info every 10 seconds."); |
| 157 | } |
| 158 | } |