Skip to content

Commit

Permalink
Merge pull request #96 from jcronenberg/better_logs
Browse files Browse the repository at this point in the history
Improve log output
  • Loading branch information
cfconrad authored Sep 25, 2024
2 parents e00e332 + 96af746 commit b56e50f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
1 change: 1 addition & 0 deletions rust/migrate-wicked/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rust/migrate-wicked/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ serde_ignored = "0.1.9"
uuid = { version = "1.3.4", features = ["v4"] }
macaddr = "1.0"
dotenv = "0.15.0"
serde_path_to_error = "0.1.16"

[[bin]]
name = "migrate-wicked"
Expand Down
14 changes: 10 additions & 4 deletions rust/migrate-wicked/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use log::*;
use migrate::migrate;
use reader::read as wicked_read;
use serde::Serialize;
use simplelog::ConfigBuilder;
use std::process::{ExitCode, Termination};
use tokio::sync::OnceCell;

Expand All @@ -33,7 +34,7 @@ struct Cli {

#[derive(Debug, Args)]
struct GlobalOpts {
#[arg(long, global = true, default_value_t = LevelFilter::Warn, value_parser = clap::builder::PossibleValuesParser::new(["TRACE", "DEBUG", "INFO", "WARN", "ERROR"]).map(|s| s.parse::<LevelFilter>().unwrap()),)]
#[arg(long, global = true, default_value_t = LevelFilter::Info, value_parser = clap::builder::PossibleValuesParser::new(["TRACE", "DEBUG", "INFO", "WARN", "ERROR"]).map(|s| s.parse::<LevelFilter>().unwrap()),)]
pub log_level: LevelFilter,

#[arg(long, global = true, env = "MIGRATE_WICKED_WITHOUT_NETCONFIG")]
Expand Down Expand Up @@ -141,7 +142,7 @@ async fn run_command(cli: Cli) -> anyhow::Result<()> {

match migrate(paths).await {
Ok(()) => Ok(()),
Err(e) => Err(anyhow::anyhow!("Migration failed: {:?}", e)),
Err(e) => Err(anyhow::anyhow!("Migration failed: {}", e)),
}
}
}
Expand Down Expand Up @@ -188,16 +189,21 @@ static MIGRATION_SETTINGS: OnceCell<MigrationSettings> = OnceCell::const_new();
async fn main() -> CliResult {
let cli = Cli::parse();

let config = ConfigBuilder::new()
.set_time_level(LevelFilter::Off)
.add_filter_allow("migrate_wicked".to_string())
.build();

simplelog::TermLogger::init(
cli.global_opts.log_level,
simplelog::Config::default(),
config,
simplelog::TerminalMode::Stderr,
simplelog::ColorChoice::Auto,
)
.unwrap();

if let Err(error) = run_command(cli).await {
eprintln!("{:?}", error);
log::error!("{}", error);
return CliResult::Error;
}

Expand Down
15 changes: 13 additions & 2 deletions rust/migrate-wicked/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,20 @@ pub fn read_xml_file(path: PathBuf) -> Result<InterfacesResult, anyhow::Error> {
let replaced_string = replace_colons(contents.as_str());
let deserializer = &mut quick_xml::de::Deserializer::from_str(replaced_string.as_str());
let mut unhandled_fields = vec![];
let interfaces: Vec<Interface> = serde_ignored::deserialize(deserializer, |path| {
let interfaces: Vec<Interface> = match serde_ignored::deserialize(deserializer, |path| {
unhandled_fields.push(path.to_string());
})?;
}) {
Ok(interfaces) => interfaces,
Err(e) => {
let deserializer2 =
&mut quick_xml::de::Deserializer::from_str(replaced_string.as_str());
let res: Result<Vec<Interface>, _> = serde_path_to_error::deserialize(deserializer2);
if let Err(path_error) = res {
log::error!("Error at {}: {}", path_error.path().to_string(), e);
}
return Err(e.into());
}
};
let mut result = InterfacesResult {
interfaces,
netconfig: None,
Expand Down

0 comments on commit b56e50f

Please sign in to comment.