From 9332a933f8aef6d06f686c0d96f7bdd564520863 Mon Sep 17 00:00:00 2001 From: Erik Schilling Date: Thu, 6 Jul 2023 19:09:01 +0200 Subject: [PATCH] tree-wide: actually pretty-print error messages We setup pretty-printing with all the thiserror-based Error structs, but then only bubble the error up to the main, where it just gets printed with the `Debug` trait. This "catches" the error in the main and performs pretty printing using the `Display` trait. Before: Error: FailedCreatingListener(SocketError(Os { code: 98, kind: AddrInUse, message: "Address already in use" })) After: [2023-07-06T17:20:47Z ERROR vhost_device_scsi] Failed creating listener: socket error: Address already in use (os error 98) vhost-device-vsock is a bit special since it does not let error messages bubble up to the main. It also does .unwrap() in most places, but it _does_ pretty print errors during the main request handling part. Had to slightly adjust the coverage since we have no tests for the main functions. Signed-off-by: Erik Schilling --- coverage_config_x86_64.json | 2 +- crates/gpio/src/backend.rs | 10 +++++++--- crates/gpio/src/main.rs | 2 +- crates/i2c/src/main.rs | 10 +++++++--- crates/rng/src/main.rs | 10 +++++++--- crates/scsi/src/main.rs | 14 ++++++++++++-- 6 files changed, 35 insertions(+), 13 deletions(-) diff --git a/coverage_config_x86_64.json b/coverage_config_x86_64.json index 9cf6dcc..d528940 100644 --- a/coverage_config_x86_64.json +++ b/coverage_config_x86_64.json @@ -1,5 +1,5 @@ { - "coverage_score": 69.6, + "coverage_score": 69.0, "exclude_path": "", "crate_features": "" } diff --git a/crates/gpio/src/backend.rs b/crates/gpio/src/backend.rs index dec98f8..aff6913 100644 --- a/crates/gpio/src/backend.rs +++ b/crates/gpio/src/backend.rs @@ -5,8 +5,9 @@ // // SPDX-License-Identifier: Apache-2.0 or BSD-3-Clause -use log::{info, warn}; +use log::{error, info, warn}; use std::num::ParseIntError; +use std::process::exit; use std::sync::{Arc, RwLock}; use std::thread::spawn; @@ -184,10 +185,13 @@ fn start_backend(args: GpioArgs) -> Resul Ok(()) } -pub(crate) fn gpio_init() -> Result<()> { +pub(crate) fn gpio_init() { env_logger::init(); - start_backend::(GpioArgs::parse()) + if let Err(e) = start_backend::(GpioArgs::parse()) { + error!("{e}"); + exit(1); + } } #[cfg(test)] diff --git a/crates/gpio/src/main.rs b/crates/gpio/src/main.rs index 075b044..9dcb24d 100644 --- a/crates/gpio/src/main.rs +++ b/crates/gpio/src/main.rs @@ -13,7 +13,7 @@ mod gpio; mod vhu_gpio; #[cfg(target_env = "gnu")] -fn main() -> backend::Result<()> { +fn main() { backend::gpio_init() } diff --git a/crates/i2c/src/main.rs b/crates/i2c/src/main.rs index 288e254..be90ecb 100644 --- a/crates/i2c/src/main.rs +++ b/crates/i2c/src/main.rs @@ -8,8 +8,9 @@ mod i2c; mod vhu_i2c; -use log::{info, warn}; +use log::{error, info, warn}; use std::num::ParseIntError; +use std::process::exit; use std::sync::{Arc, RwLock}; use std::thread::spawn; @@ -233,10 +234,13 @@ fn start_backend(args: I2cArgs) -> Result< Ok(()) } -fn main() -> Result<()> { +fn main() { env_logger::init(); - start_backend::(I2cArgs::parse()) + if let Err(e) = start_backend::(I2cArgs::parse()) { + error!("{e}"); + exit(1); + } } #[cfg(test)] diff --git a/crates/rng/src/main.rs b/crates/rng/src/main.rs index 5d550bb..30bcc96 100644 --- a/crates/rng/src/main.rs +++ b/crates/rng/src/main.rs @@ -5,8 +5,9 @@ // SPDX-License-Identifier: Apache-2.0 or BSD-3-Clause mod vhu_rng; -use log::{info, warn}; +use log::{error, info, warn}; use std::fs::File; +use std::process::exit; use std::sync::{Arc, Mutex, RwLock}; use std::thread; @@ -161,10 +162,13 @@ pub(crate) fn start_backend(config: VuRngConfig) -> Result<()> { Ok(()) } -fn main() -> Result<()> { +fn main() { env_logger::init(); - start_backend(VuRngConfig::try_from(RngArgs::parse()).unwrap()) + if let Err(e) = start_backend(VuRngConfig::try_from(RngArgs::parse()).unwrap()) { + error!("{e}"); + exit(1); + } } #[cfg(test)] diff --git a/crates/scsi/src/main.rs b/crates/scsi/src/main.rs index 9e7813f..9a4c733 100644 --- a/crates/scsi/src/main.rs +++ b/crates/scsi/src/main.rs @@ -7,6 +7,7 @@ mod virtio; use std::{ fs::File, path::PathBuf, + process::exit, sync::{Arc, RwLock}, }; @@ -126,11 +127,20 @@ fn start_backend(backend: VhostUserScsiBackend, args: ScsiArgs) -> Result<()> { Ok(()) } -fn main() -> Result<()> { +fn run() -> Result<()> { env_logger::init(); let args = ScsiArgs::parse(); let backend = create_backend(&args)?; - start_backend(backend, args) + start_backend(backend, args)?; + + Ok(()) +} + +fn main() { + if let Err(e) = run() { + error!("{e}"); + exit(1); + } } #[cfg(test)]