Make logger types public

This commit makes the logging functionality types from libimagrt public,
so the libimagruby implementation can re-use the ImagLogger type.
This commit is contained in:
Matthias Beyer 2017-01-21 12:37:11 +01:00
parent 0cf564091e
commit ddf811a589
2 changed files with 12 additions and 5 deletions

View file

@ -49,9 +49,9 @@ extern crate libimagutil;
#[macro_use] extern crate libimagerror; #[macro_use] extern crate libimagerror;
mod configuration; mod configuration;
mod logger;
pub mod error; pub mod error;
pub mod logger;
pub mod runtime; pub mod runtime;
pub mod setup; pub mod setup;

View file

@ -27,6 +27,7 @@ use ansi_term::Colour;
use ansi_term::ANSIString; use ansi_term::ANSIString;
pub struct ImagLogger { pub struct ImagLogger {
prefix: String,
lvl: LogLevel, lvl: LogLevel,
color_enabled: bool, color_enabled: bool,
} }
@ -35,11 +36,17 @@ impl ImagLogger {
pub fn new(lvl: LogLevel) -> ImagLogger { pub fn new(lvl: LogLevel) -> ImagLogger {
ImagLogger { ImagLogger {
prefix: "[imag]".to_owned(),
lvl: lvl, lvl: lvl,
color_enabled: true color_enabled: true
} }
} }
pub fn with_prefix(mut self, pref: String) -> ImagLogger {
self.prefix = pref;
self
}
pub fn with_color(mut self, b: bool) -> ImagLogger { pub fn with_color(mut self, b: bool) -> ImagLogger {
self.color_enabled = b; self.color_enabled = b;
self self
@ -84,22 +91,22 @@ impl Log for ImagLogger {
let ln = self.color_or_not(Cyan, format!("{}", loc.line())); let ln = self.color_or_not(Cyan, format!("{}", loc.line()));
let args = self.color_or_not(Cyan, format!("{}", record.args())); let args = self.color_or_not(Cyan, format!("{}", record.args()));
writeln!(stderr(), "[imag][{: <5}][{}][{: >5}]: {}", lvl, file, ln, args).ok(); writeln!(stderr(), "{}[{: <5}][{}][{: >5}]: {}", self.prefix, lvl, file, ln, args).ok();
}, },
LogLevel::Warn | LogLevel::Error => { LogLevel::Warn | LogLevel::Error => {
let lvl = self.style_or_not(Red.blink(), format!("{}", record.level())); let lvl = self.style_or_not(Red.blink(), format!("{}", record.level()));
let args = self.color_or_not(Red, format!("{}", record.args())); let args = self.color_or_not(Red, format!("{}", record.args()));
writeln!(stderr(), "[imag][{: <5}]: {}", lvl, args).ok(); writeln!(stderr(), "{}[{: <5}]: {}", self.prefix, lvl, args).ok();
}, },
LogLevel::Info => { LogLevel::Info => {
let lvl = self.color_or_not(Yellow, format!("{}", record.level())); let lvl = self.color_or_not(Yellow, format!("{}", record.level()));
let args = self.color_or_not(Yellow, format!("{}", record.args())); let args = self.color_or_not(Yellow, format!("{}", record.args()));
writeln!(stderr(), "[imag][{: <5}]: {}", lvl, args).ok(); writeln!(stderr(), "{}[{: <5}]: {}", self.prefix, lvl, args).ok();
}, },
_ => { _ => {
writeln!(stderr(), "[imag][{: <5}]: {}", record.level(), record.args()).ok(); writeln!(stderr(), "{}[{: <5}]: {}", self.prefix, record.level(), record.args()).ok();
}, },
} }
} }