Add verbose to the CLI

This commit is contained in:
Rodolphe Bréard 2023-03-26 21:49:34 +02:00
parent 0b8c0a6195
commit 3358982c31
3 changed files with 20 additions and 3 deletions

View file

@ -25,6 +25,8 @@ pub struct Config {
revocation_list: Option<PathBuf>,
#[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<PathBuf>) -> Result<Vec<String>, String> {

View file

@ -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,
}
}

View file

@ -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}"),
}
}