From 661faf51fe6c6b81b86aa37a0c3c3760e84de1bc Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Tue, 28 Apr 2026 14:27:44 +0100 Subject: [PATCH] main: Move logger into its own mod In preparation for extending its functionality, refactor the Logger struct and its implementation to a new file / module. Assisted-by: Claude:Opus-4.6 Signed-off-by: Rob Bradford --- cloud-hypervisor/src/logger.rs | 47 ++++++++++++++++++++++++++++++++++ cloud-hypervisor/src/main.rs | 44 +++---------------------------- 2 files changed, 50 insertions(+), 41 deletions(-) create mode 100644 cloud-hypervisor/src/logger.rs diff --git a/cloud-hypervisor/src/logger.rs b/cloud-hypervisor/src/logger.rs new file mode 100644 index 000000000..bd2b51345 --- /dev/null +++ b/cloud-hypervisor/src/logger.rs @@ -0,0 +1,47 @@ +// Copyright © 2026 Cloud Hypervisor Contributors +// +// SPDX-License-Identifier: Apache-2.0 +// + +use std::sync::Mutex; + +pub struct Logger { + pub output: Mutex>, + pub start: std::time::Instant, +} + +impl log::Log for Logger { + fn enabled(&self, _metadata: &log::Metadata) -> bool { + true + } + + fn log(&self, record: &log::Record) { + if !self.enabled(record.metadata()) { + return; + } + + let now = std::time::Instant::now(); + let duration = now.duration_since(self.start); + let duration_s = duration.as_secs_f32(); + + let location = if let (Some(file), Some(line)) = (record.file(), record.line()) { + format!("{file}:{line}") + } else { + record.target().to_string() + }; + + let mut out = self.output.lock().unwrap(); + write!( + &mut *out, + // 10: 6 decimal places + sep => whole seconds in range `0..=999` properly aligned + "cloud-hypervisor: {:>10.6?}s: <{}> {}:{} -- {}\r\n", + duration_s, + std::thread::current().name().unwrap_or("anonymous"), + record.level(), + location, + record.args(), + ) + .ok(); + } + fn flush(&self) {} +} diff --git a/cloud-hypervisor/src/main.rs b/cloud-hypervisor/src/main.rs index 055d64981..55484e128 100644 --- a/cloud-hypervisor/src/main.rs +++ b/cloud-hypervisor/src/main.rs @@ -3,6 +3,7 @@ // SPDX-License-Identifier: Apache-2.0 // +mod logger; #[cfg(test)] mod test_util; @@ -39,6 +40,8 @@ use vmm::vm_config::{ use vmm_sys_util::eventfd::EventFd; use vmm_sys_util::signal::block_signal; +use crate::logger::Logger; + #[cfg(feature = "dhat-heap")] #[global_allocator] static ALLOC: dhat::Alloc = dhat::Alloc; @@ -116,47 +119,6 @@ enum FdTableError { Dup2(#[source] std::io::Error), } -struct Logger { - output: Mutex>, - start: std::time::Instant, -} - -impl log::Log for Logger { - fn enabled(&self, _metadata: &log::Metadata) -> bool { - true - } - - fn log(&self, record: &log::Record) { - if !self.enabled(record.metadata()) { - return; - } - - let now = std::time::Instant::now(); - let duration = now.duration_since(self.start); - let duration_s = duration.as_secs_f32(); - - let location = if let (Some(file), Some(line)) = (record.file(), record.line()) { - format!("{file}:{line}") - } else { - record.target().to_string() - }; - - let mut out = self.output.lock().unwrap(); - write!( - &mut *out, - // 10: 6 decimal places + sep => whole seconds in range `0..=999` properly aligned - "cloud-hypervisor: {:>10.6?}s: <{}> {}:{} -- {}\r\n", - duration_s, - std::thread::current().name().unwrap_or("anonymous"), - record.level(), - location, - record.args(), - ) - .ok(); - } - fn flush(&self) {} -} - fn prepare_default_values() -> (String, String, String) { (default_vcpus(), default_memory(), default_rng()) }