From 804dc4a95fbbef41d42363e1f10acbbf040630f3 Mon Sep 17 00:00:00 2001 From: gexuyang Date: Wed, 9 Mar 2022 15:16:34 +0800 Subject: [PATCH 1/3] refactor: nydus-image: prepare ArgMatches in a new function The main function is too long to understand its logic. Refactor its ArgMatches logic by extracting it into a new function Signed-off-by: gexuyang --- src/bin/nydus-image/main.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/bin/nydus-image/main.rs b/src/bin/nydus-image/main.rs index 8cdf9bea30b..f654da77f71 100644 --- a/src/bin/nydus-image/main.rs +++ b/src/bin/nydus-image/main.rs @@ -19,7 +19,7 @@ use std::fs::{self, metadata, DirEntry, File, OpenOptions}; use std::path::{Path, PathBuf}; use anyhow::{bail, Context, Result}; -use clap::{App, Arg, SubCommand}; +use clap::{App, Arg, ArgMatches, SubCommand}; use nix::unistd::{getegid, geteuid}; use serde::{Deserialize, Serialize}; @@ -132,11 +132,9 @@ impl OutputSerializer { } } -fn main() -> Result<()> { - let (bti_string, build_info) = BuildTimeInfo::dump(crate_version!()); - +fn prepare_cmd_args(bti_string: String) -> ArgMatches<'static> { // TODO: Try to use yaml to define below options - let cmd = App::new("") + App::new("") .version(bti_string.as_str()) .author(crate_authors!()) .about("Build or inspect RAFS filesystems for nydus accelerated container images.") @@ -470,7 +468,13 @@ fn main() -> Result<()> { .required(false) .global(true), ) - .get_matches(); + .get_matches() +} + +fn main() -> Result<()> { + let (bti_string, build_info) = BuildTimeInfo::dump(crate_version!()); + + let cmd = prepare_cmd_args(bti_string); // Safe to unwrap because it has a default value and possible values are defined. let level = cmd.value_of("log-level").unwrap().parse().unwrap(); From ab9cce788bdd335300e8eee0b7846ee351231e97 Mon Sep 17 00:00:00 2001 From: gexuyang Date: Wed, 9 Mar 2022 15:47:49 +0800 Subject: [PATCH 2/3] nydus-image: add log-file to set log output Signed-off-by: gexuyang --- src/bin/nydus-image/main.rs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/bin/nydus-image/main.rs b/src/bin/nydus-image/main.rs index f654da77f71..dfc64757164 100644 --- a/src/bin/nydus-image/main.rs +++ b/src/bin/nydus-image/main.rs @@ -457,6 +457,15 @@ fn prepare_cmd_args(bti_string: String) -> ArgMatches<'static> { .help("path to JSON output file") .takes_value(true)) ) + .arg( + Arg::with_name("log-file") + .long("log-file") + .short("o") + .help("Specify log file name") + .takes_value(true) + .required(false) + .global(true), + ) .arg( Arg::with_name("log-level") .long("log-level") @@ -471,14 +480,25 @@ fn prepare_cmd_args(bti_string: String) -> ArgMatches<'static> { .get_matches() } +fn init_log(matches: &ArgMatches) -> Result<()> { + let mut log_file = None; + if let Some(file) = matches.value_of("log-file") { + let path = PathBuf::from(file); + log_file = Some(path); + } + + // Safe to unwrap because it has a default value and possible values are defined. + let level = matches.value_of("log-level").unwrap().parse().unwrap(); + + setup_logging(log_file, level).context("failed to setup logging") +} + fn main() -> Result<()> { let (bti_string, build_info) = BuildTimeInfo::dump(crate_version!()); let cmd = prepare_cmd_args(bti_string); - // Safe to unwrap because it has a default value and possible values are defined. - let level = cmd.value_of("log-level").unwrap().parse().unwrap(); - setup_logging(None, level)?; + init_log(&cmd)?; register_tracer!(TraceClass::Timing, TimingTracerClass); register_tracer!(TraceClass::Event, EventTracerClass); From 0c0fa920b09dd6e2e592d8ee67cd712974fd2379 Mon Sep 17 00:00:00 2001 From: gexuyang Date: Wed, 9 Mar 2022 16:16:46 +0800 Subject: [PATCH 3/3] nydus-image: add log info in diff build Signed-off-by: gexuyang --- src/bin/nydus-image/builder/diff.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/bin/nydus-image/builder/diff.rs b/src/bin/nydus-image/builder/diff.rs index 306eb1f174f..afb9693c31b 100644 --- a/src/bin/nydus-image/builder/diff.rs +++ b/src/bin/nydus-image/builder/diff.rs @@ -706,9 +706,13 @@ impl DiffBuilder { let ctx = Arc::new(ctx.clone()); let hint_path_idx = idx + base; let hint_path = paths[hint_path_idx].clone(); + let snapshot_path = paths[idx].clone(); let chunk_dict = chunk_dict.clone(); let worker = thread::spawn(move || -> Result<(Option, ChunkMap)> { - info!("[{}] diff building with hint {:?}", idx, hint_path); + info!( + "[{}: {:?}] diff building with hint {:?}", + idx, snapshot_path, hint_path + ); let snapshot_idx = idx as u32; let mut blob_nodes = walk_all(ctx.as_ref(), hint_path.clone(), hint_path)?;