Exposing loggin via RPC

This commit is contained in:
Tomasz Drwięga 2016-04-19 09:58:26 +02:00
parent caf4d179a2
commit a21f2a0998
4 changed files with 94 additions and 12 deletions

View File

@ -278,30 +278,40 @@ struct Args {
flag_networkid: Option<String>, flag_networkid: Option<String>,
} }
fn setup_log(init: &Option<String>) { fn setup_log(init: &Option<String>) -> Arc<RotatingLogger> {
use rlog::*; use rlog::*;
let mut levels = String::new();
let mut builder = LogBuilder::new(); let mut builder = LogBuilder::new();
builder.filter(None, LogLevelFilter::Info); builder.filter(None, LogLevelFilter::Info);
if env::var("RUST_LOG").is_ok() { if env::var("RUST_LOG").is_ok() {
builder.parse(&env::var("RUST_LOG").unwrap()); let lvl = &env::var("RUST_LOG").unwrap();
levels.push_str(&lvl);
levels.push_str(",");
builder.parse(lvl);
} }
if let Some(ref s) = *init { if let Some(ref s) = *init {
levels.push_str(s);
builder.parse(s); builder.parse(s);
} }
let format = |record: &LogRecord| { let logs = Arc::new(RotatingLogger::new(levels));
let log2 = logs.clone();
let format = move |record: &LogRecord| {
let timestamp = time::strftime("%Y-%m-%d %H:%M:%S %Z", &time::now()).unwrap(); let timestamp = time::strftime("%Y-%m-%d %H:%M:%S %Z", &time::now()).unwrap();
if max_log_level() <= LogLevelFilter::Info { let format = if max_log_level() <= LogLevelFilter::Info {
format!("{}{}", timestamp, record.args()) format!("{}{}", timestamp, record.args())
} else { } else {
format!("{}{}:{}: {}", timestamp, record.level(), record.target(), record.args()) format!("{}{}:{}: {}", timestamp, record.level(), record.target(), record.args())
} };
log2.append(format.clone());
format
}; };
builder.format(format); builder.format(format);
builder.init().unwrap(); builder.init().unwrap();
logs
} }
#[cfg(feature = "rpc")] #[cfg(feature = "rpc")]
@ -313,6 +323,7 @@ fn setup_rpc_server(
url: &SocketAddr, url: &SocketAddr,
cors_domain: Option<String>, cors_domain: Option<String>,
apis: Vec<&str>, apis: Vec<&str>,
logger: Arc<RotatingLogger>,
) -> RpcServer { ) -> RpcServer {
use rpc::v1::*; use rpc::v1::*;
@ -326,7 +337,7 @@ fn setup_rpc_server(
server.add_delegate(EthFilterClient::new(&client, &miner).to_delegate()); server.add_delegate(EthFilterClient::new(&client, &miner).to_delegate());
}, },
"personal" => server.add_delegate(PersonalClient::new(&secret_store).to_delegate()), "personal" => server.add_delegate(PersonalClient::new(&secret_store).to_delegate()),
"ethcore" => server.add_delegate(EthcoreClient::new(&miner).to_delegate()), "ethcore" => server.add_delegate(EthcoreClient::new(&miner, logger.clone()).to_delegate()),
_ => { _ => {
die!("{}: Invalid API name to be enabled.", api); die!("{}: Invalid API name to be enabled.", api);
}, },
@ -348,6 +359,7 @@ fn setup_webapp_server(
miner: Arc<Miner>, miner: Arc<Miner>,
url: &SocketAddr, url: &SocketAddr,
auth: Option<(String, String)>, auth: Option<(String, String)>,
logger: Arc<RotatingLogger>,
) -> WebappServer { ) -> WebappServer {
use rpc::v1::*; use rpc::v1::*;
@ -357,7 +369,7 @@ fn setup_webapp_server(
server.add_delegate(EthClient::new(&client, &sync, &secret_store, &miner).to_delegate()); server.add_delegate(EthClient::new(&client, &sync, &secret_store, &miner).to_delegate());
server.add_delegate(EthFilterClient::new(&client, &miner).to_delegate()); server.add_delegate(EthFilterClient::new(&client, &miner).to_delegate());
server.add_delegate(PersonalClient::new(&secret_store).to_delegate()); server.add_delegate(PersonalClient::new(&secret_store).to_delegate());
server.add_delegate(EthcoreClient::new(&miner).to_delegate()); server.add_delegate(EthcoreClient::new(&miner, logger).to_delegate());
let start_result = match auth { let start_result = match auth {
None => { None => {
server.start_unsecure_http(url) server.start_unsecure_http(url)
@ -386,6 +398,7 @@ fn setup_rpc_server(
_url: &SocketAddr, _url: &SocketAddr,
_cors_domain: Option<String>, _cors_domain: Option<String>,
_apis: Vec<&str>, _apis: Vec<&str>,
_logger: Arc<RotatingLogger>,
) -> ! { ) -> ! {
die!("Your Parity version has been compiled without JSON-RPC support.") die!("Your Parity version has been compiled without JSON-RPC support.")
} }
@ -401,6 +414,7 @@ fn setup_webapp_server(
_miner: Arc<Miner>, _miner: Arc<Miner>,
_url: &SocketAddr, _url: &SocketAddr,
_auth: Option<(String, String)>, _auth: Option<(String, String)>,
_logger: Arc<RotatingLogger>,
) -> ! { ) -> ! {
die!("Your Parity version has been compiled without WebApps support.") die!("Your Parity version has been compiled without WebApps support.")
} }
@ -687,7 +701,7 @@ impl Configuration {
let panic_handler = PanicHandler::new_in_arc(); let panic_handler = PanicHandler::new_in_arc();
// Setup logging // Setup logging
setup_log(&self.args.flag_logging); let logger = setup_log(&self.args.flag_logging);
// Raise fdlimit // Raise fdlimit
unsafe { ::fdlimit::raise_fd_limit(); } unsafe { ::fdlimit::raise_fd_limit(); }
@ -738,7 +752,8 @@ impl Configuration {
miner.clone(), miner.clone(),
&addr, &addr,
cors_domain, cors_domain,
apis.split(',').collect() apis.split(',').collect(),
logger.clone(),
)) ))
} else { } else {
None None
@ -772,6 +787,7 @@ impl Configuration {
miner.clone(), miner.clone(),
&addr, &addr,
auth, auth,
logger.clone(),
)) ))
} else { } else {
None None

View File

@ -15,8 +15,9 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
//! Ethcore-specific rpc implementation. //! Ethcore-specific rpc implementation.
use util::{U256, Address}; use util::{U256, Address, RotatingLogger};
use std::sync::{Arc, Weak}; use std::sync::{Arc, Weak};
use std::ops::Deref;
use jsonrpc_core::*; use jsonrpc_core::*;
use ethminer::{MinerService}; use ethminer::{MinerService};
use v1::traits::Ethcore; use v1::traits::Ethcore;
@ -26,13 +27,15 @@ use v1::types::Bytes;
pub struct EthcoreClient<M> pub struct EthcoreClient<M>
where M: MinerService { where M: MinerService {
miner: Weak<M>, miner: Weak<M>,
logger: Arc<RotatingLogger>,
} }
impl<M> EthcoreClient<M> where M: MinerService { impl<M> EthcoreClient<M> where M: MinerService {
/// Creates new `EthcoreClient`. /// Creates new `EthcoreClient`.
pub fn new(miner: &Arc<M>) -> Self { pub fn new(miner: &Arc<M>, logger: Arc<RotatingLogger>) -> Self {
EthcoreClient { EthcoreClient {
miner: Arc::downgrade(miner) miner: Arc::downgrade(miner),
logger: logger,
} }
} }
} }
@ -78,4 +81,14 @@ impl<M> Ethcore for EthcoreClient<M> where M: MinerService + 'static {
fn gas_floor_target(&self, _: Params) -> Result<Value, Error> { fn gas_floor_target(&self, _: Params) -> Result<Value, Error> {
to_value(&take_weak!(self.miner).gas_floor_target()) to_value(&take_weak!(self.miner).gas_floor_target())
} }
fn dev_logs(&self, _params: Params) -> Result<Value, Error> {
let logs = self.logger.logs();
to_value(&logs.deref())
}
fn dev_logs_levels(&self, _params: Params) -> Result<Value, Error> {
to_value(&self.logger.levels())
}
} }

