2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
|
|
|
// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
|
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; version
|
|
|
|
// 2.1 of the License.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
//
|
|
|
|
|
2016-02-04 17:02:50 +00:00
|
|
|
use std::io::Write;
|
|
|
|
use std::io::stderr;
|
|
|
|
|
2016-01-10 14:04:06 +00:00
|
|
|
use log::{Log, LogLevel, LogRecord, LogMetadata};
|
|
|
|
|
2016-08-02 09:38:38 +00:00
|
|
|
use ansi_term::Style;
|
|
|
|
use ansi_term::Colour;
|
|
|
|
use ansi_term::ANSIString;
|
|
|
|
|
2016-01-10 14:04:06 +00:00
|
|
|
pub struct ImagLogger {
|
2017-01-21 11:37:11 +00:00
|
|
|
prefix: String,
|
2016-01-10 14:04:06 +00:00
|
|
|
lvl: LogLevel,
|
2016-08-02 09:38:38 +00:00
|
|
|
color_enabled: bool,
|
2016-01-10 14:04:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ImagLogger {
|
|
|
|
|
|
|
|
pub fn new(lvl: LogLevel) -> ImagLogger {
|
|
|
|
ImagLogger {
|
2017-01-21 11:37:11 +00:00
|
|
|
prefix: "[imag]".to_owned(),
|
2016-01-10 14:04:06 +00:00
|
|
|
lvl: lvl,
|
2016-08-02 09:38:38 +00:00
|
|
|
color_enabled: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-21 11:37:11 +00:00
|
|
|
pub fn with_prefix(mut self, pref: String) -> ImagLogger {
|
|
|
|
self.prefix = pref;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2016-08-02 09:38:38 +00:00
|
|
|
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)
|
2016-01-10 14:04:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Log for ImagLogger {
|
|
|
|
|
|
|
|
fn enabled(&self, metadata: &LogMetadata) -> bool {
|
|
|
|
metadata.level() <= self.lvl
|
|
|
|
}
|
|
|
|
|
|
|
|
fn log(&self, record: &LogRecord) {
|
2016-05-15 15:38:02 +00:00
|
|
|
use ansi_term::Colour::Red;
|
|
|
|
use ansi_term::Colour::Yellow;
|
|
|
|
use ansi_term::Colour::Cyan;
|
|
|
|
|
2016-01-10 14:04:06 +00:00
|
|
|
if self.enabled(record.metadata()) {
|
|
|
|
// TODO: This is just simple logging. Maybe we can enhance this lateron
|
2016-05-15 15:38:02 +00:00
|
|
|
let loc = record.location();
|
|
|
|
match record.metadata().level() {
|
|
|
|
LogLevel::Debug => {
|
2016-08-02 09:38:38 +00:00
|
|
|
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()));
|
2016-05-15 15:38:02 +00:00
|
|
|
|
2017-01-21 11:37:11 +00:00
|
|
|
writeln!(stderr(), "{}[{: <5}][{}][{: >5}]: {}", self.prefix, lvl, file, ln, args).ok();
|
2016-05-15 15:38:02 +00:00
|
|
|
},
|
|
|
|
LogLevel::Warn | LogLevel::Error => {
|
2016-08-02 09:38:38 +00:00
|
|
|
let lvl = self.style_or_not(Red.blink(), format!("{}", record.level()));
|
|
|
|
let args = self.color_or_not(Red, format!("{}", record.args()));
|
2016-05-15 15:38:02 +00:00
|
|
|
|
2017-01-21 11:37:11 +00:00
|
|
|
writeln!(stderr(), "{}[{: <5}]: {}", self.prefix, lvl, args).ok();
|
2016-05-15 15:38:02 +00:00
|
|
|
},
|
|
|
|
LogLevel::Info => {
|
2016-08-02 09:38:38 +00:00
|
|
|
let lvl = self.color_or_not(Yellow, format!("{}", record.level()));
|
|
|
|
let args = self.color_or_not(Yellow, format!("{}", record.args()));
|
2016-05-15 15:38:02 +00:00
|
|
|
|
2017-01-21 11:37:11 +00:00
|
|
|
writeln!(stderr(), "{}[{: <5}]: {}", self.prefix, lvl, args).ok();
|
2016-05-15 15:38:02 +00:00
|
|
|
},
|
|
|
|
_ => {
|
2017-01-21 11:37:11 +00:00
|
|
|
writeln!(stderr(), "{}[{: <5}]: {}", self.prefix, record.level(), record.args()).ok();
|
2016-05-15 15:38:02 +00:00
|
|
|
},
|
2016-04-05 15:35:59 +00:00
|
|
|
}
|
2016-01-10 14:04:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|