From fae32412dcb76b4942245aeab2839811873521f1 Mon Sep 17 00:00:00 2001 From: Philipp Schuster Date: Tue, 26 Aug 2025 16:25:48 +0200 Subject: [PATCH] vmm: logger: improve code, remove duplication Signed-off-by: Philipp Schuster On-behalf-of: SAP philipp.schuster@sap.com --- src/main.rs | 39 ++++++++++++++++----------------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/src/main.rs b/src/main.rs index 020cd3516..19a4d4eab 100644 --- a/src/main.rs +++ b/src/main.rs @@ -135,30 +135,23 @@ impl log::Log for Logger { let duration = now.duration_since(self.start); let duration_s = duration.as_secs_f32(); - if record.file().is_some() && record.line().is_some() { - write!( - *(*(self.output.lock().unwrap())), - // 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(), - record.file().unwrap(), - record.line().unwrap(), - record.args() - ) + let location = if let (Some(file), Some(line)) = (record.file(), record.line()) { + format!("{}:{}", file, line) } else { - write!( - *(*(self.output.lock().unwrap())), - // 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(), - record.target(), - record.args() - ) - } + 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) {}