Safe coloured logging.

This commit is contained in:
Gav Wood
2016-06-29 17:16:58 +02:00
committed by arkpar
parent 4a6206c514
commit 92edf7f511
8 changed files with 33 additions and 10 deletions

View File

@@ -37,6 +37,7 @@ vergen = "0.1"
target_info = "0.1"
bigint = { path = "bigint" }
chrono = "0.2"
ansi_term = "0.7"
[features]
default = []

View File

@@ -117,6 +117,7 @@ extern crate libc;
extern crate target_info;
extern crate bigint;
extern crate chrono;
extern crate ansi_term;
pub mod standard;
#[macro_use]

View File

@@ -20,7 +20,21 @@ use std::env;
use rlog::{LogLevelFilter};
use env_logger::LogBuilder;
use std::sync::{RwLock, RwLockReadGuard};
use std::sync::atomic::{Ordering, AtomicBool};
use arrayvec::ArrayVec;
pub use ansi_term::{Colour, Style};
lazy_static! {
static ref USE_COLOR: AtomicBool = AtomicBool::new(false);
}
/// Paint, using colour if desired.
pub fn paint(c: Style, t: String) -> String {
match USE_COLOR.load(Ordering::Relaxed) {
true => format!("{}", c.paint(t)),
false => t,
}
}
lazy_static! {
static ref LOG_DUMMY: bool = {
@@ -57,7 +71,8 @@ impl RotatingLogger {
/// Creates new `RotatingLogger` with given levels.
/// It does not enforce levels - it's just read only.
pub fn new(levels: String) -> Self {
pub fn new(levels: String, enable_color: bool) -> Self {
USE_COLOR.store(enable_color, Ordering::Relaxed);
RotatingLogger {
levels: levels,
logs: RwLock::new(ArrayVec::<[_; LOG_SIZE]>::new()),
@@ -86,7 +101,7 @@ mod test {
use super::RotatingLogger;
fn logger() -> RotatingLogger {
RotatingLogger::new("test".to_owned())
RotatingLogger::new("test".to_owned(), false)
}
#[test]