2016-04-21 13:12:43 +02:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
use std::env;
|
|
|
|
use std::sync::Arc;
|
2016-07-13 09:05:26 +02:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Write;
|
2016-04-21 13:12:43 +02:00
|
|
|
use time;
|
|
|
|
use env_logger::LogBuilder;
|
2016-07-13 09:05:26 +02:00
|
|
|
use regex::Regex;
|
2016-06-29 17:16:58 +02:00
|
|
|
use util::RotatingLogger;
|
2016-07-13 09:05:26 +02:00
|
|
|
use util::log::{Applyable, Colour};
|
2016-04-21 13:12:43 +02:00
|
|
|
|
|
|
|
/// Sets up the logger
|
2016-07-13 09:05:26 +02:00
|
|
|
pub fn setup_log(init: &Option<String>, enable_color: bool, log_to_file: &Option<String>) -> Arc<RotatingLogger> {
|
2016-04-21 13:12:43 +02:00
|
|
|
use rlog::*;
|
|
|
|
|
|
|
|
let mut levels = String::new();
|
|
|
|
let mut builder = LogBuilder::new();
|
2016-05-28 19:30:31 +02:00
|
|
|
// Disable ws info logging by default.
|
|
|
|
builder.filter(Some("ws"), LogLevelFilter::Warn);
|
2016-04-21 13:12:43 +02:00
|
|
|
builder.filter(None, LogLevelFilter::Info);
|
|
|
|
|
|
|
|
if env::var("RUST_LOG").is_ok() {
|
|
|
|
let lvl = &env::var("RUST_LOG").unwrap();
|
2016-05-17 10:32:05 +02:00
|
|
|
levels.push_str(lvl);
|
2016-04-21 13:12:43 +02:00
|
|
|
levels.push_str(",");
|
|
|
|
builder.parse(lvl);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(ref s) = *init {
|
|
|
|
levels.push_str(s);
|
|
|
|
builder.parse(s);
|
|
|
|
}
|
|
|
|
|
2016-06-29 17:16:58 +02:00
|
|
|
let logs = Arc::new(RotatingLogger::new(levels, enable_color));
|
2016-04-21 16:45:04 +02:00
|
|
|
let logger = logs.clone();
|
2016-07-13 09:05:26 +02:00
|
|
|
let maybe_file = log_to_file.as_ref().map(|f| File::create(f).unwrap_or_else(|_| die!("Cannot write to log file given: {}", f)));
|
2016-04-21 13:12:43 +02:00
|
|
|
let format = move |record: &LogRecord| {
|
|
|
|
let timestamp = time::strftime("%Y-%m-%d %H:%M:%S %Z", &time::now()).unwrap();
|
2016-07-13 09:05:26 +02:00
|
|
|
|
2016-04-21 13:12:43 +02:00
|
|
|
let format = if max_log_level() <= LogLevelFilter::Info {
|
2016-07-13 09:05:26 +02:00
|
|
|
format!("{}{}", timestamp.apply(Colour::Black.bold()), record.args())
|
2016-04-21 13:12:43 +02:00
|
|
|
} else {
|
2016-07-13 09:05:26 +02:00
|
|
|
format!("{}{}:{}: {}", timestamp.apply(Colour::Black.bold()), record.level(), record.target(), record.args())
|
2016-04-21 13:12:43 +02:00
|
|
|
};
|
2016-07-13 09:05:26 +02:00
|
|
|
|
|
|
|
let removed_color = kill_color(format.as_ref());
|
|
|
|
if let Some(mut file) = maybe_file.as_ref() {
|
|
|
|
// ignore errors - there's nothing we can do
|
|
|
|
let _ = file.write_all(removed_color.as_bytes());
|
|
|
|
let _ = file.write_all(b"\n");
|
|
|
|
}
|
|
|
|
logger.append(removed_color);
|
|
|
|
|
2016-04-21 13:12:43 +02:00
|
|
|
format
|
|
|
|
};
|
|
|
|
builder.format(format);
|
|
|
|
builder.init().unwrap();
|
|
|
|
logs
|
|
|
|
}
|
|
|
|
|
2016-07-13 09:05:26 +02:00
|
|
|
fn kill_color(s: &str) -> 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");
|
|
|
|
}
|