Merge pull request #612 from matthiasbeyer/libimagrt/logging-without-color
Libimagrt/logging without color
This commit is contained in:
commit
c3cf7575c0
3 changed files with 46 additions and 12 deletions
|
@ -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();
|
||||
},
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -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 $*
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue