From 8a0200a1831db28293565a77f50b1f4e52f5ddcb Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 2 Aug 2016 11:38:38 +0200 Subject: [PATCH 1/3] Make coloring optional via flag in ImagLogger --- libimagrt/src/logger.rs | 43 +++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/libimagrt/src/logger.rs b/libimagrt/src/logger.rs index e637163d..f5b6ad76 100644 --- a/libimagrt/src/logger.rs +++ b/libimagrt/src/logger.rs @@ -3,8 +3,13 @@ use std::io::stderr; use log::{Log, LogLevel, LogRecord, LogMetadata}; +use ansi_term::Style; +use ansi_term::Colour; +use ansi_term::ANSIString; + pub struct ImagLogger { lvl: LogLevel, + color_enabled: bool, } impl ImagLogger { @@ -12,6 +17,28 @@ impl ImagLogger { pub fn new(lvl: LogLevel) -> ImagLogger { ImagLogger { lvl: lvl, + color_enabled: true + } + } + + pub fn with_color(mut self, b: bool) -> ImagLogger { + self.color_enabled = b; + self + } + + fn style_or_not(&self, c: Style, s: String) -> ANSIString { + if self.color_enabled { + c.paint(s) + } else { + ANSIString::from(s) + } + } + + fn color_or_not(&self, c: Colour, s: String) -> ANSIString { + if self.color_enabled { + c.paint(s) + } else { + ANSIString::from(s) } } @@ -33,22 +60,22 @@ impl Log for ImagLogger { let loc = record.location(); match record.metadata().level() { LogLevel::Debug => { - let lvl = Cyan.paint(format!("{}", record.level())); - let file = Cyan.paint(format!("{}", loc.file())); - let ln = Cyan.paint(format!("{}", loc.line())); - let args = Cyan.paint(format!("{}", record.args())); + let lvl = self.color_or_not(Cyan, format!("{}", record.level())); + let file = self.color_or_not(Cyan, format!("{}", loc.file())); + let ln = self.color_or_not(Cyan, format!("{}", loc.line())); + let args = self.color_or_not(Cyan, format!("{}", record.args())); writeln!(stderr(), "[imag][{: <5}][{}][{: >5}]: {}", lvl, file, ln, args).ok(); }, LogLevel::Warn | LogLevel::Error => { - let lvl = Red.blink().paint(format!("{}", record.level())); - let args = Red.paint(format!("{}", record.args())); + let lvl = self.style_or_not(Red.blink(), format!("{}", record.level())); + let args = self.color_or_not(Red, format!("{}", record.args())); writeln!(stderr(), "[imag][{: <5}]: {}", lvl, args).ok(); }, LogLevel::Info => { - let lvl = Yellow.paint(format!("{}", record.level())); - let args = Yellow.paint(format!("{}", record.args())); + let lvl = self.color_or_not(Yellow, format!("{}", record.level())); + let args = self.color_or_not(Yellow, format!("{}", record.args())); writeln!(stderr(), "[imag][{: <5}]: {}", lvl, args).ok(); }, From 8412ea195fa908bc581d66bd29fe58b2297fe71f Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 2 Aug 2016 11:39:01 +0200 Subject: [PATCH 2/3] Configure coloring in logger setup --- libimagrt/src/runtime.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/libimagrt/src/runtime.rs b/libimagrt/src/runtime.rs index 237b3fe7..10fce0ad 100644 --- a/libimagrt/src/runtime.rs +++ b/libimagrt/src/runtime.rs @@ -53,8 +53,9 @@ impl<'a> Runtime<'a> { let is_debugging = matches.is_present("debugging"); let is_verbose = matches.is_present("verbosity"); + let colored = !matches.is_present("no-color-output"); - Runtime::init_logger(is_debugging, is_verbose); + Runtime::init_logger(is_debugging, is_verbose, colored); let rtp : PathBuf = matches.value_of("runtimepath") .map_or_else(|| { @@ -178,6 +179,12 @@ impl<'a> Runtime<'a> { .required(false) .takes_value(false)) + .arg(Arg::with_name("no-color-output") + .long("no-color") + .help("Disable color output") + .required(false) + .takes_value(false)) + .arg(Arg::with_name("config") .long("config") .help("Path to alternative config file") @@ -212,7 +219,7 @@ impl<'a> Runtime<'a> { /** * Initialize the internal logger */ - fn init_logger(is_debugging: bool, is_verbose: bool) { + fn init_logger(is_debugging: bool, is_verbose: bool, colored: bool) { use std::env::var as env_var; use env_logger; @@ -230,7 +237,7 @@ impl<'a> Runtime<'a> { log::set_logger(|max_log_lvl| { max_log_lvl.set(lvl); debug!("Init logger with {}", lvl); - Box::new(ImagLogger::new(lvl.to_log_level().unwrap())) + Box::new(ImagLogger::new(lvl.to_log_level().unwrap()).with_color(colored)) }) .map_err(|_| { panic!("Could not setup logger"); From 0de0ed94e1ece9dd791871b69a2ed9ca4aacbf89 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 2 Aug 2016 11:42:35 +0200 Subject: [PATCH 3/3] Do not print logging info with color in test scripts --- tests/utils.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/utils.sh b/tests/utils.sh index e0153bdd..64999bb8 100644 --- a/tests/utils.sh +++ b/tests/utils.sh @@ -37,7 +37,7 @@ imag-call-binary() { local binary=$1; shift [[ -d $searchdir ]] || { err "FATAL: No directory $searchdir"; exit 1; } local bin=$(find $searchdir -iname $binary -type f -executable) - local flags="--config ./imagrc.toml --override-config store.implicit-create=true --debug --rtp $RUNTIME" + local flags="--no-color --config ./imagrc.toml --override-config store.implicit-create=true --debug --rtp $RUNTIME" out "Calling '$bin $flags $*'" $bin $flags $* }