Skip to main content

ferroui_splat/logging/
log_manager.rs

1//! `ILogManager`, `LogManagerExtensions` and `FuncLogManager`.
2
3use super::IFullLogger;
4use std::rc::Rc;
5
6/// Hands out the logger of a type (`ILogManager`).
7pub trait ILogManager {
8    /// The logger for the type named `type_name` (`GetLogger(Type)`).
9    fn get_logger(&self, type_name: &'static str) -> Rc<dyn IFullLogger>;
10}
11
12/// The generic form of [`ILogManager::get_logger`] (`LogManagerExtensions`).
13pub trait LogManagerExt: ILogManager {
14    /// The logger for the type `T` (`GetLogger<T>()`).
15    fn get_logger_for<T: ?Sized>(&self) -> Rc<dyn IFullLogger> {
16        self.get_logger(std::any::type_name::<T>())
17    }
18}
19
20impl<M: ILogManager + ?Sized> LogManagerExt for M {}
21
22/// A log manager over a function (`FuncLogManager`).
23pub struct FuncLogManager {
24    get_logger_func: Box<dyn Fn(&'static str) -> Rc<dyn IFullLogger>>,
25}
26
27impl FuncLogManager {
28    /// A log manager whose loggers `get_logger_func` returns.
29    pub fn new(get_logger_func: impl Fn(&'static str) -> Rc<dyn IFullLogger> + 'static) -> Self {
30        Self { get_logger_func: Box::new(get_logger_func) }
31    }
32}
33
34impl ILogManager for FuncLogManager {
35    fn get_logger(&self, type_name: &'static str) -> Rc<dyn IFullLogger> {
36        (self.get_logger_func)(type_name)
37    }
38}