Mercurial > crates > nonstick
diff src/logging.rs @ 157:0099f2f79f86
Switch logging interface to accept fmt::Arguments.
This means that we don't have to format arguments eagerly when logging;
an implementation could choose to discard them if it wanted to, avoiding
allocations and expensive format calls.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Wed, 09 Jul 2025 16:59:30 -0400 |
parents | ab8020566cd9 |
children | 634cd5f2ac8b |
line wrap: on
line diff
--- a/src/logging.rs Tue Jul 08 01:04:30 2025 -0400 +++ b/src/logging.rs Wed Jul 09 16:59:30 2025 -0400 @@ -25,7 +25,7 @@ #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Level { Error, - Warning, + Warn, Info, Debug, } @@ -64,7 +64,7 @@ #[macro_export] macro_rules! __log_internal { ($handle:expr, $level:ident, $($arg:tt)+) => { - $handle.log($crate::logging::Level::$level, $crate::location!(), &format!($($arg)+)); + $handle.log($crate::logging::Level::$level, $crate::location!(), format_args!($($arg)+)); } } @@ -122,7 +122,7 @@ /// # } /// ``` #[macro_export] -macro_rules! warn { ($handle:expr, $($arg:tt)+) => { $crate::__log_internal!($handle, Warning, $($arg)+);}} +macro_rules! warn { ($handle:expr, $($arg:tt)+) => { $crate::__log_internal!($handle, Warn, $($arg)+);}} /// Logs a message at info level via the given PAM handle. /// @@ -163,14 +163,15 @@ mod tests { use super::*; use std::cell::RefCell; + use std::fmt; #[test] fn test_logging() { struct Logger(RefCell<Vec<(Level, String)>>); impl Logger { - fn log(&self, level: Level, _: Location<'_>, text: &str) { - self.0.borrow_mut().push((level, text.to_owned())) + fn log(&self, level: Level, _: Location<'_>, text: fmt::Arguments) { + self.0.borrow_mut().push((level, text.to_string())) } } @@ -187,7 +188,7 @@ assert_eq!( vec![ (Level::Error, "here is another thing: 99".to_owned()), - (Level::Warning, "watch out!".to_owned()), + (Level::Warn, "watch out!".to_owned()), (Level::Info, "here is some info: information".to_owned()), (Level::Debug, "here is something: Error".to_owned()), ],