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:
Marek Kotewicz
2017-03-22 06:23:40 +01:00
committed by GitHub
parent 63f1ca9243
commit d530cc86f3
42 changed files with 93 additions and 80 deletions

View File

@@ -11,11 +11,9 @@ build = "build.rs"
log = "0.3"
env_logger = "0.3"
rustc-serialize = "0.3"
arrayvec = "0.3"
rand = "0.3.12"
time = "0.1.34"
rocksdb = { git = "https://github.com/ethcore/rust-rocksdb" }
lazy_static = "0.2"
eth-secp256k1 = { git = "https://github.com/ethcore/rust-secp256k1" }
rust-crypto = "0.2.34"
elastic-array = { git = "https://github.com/ethcore/elastic-array" }
@@ -32,11 +30,12 @@ ethcore-bigint = { path = "bigint" }
parking_lot = "0.3"
using_queue = { path = "using_queue" }
table = { path = "table" }
ansi_term = "0.7"
ansi_term = "0.9"
tiny-keccak= "1.0"
ethcore-bloom-journal = { path = "bloom" }
regex = "0.1"
lru-cache = "0.1.0"
ethcore-logger = { path = "../logger" }
[features]
default = []

View File

@@ -19,7 +19,7 @@ clippy = { version = "0.0.103", optional = true}
igd = "0.5.0"
libc = "0.2.7"
parking_lot = "0.3"
ansi_term = "0.7"
ansi_term = "0.9"
rustc-serialize = "0.3"
ethcore-io = { path = "../io" }
ethcore-util = { path = ".." }
@@ -27,6 +27,8 @@ ethcore-devtools = { path = "../../devtools" }
ethkey = { path = "../../ethkey" }
ethcrypto = { path = "../../ethcrypto" }
rlp = { path = "../rlp" }
path = { path = "../path" }
ethcore-logger = { path ="../../logger" }
[features]
default = []

View File

@@ -40,7 +40,7 @@ use node_table::*;
use stats::NetworkStats;
use discovery::{Discovery, TableUpdates, NodeEntry};
use ip_utils::{map_external_address, select_public_address};
use util::path::restrict_permissions_owner;
use path::restrict_permissions_owner;
use parking_lot::{Mutex, RwLock};
type Slab<T> = ::slab::Slab<T, usize>;

View File

@@ -71,6 +71,8 @@ extern crate ethkey;
extern crate ethcrypto as crypto;
extern crate rlp;
extern crate bytes;
extern crate path;
extern crate ethcore_logger;
#[macro_use]
extern crate log;

View File

@@ -98,7 +98,7 @@ fn net_service() {
#[test]
fn net_connect() {
::util::log::init_log();
::ethcore_logger::init_log();
let key1 = Random.generate().unwrap();
let mut config1 = NetworkConfiguration::new_local();
config1.use_secret = Some(key1.secret().clone());

6
util/path/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "path"
version = "0.1.0"
authors = ["debris <marek.kotewicz@gmail.com>"]
[dependencies]

View File

@@ -553,7 +553,7 @@ mod tests {
use hashdb::{HashDB, DBValue};
use super::*;
use super::super::traits::JournalDB;
use log::init_log;
use ethcore_logger::init_log;
use kvdb::{DatabaseConfig};
#[test]

View File

@@ -456,7 +456,7 @@ mod tests {
use common::*;
use super::*;
use hashdb::{HashDB, DBValue};
use log::init_log;
use ethcore_logger::init_log;
use journaldb::JournalDB;
use kvdb::Database;

View File

@@ -93,7 +93,6 @@ extern crate rocksdb;
extern crate env_logger;
extern crate crypto as rcrypto;
extern crate secp256k1;
extern crate arrayvec;
extern crate elastic_array;
extern crate time;
extern crate ethcore_devtools as devtools;
@@ -108,9 +107,8 @@ extern crate regex;
extern crate lru_cache;
extern crate heapsize;
extern crate itertools;
extern crate ethcore_logger;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate log as rlog;
@@ -137,8 +135,6 @@ pub mod trie;
pub mod nibbleslice;
pub mod nibblevec;
pub mod semantic_version;
pub mod log;
pub mod path;
pub mod snappy;
pub mod cache;
mod timer;
@@ -153,9 +149,9 @@ pub use triehash::*;
pub use trie::{Trie, TrieMut, TrieDB, TrieDBMut, TrieFactory, TrieError, SecTrieDB, SecTrieDBMut};
pub use nibbleslice::*;
pub use semantic_version::*;
pub use log::*;
pub use kvdb::*;
pub use timer::*;
pub use ansi_term::{Colour, Style};
/// 160-bit integer representing account address
pub type Address = H160;

View File

@@ -1,121 +0,0 @@
// 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;
pub use ansi_term::{Colour, Style};
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);
}
}

View File

@@ -958,7 +958,7 @@ mod tests {
#[test]
fn playpen() {
::log::init_log();
::ethcore_logger::init_log();
let mut seed = H256::new();
for test_i in 0..10 {