Browse Source

Move logging to lightclient

checkpoints
Arjun 5 years ago
committed by Aditya Kulkarni
parent
commit
96997c5a46
  1. 54
      cli/src/lib.rs
  2. 5
      lib/src/commands.rs
  3. 53
      lib/src/lightclient.rs

54
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<Config> {
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<String>, birthday: u64, first_sync: bool, print_updates: bool)
-> io::Result<(Sender<(String, Vec<String>)>, Receiver<String>)> {
// 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<String>, birthda
}
};
// Initialize logging
lightclient.init_logging()?;
// Print startup Messages
info!(""); // Blank line
info!("Starting Zecwallet-CLI");

5
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
}
}

53
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<Config> {
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<Path> {
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<String, String> {
use std::io::prelude::*;
use byteorder::{LittleEndian, ReadBytesExt,};

Loading…
Cancel
Save