From 2c87ed814f27ef8d58a4260be79710ed85c28bfe Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Sun, 18 Oct 2020 01:10:36 +0200 Subject: add support for file logging --- mumlib/src/lib.rs | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'mumlib/src/lib.rs') diff --git a/mumlib/src/lib.rs b/mumlib/src/lib.rs index d989a7e..40f37ef 100644 --- a/mumlib/src/lib.rs +++ b/mumlib/src/lib.rs @@ -5,19 +5,29 @@ pub mod state; use colored::*; use log::*; -pub fn setup_logger() { +pub fn setup_logger>(target: T, color: bool) { fern::Dispatch::new() - .format(|out, message, record| { + .format(move |out, message, record| { let message = message.to_string(); out.finish(format_args!( "{} {}:{}{}{}", //TODO runtime flag that disables color - match record.level() { - Level::Error => "ERROR".red(), - Level::Warn => "WARN ".yellow(), - Level::Info => "INFO ".normal(), - Level::Debug => "DEBUG".green(), - Level::Trace => "TRACE".normal(), + if color { + match record.level() { + Level::Error => "ERROR".red(), + Level::Warn => "WARN ".yellow(), + Level::Info => "INFO ".normal(), + Level::Debug => "DEBUG".green(), + Level::Trace => "TRACE".normal(), + } + } else { + match record.level() { + Level::Error => "ERROR", + Level::Warn => "WARN ", + Level::Info => "INFO ", + Level::Debug => "DEBUG", + Level::Trace => "TRACE", + }.normal() }, record.file().unwrap(), record.line().unwrap(), @@ -30,7 +40,7 @@ pub fn setup_logger() { )) }) .level(log::LevelFilter::Debug) - .chain(std::io::stderr()) + .chain(target.into()) .apply() .unwrap(); } -- cgit v1.2.1