From 96997c5a467b710286bc9c6fea818b9d7d76f254 Mon Sep 17 00:00:00 2001 From: Arjun Date: Fri, 15 Nov 2019 10:30:23 -0800 Subject: [PATCH] Move logging to lightclient --- cli/src/lib.rs | 54 ++++-------------------------------------- lib/src/commands.rs | 5 +--- lib/src/lightclient.rs | 53 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 58 insertions(+), 54 deletions(-) diff --git a/cli/src/lib.rs b/cli/src/lib.rs index a2dec3d..0d44911 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -1,18 +1,8 @@ -use std::io::{self, Error, ErrorKind}; +use std::io::{self}; use std::sync::Arc; use std::sync::mpsc::{channel, Sender, Receiver}; -use log::{info, error, LevelFilter}; -use log4rs::append::rolling_file::RollingFileAppender; -use log4rs::encode::pattern::PatternEncoder; -use log4rs::config::{Appender, Config, Root}; -use log4rs::filter::threshold::ThresholdFilter; -use log4rs::append::rolling_file::policy::compound::{ - CompoundPolicy, - trigger::size::SizeTrigger, - roll::fixed_window::FixedWindowRoller, -}; - +use log::{info, error}; use zecwalletlitelib::{commands, lightclient::{LightClient, LightClientConfig}, @@ -85,48 +75,11 @@ pub fn report_permission_error() { } } -/// Build the Logging config -pub fn get_log_config(config: &LightClientConfig) -> io::Result { - let window_size = 3; // log0, log1, log2 - let fixed_window_roller = - FixedWindowRoller::builder().build("zecwallet-light-wallet-log{}",window_size).unwrap(); - let size_limit = 5 * 1024 * 1024; // 5MB as max log file size to roll - let size_trigger = SizeTrigger::new(size_limit); - let compound_policy = CompoundPolicy::new(Box::new(size_trigger),Box::new(fixed_window_roller)); - - Config::builder() - .appender( - Appender::builder() - .filter(Box::new(ThresholdFilter::new(LevelFilter::Info))) - .build( - "logfile", - Box::new( - RollingFileAppender::builder() - .encoder(Box::new(PatternEncoder::new("{d} {l}::{m}{n}"))) - .build(config.get_log_path(), Box::new(compound_policy))?, - ), - ), - ) - .build( - Root::builder() - .appender("logfile") - .build(LevelFilter::Debug), - ) - .map_err(|e|Error::new(ErrorKind::Other, format!("{}", e))) -} - - pub fn startup(server: http::Uri, dangerous: bool, seed: Option, birthday: u64, first_sync: bool, print_updates: bool) -> io::Result<(Sender<(String, Vec)>, Receiver)> { // Try to get the configuration let (config, latest_block_height) = LightClientConfig::create(server.clone(), dangerous)?; - // Configure logging first. - let log_config = get_log_config(&config)?; - log4rs::init_config(log_config).map_err(|e| { - std::io::Error::new(ErrorKind::Other, e) - })?; - let lightclient = match seed { Some(phrase) => Arc::new(LightClient::new_from_phrase(phrase, &config, birthday)?), None => { @@ -139,6 +92,9 @@ pub fn startup(server: http::Uri, dangerous: bool, seed: Option, birthda } }; + // Initialize logging + lightclient.init_logging()?; + // Print startup Messages info!(""); // Blank line info!("Starting Zecwallet-CLI"); diff --git a/lib/src/commands.rs b/lib/src/commands.rs index 8f4f4f3..2d7bff8 100644 --- a/lib/src/commands.rs +++ b/lib/src/commands.rs @@ -628,10 +628,7 @@ impl Command for HeightCommand { fn exec(&self, _args: &[&str], lightclient: &LightClient) -> String { match lightclient.do_sync(true) { - Ok(_) => format!("{}", - object! { - "height" => lightclient.last_scanned_height() - }.pretty(2)), + Ok(_) => format!("{}", object! { "height" => lightclient.last_scanned_height()}.pretty(2)), Err(e) => e } } diff --git a/lib/src/lightclient.rs b/lib/src/lightclient.rs index 45de309..ab5e41a 100644 --- a/lib/src/lightclient.rs +++ b/lib/src/lightclient.rs @@ -1,6 +1,5 @@ use crate::lightwallet::LightWallet; -use log::{info, warn, error}; use rand::{rngs::OsRng, seq::SliceRandom}; use std::sync::{Arc, RwLock, Mutex}; @@ -20,6 +19,17 @@ use zcash_client_backend::{ constants::testnet, constants::mainnet, constants::regtest, encoding::encode_payment_address, }; +use log::{info, warn, error, LevelFilter}; +use log4rs::append::rolling_file::RollingFileAppender; +use log4rs::encode::pattern::PatternEncoder; +use log4rs::config::{Appender, Config, Root}; +use log4rs::filter::threshold::ThresholdFilter; +use log4rs::append::rolling_file::policy::compound::{ + CompoundPolicy, + trigger::size::SizeTrigger, + roll::fixed_window::FixedWindowRoller, +}; + use crate::grpc_client::{BlockId}; use crate::grpcconnector::{self, *}; use crate::SaplingParams; @@ -100,6 +110,37 @@ impl LightClientConfig { Ok((config, info.block_height)) } + + /// Build the Logging config + pub fn get_log_config(&self) -> io::Result { + let window_size = 3; // log0, log1, log2 + let fixed_window_roller = + FixedWindowRoller::builder().build("zecwallet-light-wallet-log{}",window_size).unwrap(); + let size_limit = 5 * 1024 * 1024; // 5MB as max log file size to roll + let size_trigger = SizeTrigger::new(size_limit); + let compound_policy = CompoundPolicy::new(Box::new(size_trigger),Box::new(fixed_window_roller)); + + Config::builder() + .appender( + Appender::builder() + .filter(Box::new(ThresholdFilter::new(LevelFilter::Info))) + .build( + "logfile", + Box::new( + RollingFileAppender::builder() + .encoder(Box::new(PatternEncoder::new("{d} {l}::{m}{n}"))) + .build(self.get_log_path(), Box::new(compound_policy))?, + ), + ), + ) + .build( + Root::builder() + .appender("logfile") + .build(LevelFilter::Debug), + ) + .map_err(|e|Error::new(ErrorKind::Other, format!("{}", e))) + } + pub fn get_zcash_data_path(&self) -> Box { let mut zcash_data_location; if self.data_dir.is_some() { @@ -361,6 +402,16 @@ impl LightClient { Ok(lc) } + pub fn init_logging(&self) -> io::Result<()> { + // Configure logging first. + let log_config = self.config.get_log_config()?; + log4rs::init_config(log_config).map_err(|e| { + std::io::Error::new(ErrorKind::Other, e) + })?; + + Ok(()) + } + pub fn attempt_recover_seed(config: &LightClientConfig) -> Result { use std::io::prelude::*; use byteorder::{LittleEndian, ReadBytesExt,};