mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
Unify log formatting and printing as `eprintln!` and `log::error!` would be used alongside each other. When using e.g. `env_logger` lines printed with `eprintln!` would lack formatting / colors. Currently only relevant in `ch-remote` + `cli_print_error_chain`. Note that the replaced messages now also end up in the logfile of `cloud-hypervisor` when configured and not any longer in stderr. Signed-off-by: Maximilian Güntner <code@mguentner.de>
47 lines
1.4 KiB
Rust
47 lines
1.4 KiB
Rust
// Copyright © 2025 Cyberus Technology GmbH
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
use std::error::Error;
|
|
|
|
use log::error;
|
|
|
|
/// Prints a chain of errors to the user in a consistent manner.
|
|
/// The user will see a clear chain of errors, followed by debug output
|
|
/// for opening issues.
|
|
pub fn cli_print_error_chain<'a>(
|
|
top_error: &'a (dyn Error + 'static),
|
|
component: &str,
|
|
// Function optionally returning the display representation of an error.
|
|
display_modifier: impl Fn(
|
|
/* level */ usize,
|
|
/*indention */ usize,
|
|
&'a (dyn Error + 'static),
|
|
) -> Option<String>,
|
|
) {
|
|
eprint!("Error: {component} exited with the following ");
|
|
if top_error.source().is_none() {
|
|
error!("error:");
|
|
error!(" {top_error}");
|
|
} else {
|
|
error!("chain of errors:");
|
|
std::iter::successors(Some(top_error), |sub_error| {
|
|
// Dereference necessary to mitigate rustc compiler bug.
|
|
// See <https://github.com/rust-lang/rust/issues/141673>
|
|
(*sub_error).source()
|
|
})
|
|
.enumerate()
|
|
.for_each(|(level, error)| {
|
|
// Special case: handling of HTTP Server responses in ch-remote
|
|
if let Some(message) = display_modifier(level, 2, error) {
|
|
error!("{message}");
|
|
} else {
|
|
error!(" {level}: {error}");
|
|
}
|
|
});
|
|
}
|
|
|
|
error!("");
|
|
error!("Debug Info: {top_error:?}");
|
|
}
|