Using ArrayVec to store logs

This commit is contained in:
Tomasz Drwięga 2016-04-19 19:08:13 +02:00
parent a21f2a0998
commit 407ab30503
4 changed files with 58 additions and 21 deletions

View File

@ -672,10 +672,10 @@ impl TransactionQueue {
} }
fn check_too_cheap(is_in: bool) -> Result<(), TransactionError> { fn check_too_cheap(is_in: bool) -> Result<(), TransactionError> {
if !is_in { if is_in {
Err(TransactionError::TooCheapToReplace)
} else {
Ok(()) Ok(())
} else {
Err(TransactionError::TooCheapToReplace)
} }
} }

View File

@ -84,7 +84,7 @@ impl<M> Ethcore for EthcoreClient<M> where M: MinerService + 'static {
fn dev_logs(&self, _params: Params) -> Result<Value, Error> { fn dev_logs(&self, _params: Params) -> Result<Value, Error> {
let logs = self.logger.logs(); let logs = self.logger.logs();
to_value(&logs.deref()) to_value(&logs.deref().as_slice())
} }
fn dev_logs_levels(&self, _params: Params) -> Result<Value, Error> { fn dev_logs_levels(&self, _params: Params) -> Result<Value, Error> {

View File

@ -22,16 +22,20 @@ use ethminer::MinerService;
use v1::tests::helpers::TestMinerService; use v1::tests::helpers::TestMinerService;
use util::numbers::*; use util::numbers::*;
use rustc_serialize::hex::FromHex; use rustc_serialize::hex::FromHex;
use util::log::RotatingLogger;
fn miner_service() -> Arc<TestMinerService> { fn miner_service() -> Arc<TestMinerService> {
Arc::new(TestMinerService::default()) Arc::new(TestMinerService::default())
} }
fn logger() -> Arc<RotatingLogger> {
Arc::new(RotatingLogger::new("rpc=trace".to_owned()))
}
#[test] #[test]
fn rpc_ethcore_extra_data() { fn rpc_ethcore_extra_data() {
let miner = miner_service(); let miner = miner_service();
let ethcore = EthcoreClient::new(&miner).to_delegate(); let ethcore = EthcoreClient::new(&miner, logger()).to_delegate();
let io = IoHandler::new(); let io = IoHandler::new();
io.add_delegate(ethcore); io.add_delegate(ethcore);
@ -45,7 +49,7 @@ fn rpc_ethcore_extra_data() {
#[test] #[test]
fn rpc_ethcore_gas_floor_target() { fn rpc_ethcore_gas_floor_target() {
let miner = miner_service(); let miner = miner_service();
let ethcore = EthcoreClient::new(&miner).to_delegate(); let ethcore = EthcoreClient::new(&miner, logger()).to_delegate();
let io = IoHandler::new(); let io = IoHandler::new();
io.add_delegate(ethcore); io.add_delegate(ethcore);
@ -58,7 +62,7 @@ fn rpc_ethcore_gas_floor_target() {
#[test] #[test]
fn rpc_ethcore_min_gas_price() { fn rpc_ethcore_min_gas_price() {
let miner = miner_service(); let miner = miner_service();
let ethcore = EthcoreClient::new(&miner).to_delegate(); let ethcore = EthcoreClient::new(&miner, logger()).to_delegate();
let io = IoHandler::new(); let io = IoHandler::new();
io.add_delegate(ethcore); io.add_delegate(ethcore);
@ -71,7 +75,7 @@ fn rpc_ethcore_min_gas_price() {
#[test] #[test]
fn rpc_ethcore_set_min_gas_price() { fn rpc_ethcore_set_min_gas_price() {
let miner = miner_service(); let miner = miner_service();
let ethcore = EthcoreClient::new(&miner).to_delegate(); let ethcore = EthcoreClient::new(&miner, logger()).to_delegate();
let io = IoHandler::new(); let io = IoHandler::new();
io.add_delegate(ethcore); io.add_delegate(ethcore);
@ -85,7 +89,7 @@ fn rpc_ethcore_set_min_gas_price() {
#[test] #[test]
fn rpc_ethcore_set_gas_floor_target() { fn rpc_ethcore_set_gas_floor_target() {
let miner = miner_service(); let miner = miner_service();
let ethcore = EthcoreClient::new(&miner).to_delegate(); let ethcore = EthcoreClient::new(&miner, logger()).to_delegate();
let io = IoHandler::new(); let io = IoHandler::new();
io.add_delegate(ethcore); io.add_delegate(ethcore);
@ -99,7 +103,7 @@ fn rpc_ethcore_set_gas_floor_target() {
#[test] #[test]
fn rpc_ethcore_set_extra_data() { fn rpc_ethcore_set_extra_data() {
let miner = miner_service(); let miner = miner_service();
let ethcore = EthcoreClient::new(&miner).to_delegate(); let ethcore = EthcoreClient::new(&miner, logger()).to_delegate();
let io = IoHandler::new(); let io = IoHandler::new();
io.add_delegate(ethcore); io.add_delegate(ethcore);
@ -113,7 +117,7 @@ fn rpc_ethcore_set_extra_data() {
#[test] #[test]
fn rpc_ethcore_set_author() { fn rpc_ethcore_set_author() {
let miner = miner_service(); let miner = miner_service();
let ethcore = EthcoreClient::new(&miner).to_delegate(); let ethcore = EthcoreClient::new(&miner, logger()).to_delegate();
let io = IoHandler::new(); let io = IoHandler::new();
io.add_delegate(ethcore); io.add_delegate(ethcore);
@ -123,3 +127,32 @@ fn rpc_ethcore_set_author() {
assert_eq!(io.handle_request(request), Some(response.to_owned())); assert_eq!(io.handle_request(request), Some(response.to_owned()));
assert_eq!(miner.author(), Address::from_str("cd1722f3947def4cf144679da39c4c32bdc35681").unwrap()); assert_eq!(miner.author(), Address::from_str("cd1722f3947def4cf144679da39c4c32bdc35681").unwrap());
} }
#[test]
fn rpc_ethcore_dev_logs() {
let miner = miner_service();
let logger = logger();
logger.append("a".to_owned());
logger.append("b".to_owned());
let ethcore = EthcoreClient::new(&miner, logger.clone()).to_delegate();
let io = IoHandler::new();
io.add_delegate(ethcore);
let request = r#"{"jsonrpc": "2.0", "method": "ethcore_devLogs", "params":[], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":["b","a"],"id":1}"#;
assert_eq!(io.handle_request(request), Some(response.to_owned()));
}
#[test]
fn rpc_ethcore_dev_logs_levels() {
let miner = miner_service();
let ethcore = EthcoreClient::new(&miner, logger()).to_delegate();
let io = IoHandler::new();
io.add_delegate(ethcore);
let request = r#"{"jsonrpc": "2.0", "method": "ethcore_devLogsLevels", "params":[], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":"rpc=trace","id":1}"#;
assert_eq!(io.handle_request(request), Some(response.to_owned()));
}

View File

@ -20,7 +20,7 @@ use std::env;
use rlog::{LogLevelFilter}; use rlog::{LogLevelFilter};
use env_logger::LogBuilder; use env_logger::LogBuilder;
use std::sync::{RwLock, RwLockReadGuard}; use std::sync::{RwLock, RwLockReadGuard};
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use arrayvec::ArrayVec;
lazy_static! { lazy_static! {
static ref LOG_DUMMY: bool = { static ref LOG_DUMMY: bool = {
@ -43,35 +43,39 @@ pub fn init_log() {
let _ = *LOG_DUMMY; let _ = *LOG_DUMMY;
} }
static LOG_SIZE : usize = 128; const LOG_SIZE : usize = 128;
/// Logger implementation that keeps up to `LOG_SIZE` log elements.
pub struct RotatingLogger { pub struct RotatingLogger {
idx: AtomicUsize, /// Defined logger levels
levels: String, levels: String,
logs: RwLock<Vec<String>>, /// Logs array. Latest log is always at index 0
logs: RwLock<ArrayVec<[String; LOG_SIZE]>>,
} }
impl RotatingLogger { impl RotatingLogger {
/// Creates new `RotatingLogger` with given levels.
/// It does not enforce levels - it's just read only.
pub fn new(levels: String) -> Self { pub fn new(levels: String) -> Self {
RotatingLogger { RotatingLogger {
idx: AtomicUsize::new(0),
levels: levels, levels: levels,
logs: RwLock::new(Vec::with_capacity(LOG_SIZE)), logs: RwLock::new(ArrayVec::<[_; LOG_SIZE]>::new()),
} }
} }
/// Append new log entry
pub fn append(&self, log: String) { pub fn append(&self, log: String) {
let idx = self.idx.fetch_add(1, Ordering::SeqCst); self.logs.write().unwrap().insert(0, log);
let idx = idx % LOG_SIZE;
self.logs.write().unwrap().insert(idx, log);
} }
/// Return levels
pub fn levels(&self) -> &str { pub fn levels(&self) -> &str {
&self.levels &self.levels
} }
pub fn logs(&self) -> RwLockReadGuard<Vec<String>> { /// Return logs
pub fn logs(&self) -> RwLockReadGuard<ArrayVec<[String; LOG_SIZE]>> {
self.logs.read().unwrap() self.logs.read().unwrap()
} }