From 59f16d0eab6084dbb6e4f46f2002247092580eaf Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 31 Oct 2018 23:01:52 +0100 Subject: [PATCH 1/4] Add feature: Public logging initialization This feature is required for the `imag` binary. It allows the binary to use the imag internal logger for logging its own log output. We need to be able to initialize the logger from an external module (in all imag modules, the Runtime::new() implementation takes care of this, but as we cannot use that in the `imag` binary itself, we allow this method to be public behind a feature flag). Signed-off-by: Matthias Beyer --- lib/core/libimagrt/Cargo.toml | 5 +++++ lib/core/libimagrt/src/runtime.rs | 12 ++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/core/libimagrt/Cargo.toml b/lib/core/libimagrt/Cargo.toml index 7c31ae64..611a49c2 100644 --- a/lib/core/libimagrt/Cargo.toml +++ b/lib/core/libimagrt/Cargo.toml @@ -53,6 +53,11 @@ features = ["no_logging"] [features] default = [] +# Make logger initialization inside `runtime::Runtime` public. +# This feature is _only_ used for the `imag` binary itself. You do not need this +# feature and if you think you do you're doing it wrong. +pub_logging_initialization = [] + # Enable testing functionality. Used for building the libimagrt for testing CLI # apps. Do not use in production! testing = [] diff --git a/lib/core/libimagrt/src/runtime.rs b/lib/core/libimagrt/src/runtime.rs index 4dfe3b5f..eb866524 100644 --- a/lib/core/libimagrt/src/runtime.rs +++ b/lib/core/libimagrt/src/runtime.rs @@ -317,14 +317,22 @@ impl<'a> Runtime<'a> { "logging-destinations" } + #[cfg(feature = "pub_logging_initialization")] + pub fn init_logger(matches: &ArgMatches, config: Option<&Value>) { + Self::_init_logger(matches, config) + } + #[cfg(not(feature = "pub_logging_initialization"))] + fn init_logger(matches: &ArgMatches, config: Option<&Value>) { + Self::_init_logger(matches, config) + } + /// Initialize the internal logger /// /// If the environment variable "IMAG_LOG_ENV" is set, this simply /// initializes a env-logger instance. Errors are ignored in this case. /// If the environment variable is not set, this initializes the internal imag logger. On /// error, this exits (as there is nothing we can do about that) - /// - fn init_logger(matches: &ArgMatches, config: Option<&Value>) { + fn _init_logger(matches: &ArgMatches, config: Option<&Value>) { use log::set_max_level; use log::set_boxed_logger; use std::env::var as env_var; From e4ffeddf856c34e282228ce8725c4a5d94076261 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 31 Oct 2018 23:05:58 +0100 Subject: [PATCH 2/4] Use libimagrt logging Signed-off-by: Matthias Beyer --- bin/core/imag/Cargo.toml | 6 +++++- bin/core/imag/src/main.rs | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/bin/core/imag/Cargo.toml b/bin/core/imag/Cargo.toml index 77472b8c..d40da6c3 100644 --- a/bin/core/imag/Cargo.toml +++ b/bin/core/imag/Cargo.toml @@ -33,7 +33,6 @@ log = "0.4.0" toml = "0.4" toml-query = "0.7" -libimagrt = { version = "0.9.0", path = "../../../lib/core/libimagrt" } libimagerror = { version = "0.9.0", path = "../../../lib/core/libimagerror" } [dependencies.clap] @@ -41,3 +40,8 @@ version = "^2.29" default-features = false features = ["suggestions", "color", "wrap_help"] +[dependencies.libimagrt] +version = "0.9.0" +path = "../../../lib/core/libimagrt" +features = ["pub_logging_initialization"] + diff --git a/bin/core/imag/src/main.rs b/bin/core/imag/src/main.rs index 37279dd0..12fc8d4e 100644 --- a/bin/core/imag/src/main.rs +++ b/bin/core/imag/src/main.rs @@ -60,6 +60,7 @@ use libimagrt::spec::CliSpec; use libimagerror::io::ToExitCode; use libimagerror::exit::ExitUnwrap; use libimagerror::trace::trace_error; +use libimagrt::configuration::InternalConfiguration; /// Returns the helptext, putting the Strings in cmds as possible /// subcommands into it @@ -182,7 +183,9 @@ fn main() { } } + let enable_logging = app.enable_logging(); let matches = app.matches(); + let rtp = ::libimagrt::runtime::get_rtp_match(&matches); let configpath = matches .value_of(Runtime::arg_config_name()) @@ -194,6 +197,10 @@ fn main() { exit(1) }); + if enable_logging { + Runtime::init_logger(&matches, config.as_ref()) + } + debug!("matches: {:?}", matches); // Begin checking for arguments From 9636cef7a827066a1891e62978b639c0d80cf3f4 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 31 Oct 2018 23:05:20 +0100 Subject: [PATCH 3/4] Print (debugging) CLI when initializing runtime Signed-off-by: Matthias Beyer --- lib/core/libimagrt/src/runtime.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/core/libimagrt/src/runtime.rs b/lib/core/libimagrt/src/runtime.rs index eb866524..f42ee88d 100644 --- a/lib/core/libimagrt/src/runtime.rs +++ b/lib/core/libimagrt/src/runtime.rs @@ -127,6 +127,7 @@ impl<'a> Runtime<'a> { debug!("RTP path = {:?}", rtp); debug!("Store path = {:?}", storepath); + debug!("CLI = {:?}", matches); let store_result = if cli_app.use_inmemory_fs() { Store::new_with_backend(storepath, From c4c52e43a6b4b1634f780e8e9a4d9b015874736f Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 31 Oct 2018 23:07:27 +0100 Subject: [PATCH 4/4] Add some debugging output --- bin/core/imag/src/main.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bin/core/imag/src/main.rs b/bin/core/imag/src/main.rs index 12fc8d4e..1c469ec1 100644 --- a/bin/core/imag/src/main.rs +++ b/bin/core/imag/src/main.rs @@ -268,6 +268,7 @@ fn main() { None => Vec::new() }; + debug!("Processing forwarding of commandline arguments"); forward_commandline_arguments(&matches, &mut subcommand_args); let subcommand = String::from(subcommand); @@ -373,6 +374,9 @@ fn fetch_aliases(config: Option<&Value>) -> Result, Str fn forward_commandline_arguments(m: &ArgMatches, scmd: &mut Vec) { let push = |flag: Option<&str>, val_name: &str, m: &ArgMatches, v: &mut Vec| { + debug!("Push({flag:?}, {val_name:?}, {matches:?}, {v:?}", + flag = flag, val_name = val_name, matches = m, v = v); + let _ = m .value_of(val_name) .map(|val| {