2020-09-22 14:53:52 +02:00
|
|
|
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of OpenEthereum.
|
2016-04-21 13:12:43 +02:00
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
// OpenEthereum is free software: you can redistribute it and/or modify
|
2016-04-21 13:12:43 +02:00
|
|
|
// 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.
|
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
// OpenEthereum is distributed in the hope that it will be useful,
|
2016-04-21 13:12:43 +02:00
|
|
|
// 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
|
2020-09-22 14:53:52 +02:00
|
|
|
// along with OpenEthereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-04-21 13:12:43 +02:00
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
//! Logger for OpenEthereum executables
|
2016-07-20 18:13:56 +02:00
|
|
|
|
2018-04-11 12:56:37 +02:00
|
|
|
extern crate ansi_term;
|
2017-03-22 06:23:40 +01:00
|
|
|
extern crate arrayvec;
|
2018-04-11 12:56:37 +02:00
|
|
|
extern crate atty;
|
|
|
|
extern crate env_logger;
|
2016-07-20 18:13:56 +02:00
|
|
|
extern crate log as rlog;
|
2018-04-11 12:56:37 +02:00
|
|
|
extern crate parking_lot;
|
2016-07-20 18:13:56 +02:00
|
|
|
extern crate regex;
|
|
|
|
extern crate time;
|
2018-04-11 12:56:37 +02:00
|
|
|
|
2016-07-20 18:13:56 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
2017-03-22 06:23:40 +01:00
|
|
|
|
|
|
|
mod rotating;
|
2016-04-21 13:12:43 +02:00
|
|
|
|
2017-03-22 06:23:40 +01:00
|
|
|
use ansi_term::Colour;
|
2020-08-05 06:08:03 +02:00
|
|
|
use env_logger::{Builder as LogBuilder, Formatter};
|
2017-03-22 06:23:40 +01:00
|
|
|
use parking_lot::Mutex;
|
2020-08-05 06:08:03 +02:00
|
|
|
use regex::Regex;
|
|
|
|
use std::{
|
|
|
|
env, fs,
|
|
|
|
io::Write,
|
|
|
|
sync::{Arc, Weak},
|
|
|
|
thread,
|
|
|
|
};
|
2017-03-22 06:23:40 +01:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
pub use rotating::{init_log, RotatingLogger};
|
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 {
|
2020-08-05 06:08:03 +02:00
|
|
|
pub mode: Option<String>,
|
|
|
|
pub color: bool,
|
|
|
|
pub file: Option<String>,
|
2016-07-20 18:13:56 +02:00
|
|
|
}
|
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
impl Default for Config {
|
2020-08-05 06:08:03 +02:00
|
|
|
fn default() -> Self {
|
|
|
|
Config {
|
|
|
|
mode: None,
|
|
|
|
color: !cfg!(windows),
|
|
|
|
file: None,
|
|
|
|
}
|
|
|
|
}
|
2016-07-20 18:13:56 +02:00
|
|
|
}
|
|
|
|
|
2016-12-11 13:35:00 +01:00
|
|
|
lazy_static! {
|
2020-08-05 06:08:03 +02:00
|
|
|
static ref ROTATING_LOGGER: Mutex<Weak<RotatingLogger>> = Mutex::new(Default::default());
|
2016-12-11 13:35:00 +01:00
|
|
|
}
|
|
|
|
|
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> {
|
2020-08-05 06:08:03 +02:00
|
|
|
use rlog::*;
|
|
|
|
|
|
|
|
let mut levels = String::new();
|
|
|
|
let mut builder = LogBuilder::new();
|
|
|
|
// Disable info logging by default for some modules:
|
|
|
|
builder.filter(Some("ws"), LevelFilter::Warn);
|
|
|
|
builder.filter(Some("hyper"), LevelFilter::Warn);
|
|
|
|
builder.filter(Some("rustls"), LevelFilter::Error);
|
|
|
|
// Enable info for others.
|
|
|
|
builder.filter(None, LevelFilter::Info);
|
|
|
|
|
|
|
|
if let Ok(lvl) = env::var("RUST_LOG") {
|
|
|
|
levels.push_str(&lvl);
|
|
|
|
levels.push_str(",");
|
|
|
|
builder.parse(&lvl);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(ref s) = config.mode {
|
|
|
|
levels.push_str(s);
|
|
|
|
builder.parse(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
let isatty = atty::is(atty::Stream::Stderr);
|
|
|
|
let enable_color = config.color && isatty;
|
|
|
|
let logs = Arc::new(RotatingLogger::new(levels));
|
|
|
|
let logger = logs.clone();
|
|
|
|
let mut open_options = fs::OpenOptions::new();
|
|
|
|
|
|
|
|
let maybe_file = match config.file.as_ref() {
|
|
|
|
Some(f) => Some(
|
|
|
|
open_options
|
|
|
|
.append(true)
|
|
|
|
.create(true)
|
|
|
|
.open(f)
|
|
|
|
.map_err(|e| format!("Cannot write to log file given: {}, {}", f, e))?,
|
|
|
|
),
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let format = move |buf: &mut Formatter, record: &Record| {
|
|
|
|
let timestamp = time::strftime("%Y-%m-%d %H:%M:%S %Z", &time::now()).unwrap();
|
|
|
|
|
|
|
|
let with_color = if max_level() <= LevelFilter::Info {
|
|
|
|
format!(
|
|
|
|
"{} {}",
|
|
|
|
Colour::Black.bold().paint(timestamp),
|
|
|
|
record.args()
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
let name = thread::current().name().map_or_else(Default::default, |x| {
|
|
|
|
format!("{}", Colour::Blue.bold().paint(x))
|
|
|
|
});
|
|
|
|
format!(
|
|
|
|
"{} {} {} {} {}",
|
|
|
|
Colour::Black.bold().paint(timestamp),
|
|
|
|
name,
|
|
|
|
record.level(),
|
|
|
|
record.target(),
|
|
|
|
record.args()
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
let removed_color = kill_color(with_color.as_ref());
|
|
|
|
|
|
|
|
let ret = match enable_color {
|
|
|
|
true => with_color,
|
|
|
|
false => removed_color.clone(),
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
if !isatty && record.level() <= Level::Info && atty::is(atty::Stream::Stdout) {
|
|
|
|
// duplicate INFO/WARN output to console
|
|
|
|
println!("{}", ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeln!(buf, "{}", ret)
|
2016-04-21 13:12:43 +02:00
|
|
|
};
|
2016-07-25 16:09:47 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
builder.format(format);
|
|
|
|
builder
|
|
|
|
.try_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 {
|
2020-08-05 06:08:03 +02:00
|
|
|
lazy_static! {
|
|
|
|
static ref RE: Regex = Regex::new("\x1b\\[[^m]+m").unwrap();
|
|
|
|
}
|
|
|
|
RE.replace_all(s, "").to_string()
|
2016-07-13 09:05:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_remove_colour() {
|
2020-08-05 06:08:03 +02:00
|
|
|
let before = "test";
|
|
|
|
let after = kill_color(&Colour::Red.bold().paint(before));
|
|
|
|
assert_eq!(after, "test");
|
2016-07-13 09:05:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_remove_multiple_colour() {
|
2020-08-05 06:08:03 +02:00
|
|
|
let t = format!(
|
|
|
|
"{} {}",
|
|
|
|
Colour::Red.bold().paint("test"),
|
|
|
|
Colour::White.normal().paint("again")
|
|
|
|
);
|
|
|
|
let after = kill_color(&t);
|
|
|
|
assert_eq!(after, "test again");
|
2016-07-13 09:05:26 +02:00
|
|
|
}
|