|
| 1 | +//! Logging adapter for Laminar |
| 2 | +//! |
| 3 | +//! This module implements a simple, threaded-logger-friendly logging adapter. Logging adapters are |
| 4 | +//! used to attach an arbitrary logger into Laminar. |
| 5 | +use std::fmt; |
| 6 | +use std::sync::Arc; |
| 7 | + |
| 8 | +/// Logger trait for laminar |
| 9 | +/// |
| 10 | +/// Any user of Laminar can implement this trait to attach their favorite logger to an instance of |
| 11 | +/// laminar. The log levels correspond to the same log levels as in the `log` crate. |
| 12 | +pub trait LaminarLogger { |
| 13 | + /// Log a trace message |
| 14 | + fn trace(&self, disp: Displayer); |
| 15 | + /// Log a debug message |
| 16 | + fn debug(&self, disp: Displayer); |
| 17 | + /// Log an info message |
| 18 | + fn info(&self, disp: Displayer); |
| 19 | + /// Log a warning message |
| 20 | + fn warn(&self, disp: Displayer); |
| 21 | + /// Log an error message |
| 22 | + fn error(&self, disp: Displayer); |
| 23 | +} |
| 24 | + |
| 25 | +// --- |
| 26 | + |
| 27 | +/// Holds a handle to a formatter function while implementing the [fmt::Display] trait. |
| 28 | +pub struct Displayer { |
| 29 | + data: Arc<dyn Fn(&mut ::std::fmt::Formatter) -> ::std::fmt::Result + Send + Sync>, |
| 30 | +} |
| 31 | + |
| 32 | +impl Displayer { |
| 33 | + pub(crate) fn new( |
| 34 | + delegate: Arc<dyn Fn(&mut ::std::fmt::Formatter) -> ::std::fmt::Result + Send + Sync>, |
| 35 | + ) -> Self { |
| 36 | + Self { data: delegate } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +impl fmt::Display for Displayer { |
| 41 | + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { |
| 42 | + (self.data)(f) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// --- |
| 47 | + |
| 48 | +pub(crate) struct DefaultLogger; |
| 49 | + |
| 50 | +impl LaminarLogger for DefaultLogger { |
| 51 | + fn trace(&self, _: Displayer) {} |
| 52 | + fn debug(&self, _: Displayer) {} |
| 53 | + fn info(&self, _: Displayer) {} |
| 54 | + fn warn(&self, _: Displayer) {} |
| 55 | + fn error(&self, _: Displayer) {} |
| 56 | +} |
| 57 | + |
| 58 | +impl fmt::Debug for dyn LaminarLogger { |
| 59 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 60 | + write![f, "LaminarLogger"] |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// --- |
| 65 | + |
| 66 | +/// Format-friendly form of [log::LaminarLogger::trace] |
| 67 | +#[macro_export] |
| 68 | +macro_rules! trace { |
| 69 | + ($logger:expr, $($fmt:expr),* $(,)?) => {{ |
| 70 | + $logger.trace($crate::log::Displayer::new(::std::sync::Arc::new(move |f: &mut ::std::fmt::Formatter| { write![f, $($fmt),*] }) )); |
| 71 | + }}; |
| 72 | +} |
| 73 | + |
| 74 | +/// Format-friendly form of [log::LaminarLogger::debug] |
| 75 | +#[macro_export] |
| 76 | +macro_rules! debug { |
| 77 | + ($logger:expr, $($fmt:expr),* $(,)?) => {{ |
| 78 | + $logger.debug($crate::log::Displayer::new(::std::sync::Arc::new(move |f: &mut ::std::fmt::Formatter| { write![f, $($fmt),*] }) )); |
| 79 | + }}; |
| 80 | +} |
| 81 | + |
| 82 | +/// Format-friendly form of [log::LaminarLogger::info] |
| 83 | +#[macro_export] |
| 84 | +macro_rules! info { |
| 85 | + ($logger:expr, $($fmt:expr),* $(,)?) => {{ |
| 86 | + $logger.info($crate::log::Displayer::new(::std::sync::Arc::new(move |f: &mut ::std::fmt::Formatter| { write![f, $($fmt),*] }) )); |
| 87 | + }}; |
| 88 | +} |
| 89 | + |
| 90 | +/// Format-friendly form of [log::LaminarLogger::warn] |
| 91 | +#[macro_export] |
| 92 | +macro_rules! warn { |
| 93 | + ($logger:expr, $($fmt:expr),* $(,)?) => {{ |
| 94 | + $logger.warn($crate::log::Displayer::new(::std::sync::Arc::new(move |f: &mut ::std::fmt::Formatter| { write![f, $($fmt),*] }) )); |
| 95 | + }}; |
| 96 | +} |
| 97 | + |
| 98 | +/// Format-friendly form of [log::LaminarLogger::error] |
| 99 | +#[macro_export] |
| 100 | +macro_rules! error { |
| 101 | + ($logger:expr, $($fmt:expr),* $(,)?) => {{ |
| 102 | + $logger.error($crate::log::Displayer::new(::std::sync::Arc::new(move |f: &mut ::std::fmt::Formatter| { write![f, $($fmt),*] }) )); |
| 103 | + }}; |
| 104 | +} |
0 commit comments