Commit ffc792e1 authored by Alexis Janon's avatar Alexis Janon
Browse files

Error handling for log4rs configuration

parent ff5858b2
......@@ -10,6 +10,10 @@ use cuttr::error::*;
use cuttr::result::*;
use cuttr::*;
use log::LevelFilter;
use log4rs::append::console::{ConsoleAppender, Target};
use log4rs::config::{Appender, Config, Root};
use log4rs::encode::pattern::PatternEncoder;
use log4rs::Handle;
use std::process::exit;
use std::rc::Rc;
......@@ -79,7 +83,7 @@ fn exec() -> Result<CommandResult> {
v if v >= 5 => LevelFilter::Trace,
_ => LevelFilter::Off,
};
configure_logger(level);
let _log4rs_handle = configure_logger(level);
let controllers = Rc::new(
get_sys_fs_controllers()
.map_err(Error::from)
......@@ -128,12 +132,7 @@ fn show(matches: &ArgMatches) -> Result<CommandResult> {
}
}
fn configure_logger(level: LevelFilter) {
// TODO: error handling
use log4rs::append::console::{ConsoleAppender, Target};
use log4rs::config::{Appender, Config, Root};
use log4rs::encode::pattern::PatternEncoder;
fn configure_logger(level: LevelFilter) -> Result<Handle> {
let stdout = ConsoleAppender::builder()
.encoder(Box::new(PatternEncoder::new(
"[{d(%Y-%m-%d %H:%M:%S)}] {h({l:10.10})}: {m}{n}",
......@@ -143,7 +142,17 @@ fn configure_logger(level: LevelFilter) {
let config = Config::builder()
.appender(Appender::builder().build("stdout", Box::new(stdout)))
.build(Root::builder().appender("stdout").build(level))
.unwrap();
.map_err(|err| {
Error::from(format!(
"Could not build log4rs config: {}",
err.to_string()
))
})?;
log4rs::init_config(config).unwrap();
log4rs::init_config(config).map_err(|err| {
Error::from(format!(
"Could not initialize log4rs config: {}",
err.to_string()
))
})
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment