Skip to main content

ferroui_splat/logging/
log_host.rs

1//! `LogHost`, `IEnableLogger` and `LogHostExtensions`.
2
3use super::{IFullLogger, ILogManager, LoggingException, StaticFullLogger};
4use crate::service_location::{AppLocator, ReadonlyDependencyResolverExt};
5use std::rc::Rc;
6
7/// The loggers of the application, from the `dyn ILogManager` of the current resolver of
8/// [`AppLocator`] (`LogHost`).
9pub struct LogHost;
10
11impl LogHost {
12    /// A static logger for `LogHost` (`LogHost.Default`).
13    ///
14    /// Panics with a [`LoggingException`] when the current resolver has no log manager.
15    pub fn default_logger() -> StaticFullLogger {
16        let Some(factory) = AppLocator::current().get_service::<dyn ILogManager>() else {
17            std::panic::panic_any(LoggingException::with_message(
18                "ILogManager is null. This should never happen, your dependency resolver is broken",
19            ));
20        };
21        let full_logger = factory.get_logger(std::any::type_name::<LogHost>());
22        StaticFullLogger::new(full_logger)
23    }
24}
25
26/// A type that logs through the logger of its type (`IEnableLogger`).
27///
28/// `this.Log()` of upstream is the provided [`log`](Self::log).
29pub trait IEnableLogger {
30    /// The logger of the type, from the `dyn ILogManager` of the current resolver of
31    /// [`AppLocator`] (`LogHostExtensions.Log`).
32    ///
33    /// Panics when the current resolver has no log manager (`InvalidOperationException`).
34    fn log(&self) -> Rc<dyn IFullLogger> {
35        let Some(factory) = AppLocator::current().get_service::<dyn ILogManager>() else {
36            panic!("ILogManager is null. This should never happen, your dependency resolver is broken");
37        };
38        factory.get_logger(std::any::type_name::<Self>())
39    }
40}