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

1
Cargo.lock generated
View File

@ -377,6 +377,7 @@ dependencies = [
name = "ethcore-util"
version = "1.3.0"
dependencies = [
"ansi_term 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
"arrayvec 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)",
"bigint 0.1.0",
"chrono 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -19,6 +19,7 @@ use std::sync::atomic::AtomicBool;
use std::time::{Instant, Duration};
use util::*;
use util::Colour::White;
use account_provider::AccountProvider;
use views::{BlockView, HeaderView};
use client::{MiningBlockChainClient, Executive, Executed, EnvInfo, TransactOptions, BlockID, CallAnalytics};
@ -613,7 +614,7 @@ impl MinerService for Miner {
let result = if let Some(b) = self.sealing_work.lock().unwrap().take_used_if(|b| &b.hash() == &pow_hash) {
match b.lock().try_seal(self.engine(), seal) {
Err(_) => {
info!(target: "miner", "Mined block rejected, PoW was invalid.");
info!(target: "miner", "Mined solution rejected: Invalid.");
Err(Error::PowInvalid)
}
Ok(sealed) => {
@ -622,14 +623,14 @@ impl MinerService for Miner {
}
}
} else {
info!(target: "miner", "Mined block rejected, PoW hash invalid or out of date.");
info!(target: "miner", "Mined solution rejected: Block unknown or out of date.");
Err(Error::PowHashInvalid)
};
result.and_then(|sealed| {
let n = sealed.header().number();
let h = sealed.header().hash();
try!(chain.import_sealed_block(sealed));
info!("Mined block imported OK. #{}: {}", n, h.hex());
info!("Mined block imported OK. #{}: {}", paint(White.bold(), format!("{}", n)), paint(White.bold(), h.hex()));
Ok(())
})
}

View File

@ -184,7 +184,7 @@ fn execute_client(conf: Configuration, spec: Spec, client_config: ClientConfig)
let panic_handler = PanicHandler::new_in_arc();
// Setup logging
let logger = setup_log::setup_log(&conf.args.flag_logging);
let logger = setup_log::setup_log(&conf.args.flag_logging, conf.args.flag_no_color);
// Raise fdlimit
unsafe { ::fdlimit::raise_fd_limit(); }
@ -320,6 +320,8 @@ fn execute_export(conf: Configuration) {
// Setup panic handler
let panic_handler = PanicHandler::new_in_arc();
// Setup logging
let _logger = setup_log::setup_log(&conf.args.flag_logging, conf.args.flag_no_color);
// Raise fdlimit
unsafe { ::fdlimit::raise_fd_limit(); }
@ -392,6 +394,8 @@ fn execute_import(conf: Configuration) {
// Setup panic handler
let panic_handler = PanicHandler::new_in_arc();
// Setup logging
let _logger = setup_log::setup_log(&conf.args.flag_logging, conf.args.flag_no_color);
// Raise fdlimit
unsafe { ::fdlimit::raise_fd_limit(); }

View File

@ -19,10 +19,10 @@ use std::env;
use std::sync::Arc;
use time;
use env_logger::LogBuilder;
use util::{RotatingLogger};
use util::RotatingLogger;
/// Sets up the logger
pub fn setup_log(init: &Option<String>) -> Arc<RotatingLogger> {
pub fn setup_log(init: &Option<String>, enable_color: bool) -> Arc<RotatingLogger> {
use rlog::*;
let mut levels = String::new();
@ -43,7 +43,7 @@ pub fn setup_log(init: &Option<String>) -> Arc<RotatingLogger> {
builder.parse(s);
}
let logs = Arc::new(RotatingLogger::new(levels));
let logs = Arc::new(RotatingLogger::new(levels, enable_color));
let logger = logs.clone();
let format = move |record: &LogRecord| {
let timestamp = time::strftime("%Y-%m-%d %H:%M:%S %Z", &time::now()).unwrap();

View File

@ -32,7 +32,7 @@ fn client_service() -> Arc<TestBlockChainClient> {
}
fn logger() -> Arc<RotatingLogger> {
Arc::new(RotatingLogger::new("rpc=trace".to_owned()))
Arc::new(RotatingLogger::new("rpc=trace".to_owned(), false))
}
fn settings() -> Arc<NetworkSettings> {

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]