Skip to content

Commit

Permalink
tree-wide: actually pretty-print error messages
Browse files Browse the repository at this point in the history
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.

Signed-off-by: Erik Schilling <[email protected]>
  • Loading branch information
Ablu committed Jul 6, 2023
1 parent 055f27f commit 750301e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
10 changes: 7 additions & 3 deletions crates/gpio/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -184,10 +185,13 @@ fn start_backend<D: 'static + GpioDevice + Send + Sync>(args: GpioArgs) -> Resul
Ok(())
}

pub(crate) fn gpio_init() -> Result<()> {
pub(crate) fn gpio_init() {
env_logger::init();

start_backend::<PhysDevice>(GpioArgs::parse())
if let Err(e) = start_backend::<PhysDevice>(GpioArgs::parse()) {
error!("{e}");
exit(1);
}
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion crates/gpio/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod gpio;
mod vhu_gpio;

#[cfg(target_env = "gnu")]
fn main() -> backend::Result<()> {
fn main() {
backend::gpio_init()
}

Expand Down
10 changes: 7 additions & 3 deletions crates/i2c/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -233,10 +234,13 @@ fn start_backend<D: 'static + I2cDevice + Send + Sync>(args: I2cArgs) -> Result<
Ok(())
}

fn main() -> Result<()> {
fn main() {
env_logger::init();

start_backend::<PhysDevice>(I2cArgs::parse())
if let Err(e) = start_backend::<PhysDevice>(I2cArgs::parse()) {
error!("{e}");
exit(1);
}
}

#[cfg(test)]
Expand Down
10 changes: 7 additions & 3 deletions crates/rng/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)]
Expand Down
14 changes: 12 additions & 2 deletions crates/scsi/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod virtio;
use std::{
fs::File,
path::PathBuf,
process::exit,
sync::{Arc, RwLock},
};

Expand Down Expand Up @@ -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)]
Expand Down

0 comments on commit 750301e

Please sign in to comment.