From 3abe3e1fbc2a7b60f2f07ff7e3fd61786d848d3f Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Wed, 13 Jul 2016 09:05:26 +0200 Subject: [PATCH] Fix logging (#1590) * Strip colour for everywhere except the terminal. * Log to file. Fixes #1560. * Fix indentation. [ci:skip] * Remove unnecessary clone()ing. * Update setup_log.rs * remove unnecessary mutex in logging (#1601) --- Cargo.lock | 2 ++ Cargo.toml | 2 ++ parity/cli.rs | 5 ++++- parity/main.rs | 9 ++++++--- parity/setup_log.rs | 42 ++++++++++++++++++++++++++++++++++++++---- 5 files changed, 52 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b223cd57f..0a5b8f4ba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -22,9 +22,11 @@ dependencies = [ "fdlimit 0.1.0", "hyper 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", "json-ipc-server 0.2.4 (git+https://github.com/ethcore/json-ipc-server.git)", + "lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "number_prefix 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 0.1.68 (registry+https://github.com/rust-lang/crates.io-index)", "rpassword 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index fe035c348..7776172c7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,8 @@ ethcore-ipc = { path = "ipc/rpc" } ethcore-ipc-hypervisor = { path = "ipc/hypervisor" } json-ipc-server = { git = "https://github.com/ethcore/json-ipc-server.git" } ansi_term = "0.7" +lazy_static = "0.2" +regex = "0.1" [target.'cfg(windows)'.dependencies] winapi = "0.2" diff --git a/parity/cli.rs b/parity/cli.rs index 07bba06d1..4fb2d2e51 100644 --- a/parity/cli.rs +++ b/parity/cli.rs @@ -264,6 +264,8 @@ Legacy Options: Miscellaneous Options: -l --logging LOGGING Specify the logging level. Must conform to the same format as RUST_LOG. + --log-file FILENAME Specify a filename into which logging should be + directed. --no-color Don't use terminal color codes in output. -v --version Show information about version. -h --help Show this screen. @@ -339,7 +341,7 @@ pub struct Args { pub flag_author: Option, pub flag_usd_per_tx: String, pub flag_usd_per_eth: String, - pub flag_price_update_period: String, + pub flag_price_update_period: String, pub flag_gas_floor_target: String, pub flag_gas_cap: String, pub flag_extra_data: Option, @@ -351,6 +353,7 @@ pub struct Args { pub flag_to: String, pub flag_format: Option, pub flag_jitvm: bool, + pub flag_log_file: Option, pub flag_no_color: bool, pub flag_no_network: bool, // legacy... diff --git a/parity/main.rs b/parity/main.rs index f1f018cc2..7314b9f99 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -50,6 +50,9 @@ extern crate ethcore_rpc; extern crate ethcore_signer; extern crate ansi_term; +#[macro_use] +extern crate lazy_static; +extern crate regex; #[cfg(feature = "dapps")] extern crate ethcore_dapps; @@ -184,7 +187,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, conf.have_color()); + let logger = setup_log::setup_log(&conf.args.flag_logging, conf.have_color(), &conf.args.flag_log_file); // Raise fdlimit unsafe { ::fdlimit::raise_fd_limit(); } @@ -342,7 +345,7 @@ fn execute_export(conf: Configuration) { let panic_handler = PanicHandler::new_in_arc(); // Setup logging - let _logger = setup_log::setup_log(&conf.args.flag_logging, conf.have_color()); + let _logger = setup_log::setup_log(&conf.args.flag_logging, conf.have_color(), &conf.args.flag_log_file); // Raise fdlimit unsafe { ::fdlimit::raise_fd_limit(); } @@ -403,7 +406,7 @@ fn execute_import(conf: Configuration) { let panic_handler = PanicHandler::new_in_arc(); // Setup logging - let _logger = setup_log::setup_log(&conf.args.flag_logging, conf.have_color()); + let _logger = setup_log::setup_log(&conf.args.flag_logging, conf.have_color(), &conf.args.flag_log_file); // Raise fdlimit unsafe { ::fdlimit::raise_fd_limit(); } diff --git a/parity/setup_log.rs b/parity/setup_log.rs index d347a6bf0..fb45bd549 100644 --- a/parity/setup_log.rs +++ b/parity/setup_log.rs @@ -17,12 +17,16 @@ use std::env; use std::sync::Arc; +use std::fs::File; +use std::io::Write; use time; use env_logger::LogBuilder; +use regex::Regex; use util::RotatingLogger; +use util::log::{Applyable, Colour}; /// Sets up the logger -pub fn setup_log(init: &Option, enable_color: bool) -> Arc { +pub fn setup_log(init: &Option, enable_color: bool, log_to_file: &Option) -> Arc { use rlog::*; let mut levels = String::new(); @@ -45,14 +49,24 @@ pub fn setup_log(init: &Option, enable_color: bool) -> Arc, enable_color: bool) -> Arc String { + lazy_static! { + static ref RE: Regex = Regex::new("\x1b\\[[^m]+m").unwrap(); + } + RE.replace_all(s, "") +} + +#[test] +fn should_remove_colour() { + let before = "test"; + let after = kill_color(&before.apply(Colour::Red.bold())); + assert_eq!(after, "test"); +} + +#[test] +fn should_remove_multiple_colour() { + let t = format!("{} {}", Colour::Red.bold().paint("test"), Colour::White.normal().paint("again")); + let after = kill_color(&t); + assert_eq!(after, "test again"); +}