View File

@ -42,6 +42,12 @@ pub trait Ethcore: Sized + Send + Sync + 'static {
/// Returns minimal gas price for transaction to be included in queue. /// Returns minimal gas price for transaction to be included in queue.
fn min_gas_price(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() } fn min_gas_price(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns latest logs
fn dev_logs(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns logs levels
fn dev_logs_levels(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Should be used to convert object to io delegate. /// Should be used to convert object to io delegate.
fn to_delegate(self) -> IoDelegate<Self> { fn to_delegate(self) -> IoDelegate<Self> {
let mut delegate = IoDelegate::new(Arc::new(self)); let mut delegate = IoDelegate::new(Arc::new(self));
@ -53,6 +59,8 @@ pub trait Ethcore: Sized + Send + Sync + 'static {
delegate.add_method("ethcore_extraData", Ethcore::extra_data); delegate.add_method("ethcore_extraData", Ethcore::extra_data);
delegate.add_method("ethcore_gasFloorTarget", Ethcore::gas_floor_target); delegate.add_method("ethcore_gasFloorTarget", Ethcore::gas_floor_target);
delegate.add_method("ethcore_minGasPrice", Ethcore::min_gas_price); delegate.add_method("ethcore_minGasPrice", Ethcore::min_gas_price);
delegate.add_method("ethcore_devLogs", Ethcore::dev_logs);
delegate.add_method("ethcore_devLogsLevels", Ethcore::dev_logs_levels);
delegate delegate
} }
} }

View File

@ -19,6 +19,8 @@
use std::env; 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::atomic::{AtomicBool, AtomicUsize, Ordering};
lazy_static! { lazy_static! {
static ref LOG_DUMMY: bool = { static ref LOG_DUMMY: bool = {
@ -40,3 +42,46 @@ lazy_static! {
pub fn init_log() { pub fn init_log() {
let _ = *LOG_DUMMY; let _ = *LOG_DUMMY;
} }
static LOG_SIZE : usize = 128;
pub struct RotatingLogger {
idx: AtomicUsize,
levels: String,
logs: RwLock<Vec<String>>,
}
impl RotatingLogger {
pub fn new(levels: String) -> Self {
RotatingLogger {
idx: AtomicUsize::new(0),
levels: levels,
logs: RwLock::new(Vec::with_capacity(LOG_SIZE)),
}
}
pub fn append(&self, log: String) {
let idx = self.idx.fetch_add(1, Ordering::SeqCst);
let idx = idx % LOG_SIZE;
self.logs.write().unwrap().insert(idx, log);
}
pub fn levels(&self) -> &str {
&self.levels
}
pub fn logs(&self) -> RwLockReadGuard<Vec<String>> {
self.logs.read().unwrap()
}
}
#[cfg(test)]
mod test {
#[test]
fn should_have_some_tests() {
assert_eq!(true, false);
}
}