| 1 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 2 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 4 |
// option. This file may not be copied, modified, or distributed |
| 5 |
// except according to those terms. |
| 6 |
|
| 7 |
use std::{ |
| 8 |
io::Write as _, |
| 9 |
sync::{Once, OnceLock}, |
| 10 |
time::{Duration, Instant}, |
| 11 |
}; |
| 12 |
|
| 13 |
use env_logger::Builder; |
| 14 |
|
| 15 |
fn since_start() -> Duration { |
| 16 |
static START_TIME: OnceLock<Instant> = OnceLock::new(); |
| 17 |
START_TIME.get_or_init(Instant::now).elapsed() |
| 18 |
} |
| 19 |
|
| 20 |
pub fn init(level_filter: Option<log::LevelFilter>) { |
| 21 |
static INIT_ONCE: Once = Once::new(); |
| 22 |
|
| 23 |
if ::log::STATIC_MAX_LEVEL == ::log::LevelFilter::Off { |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
INIT_ONCE.call_once(|| { |
| 28 |
let mut builder = Builder::from_env("RUST_LOG"); |
| 29 |
if let Some(filter) = level_filter { |
| 30 |
builder.filter_level(filter); |
| 31 |
} |
| 32 |
builder.format(|buf, record| { |
| 33 |
let elapsed = since_start(); |
| 34 |
writeln!( |
| 35 |
buf, |
| 36 |
"{}.{:03} {} {}", |
| 37 |
elapsed.as_secs(), |
| 38 |
elapsed.as_millis() % 1000, |
| 39 |
record.level(), |
| 40 |
record.args() |
| 41 |
) |
| 42 |
}); |
| 43 |
if let Err(e) = builder.try_init() { |
| 44 |
eprintln!("Logging initialization error {e:?}"); |
| 45 |
} else { |
| 46 |
::log::debug!("Logging initialized"); |
| 47 |
} |
| 48 |
}); |
| 49 |
} |
| 50 |
|
| 51 |
#[macro_export] |
| 52 |
// TODO: Enable `#[clippy::format_args]` once our MSRV is >= 1.84 |
| 53 |
macro_rules! qerror { |
| 54 |
($($arg:tt)*) => ( { |
| 55 |
#[cfg(any(test, feature = "bench"))] |
| 56 |
::neqo_common::log::init(None); |
| 57 |
::log::error!($($arg)*); |
| 58 |
} ); |
| 59 |
} |
| 60 |
#[macro_export] |
| 61 |
// TODO: Enable `#[clippy::format_args]` once our MSRV is >= 1.84 |
| 62 |
macro_rules! qwarn { |
| 63 |
($($arg:tt)*) => ( { |
| 64 |
#[cfg(any(test, feature = "bench"))] |
| 65 |
::neqo_common::log::init(None); |
| 66 |
::log::warn!($($arg)*); |
| 67 |
} ); |
| 68 |
} |
| 69 |
#[macro_export] |
| 70 |
// TODO: Enable `#[clippy::format_args]` once our MSRV is >= 1.84 |
| 71 |
macro_rules! qinfo { |
| 72 |
($($arg:tt)*) => ( { |
| 73 |
#[cfg(any(test, feature = "bench"))] |
| 74 |
::neqo_common::log::init(None); |
| 75 |
::log::info!($($arg)*); |
| 76 |
} ); |
| 77 |
} |
| 78 |
#[macro_export] |
| 79 |
// TODO: Enable `#[clippy::format_args]` once our MSRV is >= 1.84 |
| 80 |
macro_rules! qdebug { |
| 81 |
($($arg:tt)*) => ( { |
| 82 |
#[cfg(any(test, feature = "bench"))] |
| 83 |
::neqo_common::log::init(None); |
| 84 |
::log::debug!($($arg)*); |
| 85 |
} ); |
| 86 |
} |
| 87 |
#[macro_export] |
| 88 |
// TODO: Enable `#[clippy::format_args]` once our MSRV is >= 1.84 |
| 89 |
macro_rules! qtrace { |
| 90 |
($($arg:tt)*) => ( { |
| 91 |
#[cfg(any(test, feature = "bench"))] |
| 92 |
::neqo_common::log::init(None); |
| 93 |
::log::trace!($($arg)*); |
| 94 |
} ); |
| 95 |
} |