mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
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 <rbradford@meta.com>
This commit is contained in:
47
cloud-hypervisor/src/logger.rs
Normal file
47
cloud-hypervisor/src/logger.rs
Normal file
@@ -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<Box<dyn std::io::Write + Send>>,
|
||||
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) {}
|
||||
}
|
||||
Reference in New Issue
Block a user