From 87d9b540af0cdbf192dd830242a7d202298e4f09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Wed, 2 Nov 2016 12:00:30 +0100 Subject: [PATCH 1/3] Initializing logger for each command --- parity/blockchain.rs | 12 ++----- parity/configuration.rs | 73 +++++++++++++++++++++-------------------- parity/main.rs | 40 +++++++++++++++++----- parity/signer.rs | 9 +++-- parity/snapshot.rs | 5 --- 5 files changed, 78 insertions(+), 61 deletions(-) diff --git a/parity/blockchain.rs b/parity/blockchain.rs index 2e0fb4233f8..8e84c488cd3 100644 --- a/parity/blockchain.rs +++ b/parity/blockchain.rs @@ -21,7 +21,6 @@ use std::time::{Instant, Duration}; use std::thread::sleep; use std::sync::Arc; use rustc_serialize::hex::FromHex; -use ethcore_logger::{setup_log, Config as LogConfig}; use io::{PanicHandler, ForwardPanic}; use util::{ToPretty, Uint}; use rlp::PayloadInfo; @@ -71,7 +70,6 @@ pub enum BlockchainCmd { #[derive(Debug, PartialEq)] pub struct ImportBlockchain { pub spec: SpecType, - pub logger_config: LogConfig, pub cache_config: CacheConfig, pub dirs: Directories, pub file_path: Option, @@ -85,12 +83,12 @@ pub struct ImportBlockchain { pub fat_db: Switch, pub vm_type: VMType, pub check_seal: bool, + pub with_color: bool, } #[derive(Debug, PartialEq)] pub struct ExportBlockchain { pub spec: SpecType, - pub logger_config: LogConfig, pub cache_config: CacheConfig, pub dirs: Directories, pub file_path: Option, @@ -120,9 +118,6 @@ fn execute_import(cmd: ImportBlockchain) -> Result { // Setup panic handler let panic_handler = PanicHandler::new_in_arc(); - // Setup logging - let _logger = setup_log(&cmd.logger_config); - // create dirs used by parity try!(cmd.dirs.create_dirs()); @@ -196,7 +191,7 @@ fn execute_import(cmd: ImportBlockchain) -> Result { } }; - let informant = Informant::new(client.clone(), None, None, None, cmd.logger_config.color); + let informant = Informant::new(client.clone(), None, None, None, cmd.with_color); try!(service.register_io_handler(Arc::new(ImportIoHandler { info: Arc::new(informant), @@ -269,9 +264,6 @@ fn execute_export(cmd: ExportBlockchain) -> Result { // Setup panic handler let panic_handler = PanicHandler::new_in_arc(); - // Setup logging - let _logger = setup_log(&cmd.logger_config); - // create dirs used by parity try!(cmd.dirs.create_dirs()); diff --git a/parity/configuration.rs b/parity/configuration.rs index 1972b9f1ebc..4f7df126c2c 100644 --- a/parity/configuration.rs +++ b/parity/configuration.rs @@ -35,7 +35,7 @@ use params::{ResealPolicy, AccountsConfig, GasPricerConfig, MinerExtras}; use ethcore_logger::Config as LogConfig; use dir::Directories; use dapps::Configuration as DappsConfiguration; -use signer::Configuration as SignerConfiguration; +use signer::{Configuration as SignerConfiguration, SignerCommand}; use run::RunCmd; use blockchain::{BlockchainCmd, ImportBlockchain, ExportBlockchain, DataFormat}; use presale::ImportWallet; @@ -45,13 +45,13 @@ use snapshot::{self, SnapshotCommand}; #[derive(Debug, PartialEq)] pub enum Cmd { Run(RunCmd), - Version, - Account(AccountCmd), - ImportPresaleWallet(ImportWallet), - Blockchain(BlockchainCmd), - SignerToken(String), - Snapshot(SnapshotCommand), - Hash(Option), + Version(LogConfig), + Account(AccountCmd, LogConfig), + ImportPresaleWallet(ImportWallet, LogConfig), + Blockchain(BlockchainCmd, LogConfig), + SignerToken(SignerCommand, LogConfig), + Snapshot(SnapshotCommand, LogConfig), + Hash(Option, LogConfig), } #[derive(Debug, PartialEq)] @@ -96,12 +96,15 @@ impl Configuration { let signer_conf = self.signer_config(); let format = try!(self.format()); + // NOTE Each command should initialize logger! let cmd = if self.args.flag_version { - Cmd::Version + Cmd::Version(logger_config) } else if self.args.cmd_signer && self.args.cmd_new_token { - Cmd::SignerToken(dirs.signer) + Cmd::SignerToken(SignerCommand { + path: dirs.signer + }, logger_config) } else if self.args.cmd_tools && self.args.cmd_hash { - Cmd::Hash(self.args.arg_file) + Cmd::Hash(self.args.arg_file, logger_config) } else if self.args.cmd_account { let account_cmd = if self.args.cmd_new { let new_acc = NewAccount { @@ -121,7 +124,7 @@ impl Configuration { } else { unreachable!(); }; - Cmd::Account(account_cmd) + Cmd::Account(account_cmd, logger_config) } else if self.args.flag_import_geth_keys { let account_cmd = AccountCmd::ImportFromGeth( ImportFromGethAccounts { @@ -129,7 +132,7 @@ impl Configuration { testnet: self.args.flag_testnet } ); - Cmd::Account(account_cmd) + Cmd::Account(account_cmd, logger_config) } else if self.args.cmd_wallet { let presale_cmd = ImportWallet { iterations: self.args.flag_keys_iterations, @@ -137,11 +140,10 @@ impl Configuration { wallet_path: self.args.arg_path.first().unwrap().clone(), password_file: self.args.flag_password.first().cloned(), }; - Cmd::ImportPresaleWallet(presale_cmd) + Cmd::ImportPresaleWallet(presale_cmd, logger_config) } else if self.args.cmd_import { let import_cmd = ImportBlockchain { spec: spec, - logger_config: logger_config, cache_config: cache_config, dirs: dirs, file_path: self.args.arg_file.clone(), @@ -155,12 +157,12 @@ impl Configuration { fat_db: fat_db, vm_type: vm_type, check_seal: !self.args.flag_no_seal_check, + with_color: logger_config.color, }; - Cmd::Blockchain(BlockchainCmd::Import(import_cmd)) + Cmd::Blockchain(BlockchainCmd::Import(import_cmd), logger_config) } else if self.args.cmd_export { let export_cmd = ExportBlockchain { spec: spec, - logger_config: logger_config, cache_config: cache_config, dirs: dirs, file_path: self.args.arg_file.clone(), @@ -176,7 +178,7 @@ impl Configuration { to_block: try!(to_block_id(&self.args.flag_to)), check_seal: !self.args.flag_no_seal_check, }; - Cmd::Blockchain(BlockchainCmd::Export(export_cmd)) + Cmd::Blockchain(BlockchainCmd::Export(export_cmd), logger_config) } else if self.args.cmd_snapshot { let snapshot_cmd = SnapshotCommand { cache_config: cache_config, @@ -184,7 +186,6 @@ impl Configuration { spec: spec, pruning: pruning, pruning_history: pruning_history, - logger_config: logger_config, mode: mode, tracing: tracing, fat_db: fat_db, @@ -194,7 +195,7 @@ impl Configuration { kind: snapshot::Kind::Take, block_at: try!(to_block_id(&self.args.flag_at)), }; - Cmd::Snapshot(snapshot_cmd) + Cmd::Snapshot(snapshot_cmd, logger_config) } else if self.args.cmd_restore { let restore_cmd = SnapshotCommand { cache_config: cache_config, @@ -202,7 +203,6 @@ impl Configuration { spec: spec, pruning: pruning, pruning_history: pruning_history, - logger_config: logger_config, mode: mode, tracing: tracing, fat_db: fat_db, @@ -212,7 +212,7 @@ impl Configuration { kind: snapshot::Kind::Restore, block_at: try!(to_block_id("latest")), // unimportant. }; - Cmd::Snapshot(restore_cmd) + Cmd::Snapshot(restore_cmd, logger_config) } else { let daemon = if self.args.cmd_daemon { Some(self.args.arg_pid_file.clone()) @@ -684,7 +684,7 @@ mod tests { use ethcore::miner::{MinerOptions, PrioritizationStrategy}; use helpers::{replace_home, default_network_config}; use run::RunCmd; - use signer::Configuration as SignerConfiguration; + use signer::{Configuration as SignerConfiguration, SignerCommand}; use blockchain::{BlockchainCmd, ImportBlockchain, ExportBlockchain, DataFormat}; use presale::ImportWallet; use account::{AccountCmd, NewAccount, ImportAccounts}; @@ -705,7 +705,7 @@ mod tests { fn test_command_version() { let args = vec!["parity", "--version"]; let conf = parse(&args); - assert_eq!(conf.into_command().unwrap(), Cmd::Version); + assert_eq!(conf.into_command().unwrap(), Cmd::Version(Default::default())); } #[test] @@ -716,7 +716,7 @@ mod tests { iterations: 10240, path: replace_home("$HOME/.parity/keys"), password_file: None, - }))); + }), Default::default())); } #[test] @@ -724,8 +724,9 @@ mod tests { let args = vec!["parity", "account", "list"]; let conf = parse(&args); assert_eq!(conf.into_command().unwrap(), Cmd::Account( - AccountCmd::List(replace_home("$HOME/.parity/keys"))) - ); + AccountCmd::List(replace_home("$HOME/.parity/keys")), + Default::default(), + )); } #[test] @@ -735,7 +736,7 @@ mod tests { assert_eq!(conf.into_command().unwrap(), Cmd::Account(AccountCmd::Import(ImportAccounts { from: vec!["my_dir".into(), "another_dir".into()], to: replace_home("$HOME/.parity/keys"), - }))); + }), Default::default())); } #[test] @@ -747,7 +748,7 @@ mod tests { path: replace_home("$HOME/.parity/keys"), wallet_path: "my_wallet.json".into(), password_file: Some("pwd".into()), - })); + }, Default::default())); } #[test] @@ -756,7 +757,6 @@ mod tests { let conf = parse(&args); assert_eq!(conf.into_command().unwrap(), Cmd::Blockchain(BlockchainCmd::Import(ImportBlockchain { spec: Default::default(), - logger_config: Default::default(), cache_config: Default::default(), dirs: Default::default(), file_path: Some("blockchain.json".into()), @@ -770,7 +770,8 @@ mod tests { fat_db: Default::default(), vm_type: VMType::Interpreter, check_seal: true, - }))); + with_color: true, + }), Default::default())); } #[test] @@ -779,7 +780,6 @@ mod tests { let conf = parse(&args); assert_eq!(conf.into_command().unwrap(), Cmd::Blockchain(BlockchainCmd::Export(ExportBlockchain { spec: Default::default(), - logger_config: Default::default(), cache_config: Default::default(), dirs: Default::default(), file_path: Some("blockchain.json".into()), @@ -794,7 +794,7 @@ mod tests { from_block: BlockID::Number(1), to_block: BlockID::Latest, check_seal: true, - }))); + }), Default::default())); } #[test] @@ -803,7 +803,6 @@ mod tests { let conf = parse(&args); assert_eq!(conf.into_command().unwrap(), Cmd::Blockchain(BlockchainCmd::Export(ExportBlockchain { spec: Default::default(), - logger_config: Default::default(), cache_config: Default::default(), dirs: Default::default(), file_path: Some("blockchain.json".into()), @@ -818,7 +817,7 @@ mod tests { from_block: BlockID::Number(1), to_block: BlockID::Latest, check_seal: true, - }))); + }), Default::default())); } #[test] @@ -826,7 +825,9 @@ mod tests { let args = vec!["parity", "signer", "new-token"]; let conf = parse(&args); let expected = replace_home("$HOME/.parity/signer"); - assert_eq!(conf.into_command().unwrap(), Cmd::SignerToken(expected)); + assert_eq!(conf.into_command().unwrap(), Cmd::SignerToken(SignerCommand { + path: expected, + }, Default::default())); } #[test] diff --git a/parity/main.rs b/parity/main.rs index 83598231aaf..909957c59a4 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -120,6 +120,7 @@ use util::sha3::sha3; use cli::Args; use configuration::{Cmd, Configuration}; use deprecated::find_deprecated; +use ethcore_logger::setup_log; fn print_hash_of(maybe_file: Option) -> Result { if let Some(file) = maybe_file { @@ -131,19 +132,42 @@ fn print_hash_of(maybe_file: Option) -> Result { } } +const LOGGER_PROOF: &'static str = "Logger is initialized only once; qed"; + fn execute(command: Cmd) -> Result { match command { Cmd::Run(run_cmd) => { try!(run::execute(run_cmd)); Ok("".into()) }, - Cmd::Version => Ok(Args::print_version()), - Cmd::Hash(maybe_file) => print_hash_of(maybe_file), - Cmd::Account(account_cmd) => account::execute(account_cmd), - Cmd::ImportPresaleWallet(presale_cmd) => presale::execute(presale_cmd), - Cmd::Blockchain(blockchain_cmd) => blockchain::execute(blockchain_cmd), - Cmd::SignerToken(path) => signer::new_token(path), - Cmd::Snapshot(snapshot_cmd) => snapshot::execute(snapshot_cmd), + Cmd::Version(log) => { + setup_log(&log).expect(LOGGER_PROOF); + Ok(Args::print_version()) + }, + Cmd::Hash(maybe_file, log) => { + setup_log(&log).expect(LOGGER_PROOF); + print_hash_of(maybe_file) + }, + Cmd::Account(account_cmd, log) => { + setup_log(&log).expect(LOGGER_PROOF); + account::execute(account_cmd) + }, + Cmd::ImportPresaleWallet(presale_cmd, log) => { + setup_log(&log).expect(LOGGER_PROOF); + presale::execute(presale_cmd) + }, + Cmd::Blockchain(blockchain_cmd, log) => { + setup_log(&log).expect(LOGGER_PROOF); + blockchain::execute(blockchain_cmd) + }, + Cmd::SignerToken(signer_cmd, log) => { + setup_log(&log).expect(LOGGER_PROOF); + signer::execute(signer_cmd) + }, + Cmd::Snapshot(snapshot_cmd, log) => { + setup_log(&log).expect(LOGGER_PROOF); + snapshot::execute(snapshot_cmd) + }, } } @@ -198,7 +222,7 @@ fn sync_main() -> bool { fn main() { // Always print backtrace on panic. ::std::env::set_var("RUST_BACKTRACE", "1"); - + if sync_main() { return; } diff --git a/parity/signer.rs b/parity/signer.rs index 5097192ad1b..b3047c739a3 100644 --- a/parity/signer.rs +++ b/parity/signer.rs @@ -68,8 +68,13 @@ fn codes_path(path: String) -> PathBuf { p } -pub fn new_token(path: String) -> Result { - generate_new_token(path) +#[derive(Debug, PartialEq)] +pub struct SignerCommand { + pub path: String, +} + +pub fn execute(cmd: SignerCommand) -> Result { + generate_new_token(cmd.path) .map(|code| format!("This key code will authorise your System Signer UI: {}", Colour::White.bold().paint(code))) .map_err(|err| format!("Error generating token: {:?}", err)) } diff --git a/parity/snapshot.rs b/parity/snapshot.rs index 4cd972230f1..85e1f90eb2c 100644 --- a/parity/snapshot.rs +++ b/parity/snapshot.rs @@ -20,7 +20,6 @@ use std::time::Duration; use std::path::{Path, PathBuf}; use std::sync::Arc; -use ethcore_logger::{setup_log, Config as LogConfig}; use ethcore::snapshot::{Progress, RestorationStatus, SnapshotService as SS}; use ethcore::snapshot::io::{SnapshotReader, PackedReader, PackedWriter}; use ethcore::snapshot::service::Service as SnapshotService; @@ -55,7 +54,6 @@ pub struct SnapshotCommand { pub spec: SpecType, pub pruning: Pruning, pub pruning_history: u64, - pub logger_config: LogConfig, pub mode: Mode, pub tracing: Switch, pub fat_db: Switch, @@ -141,9 +139,6 @@ impl SnapshotCommand { // load user defaults let user_defaults = try!(UserDefaults::load(&user_defaults_path)); - // Setup logging - let _logger = setup_log(&self.logger_config); - fdlimit::raise_fd_limit(); // select pruning algorithm From cbc55a42604ea3080b47ff2297dac346d76842de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Wed, 2 Nov 2016 13:50:33 +0100 Subject: [PATCH 2/3] Single logger setup --- logger/src/lib.rs | 2 +- parity/configuration.rs | 83 ++++++++++++++++++++++------------------- parity/main.rs | 45 ++++++---------------- parity/run.rs | 9 ++--- 4 files changed, 61 insertions(+), 78 deletions(-) diff --git a/logger/src/lib.rs b/logger/src/lib.rs index a79f6fc437d..7e13f406cf5 100644 --- a/logger/src/lib.rs +++ b/logger/src/lib.rs @@ -36,7 +36,7 @@ use regex::Regex; use util::RotatingLogger; use util::log::Colour; -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Clone)] pub struct Config { pub mode: Option, pub color: bool, diff --git a/parity/configuration.rs b/parity/configuration.rs index 4f7df126c2c..8f0a87f667a 100644 --- a/parity/configuration.rs +++ b/parity/configuration.rs @@ -45,13 +45,18 @@ use snapshot::{self, SnapshotCommand}; #[derive(Debug, PartialEq)] pub enum Cmd { Run(RunCmd), - Version(LogConfig), - Account(AccountCmd, LogConfig), - ImportPresaleWallet(ImportWallet, LogConfig), - Blockchain(BlockchainCmd, LogConfig), - SignerToken(SignerCommand, LogConfig), - Snapshot(SnapshotCommand, LogConfig), - Hash(Option, LogConfig), + Version, + Account(AccountCmd), + ImportPresaleWallet(ImportWallet), + Blockchain(BlockchainCmd), + SignerToken(SignerCommand), + Snapshot(SnapshotCommand), + Hash(Option), +} + +pub struct Execute { + pub logger: LogConfig, + pub cmd: Cmd, } #[derive(Debug, PartialEq)] @@ -70,7 +75,7 @@ impl Configuration { Ok(config) } - pub fn into_command(self) -> Result { + pub fn into_command(self) -> Result { let dirs = self.directories(); let pruning = try!(self.args.flag_pruning.parse()); let pruning_history = self.args.flag_pruning_history; @@ -98,13 +103,13 @@ impl Configuration { // NOTE Each command should initialize logger! let cmd = if self.args.flag_version { - Cmd::Version(logger_config) + Cmd::Version } else if self.args.cmd_signer && self.args.cmd_new_token { Cmd::SignerToken(SignerCommand { path: dirs.signer - }, logger_config) + }) } else if self.args.cmd_tools && self.args.cmd_hash { - Cmd::Hash(self.args.arg_file, logger_config) + Cmd::Hash(self.args.arg_file) } else if self.args.cmd_account { let account_cmd = if self.args.cmd_new { let new_acc = NewAccount { @@ -124,7 +129,7 @@ impl Configuration { } else { unreachable!(); }; - Cmd::Account(account_cmd, logger_config) + Cmd::Account(account_cmd) } else if self.args.flag_import_geth_keys { let account_cmd = AccountCmd::ImportFromGeth( ImportFromGethAccounts { @@ -132,7 +137,7 @@ impl Configuration { testnet: self.args.flag_testnet } ); - Cmd::Account(account_cmd, logger_config) + Cmd::Account(account_cmd) } else if self.args.cmd_wallet { let presale_cmd = ImportWallet { iterations: self.args.flag_keys_iterations, @@ -140,7 +145,7 @@ impl Configuration { wallet_path: self.args.arg_path.first().unwrap().clone(), password_file: self.args.flag_password.first().cloned(), }; - Cmd::ImportPresaleWallet(presale_cmd, logger_config) + Cmd::ImportPresaleWallet(presale_cmd) } else if self.args.cmd_import { let import_cmd = ImportBlockchain { spec: spec, @@ -159,7 +164,7 @@ impl Configuration { check_seal: !self.args.flag_no_seal_check, with_color: logger_config.color, }; - Cmd::Blockchain(BlockchainCmd::Import(import_cmd), logger_config) + Cmd::Blockchain(BlockchainCmd::Import(import_cmd)) } else if self.args.cmd_export { let export_cmd = ExportBlockchain { spec: spec, @@ -178,7 +183,7 @@ impl Configuration { to_block: try!(to_block_id(&self.args.flag_to)), check_seal: !self.args.flag_no_seal_check, }; - Cmd::Blockchain(BlockchainCmd::Export(export_cmd), logger_config) + Cmd::Blockchain(BlockchainCmd::Export(export_cmd)) } else if self.args.cmd_snapshot { let snapshot_cmd = SnapshotCommand { cache_config: cache_config, @@ -195,7 +200,7 @@ impl Configuration { kind: snapshot::Kind::Take, block_at: try!(to_block_id(&self.args.flag_at)), }; - Cmd::Snapshot(snapshot_cmd, logger_config) + Cmd::Snapshot(snapshot_cmd) } else if self.args.cmd_restore { let restore_cmd = SnapshotCommand { cache_config: cache_config, @@ -212,7 +217,7 @@ impl Configuration { kind: snapshot::Kind::Restore, block_at: try!(to_block_id("latest")), // unimportant. }; - Cmd::Snapshot(restore_cmd, logger_config) + Cmd::Snapshot(restore_cmd) } else { let daemon = if self.args.cmd_daemon { Some(self.args.arg_pid_file.clone()) @@ -227,7 +232,7 @@ impl Configuration { pruning: pruning, pruning_history: pruning_history, daemon: daemon, - logger_config: logger_config, + logger_config: logger_config.clone(), miner_options: miner_options, http_conf: http_conf, ipc_conf: ipc_conf, @@ -258,7 +263,10 @@ impl Configuration { Cmd::Run(run_cmd) }; - Ok(cmd) + Ok(Execute { + logger: logger_config, + cmd: cmd, + }) } fn enable_network(&self, mode: &Mode) -> bool { @@ -705,27 +713,26 @@ mod tests { fn test_command_version() { let args = vec!["parity", "--version"]; let conf = parse(&args); - assert_eq!(conf.into_command().unwrap(), Cmd::Version(Default::default())); + assert_eq!(conf.into_command().unwrap().cmd, Cmd::Version); } #[test] fn test_command_account_new() { let args = vec!["parity", "account", "new"]; let conf = parse(&args); - assert_eq!(conf.into_command().unwrap(), Cmd::Account(AccountCmd::New(NewAccount { + assert_eq!(conf.into_command().unwrap().cmd, Cmd::Account(AccountCmd::New(NewAccount { iterations: 10240, path: replace_home("$HOME/.parity/keys"), password_file: None, - }), Default::default())); + }))); } #[test] fn test_command_account_list() { let args = vec!["parity", "account", "list"]; let conf = parse(&args); - assert_eq!(conf.into_command().unwrap(), Cmd::Account( + assert_eq!(conf.into_command().unwrap().cmd, Cmd::Account( AccountCmd::List(replace_home("$HOME/.parity/keys")), - Default::default(), )); } @@ -733,29 +740,29 @@ mod tests { fn test_command_account_import() { let args = vec!["parity", "account", "import", "my_dir", "another_dir"]; let conf = parse(&args); - assert_eq!(conf.into_command().unwrap(), Cmd::Account(AccountCmd::Import(ImportAccounts { + assert_eq!(conf.into_command().unwrap().cmd, Cmd::Account(AccountCmd::Import(ImportAccounts { from: vec!["my_dir".into(), "another_dir".into()], to: replace_home("$HOME/.parity/keys"), - }), Default::default())); + }))); } #[test] fn test_command_wallet_import() { let args = vec!["parity", "wallet", "import", "my_wallet.json", "--password", "pwd"]; let conf = parse(&args); - assert_eq!(conf.into_command().unwrap(), Cmd::ImportPresaleWallet(ImportWallet { + assert_eq!(conf.into_command().unwrap().cmd, Cmd::ImportPresaleWallet(ImportWallet { iterations: 10240, path: replace_home("$HOME/.parity/keys"), wallet_path: "my_wallet.json".into(), password_file: Some("pwd".into()), - }, Default::default())); + })); } #[test] fn test_command_blockchain_import() { let args = vec!["parity", "import", "blockchain.json"]; let conf = parse(&args); - assert_eq!(conf.into_command().unwrap(), Cmd::Blockchain(BlockchainCmd::Import(ImportBlockchain { + assert_eq!(conf.into_command().unwrap().cmd, Cmd::Blockchain(BlockchainCmd::Import(ImportBlockchain { spec: Default::default(), cache_config: Default::default(), dirs: Default::default(), @@ -771,14 +778,14 @@ mod tests { vm_type: VMType::Interpreter, check_seal: true, with_color: true, - }), Default::default())); + }))); } #[test] fn test_command_blockchain_export() { let args = vec!["parity", "export", "blockchain.json"]; let conf = parse(&args); - assert_eq!(conf.into_command().unwrap(), Cmd::Blockchain(BlockchainCmd::Export(ExportBlockchain { + assert_eq!(conf.into_command().unwrap().cmd, Cmd::Blockchain(BlockchainCmd::Export(ExportBlockchain { spec: Default::default(), cache_config: Default::default(), dirs: Default::default(), @@ -794,14 +801,14 @@ mod tests { from_block: BlockID::Number(1), to_block: BlockID::Latest, check_seal: true, - }), Default::default())); + }))); } #[test] fn test_command_blockchain_export_with_custom_format() { let args = vec!["parity", "export", "--format", "hex", "blockchain.json"]; let conf = parse(&args); - assert_eq!(conf.into_command().unwrap(), Cmd::Blockchain(BlockchainCmd::Export(ExportBlockchain { + assert_eq!(conf.into_command().unwrap().cmd, Cmd::Blockchain(BlockchainCmd::Export(ExportBlockchain { spec: Default::default(), cache_config: Default::default(), dirs: Default::default(), @@ -817,7 +824,7 @@ mod tests { from_block: BlockID::Number(1), to_block: BlockID::Latest, check_seal: true, - }), Default::default())); + }))); } #[test] @@ -825,16 +832,16 @@ mod tests { let args = vec!["parity", "signer", "new-token"]; let conf = parse(&args); let expected = replace_home("$HOME/.parity/signer"); - assert_eq!(conf.into_command().unwrap(), Cmd::SignerToken(SignerCommand { + assert_eq!(conf.into_command().unwrap().cmd, Cmd::SignerToken(SignerCommand { path: expected, - }, Default::default())); + })); } #[test] fn test_run_cmd() { let args = vec!["parity"]; let conf = parse(&args); - assert_eq!(conf.into_command().unwrap(), Cmd::Run(RunCmd { + assert_eq!(conf.into_command().unwrap().cmd, Cmd::Run(RunCmd { cache_config: Default::default(), dirs: Default::default(), spec: Default::default(), diff --git a/parity/main.rs b/parity/main.rs index 909957c59a4..1b650053036 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -118,7 +118,7 @@ use std::io::BufReader; use std::fs::File; use util::sha3::sha3; use cli::Args; -use configuration::{Cmd, Configuration}; +use configuration::{Cmd, Execute, Configuration}; use deprecated::find_deprecated; use ethcore_logger::setup_log; @@ -132,42 +132,21 @@ fn print_hash_of(maybe_file: Option) -> Result { } } -const LOGGER_PROOF: &'static str = "Logger is initialized only once; qed"; +fn execute(command: Execute) -> Result { + let logger = setup_log(&command.logger).expect("Logger is initialized only once; qed"); -fn execute(command: Cmd) -> Result { - match command { + match command.cmd { Cmd::Run(run_cmd) => { - try!(run::execute(run_cmd)); + try!(run::execute(run_cmd, logger)); Ok("".into()) }, - Cmd::Version(log) => { - setup_log(&log).expect(LOGGER_PROOF); - Ok(Args::print_version()) - }, - Cmd::Hash(maybe_file, log) => { - setup_log(&log).expect(LOGGER_PROOF); - print_hash_of(maybe_file) - }, - Cmd::Account(account_cmd, log) => { - setup_log(&log).expect(LOGGER_PROOF); - account::execute(account_cmd) - }, - Cmd::ImportPresaleWallet(presale_cmd, log) => { - setup_log(&log).expect(LOGGER_PROOF); - presale::execute(presale_cmd) - }, - Cmd::Blockchain(blockchain_cmd, log) => { - setup_log(&log).expect(LOGGER_PROOF); - blockchain::execute(blockchain_cmd) - }, - Cmd::SignerToken(signer_cmd, log) => { - setup_log(&log).expect(LOGGER_PROOF); - signer::execute(signer_cmd) - }, - Cmd::Snapshot(snapshot_cmd, log) => { - setup_log(&log).expect(LOGGER_PROOF); - snapshot::execute(snapshot_cmd) - }, + Cmd::Version => Ok(Args::print_version()), + Cmd::Hash(maybe_file) => print_hash_of(maybe_file), + Cmd::Account(account_cmd) => account::execute(account_cmd), + Cmd::ImportPresaleWallet(presale_cmd) => presale::execute(presale_cmd), + Cmd::Blockchain(blockchain_cmd) => blockchain::execute(blockchain_cmd), + Cmd::SignerToken(signer_cmd) => signer::execute(signer_cmd), + Cmd::Snapshot(snapshot_cmd) => snapshot::execute(snapshot_cmd), } } diff --git a/parity/run.rs b/parity/run.rs index 8a3ae06ecb8..b0f8dce686e 100644 --- a/parity/run.rs +++ b/parity/run.rs @@ -17,11 +17,11 @@ use std::sync::{Arc, Mutex, Condvar}; use ctrlc::CtrlC; use fdlimit::raise_fd_limit; -use ethcore_logger::{Config as LogConfig, setup_log}; use ethcore_rpc::{NetworkSettings, is_major_importing}; use ethsync::NetworkConfiguration; -use util::{Colour, version, U256}; +use util::{Colour, version, U256, RotatingLogger}; use io::{MayPanic, ForwardPanic, PanicHandler}; +use ethcore_logger::{Config as LogConfig}; use ethcore::client::{Mode, DatabaseCompactionProfile, VMType, ChainNotify, BlockChainClient}; use ethcore::service::ClientService; use ethcore::account_provider::AccountProvider; @@ -93,13 +93,10 @@ pub struct RunCmd { pub check_seal: bool, } -pub fn execute(cmd: RunCmd) -> Result<(), String> { +pub fn execute(cmd: RunCmd, logger: Arc) -> Result<(), String> { // set up panic handler let panic_handler = PanicHandler::new_in_arc(); - // set up logger - let logger = try!(setup_log(&cmd.logger_config)); - // increase max number of open files raise_fd_limit(); From 82190b0a791cd5bd0bcf822a82056841955ee1c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Wed, 2 Nov 2016 13:59:54 +0100 Subject: [PATCH 3/3] Whitespace [ci:skip] --- parity/configuration.rs | 1 - parity/main.rs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/parity/configuration.rs b/parity/configuration.rs index 8f0a87f667a..8c9a4d0bd51 100644 --- a/parity/configuration.rs +++ b/parity/configuration.rs @@ -101,7 +101,6 @@ impl Configuration { let signer_conf = self.signer_config(); let format = try!(self.format()); - // NOTE Each command should initialize logger! let cmd = if self.args.flag_version { Cmd::Version } else if self.args.cmd_signer && self.args.cmd_new_token { diff --git a/parity/main.rs b/parity/main.rs index 1b650053036..8fe75221661 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -146,7 +146,7 @@ fn execute(command: Execute) -> Result { Cmd::ImportPresaleWallet(presale_cmd) => presale::execute(presale_cmd), Cmd::Blockchain(blockchain_cmd) => blockchain::execute(blockchain_cmd), Cmd::SignerToken(signer_cmd) => signer::execute(signer_cmd), - Cmd::Snapshot(snapshot_cmd) => snapshot::execute(snapshot_cmd), + Cmd::Snapshot(snapshot_cmd) => snapshot::execute(snapshot_cmd), } }