From 3d0774772e49f2800a63233bc9807d7118aae019 Mon Sep 17 00:00:00 2001 From: Jonathan Woollett-Light Date: Tue, 26 Sep 2023 10:39:53 +0100 Subject: [PATCH] fix: Logger initialization function Adds a logger initialization function that allows removing the `log` dependencies. Signed-off-by: Jonathan Woollett-Light --- Cargo.lock | 1 - src/firecracker/Cargo.toml | 1 - src/firecracker/src/main.rs | 5 ++--- src/vmm/src/logger/logging.rs | 10 ++++++++++ src/vmm/src/logger/mod.rs | 4 ++-- 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 059bbe5127c..ba2ea6c69e2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -538,7 +538,6 @@ dependencies = [ "displaydoc", "event-manager", "libc", - "log", "regex", "seccompiler", "serde", diff --git a/src/firecracker/Cargo.toml b/src/firecracker/Cargo.toml index c3e5e9092e6..105ff1b2f95 100644 --- a/src/firecracker/Cargo.toml +++ b/src/firecracker/Cargo.toml @@ -16,7 +16,6 @@ bench = false displaydoc = "0.2.4" event-manager = "0.3.0" libc = "0.2.148" -log = "0.4.20" serde_json = "1.0.107" thiserror = "1.0.49" timerfd = "1.5.0" diff --git a/src/firecracker/src/main.rs b/src/firecracker/src/main.rs index 2539ae50b52..b39ef1cbf67 100644 --- a/src/firecracker/src/main.rs +++ b/src/firecracker/src/main.rs @@ -43,7 +43,7 @@ const MMDS_CONTENT_ARG: &str = "metadata"; #[derive(Debug, thiserror::Error, displaydoc::Display)] enum MainError { /// Failed to set the logger: {0} - SetLogger(log::SetLoggerError), + SetLogger(vmm::logger::LoggerInitError), /// Failed to register signal handlers: {0} RegisterSignalHandlers(#[source] utils::errno::Error), /// Arguments parsing error: {0} \n\nFor more information try --help. @@ -103,8 +103,7 @@ fn main() -> ExitCode { fn main_exec() -> Result<(), MainError> { // Initialize the logger. - log::set_logger(&LOGGER).map_err(MainError::SetLogger)?; - log::set_max_level(vmm::logger::DEFAULT_LEVEL); + LOGGER.init().map_err(MainError::SetLogger)?; info!("Running Firecracker v{FIRECRACKER_VERSION}"); register_signal_handlers().map_err(MainError::RegisterSignalHandlers)?; diff --git a/src/vmm/src/logger/logging.rs b/src/vmm/src/logger/logging.rs index 04f6fb61181..fb9a27eacc1 100644 --- a/src/vmm/src/logger/logging.rs +++ b/src/vmm/src/logger/logging.rs @@ -36,12 +36,22 @@ pub static LOGGER: Logger = Logger(Mutex::new(LoggerConfiguration { }, })); +/// Error type for [`Logger::init`]. +pub type LoggerInitError = log::SetLoggerError; + /// Error type for [`Logger::update`]. #[derive(Debug, thiserror::Error)] #[error("Failed to open target file: {0}")] pub struct LoggerUpdateError(pub std::io::Error); impl Logger { + /// Initialize the logger. + pub fn init(&'static self) -> Result<(), LoggerInitError> { + log::set_logger(self)?; + log::set_max_level(DEFAULT_LEVEL); + Ok(()) + } + /// Applies the given logger configuration the logger. pub fn update(&self, config: LoggerConfig) -> Result<(), LoggerUpdateError> { let mut guard = self.0.lock().unwrap(); diff --git a/src/vmm/src/logger/mod.rs b/src/vmm/src/logger/mod.rs index 31272585e32..42cbb21e735 100644 --- a/src/vmm/src/logger/mod.rs +++ b/src/vmm/src/logger/mod.rs @@ -11,8 +11,8 @@ mod metrics; pub use log::{debug, error, info, log_enabled, trace, warn, Level}; pub use logging::{ - LevelFilter, LevelFilterFromStrError, LoggerConfig, LoggerUpdateError, DEFAULT_INSTANCE_ID, - DEFAULT_LEVEL, INSTANCE_ID, LOGGER, + LevelFilter, LevelFilterFromStrError, LoggerConfig, LoggerInitError, LoggerUpdateError, + DEFAULT_INSTANCE_ID, DEFAULT_LEVEL, INSTANCE_ID, LOGGER, }; #[cfg(target_arch = "aarch64")] pub use metrics::RTCDeviceMetrics;