Skip to content

Commit

Permalink
fix: Logger initialization function
Browse files Browse the repository at this point in the history
Adds a logger initialization function that allows removing the `log`
dependencies.

Signed-off-by: Jonathan Woollett-Light <[email protected]>
  • Loading branch information
Jonathan Woollett-Light authored and JonathanWoollett-Light committed Oct 6, 2023
1 parent 332f218 commit 3d07747
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/firecracker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 2 additions & 3 deletions src/firecracker/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)?;
Expand Down
10 changes: 10 additions & 0 deletions src/vmm/src/logger/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/vmm/src/logger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3d07747

Please sign in to comment.