From 3358982c314a9888c1edb12bad4ec553ac3c4b3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20Br=C3=A9ard?= Date: Sun, 26 Mar 2023 21:49:34 +0200 Subject: [PATCH] Add verbose to the CLI --- src/config.rs | 6 ++++++ src/logs.rs | 13 ++++++++++++- src/main.rs | 4 ++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/config.rs b/src/config.rs index 3c537de..9176d22 100644 --- a/src/config.rs +++ b/src/config.rs @@ -25,6 +25,8 @@ pub struct Config { revocation_list: Option, #[arg(short = 'x', long, default_value_t = 1296000)] expiration: u64, + #[arg(short, long, action = clap::ArgAction::Count)] + verbose: u8, } impl Config { @@ -63,6 +65,10 @@ impl Config { pub fn headers_optional(&self) -> &[String] { &self.header_optional } + + pub fn verbosity(&self) -> log::LevelFilter { + crate::logs::log_level(self.verbose) + } } fn process_domains(lst: &[String], domain_file: &Option) -> Result, String> { diff --git a/src/logs.rs b/src/logs.rs index 2727784..96147cf 100644 --- a/src/logs.rs +++ b/src/logs.rs @@ -1,7 +1,9 @@ +use crate::config::Config; use env_logger::{Builder, Env, Target}; +use log::LevelFilter; use std::io::Write; -pub fn init_log_system() { +pub fn init_log_system(cnf: &Config) { let env = Env::new().filter_or(crate::LOG_LEVEL_ENV_VAR, "warn"); let mut builder = Builder::from_env(env); builder.format(|buf, record| { @@ -13,5 +15,14 @@ pub fn init_log_system() { ) }); builder.target(Target::Stderr); + builder.filter_level(cnf.verbosity()); builder.init(); } + +pub fn log_level(level_nb: u8) -> LevelFilter { + match level_nb { + 0 => LevelFilter::Info, + 1 => LevelFilter::Debug, + _ => LevelFilter::Trace, + } +} diff --git a/src/main.rs b/src/main.rs index 4bd107c..3cc5909 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,13 +46,13 @@ macro_rules! log_messages { } fn main() { - logs::init_log_system(); match config::Config::init() { Ok(cnf) => { + logs::init_log_system(&cnf); log::debug!("{cnf:?}"); main_loop(&cnf) } - Err(e) => log::error!("{e}"), + Err(e) => eprintln!("{e}"), } }