Cleanup of colour code. Use is_a_tty. (#1621)
* Cleanup of colour code. Use is_a_tty. * Fix test build. * Another fix.
This commit is contained in:
@@ -17,35 +17,13 @@
|
||||
//! Common log helper functions
|
||||
|
||||
use std::env;
|
||||
use std::borrow::Cow;
|
||||
use rlog::LogLevelFilter;
|
||||
use env_logger::LogBuilder;
|
||||
use std::sync::atomic::{Ordering, AtomicBool};
|
||||
use arrayvec::ArrayVec;
|
||||
pub use ansi_term::{Colour, Style};
|
||||
|
||||
use parking_lot::{RwLock, RwLockReadGuard};
|
||||
|
||||
lazy_static! {
|
||||
static ref USE_COLOR: AtomicBool = AtomicBool::new(false);
|
||||
}
|
||||
|
||||
/// Something which can be apply()ed.
|
||||
pub trait Applyable: AsRef<str> {
|
||||
/// Apply the style `c` to ourself, returning us styled in that manner.
|
||||
fn apply(&self, c: Style) -> Cow<str>;
|
||||
}
|
||||
|
||||
impl<T: AsRef<str>> Applyable for T {
|
||||
fn apply(&self, c: Style) -> Cow<str> {
|
||||
let s = self.as_ref();
|
||||
match USE_COLOR.load(Ordering::Relaxed) {
|
||||
true => Cow::Owned(format!("{}", c.paint(s))),
|
||||
false => Cow::Borrowed(s),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref LOG_DUMMY: bool = {
|
||||
let mut builder = LogBuilder::new();
|
||||
@@ -81,8 +59,7 @@ impl RotatingLogger {
|
||||
|
||||
/// Creates new `RotatingLogger` with given levels.
|
||||
/// It does not enforce levels - it's just read only.
|
||||
pub fn new(levels: String, enable_color: bool) -> Self {
|
||||
USE_COLOR.store(enable_color, Ordering::Relaxed);
|
||||
pub fn new(levels: String) -> Self {
|
||||
RotatingLogger {
|
||||
levels: levels,
|
||||
logs: RwLock::new(ArrayVec::<[_; LOG_SIZE]>::new()),
|
||||
@@ -111,7 +88,7 @@ mod test {
|
||||
use super::RotatingLogger;
|
||||
|
||||
fn logger() -> RotatingLogger {
|
||||
RotatingLogger::new("test".to_owned(), false)
|
||||
RotatingLogger::new("test".to_owned())
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user