Merge pull request #241 from ethcore/print-to-console

basic print-to-console
This commit is contained in:
Marek Kotewicz 2016-12-01 11:09:43 +01:00 committed by GitHub
commit c29c952285
7 changed files with 79 additions and 5 deletions

11
Cargo.lock generated
View File

@ -314,6 +314,16 @@ name = "log"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "logs"
version = "0.1.0"
dependencies = [
"ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
"env_logger 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "memchr"
version = "0.1.11"
@ -516,6 +526,7 @@ dependencies = [
"import 0.1.0",
"keys 0.1.0",
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"logs 0.1.0",
"message 0.1.0",
"miner 0.1.0",
"network 0.1.0",

View File

@ -22,6 +22,7 @@ verification = { path = "verification" }
sync = { path = "sync" }
import = { path = "import" }
bencher = { path = "bencher" }
logs = { path = "logs" }
[[bin]]
path = "pbtc/main.rs"

10
logs/Cargo.toml Normal file
View File

@ -0,0 +1,10 @@
[package]
name = "logs"
version = "0.1.0"
authors = ["debris <marek.kotewicz@gmail.com>"]
[dependencies]
ansi_term = "0.9"
log = "0.3"
env_logger = "0.3"
time = "0.1"

41
logs/src/lib.rs Normal file
View File

@ -0,0 +1,41 @@
extern crate ansi_term;
extern crate log;
extern crate env_logger;
extern crate time;
use ansi_term::Colour as Color;
use log::LogRecord;
use env_logger::LogBuilder;
fn strftime() -> String {
time::strftime("%Y-%m-%d %H:%M:%S %Z", &time::now()).expect("Time is incorrectly formatted")
}
pub trait LogFormatter: Send + Sync + 'static {
fn format(&self, log_record: &LogRecord) -> String;
}
pub struct DateLogFormatter;
impl LogFormatter for DateLogFormatter {
fn format(&self, record: &LogRecord) -> String {
let timestamp = strftime();
format!("{} {} {} {}", timestamp, record.level(), record.target(), record.args())
}
}
pub struct DateAndColorLogFormatter;
impl LogFormatter for DateAndColorLogFormatter {
fn format(&self, record: &LogRecord) -> String {
let timestamp = strftime();
format!("{} {} {} {}", Color::Black.bold().paint(timestamp), record.level(), record.target(), record.args())
}
}
pub fn init<T>(filters: &str, formatter: T) where T: LogFormatter {
let mut builder = LogBuilder::new();
builder.parse(filters);
builder.format(move |record| formatter.format(record));
builder.init().expect("Logger can be initialized only once");
}

View File

@ -26,9 +26,9 @@ args:
value_name: PORT
help: Listen for connections on PORT
takes_value: true
- printtoconsole:
long: printtoconsole
help: Send trace/debug info to console instead of debug.log file
- print-to-console:
long: print-to-console
help: Send sync info to console
- data-dir:
short: d
long: data-dir

View File

@ -23,7 +23,7 @@ pub struct Config {
pub const DEFAULT_DB_CACHE: usize = 512;
pub fn parse(matches: &clap::ArgMatches) -> Result<Config, String> {
let print_to_console = matches.is_present("printtoconsole");
let print_to_console = matches.is_present("print-to-console");
let magic = match (matches.is_present("testnet"), matches.is_present("regtest")) {
(true, false) => Magic::Testnet,
(false, true) => Magic::Regtest,

View File

@ -10,6 +10,7 @@ extern crate app_dirs;
extern crate db;
extern crate chain;
extern crate keys;
extern crate logs;
extern crate script;
extern crate message;
extern crate network;
@ -29,9 +30,9 @@ pub const PROTOCOL_VERSION: u32 = 70_014;
pub const PROTOCOL_MINIMUM: u32 = 70_001;
pub const USER_AGENT: &'static str = "pbtc";
pub const REGTEST_USER_AGENT: &'static str = "/Satoshi:0.12.1/";
pub const LOG_INFO: &'static str = "sync=info";
fn main() {
env_logger::init().unwrap();
if let Err(err) = run() {
println!("{}", err);
}
@ -42,6 +43,16 @@ fn run() -> Result<(), String> {
let matches = clap::App::from_yaml(yaml).get_matches();
let cfg = try!(config::parse(&matches));
if cfg.print_to_console {
if cfg!(windows) {
logs::init(LOG_INFO, logs::DateLogFormatter);
} else {
logs::init(LOG_INFO, logs::DateAndColorLogFormatter);
}
} else {
env_logger::init().expect("Logger can be initialized only once");
}
match matches.subcommand() {
("import", Some(import_matches)) => commands::import(cfg, import_matches),
_ => commands::start(cfg),