From 817933c99f974b06eccc3e8507fca1a102348c5f Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 15 Oct 2017 19:45:34 +0200 Subject: [PATCH] Fix: Overide logging with --debug Formerly, the --debug flag was ignores. This change overrides the logging with the CLI specified logging if it was provided. If --debug was provided, the logging is set to debugging, if --verbose was provided info logging is used. --- lib/core/libimagrt/src/logger.rs | 44 ++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/lib/core/libimagrt/src/logger.rs b/lib/core/libimagrt/src/logger.rs index f24516dc..a6782049 100644 --- a/lib/core/libimagrt/src/logger.rs +++ b/lib/core/libimagrt/src/logger.rs @@ -223,27 +223,43 @@ fn match_log_level_str(s: &str) -> Result { fn aggregate_global_loglevel(matches: &ArgMatches, config: Option<&Configuration>) -> Result { - match config { - Some(cfg) => match cfg.read("imag.logging.level") { + fn get_arg_loglevel(matches: &ArgMatches) -> Result> { + if matches.is_present(Runtime::arg_debugging_name()) { + return Ok(Some(LogLevel::Debug)) + } + + if matches.is_present(Runtime::arg_verbosity_name()) { + return Ok(Some(LogLevel::Info)) + } + + match matches.value_of(Runtime::arg_verbosity_name()) { + Some(v) => match_log_level_str(v).map(Some), + None => Ok(None), + } + } + + if let Some(cfg) = config { + let cfg_loglevel = match cfg.read("imag.logging.level") { Ok(Some(&Value::String(ref s))) => match_log_level_str(s), Ok(Some(_)) => { let path = "imag.logging.level".to_owned(); let ty = "String"; - Err(RE::from_kind(EK::ConfigTypeError(path, ty))) + return Err(RE::from_kind(EK::ConfigTypeError(path, ty))) }, - Ok(None) => Err(RE::from_kind(EK::GlobalLogLevelConfigMissing)), - Err(e) => Err(e).map_err(From::from), - }, - None => { - if matches.is_present(Runtime::arg_debugging_name()) { - return Ok(LogLevel::Debug) - } + Ok(None) => return Err(RE::from_kind(EK::GlobalLogLevelConfigMissing)), + Err(e) => return Err(e).map_err(From::from), + }?; - matches - .value_of(Runtime::arg_verbosity_name()) - .map(match_log_level_str) - .unwrap_or(Ok(LogLevel::Info)) + if let Some(cli_loglevel) = get_arg_loglevel(matches)? { + if cli_loglevel > cfg_loglevel { + return Ok(cli_loglevel) + } } + + Ok(cfg_loglevel) + + } else { + get_arg_loglevel(matches).map(|o| o.unwrap_or(LogLevel::Info)) } }