Adapt libimagrt for new log setup

This commit is contained in:
Matthias Beyer 2017-12-06 18:08:25 +01:00
parent f209e12410
commit 0aba7d2939
3 changed files with 43 additions and 34 deletions

View file

@ -23,7 +23,6 @@ maintenance = { status = "actively-developed" }
clap = ">=2.17"
env_logger = "0.4"
toml = "0.4"
log = "0.4.0-rc.1"
xdg-basedir = "1.0"
itertools = "0.7"
ansi_term = "0.10"
@ -37,6 +36,11 @@ libimagerror = { version = "0.6.0", path = "../../../lib/core/libimagerror" }
libimagutil = { version = "0.6.0", path = "../../../lib/etc/libimagutil" }
libimaginteraction = { version = "0.6.0", path = "../../../lib/etc/libimaginteraction" }
[dependencies.log]
version = "0.4.0-rc.1"
default-features = false
features = ["std"]
[features]
default = []

View file

@ -30,7 +30,7 @@ use error::ResultExt;
use runtime::Runtime;
use clap::ArgMatches;
use log::{Log, LogLevel, LogRecord, LogMetadata};
use log::{Log, Level, Record, Metadata};
use toml::Value;
use toml_query::read::TomlValueReadExt;
use toml_query::read::TomlValueReadTypeExt;
@ -52,7 +52,7 @@ impl Default for LogDestination {
struct ModuleSettings {
enabled: bool,
level: Option<LogLevel>,
level: Option<Level>,
#[allow(unused)]
destinations: Option<Vec<LogDestination>>,
@ -60,7 +60,7 @@ struct ModuleSettings {
/// Logger implementation for `log` crate.
pub struct ImagLogger {
global_loglevel : LogLevel,
global_loglevel : Level,
#[allow(unused)]
global_destinations : Vec<LogDestination>,
@ -114,7 +114,7 @@ impl ImagLogger {
})
}
pub fn global_loglevel(&self) -> LogLevel {
pub fn global_loglevel(&self) -> Level {
self.global_loglevel
}
@ -122,12 +122,16 @@ impl ImagLogger {
impl Log for ImagLogger {
fn enabled(&self, metadata: &LogMetadata) -> bool {
fn enabled(&self, metadata: &Metadata) -> bool {
metadata.level() <= self.global_loglevel
}
fn log(&self, record: &LogRecord) {
if record.location().module_path().starts_with("handlebars") {
fn flush(&self) {
// nothing?
}
fn log(&self, record: &Record) {
if record.module_path().map(|m| m.starts_with("handlebars")).unwrap_or(false) {
// This is a ugly, yet necessary hack. When logging, we use handlebars for templating.
// But as the handlebars library itselfs logs via a normal logging macro ("debug!()"),
// we have a recursion in our chain.
@ -143,9 +147,9 @@ impl Log for ImagLogger {
{
data.insert("level", format!("{}", record.level()));
data.insert("module_path", String::from(record.location().module_path()));
data.insert("file", String::from(record.location().file()));
data.insert("line", format!("{}", record.location().line()));
data.insert("module_path", String::from(record.module_path().unwrap_or("<modulepath unknown>")));
data.insert("file", String::from(record.file().unwrap_or("<file unknown>")));
data.insert("line", format!("{}", record.line().unwrap_or(0)));
data.insert("target", String::from(record.target()));
data.insert("message", format!("{}", record.args()));
}
@ -209,27 +213,26 @@ impl Log for ImagLogger {
}
}
fn match_log_level_str(s: &str) -> Result<LogLevel> {
fn match_log_level_str(s: &str) -> Result<Level> {
match s {
"trace" => Ok(LogLevel::Trace),
"debug" => Ok(LogLevel::Debug),
"info" => Ok(LogLevel::Info),
"warn" => Ok(LogLevel::Warn),
"error" => Ok(LogLevel::Error),
"trace" => Ok(Level::Trace),
"debug" => Ok(Level::Debug),
"info" => Ok(Level::Info),
"warn" => Ok(Level::Warn),
"error" => Ok(Level::Error),
_ => return Err(RE::from_kind(EK::InvalidLogLevelSpec)),
}
}
fn aggregate_global_loglevel(matches: &ArgMatches, config: Option<&Value>)
-> Result<LogLevel>
fn aggregate_global_loglevel(matches: &ArgMatches, config: Option<&Value>) -> Result<Level>
{
fn get_arg_loglevel(matches: &ArgMatches) -> Result<Option<LogLevel>> {
fn get_arg_loglevel(matches: &ArgMatches) -> Result<Option<Level>> {
if matches.is_present(Runtime::arg_debugging_name()) {
return Ok(Some(LogLevel::Debug))
return Ok(Some(Level::Debug))
}
if matches.is_present(Runtime::arg_verbosity_name()) {
return Ok(Some(LogLevel::Info))
return Ok(Some(Level::Info))
}
match matches.value_of(Runtime::arg_verbosity_name()) {
@ -253,7 +256,7 @@ fn aggregate_global_loglevel(matches: &ArgMatches, config: Option<&Value>)
Ok(cfg_loglevel)
} else {
get_arg_loglevel(matches).map(|o| o.unwrap_or(LogLevel::Info))
get_arg_loglevel(matches).map(|o| o.unwrap_or(Level::Info))
}
}

View file

@ -26,7 +26,6 @@ pub use clap::App;
use toml::Value;
use clap::{Arg, ArgMatches};
use log;
use configuration::{fetch_config, override_config, InternalConfiguration};
use error::RuntimeError;
@ -335,22 +334,25 @@ impl<'a> Runtime<'a> {
/// Initialize the internal logger
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;
use env_logger;
if env_var("IMAG_LOG_ENV").is_ok() {
env_logger::init().unwrap();
} else {
log::set_logger(|max_log_lvl| {
let logger = ImagLogger::new(matches, config)
.map_err_trace()
.unwrap_or_else(|_| exit(1));
max_log_lvl.set(logger.global_loglevel().to_log_level_filter());
debug!("Init logger with {}", logger.global_loglevel());
Box::new(logger)
})
.map_err(|e| panic!("Could not setup logger: {:?}", e))
.ok();
let logger = ImagLogger::new(matches, config)
.map_err_trace()
.unwrap_or_else(|_| exit(1));
set_max_level(logger.global_loglevel().to_level_filter());
debug!("Init logger with {}", logger.global_loglevel());
set_boxed_logger(Box::new(logger))
.map_err(|e| panic!("Could not setup logger: {:?}", e))
.ok();
}
}