From 7d6a36b418bc2fa707f373ca237b019b06b0b45b Mon Sep 17 00:00:00 2001 From: librelois <elois@ifee.fr> Date: Fri, 24 Jan 2020 00:28:27 +0100 Subject: [PATCH] [ref] server:cli: handle logs level possible values in structopt --- bin/dunitrust-server/src/cli.rs | 12 ++++++++---- lib/core/core/src/commands/mod.rs | 2 +- lib/core/core/src/logger.rs | 5 +---- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/bin/dunitrust-server/src/cli.rs b/bin/dunitrust-server/src/cli.rs index 4e0ca9ca..eaf19610 100644 --- a/bin/dunitrust-server/src/cli.rs +++ b/bin/dunitrust-server/src/cli.rs @@ -33,6 +33,11 @@ use log::Level; use std::path::PathBuf; use structopt::StructOpt; +// Default log level is "info". +const LOG_LEVEL_DEFAULT: usize = 2; + +static LOG_LEVEL_NAMES: [&str; 5] = ["error", "warn", "info", "debug", "trace"]; + #[derive(StructOpt, Debug)] #[structopt(name = "durs", setting(structopt::clap::AppSettings::ColoredHelp))] /// Dunitrust command line options @@ -46,10 +51,9 @@ pub struct DursCliOpt { /// Keypairs file path #[structopt(long = "keypairs-file", parse(from_os_str))] keypairs_file: Option<PathBuf>, - /// Set log level. (Defaults to INFO). - /// Available levels: [ERROR, WARN, INFO, DEBUG, TRACE] - #[structopt(short = "l", long = "logs", next_line_help = true)] - logs_level: Option<Level>, + /// Change logs level. + #[structopt(short = "l", long = "logs", default_value = LOG_LEVEL_NAMES[LOG_LEVEL_DEFAULT], possible_values = &LOG_LEVEL_NAMES)] + logs_level: Level, /// Print logs in standard output #[structopt(long = "log-stdout")] log_stdout: bool, diff --git a/lib/core/core/src/commands/mod.rs b/lib/core/core/src/commands/mod.rs index 0c51c03b..44676b16 100644 --- a/lib/core/core/src/commands/mod.rs +++ b/lib/core/core/src/commands/mod.rs @@ -40,7 +40,7 @@ pub struct DursCoreOptions { /// Keypairs file path pub keypairs_file: Option<PathBuf>, /// Set log level. - pub logs_level: Option<Level>, + pub logs_level: Level, /// Print logs in standard output pub log_stdout: bool, /// Set a custom user profile name diff --git a/lib/core/core/src/logger.rs b/lib/core/core/src/logger.rs index 334d7142..b5f57123 100644 --- a/lib/core/core/src/logger.rs +++ b/lib/core/core/src/logger.rs @@ -66,10 +66,7 @@ pub fn init( } // Get log level filter - let logs_level_filter = durs_core_opts - .logs_level - .unwrap_or(Level::Info) - .to_level_filter(); + let logs_level_filter = durs_core_opts.logs_level.to_level_filter(); // Config loggers let loggers_common_config = fern::Dispatch::new() -- GitLab