From ec356713981974b5a29cbcda2545ab9c878550b8 Mon Sep 17 00:00:00 2001 From: Avril Date: Tue, 16 Feb 2021 21:19:26 +0000 Subject: [PATCH] added config speicifc printing macros --- src/config.rs | 30 ++++++++++++++++++++++++++++++ src/work.rs | 6 +----- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/config.rs b/src/config.rs index ece5ea0..5e66e6c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -54,6 +54,8 @@ pub struct Config pub recursive: Recursion, pub max_tasks: Option, + pub silent: bool, + #[cfg(feature="inspect")] pub serialise_output: Option>, // Some(None) means dump to `stdout` } @@ -94,6 +96,7 @@ impl Default for Config paths: Vec::new(), recursive: Default::default(), max_tasks: None, //max_tasks_cpus(), + silent: false, #[cfg(feature="inspect")] serialise_output: None, } @@ -145,3 +148,30 @@ impl fmt::Display for InvalidConfigError } } } + +/// Print an error line in accordance with `Config`'s output directives. +#[macro_export] macro_rules! cfg_eprintln { + ($cfg:expr, $fmt:literal $($tt:tt)*) => { + { + if !$cfg.silent { + eprintln!($fmt $($tt)*); + } + } + } +} + +/// Print a line in accordance with `Config`'s output directives. +#[macro_export] macro_rules! cfg_println { + ($cfg:expr, $fmt:literal $($tt:tt)*) => { + { + let cfg = &$cfg; + if !cfg.silent { + if cfg.is_using_stdout() { + eprintln!($fmt $($tt)*); + } else { + println!($fmt $($tt)*); + } + } + } + } +} diff --git a/src/work.rs b/src/work.rs index f36125b..b95a615 100644 --- a/src/work.rs +++ b/src/work.rs @@ -88,11 +88,7 @@ fn walk(state: State, root: PathBuf, root_ino: INode) -> BoxFuture<'static, Hash async move { { let _guard = state.lock().enter().await; - if state.config().is_using_stdout() { - eprintln!(" -> {:?}", root); - } else { - println!(" -> {:?}", root); - } + cfg_println!(state.config(), " -> {:?}", root); match fs::read_dir(&root).await { Ok(mut dir) => {