From 59f16d0eab6084dbb6e4f46f2002247092580eaf Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 31 Oct 2018 23:01:52 +0100 Subject: [PATCH] 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;