Move logging initialization into config.rs

This commit is contained in:
Roman Zeyde 2018-06-21 22:39:17 +03:00
parent 0171b8f63c
commit 8925857085
No known key found for this signature in database
GPG Key ID: 87CAE5FA46917CBB
3 changed files with 14 additions and 17 deletions

View File

@ -1,6 +1,5 @@
extern crate electrs;
extern crate error_chain;
extern crate stderrlog;
use error_chain::ChainedError;
use std::time::Duration;
@ -48,17 +47,6 @@ fn run_server(config: &Config) -> Result<()> {
fn main() {
let config = Config::from_args();
eprintln!("{:?}", config);
stderrlog::new()
.module(module_path!())
.verbosity(config.verbosity)
.timestamp(if config.timestamp {
stderrlog::Timestamp::Microsecond
} else {
stderrlog::Timestamp::Off
})
.init()
.expect("logging initialization failed");
if let Err(e) = run_server(&config) {
eprintln!("server failed: {}", e.display_chain());
}

View File

@ -1,11 +1,12 @@
use clap::{App, Arg};
use daemon::Network;
use std::net::SocketAddr;
use stderrlog;
use daemon::Network;
#[derive(Debug)]
pub struct Config {
pub verbosity: usize,
pub timestamp: bool,
pub log: stderrlog::StdErrLog,
pub network_type: Network, // bitcoind JSONRPC endpoint
pub db_path: String, // RocksDB directory path
pub rpc_addr: SocketAddr, // for serving Electrum clients
@ -45,9 +46,16 @@ impl Config {
true => Network::Testnet,
};
let db_dir = m.value_of("db_dir").unwrap_or("./db");
let mut log = stderrlog::new();
log.verbosity(m.occurrences_of("verbosity") as usize);
log.timestamp(if m.is_present("timestamp") {
stderrlog::Timestamp::Microsecond
} else {
stderrlog::Timestamp::Off
});
log.init().expect("logging initialization failed");
Config {
verbosity: m.occurrences_of("verbosity") as usize,
timestamp: m.is_present("timestamp"),
log,
network_type,
db_path: match network_type {
Network::Mainnet => format!("{}/mainnet", db_dir),

View File

@ -10,6 +10,7 @@ extern crate hex;
extern crate prometheus;
extern crate rocksdb;
extern crate serde;
extern crate stderrlog;
extern crate time;
extern crate tiny_http;