openethereum/bin/oe/logger/src/rotating.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

122 lines
3.0 KiB
Rust
Raw Normal View History

2020-09-22 14:53:52 +02:00
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
// This file is part of OpenEthereum.
2016-02-05 13:40:41 +01:00
2020-09-22 14:53:52 +02:00
// OpenEthereum is free software: you can redistribute it and/or modify
2016-02-05 13:40:41 +01: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-02-05 13:40:41 +01: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-02-05 13:40:41 +01:00
2016-01-29 15:01:39 +01:00
//! Common log helper functions
2016-04-19 19:08:13 +02:00
use arrayvec::ArrayVec;
use env_logger::Builder as LogBuilder;
use rlog::LevelFilter;
2016-01-29 15:01:39 +01:00
use std::env;
2016-06-29 17:16:58 +02:00
use parking_lot::{RwLock, RwLockReadGuard};
2016-01-29 15:01:39 +01:00
lazy_static! {
static ref LOG_DUMMY: () = {
2016-01-29 15:01:39 +01:00
let mut builder = LogBuilder::new();
builder.filter(None, LevelFilter::Info);
2020-08-05 06:08:03 +02:00
2016-01-29 15:01:39 +01:00
if let Ok(log) = env::var("RUST_LOG") {
builder.parse(&log);
}
2020-08-05 06:08:03 +02:00
if !builder.try_init().is_ok() {
2017-11-27 14:19:02 +01:00
println!("logger initialization failed!");
2016-01-29 15:01:39 +01:00
}
};
}
/// Intialize log with default settings
pub fn init_log() {
*LOG_DUMMY
2016-01-29 15:01:39 +01:00
}
2016-04-19 09:58:26 +02:00
2016-04-19 19:08:13 +02:00
const LOG_SIZE: usize = 128;
2016-04-19 09:58:26 +02:00
2016-04-19 19:08:13 +02:00
/// Logger implementation that keeps up to `LOG_SIZE` log elements.
2016-04-19 09:58:26 +02:00
pub struct RotatingLogger {
2016-04-19 19:08:13 +02:00
/// Defined logger levels
2016-04-19 09:58:26 +02:00
levels: String,
2016-04-19 19:08:13 +02:00
/// Logs array. Latest log is always at index 0
logs: RwLock<ArrayVec<[String; LOG_SIZE]>>,
2016-04-19 09:58:26 +02:00
}
impl RotatingLogger {
2016-04-19 19:08:13 +02:00
/// Creates new `RotatingLogger` with given levels.
/// It does not enforce levels - it's just read only.
pub fn new(levels: String) -> Self {
2016-04-19 09:58:26 +02:00
RotatingLogger {
levels: levels,
2016-04-19 19:08:13 +02:00
logs: RwLock::new(ArrayVec::<[_; LOG_SIZE]>::new()),
2016-04-19 09:58:26 +02:00
}
}
2020-08-05 06:08:03 +02:00
2016-04-19 19:08:13 +02:00
/// Append new log entry
2016-04-19 09:58:26 +02:00
pub fn append(&self, log: String) {
let mut logs = self.logs.write();
if logs.is_full() {
logs.pop();
}
logs.insert(0, log);
2016-04-19 09:58:26 +02:00
}
2020-08-05 06:08:03 +02:00
2016-04-19 19:08:13 +02:00
/// Return levels
2016-04-19 09:58:26 +02:00
pub fn levels(&self) -> &str {
&self.levels
}
2020-08-05 06:08:03 +02:00
2016-04-19 19:08:13 +02:00
/// Return logs
pub fn logs(&self) -> RwLockReadGuard<ArrayVec<[String; LOG_SIZE]>> {
self.logs.read()
2016-04-19 09:58:26 +02:00
}
}
#[cfg(test)]
mod test {
2016-04-20 00:47:56 +02:00
use super::RotatingLogger;
2020-08-05 06:08:03 +02:00
2016-04-20 00:47:56 +02:00
fn logger() -> RotatingLogger {
RotatingLogger::new("test".to_owned())
2016-04-20 00:47:56 +02:00
}
2020-08-05 06:08:03 +02:00
2016-04-20 00:47:56 +02:00
#[test]
fn should_return_log_levels() {
// given
let logger = logger();
2020-08-05 06:08:03 +02:00
2016-04-20 00:47:56 +02:00
// when
let levels = logger.levels();
2020-08-05 06:08:03 +02:00
2016-04-20 00:47:56 +02:00
// then
assert_eq!(levels, "test");
}
2020-08-05 06:08:03 +02:00
2016-04-19 09:58:26 +02:00
#[test]
2016-04-20 00:47:56 +02:00
fn should_return_latest_logs() {
// given
let logger = logger();
2020-08-05 06:08:03 +02:00
2016-04-20 00:47:56 +02:00
// when
logger.append("a".to_owned());
logger.append("b".to_owned());
2020-08-05 06:08:03 +02:00
2016-04-20 00:47:56 +02:00
// then
let logs = logger.logs();
assert_eq!(logs[0], "b".to_owned());
assert_eq!(logs[1], "a".to_owned());
assert_eq!(logs.len(), 2);
2016-04-19 09:58:26 +02:00
}
}