Merge pull request #412 from matthiasbeyer/libimagrt/color-log-output

Libimagrt/color log output
This commit is contained in:
Matthias Beyer 2016-05-17 00:23:12 +02:00
commit bd6c8ff3ff
3 changed files with 31 additions and 6 deletions

View file

@ -10,6 +10,7 @@ log = "0.3"
xdg-basedir = "0.2.2" xdg-basedir = "0.2.2"
itertools = "0.4" itertools = "0.4"
tempfile = "2.1.1" tempfile = "2.1.1"
ansi_term = "0.7"
[dependencies.libimagstore] [dependencies.libimagstore]
path = "../libimagstore" path = "../libimagstore"

View file

@ -18,6 +18,7 @@
#[macro_use] extern crate itertools; #[macro_use] extern crate itertools;
#[cfg(unix)] extern crate xdg_basedir; #[cfg(unix)] extern crate xdg_basedir;
extern crate tempfile; extern crate tempfile;
extern crate ansi_term;
extern crate clap; extern crate clap;
extern crate toml; extern crate toml;

View file

@ -24,14 +24,37 @@ impl Log for ImagLogger {
} }
fn log(&self, record: &LogRecord) { fn log(&self, record: &LogRecord) {
use ansi_term::Colour::Red;
use ansi_term::Colour::Yellow;
use ansi_term::Colour::Cyan;
if self.enabled(record.metadata()) { if self.enabled(record.metadata()) {
// TODO: This is just simple logging. Maybe we can enhance this lateron // TODO: This is just simple logging. Maybe we can enhance this lateron
if record.metadata().level() == LogLevel::Debug { let loc = record.location();
let loc = record.location(); match record.metadata().level() {
writeln!(stderr(), "[imag][{: <5}][{}][{: >5}]: {}", LogLevel::Debug => {
record.level(), loc.file(), loc.line(), record.args()).ok(); let lvl = Cyan.paint(format!("{}", record.level()));
} else { let file = Cyan.paint(format!("{}", loc.file()));
writeln!(stderr(), "[imag][{: <5}]: {}", record.level(), record.args()).ok(); let ln = Cyan.paint(format!("{}", loc.line()));
let args = Cyan.paint(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()));
writeln!(stderr(), "[imag][{: <5}]: {}", lvl, args).ok();
},
LogLevel::Info => {
let lvl = Yellow.paint(format!("{}", record.level()));
let args = Yellow.paint(format!("{}", record.args()));
writeln!(stderr(), "[imag][{: <5}]: {}", lvl, args).ok();
},
_ => {
writeln!(stderr(), "[imag][{: <5}]: {}", record.level(), record.args()).ok();
},
} }
} }
} }