Mercurial > crates > nonstick
diff src/logging.rs @ 159:634cd5f2ac8b
Separate logging into its own trait apart from the rest of PAM.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Sat, 12 Jul 2025 18:16:18 -0400 |
parents | 0099f2f79f86 |
children | e27c5c667a5a |
line wrap: on
line diff
--- a/src/logging.rs Sat Jul 12 17:17:37 2025 -0400 +++ b/src/logging.rs Sat Jul 12 18:16:18 2025 -0400 @@ -15,6 +15,46 @@ //! and may even itself implement `log::Log`, but that interface is not exposed //! to the generic PAM user. +use crate::_doc::{man7, manbsd}; +use std::fmt; + +/// A trait for logging. +pub trait Logger { + /// Logs something via this PAM handle. + /// + /// You probably want to use one of the logging macros, + /// like [`error!`](crate::error!), + /// [`warn!`](crate::warn!), + /// [`info!`](crate::info!), + /// or [`debug!`](crate::debug!). + /// + /// In most PAM implementations, this will go to syslog. + /// See [Linux-PAM's `pam_syslog`][man7] or + /// [OpenPAM's `openpam_log`][manbsd] for more details. + /// + /// # Example + /// + /// ```no_run + /// # use nonstick::PamShared; + /// use nonstick::logging::Level; + /// use nonstick::location; + /// # fn _test(pam_hdl: impl PamShared) { + /// # let delay_ms = 100; + /// # let url = "https://zombo.com"; + /// // Usually, instead of calling this manually, just use the macros. + /// nonstick::error!(pam_hdl, "something bad happened!"); + /// nonstick::warn!(pam_hdl, "loading information took {delay_ms} ms"); + /// nonstick::info!(pam_hdl, "using network backend"); + /// nonstick::debug!(pam_hdl, "sending GET request to {url}"); + /// // But if you really want to, you can call this yourself: + /// pam_hdl.log(Level::Warn, location!(), format_args!("this is unnecessarily verbose")); + /// # } + /// ``` + #[doc = man7!(3 pam_syslog)] + #[doc = manbsd!(3 openpam_log)] + fn log(&self, level: Level, loc: Location<'_>, entry: fmt::Arguments); +} + /// An entry to be added to the log. /// /// The levels are in descending order of importance and correspond roughly @@ -167,15 +207,15 @@ #[test] fn test_logging() { - struct Logger(RefCell<Vec<(Level, String)>>); + struct TestLog(RefCell<Vec<(Level, String)>>); - impl Logger { + impl Logger for TestLog { fn log(&self, level: Level, _: Location<'_>, text: fmt::Arguments) { self.0.borrow_mut().push((level, text.to_string())) } } - let logger = Logger(Default::default()); + let logger = TestLog(Default::default()); let something = Level::Error; error!(logger, "here is another thing: {}", 99);