//! Trusty simple logger backend //! //! Logs to stderr based on a compile-time configured log level.
use log::{Level, Log, Metadata, Record}; use std::io::{stderr, Write}; use std::sync::OnceLock;
/// Gets the log level based on the `debug_level`. #[macro_export]
macro_rules! get_debug_level {
() => {{ match () {
_ if cfg!(debug_level = "5") => log::Level::Trace,
_ if cfg!(debug_level = "4") => log::Level::Debug,
_ => log::Level::Info,
}
}};
}
/// Closure type that can be used by external callers to write a custom log formatter /// /// # Examples /// /// ``` /// fn log_function(record: &log::Record) -> String { /// let line = match record.line() { /// Some(line) => line, /// None => 0, /// }; /// let file = match record.file() { /// Some(file) => file, /// None => "unknown file", /// }; /// format!("{}: MyApp - {}:{} {}\n", record.level(), file, line, record.args()) /// } /// ``` type FormatFn = Box<dynFn(&log::Record) -> String + Sync + Send>;
/// Structure used to modify the logger behavior. It is based on the Android logger configuration /// and implements a subset of its functionality. /// It can be used to override the maximum logging level and to provide a custom log formatting /// function. /// Its default values are Level::Info for its log level and None for its formatter. /// /// # Examples /// /// ``` /// let config = trusty_log::TrustyLoggerConfig::default() /// .with_min_level(log::Level::Trace) /// .format(&log_function); /// ``` pubstruct TrustyLoggerConfig {
log_level: log::Level,
custom_format: Option<FormatFn>,
}
/// Main structure used by the logger operations. /// The default values for its config are Level::Info for the log level and None for the formatter. pubstruct TrustyLogger {
config: TrustyLoggerConfig,
}
pubfn init_with_config(config: TrustyLoggerConfig) { let log_level_filter = config.log_level.to_level_filter(); let global_logger = LOGGER.get_or_init(|| TrustyLogger::new(config));
log::set_logger(global_logger).expect("Could not set global logger");
log::set_max_level(log_level_filter);
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.10 Sekunden
(vorverarbeitet am 2026-06-27)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.