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:
@@ -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]
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
121
util/src/log.rs
121
util/src/log.rs
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
101
util/src/path.rs
101
util/src/path.rs
@@ -1,101 +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/>.
|
||||
|
||||
//! Path utilities
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
/// Get the config path for application `name`.
|
||||
/// `name` should be capitalized, e.g. `"Ethereum"`, `"Parity"`.
|
||||
pub fn config_path(name: &str) -> PathBuf {
|
||||
let mut home = ::std::env::home_dir().expect("Failed to get home dir");
|
||||
home.push("Library");
|
||||
home.push(name);
|
||||
home
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
/// Get the config path for application `name`.
|
||||
/// `name` should be capitalized, e.g. `"Ethereum"`, `"Parity"`.
|
||||
pub fn config_path(name: &str) -> PathBuf {
|
||||
let mut home = ::std::env::home_dir().expect("Failed to get home dir");
|
||||
home.push("AppData");
|
||||
home.push("Roaming");
|
||||
home.push(name);
|
||||
home
|
||||
}
|
||||
|
||||
#[cfg(not(any(target_os = "macos", windows)))]
|
||||
/// Get the config path for application `name`.
|
||||
/// `name` should be capitalized, e.g. `"Ethereum"`, `"Parity"`.
|
||||
pub fn config_path(name: &str) -> PathBuf {
|
||||
let mut home = ::std::env::home_dir().expect("Failed to get home dir");
|
||||
home.push(format!(".{}", name.to_lowercase()));
|
||||
home
|
||||
}
|
||||
|
||||
/// Get the specific folder inside a config path.
|
||||
pub fn config_path_with(name: &str, then: &str) -> PathBuf {
|
||||
let mut path = config_path(name);
|
||||
path.push(then);
|
||||
path
|
||||
}
|
||||
|
||||
/// Default ethereum paths
|
||||
pub mod ethereum {
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// Default path for ethereum installation on Mac Os
|
||||
pub fn default() -> PathBuf { super::config_path("Ethereum") }
|
||||
|
||||
/// Default path for ethereum installation (testnet)
|
||||
pub fn test() -> PathBuf {
|
||||
let mut path = default();
|
||||
path.push("testnet");
|
||||
path
|
||||
}
|
||||
|
||||
/// Get the specific folder inside default ethereum installation
|
||||
pub fn with_default(s: &str) -> PathBuf {
|
||||
let mut path = default();
|
||||
path.push(s);
|
||||
path
|
||||
}
|
||||
|
||||
/// Get the specific folder inside default ethereum installation configured for testnet
|
||||
pub fn with_testnet(s: &str) -> PathBuf {
|
||||
let mut path = default();
|
||||
path.push("testnet");
|
||||
path.push(s);
|
||||
path
|
||||
}
|
||||
}
|
||||
|
||||
/// Restricts the permissions of given path only to the owner.
|
||||
#[cfg(unix)]
|
||||
pub fn restrict_permissions_owner(file_path: &Path, write: bool, executable: bool) -> Result<(), String> {
|
||||
let perms = ::std::os::unix::fs::PermissionsExt::from_mode(0o400 + write as u32 * 0o200 + executable as u32 * 0o100);
|
||||
::std::fs::set_permissions(file_path, perms).map_err(|e| format!("{:?}", e))
|
||||
}
|
||||
|
||||
/// Restricts the permissions of given path only to the owner.
|
||||
#[cfg(not(unix))]
|
||||
pub fn restrict_permissions_owner(_file_path: &Path, _write: bool, _executable: bool) -> Result<(), String> {
|
||||
//TODO: implement me
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user