feat: add logging init

This commit is contained in:
2020-07-25 21:21:05 +08:00
parent 08fa747430
commit 96045ffcc6
2 changed files with 22 additions and 8 deletions

View File

@@ -9,10 +9,7 @@ impl CommandDefault {
app.arg(Arg::with_name("verbose").long("verbose").short("v").multiple(true).help("Show verbose info")) app.arg(Arg::with_name("verbose").long("verbose").short("v").multiple(true).help("Show verbose info"))
} }
pub fn run(arg_matches: &ArgMatches) -> CommandError { pub fn run(_arg_matches: &ArgMatches) -> CommandError {
let verbose_count = arg_matches.occurrences_of("verbose");
println!("Verbose count: {}", verbose_count);
error!("Default command is not implemented!"); error!("Default command is not implemented!");
Ok(()) Ok(())
} }

View File

@@ -1,6 +1,7 @@
#[macro_use] extern crate log; #[macro_use] extern crate log;
//#[macro_use] extern crate lazy_static; //#[macro_use] extern crate lazy_static;
use clap::App; use log::LevelFilter;
use clap::{ App, ArgMatches };
mod cmd; mod cmd;
mod cmd_default; mod cmd_default;
@@ -27,9 +28,6 @@ fn print_wa (msg: &str) {
} }
fn main() -> CommandError { fn main() -> CommandError {
pretty_env_logger::init();
info!("rust-script-tool started");
let commands: Vec<Box<dyn Command>> = vec![ let commands: Vec<Box<dyn Command>> = vec![
Box::new(CommandNew{}), Box::new(CommandNew{}),
]; ];
@@ -42,6 +40,9 @@ fn main() -> CommandError {
} }
let matches = app.get_matches(); let matches = app.get_matches();
init_log(&matches);
info!("rust-script-tool started");
for command in &commands { for command in &commands {
if let Some(sub_cmd_matches) = matches.subcommand_matches(command.name()) { if let Some(sub_cmd_matches) = matches.subcommand_matches(command.name()) {
info!("matched subcommand: {}", command.name()); info!("matched subcommand: {}", command.name());
@@ -52,3 +53,19 @@ fn main() -> CommandError {
info!("matched default subcommand"); info!("matched default subcommand");
CommandDefault::run(&matches) CommandDefault::run(&matches)
} }
fn init_log(arg_matches: &ArgMatches) {
let verbose_count = arg_matches.occurrences_of("verbose");
if verbose_count == 0 {
pretty_env_logger::init();
} else {
let mut builder = pretty_env_logger::formatted_builder();
match verbose_count {
1 => builder.filter_level(LevelFilter::Warn),
2 => builder.filter_level(LevelFilter::Info),
3 => builder.filter_level(LevelFilter::Debug),
_ => builder.filter_level(LevelFilter::Trace),
};
builder.init();
}
}