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 <mail@beyermatthias.de>
This commit is contained in:
Matthias Beyer 2018-10-31 23:01:52 +01:00
parent 84f2f2c46f
commit 59f16d0eab
2 changed files with 15 additions and 2 deletions

View file

@ -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 = []

View file

@ -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;