Implement format-fetching from config/cli

This commit is contained in:
Matthias Beyer 2017-08-26 23:00:48 +02:00
parent ae24954020
commit 5ec1cd48a0
3 changed files with 119 additions and 7 deletions

View File

@ -30,7 +30,12 @@ generate_error_types!(RuntimeError, RuntimeErrorKind,
GlobalLogLevelConfigMissing => "Global config 'imag.logging.level' missing", GlobalLogLevelConfigMissing => "Global config 'imag.logging.level' missing",
InvalidLogLevelSpec => "Invalid log level specification: Only 'trace', 'debug', 'info', 'warn', 'error' are allowed", InvalidLogLevelSpec => "Invalid log level specification: Only 'trace', 'debug', 'info', 'warn', 'error' are allowed",
TomlReadError => "Error while reading in TOML document", TomlReadError => "Error while reading in TOML document",
TemplateStringRegistrationError => "Error while registering logging template string" TemplateStringRegistrationError => "Error while registering logging template string",
ConfigMissingLoggingFormatTrace => "Missing config for logging format for trace logging",
ConfigMissingLoggingFormatDebug => "Missing config for logging format for debug logging",
ConfigMissingLoggingFormatInfo => "Missing config for logging format for info logging",
ConfigMissingLoggingFormatWarn => "Missing config for logging format for warn logging",
ConfigMissingLoggingFormatError => "Missing config for logging format for error logging"
); );
impl From<IOError> for RuntimeError { impl From<IOError> for RuntimeError {

View File

@ -269,37 +269,84 @@ fn aggregate_global_destinations(matches: &ArgMatches, config: Option<&Configura
} }
} }
#[inline]
fn aggregate_global_format(
read_str: &str,
cli_match_name: &str,
error_kind_if_missing: EK,
matches: &ArgMatches,
config: Option<&Configuration>
)
-> Result<String>
{
match config {
Some(cfg) => match cfg
.read(read_str)
.map_err_into(EK::ConfigReadError)
{
Ok(Some(&Value::String(ref s))) => Ok(s.clone()),
Ok(Some(_)) => Err(EK::ConfigTypeError.into_error()),
Ok(None) => Err(EK::GlobalLogLevelConfigMissing.into_error()),
Err(e) => Err(e)
},
None => match matches.value_of(cli_match_name).map(String::from) {
Some(s) => Ok(s),
None => Err(error_kind_if_missing.into_error())
}
}
}
fn aggregate_global_format_trace(matches: &ArgMatches, config: Option<&Configuration>) fn aggregate_global_format_trace(matches: &ArgMatches, config: Option<&Configuration>)
-> Result<String> -> Result<String>
{ {
unimplemented!() aggregate_global_format("imag.logging.format.trace",
Runtime::arg_override_trace_logging_format(),
EK::ConfigMissingLoggingFormatTrace,
matches,
config)
} }
fn aggregate_global_format_debug(matches: &ArgMatches, config: Option<&Configuration>) fn aggregate_global_format_debug(matches: &ArgMatches, config: Option<&Configuration>)
-> Result<String> -> Result<String>
{ {
unimplemented!() aggregate_global_format("imag.logging.format.debug",
Runtime::arg_override_debug_logging_format(),
EK::ConfigMissingLoggingFormatDebug,
matches,
config)
} }
fn aggregate_global_format_info(matches: &ArgMatches, config: Option<&Configuration>) fn aggregate_global_format_info(matches: &ArgMatches, config: Option<&Configuration>)
-> Result<String> -> Result<String>
{ {
unimplemented!() aggregate_global_format("imag.logging.format.info",
Runtime::arg_override_info_logging_format(),
EK::ConfigMissingLoggingFormatInfo,
matches,
config)
} }
fn aggregate_global_format_warn(matches: &ArgMatches, config: Option<&Configuration>) fn aggregate_global_format_warn(matches: &ArgMatches, config: Option<&Configuration>)
-> Result<String> -> Result<String>
{ {
unimplemented!() aggregate_global_format("imag.logging.format.warn",
Runtime::arg_override_warn_logging_format(),
EK::ConfigMissingLoggingFormatWarn,
matches,
config)
} }
fn aggregate_global_format_error(matches: &ArgMatches, config: Option<&Configuration>) fn aggregate_global_format_error(matches: &ArgMatches, config: Option<&Configuration>)
-> Result<String> -> Result<String>
{ {
unimplemented!() aggregate_global_format("imag.logging.format.error",
Runtime::arg_override_error_logging_format(),
EK::ConfigMissingLoggingFormatError,
matches,
config)
} }
fn aggregate_module_settings(matches: &ArgMatches, config: Option<&Configuration>) fn aggregate_module_settings(_matches: &ArgMatches, config: Option<&Configuration>)
-> Result<BTreeMap<ModuleName, ModuleSettings>> -> Result<BTreeMap<ModuleName, ModuleSettings>>
{ {
match config { match config {

View File

@ -264,6 +264,46 @@ impl<'a> Runtime<'a> {
.takes_value(true) .takes_value(true)
.value_name("SPEC")) .value_name("SPEC"))
.arg(Arg::with_name(Runtime::arg_override_trace_logging_format())
.long(Runtime::arg_override_trace_logging_format())
.help("Override the logging format for the trace logging")
.multiple(false)
.required(false)
.takes_value(true)
.value_name("FMT"))
.arg(Arg::with_name(Runtime::arg_override_debug_logging_format())
.long(Runtime::arg_override_debug_logging_format())
.help("Override the logging format for the debug logging")
.multiple(false)
.required(false)
.takes_value(true)
.value_name("FMT"))
.arg(Arg::with_name(Runtime::arg_override_info_logging_format())
.long(Runtime::arg_override_info_logging_format())
.help("Override the logging format for the info logging")
.multiple(false)
.required(false)
.takes_value(true)
.value_name("FMT"))
.arg(Arg::with_name(Runtime::arg_override_warn_logging_format())
.long(Runtime::arg_override_warn_logging_format())
.help("Override the logging format for the warn logging")
.multiple(false)
.required(false)
.takes_value(true)
.value_name("FMT"))
.arg(Arg::with_name(Runtime::arg_override_error_logging_format())
.long(Runtime::arg_override_error_logging_format())
.help("Override the logging format for the error logging")
.multiple(false)
.required(false)
.takes_value(true)
.value_name("FMT"))
} }
/// Get the argument names of the Runtime which are available /// Get the argument names of the Runtime which are available
@ -357,6 +397,26 @@ impl<'a> Runtime<'a> {
"override-module-log-setting" "override-module-log-setting"
} }
pub fn arg_override_trace_logging_format() -> &'static str {
"override-logging-format-trace"
}
pub fn arg_override_debug_logging_format() -> &'static str {
"override-logging-format-debug"
}
pub fn arg_override_info_logging_format() -> &'static str {
"override-logging-format-info"
}
pub fn arg_override_warn_logging_format() -> &'static str {
"override-logging-format-warn"
}
pub fn arg_override_error_logging_format() -> &'static str {
"override-logging-format-error"
}
/// Initialize the internal logger /// Initialize the internal logger
fn init_logger(matches: &ArgMatches, config: Option<&Configuration>) { fn init_logger(matches: &ArgMatches, config: Option<&Configuration>) {
use std::env::var as env_var; use std::env::var as env_var;