hw_msg/
logging.rs

1use log::{Level, Metadata, Record};
2
3/// This struct allows you to use the
4/// [`log` crate's macros](https://docs.rs/log/latest/log/index.html#usage)
5/// instead of the ones provided by this crate.
6///
7/// # Example
8///
9/// ```rust
10/// use hw_msg::logging::HwLogger;
11///
12/// log::set_logger(&hw_msg::HwLogger).unwrap();
13/// log::info!("We're using `hw_msg` from the `log` crate!");
14/// ```
15///
16/// **Note**: This struct is currently incompatible with the [`log::trace`] macro, and will panic if
17/// you call it with this logger.
18pub struct HwLogger;
19
20impl log::Log for HwLogger {
21    fn enabled(&self, metadata: &Metadata) -> bool {
22        if let Some(level) = log::max_level().to_level() {
23            metadata.level() <= level
24        } else {
25            false
26        }
27    }
28
29    fn log(&self, record: &Record) {
30        if !self.enabled(record.metadata()) {
31            return;
32        }
33
34        let args = record.args();
35
36        match record.level() {
37            Level::Error => crate::errorln!("{args}"),
38            Level::Warn => crate::warningln!("{args}"),
39            Level::Info => crate::infoln!("{args}"),
40            Level::Debug => crate::debugln!("{args}"),
41            Level::Trace => {
42                unimplemented!("`Level::Trace` is unsupported for the `HwLogger` logger")
43            }
44        }
45    }
46
47    fn flush(&self) {}
48}