Type Definition fast_logger::Compatibility

source ·
pub type Compatibility = Box<dyn Fn(u8, Box<dyn Fn(&mut Formatter<'_>) -> Result + Send + Sync>)>;
Expand description

Compatibility type when using loggers across library boundaries

Example

Here, the host as well as MyLibrary can both use any logger they wish as they are decoupled with respect to their loggers. The only bridge between them is the Compatibility type.

This is especially useful so a library can expose the compatibility type, while hosts of that library can pick and choose which logger to connect to it.

use fast_logger::*;
fn main() {
    let logger = Logger::<Generic>::spawn("tst");

    type MyCompatibility = Box<dyn Fn(u8, Box<dyn Fn(&mut std::fmt::Formatter) -> std::fmt::Result + Send + Sync>)>;
    struct MyLibrary {
        log: Logpass,
    }

    impl MyLibrary {
        pub fn new(log: MyCompatibility) -> Self {
            Self {
                log: Logpass::from_compatibility(log),
            }
        }
        pub fn function(&mut self) {
            info!(self.log, "Compatibility layer");
        }
    }

    let mut my_lib = MyLibrary::new(logger.to_compatibility());
    my_lib.function();
}