splitting part of util into smaller crates (#4956)
* split path module from util * moved RotatingLogger from util to logger crate * fix tests * fix tests * use only one version of ansi_term crate
This commit is contained in:
@@ -16,7 +16,7 @@
|
||||
|
||||
//! Logger for parity executables
|
||||
|
||||
extern crate ethcore_util as util;
|
||||
extern crate arrayvec;
|
||||
extern crate log as rlog;
|
||||
extern crate isatty;
|
||||
extern crate regex;
|
||||
@@ -24,6 +24,10 @@ extern crate env_logger;
|
||||
extern crate time;
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
extern crate parking_lot;
|
||||
extern crate ansi_term;
|
||||
|
||||
mod rotating;
|
||||
|
||||
use std::{env, thread, fs};
|
||||
use std::sync::{Weak, Arc};
|
||||
@@ -31,8 +35,10 @@ use std::io::Write;
|
||||
use isatty::{stderr_isatty, stdout_isatty};
|
||||
use env_logger::LogBuilder;
|
||||
use regex::Regex;
|
||||
use util::{Mutex, RotatingLogger} ;
|
||||
use util::log::Colour;
|
||||
use ansi_term::Colour;
|
||||
use parking_lot::Mutex;
|
||||
|
||||
pub use rotating::{RotatingLogger, init_log};
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct Config {
|
||||
|
||||
120
logger/src/rotating.rs
Normal file
120
logger/src/rotating.rs
Normal file
@@ -0,0 +1,120 @@
|
||||
// Copyright 2015-2017 Parity Technologies (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/>.
|
||||
|
||||
//! Common log helper functions
|
||||
|
||||
use std::env;
|
||||
use rlog::LogLevelFilter;
|
||||
use env_logger::LogBuilder;
|
||||
use arrayvec::ArrayVec;
|
||||
|
||||
use parking_lot::{RwLock, RwLockReadGuard};
|
||||
|
||||
lazy_static! {
|
||||
static ref LOG_DUMMY: () = {
|
||||
let mut builder = LogBuilder::new();
|
||||
builder.filter(None, LogLevelFilter::Info);
|
||||
|
||||
if let Ok(log) = env::var("RUST_LOG") {
|
||||
builder.parse(&log);
|
||||
}
|
||||
|
||||
if builder.init().is_ok() {
|
||||
println!("logger initialized");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Intialize log with default settings
|
||||
pub fn init_log() {
|
||||
*LOG_DUMMY
|
||||
}
|
||||
|
||||
const LOG_SIZE : usize = 128;
|
||||
|
||||
/// Logger implementation that keeps up to `LOG_SIZE` log elements.
|
||||
pub struct RotatingLogger {
|
||||
/// Defined logger levels
|
||||
levels: String,
|
||||
/// Logs array. Latest log is always at index 0
|
||||
logs: RwLock<ArrayVec<[String; LOG_SIZE]>>,
|
||||
}
|
||||
|
||||
impl RotatingLogger {
|
||||
|
||||
/// Creates new `RotatingLogger` with given levels.
|
||||
/// It does not enforce levels - it's just read only.
|
||||
pub fn new(levels: String) -> Self {
|
||||
RotatingLogger {
|
||||
levels: levels,
|
||||
logs: RwLock::new(ArrayVec::<[_; LOG_SIZE]>::new()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Append new log entry
|
||||
pub fn append(&self, log: String) {
|
||||
self.logs.write().insert(0, log);
|
||||
}
|
||||
|
||||
/// Return levels
|
||||
pub fn levels(&self) -> &str {
|
||||
&self.levels
|
||||
}
|
||||
|
||||
/// Return logs
|
||||
pub fn logs(&self) -> RwLockReadGuard<ArrayVec<[String; LOG_SIZE]>> {
|
||||
self.logs.read()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::RotatingLogger;
|
||||
|
||||
fn logger() -> RotatingLogger {
|
||||
RotatingLogger::new("test".to_owned())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_return_log_levels() {
|
||||
// given
|
||||
let logger = logger();
|
||||
|
||||
// when
|
||||
let levels = logger.levels();
|
||||
|
||||
// then
|
||||
assert_eq!(levels, "test");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_return_latest_logs() {
|
||||
// given
|
||||
let logger = logger();
|
||||
|
||||
// when
|
||||
logger.append("a".to_owned());
|
||||
logger.append("b".to_owned());
|
||||
|
||||
// then
|
||||
let logs = logger.logs();
|
||||
assert_eq!(logs[0], "b".to_owned());
|
||||
assert_eq!(logs[1], "a".to_owned());
|
||||
assert_eq!(logs.len(), 2);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user