2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-04-21 13:12:43 +02:00
|
|
|
// 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/>.
|
|
|
|
|
2016-07-20 18:13:56 +02:00
|
|
|
//! Logger for parity executables
|
|
|
|
|
2017-03-22 06:23:40 +01:00
|
|
|
extern crate arrayvec;
|
2016-07-20 18:13:56 +02:00
|
|
|
extern crate log as rlog;
|
|
|
|
extern crate isatty;
|
|
|
|
extern crate regex;
|
|
|
|
extern crate env_logger;
|
|
|
|
extern crate time;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
2017-03-22 06:23:40 +01:00
|
|
|
extern crate parking_lot;
|
|
|
|
extern crate ansi_term;
|
|
|
|
|
|
|
|
mod rotating;
|
2016-04-21 13:12:43 +02:00
|
|
|
|
2016-11-25 13:04:02 +01:00
|
|
|
use std::{env, thread, fs};
|
2016-12-11 13:35:00 +01:00
|
|
|
use std::sync::{Weak, Arc};
|
2016-07-13 09:05:26 +02:00
|
|
|
use std::io::Write;
|
2016-07-23 17:27:23 +02:00
|
|
|
use isatty::{stderr_isatty, stdout_isatty};
|
2016-04-21 13:12:43 +02:00
|
|
|
use env_logger::LogBuilder;
|
2016-07-13 09:05:26 +02:00
|
|
|
use regex::Regex;
|
2017-03-22 06:23:40 +01:00
|
|
|
use ansi_term::Colour;
|
|
|
|
use parking_lot::Mutex;
|
|
|
|
|
|
|
|
pub use rotating::{RotatingLogger, init_log};
|
2016-04-21 13:12:43 +02:00
|
|
|
|
2016-11-02 19:42:21 +01:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2016-07-25 16:09:47 +02:00
|
|
|
pub struct Config {
|
|
|
|
pub mode: Option<String>,
|
2016-07-20 18:13:56 +02:00
|
|
|
pub color: bool,
|
|
|
|
pub file: Option<String>,
|
|
|
|
}
|
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
impl Default for Config {
|
|
|
|
fn default() -> Self {
|
|
|
|
Config {
|
|
|
|
mode: None,
|
|
|
|
color: !cfg!(windows),
|
2016-07-20 18:13:56 +02:00
|
|
|
file: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-11 13:35:00 +01:00
|
|
|
lazy_static! {
|
|
|
|
static ref ROTATING_LOGGER : Mutex<Weak<RotatingLogger>> = Mutex::new(Default::default());
|
|
|
|
}
|
|
|
|
|
2016-04-21 13:12:43 +02:00
|
|
|
/// Sets up the logger
|
2016-07-25 16:09:47 +02:00
|
|
|
pub fn setup_log(config: &Config) -> Result<Arc<RotatingLogger>, String> {
|
2016-04-21 13:12:43 +02:00
|
|
|
use rlog::*;
|
|
|
|
|
|
|
|
let mut levels = String::new();
|
|
|
|
let mut builder = LogBuilder::new();
|
2017-07-11 12:23:46 +02:00
|
|
|
// Disable info logging by default for some modules:
|
2016-05-28 19:30:31 +02:00
|
|
|
builder.filter(Some("ws"), LogLevelFilter::Warn);
|
2017-07-11 12:23:46 +02:00
|
|
|
builder.filter(Some("reqwest"), LogLevelFilter::Warn);
|
2017-10-05 12:35:01 +02:00
|
|
|
builder.filter(Some("hyper"), LogLevelFilter::Warn);
|
2016-09-03 01:34:32 +02:00
|
|
|
builder.filter(Some("rustls"), LogLevelFilter::Warn);
|
2017-07-11 12:23:46 +02:00
|
|
|
// Enable info for others.
|
2016-04-21 13:12:43 +02:00
|
|
|
builder.filter(None, LogLevelFilter::Info);
|
|
|
|
|
2016-10-22 15:21:41 +02:00
|
|
|
if let Ok(lvl) = env::var("RUST_LOG") {
|
|
|
|
levels.push_str(&lvl);
|
2016-04-21 13:12:43 +02:00
|
|
|
levels.push_str(",");
|
2016-10-22 15:21:41 +02:00
|
|
|
builder.parse(&lvl);
|
2016-04-21 13:12:43 +02:00
|
|
|
}
|
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
if let Some(ref s) = config.mode {
|
2016-04-21 13:12:43 +02:00
|
|
|
levels.push_str(s);
|
|
|
|
builder.parse(s);
|
|
|
|
}
|
|
|
|
|
2016-07-23 17:27:23 +02:00
|
|
|
let isatty = stderr_isatty();
|
2016-07-25 16:09:47 +02:00
|
|
|
let enable_color = config.color && isatty;
|
2016-07-15 10:11:14 +02:00
|
|
|
let logs = Arc::new(RotatingLogger::new(levels));
|
2016-04-21 16:45:04 +02:00
|
|
|
let logger = logs.clone();
|
2016-11-25 13:04:02 +01:00
|
|
|
let mut open_options = fs::OpenOptions::new();
|
2016-07-25 16:09:47 +02:00
|
|
|
|
|
|
|
let maybe_file = match config.file.as_ref() {
|
2016-12-27 12:53:56 +01:00
|
|
|
Some(f) => Some(open_options
|
2016-11-25 13:04:02 +01:00
|
|
|
.append(true).create(true).open(f)
|
2016-12-27 12:53:56 +01:00
|
|
|
.map_err(|_| format!("Cannot write to log file given: {}", f))?),
|
2016-07-25 16:09:47 +02:00
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
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-07-15 10:11:14 +02:00
|
|
|
let with_color = if max_log_level() <= LogLevelFilter::Info {
|
2016-09-27 18:02:11 +02:00
|
|
|
format!("{} {}", Colour::Black.bold().paint(timestamp), record.args())
|
2016-04-21 13:12:43 +02:00
|
|
|
} else {
|
2016-08-01 19:07:09 +02:00
|
|
|
let name = thread::current().name().map_or_else(Default::default, |x| format!("{}", Colour::Blue.bold().paint(x)));
|
2016-09-27 18:02:11 +02:00
|
|
|
format!("{} {} {} {} {}", Colour::Black.bold().paint(timestamp), name, record.level(), record.target(), record.args())
|
2016-07-15 10:11:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let removed_color = kill_color(with_color.as_ref());
|
|
|
|
|
|
|
|
let ret = match enable_color {
|
|
|
|
true => with_color,
|
|
|
|
false => removed_color.clone(),
|
2016-04-21 13:12:43 +02:00
|
|
|
};
|
2016-07-13 09:05:26 +02:00
|
|
|
|
|
|
|
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-07-23 17:27:23 +02:00
|
|
|
if !isatty && record.level() <= LogLevel::Info && stdout_isatty() {
|
|
|
|
// duplicate INFO/WARN output to console
|
|
|
|
println!("{}", ret);
|
|
|
|
}
|
2016-07-13 09:05:26 +02:00
|
|
|
|
2016-07-15 10:11:14 +02:00
|
|
|
ret
|
2016-04-21 13:12:43 +02:00
|
|
|
};
|
2016-07-25 16:09:47 +02:00
|
|
|
|
2016-04-21 13:12:43 +02:00
|
|
|
builder.format(format);
|
2016-12-11 13:35:00 +01:00
|
|
|
builder.init()
|
|
|
|
.and_then(|_| {
|
|
|
|
*ROTATING_LOGGER.lock() = Arc::downgrade(&logs);
|
|
|
|
Ok(logs)
|
|
|
|
})
|
|
|
|
// couldn't create new logger - try to fall back on previous logger.
|
|
|
|
.or_else(|err| match ROTATING_LOGGER.lock().upgrade() {
|
|
|
|
Some(l) => Ok(l),
|
|
|
|
// no previous logger. fatal.
|
|
|
|
None => Err(format!("{:?}", err)),
|
|
|
|
})
|
2016-04-21 13:12:43 +02:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
2017-03-28 19:06:08 +02:00
|
|
|
RE.replace_all(s, "").to_string()
|
2016-07-13 09:05:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_remove_colour() {
|
|
|
|
let before = "test";
|
2016-07-15 10:11:14 +02:00
|
|
|
let after = kill_color(&Colour::Red.bold().paint(before));
|
2016-07-13 09:05:26 +02:00
|
|
|
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");
|
|
|
|
}
|