feat: add logging init
This commit is contained in:
@@ -9,10 +9,7 @@ impl CommandDefault {
|
||||
app.arg(Arg::with_name("verbose").long("verbose").short("v").multiple(true).help("Show verbose info"))
|
||||
}
|
||||
|
||||
pub fn run(arg_matches: &ArgMatches) -> CommandError {
|
||||
let verbose_count = arg_matches.occurrences_of("verbose");
|
||||
println!("Verbose count: {}", verbose_count);
|
||||
|
||||
pub fn run(_arg_matches: &ArgMatches) -> CommandError {
|
||||
error!("Default command is not implemented!");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
25
src/main.rs
25
src/main.rs
@@ -1,6 +1,7 @@
|
||||
#[macro_use] extern crate log;
|
||||
//#[macro_use] extern crate lazy_static;
|
||||
use clap::App;
|
||||
use log::LevelFilter;
|
||||
use clap::{ App, ArgMatches };
|
||||
|
||||
mod cmd;
|
||||
mod cmd_default;
|
||||
@@ -27,9 +28,6 @@ fn print_wa (msg: &str) {
|
||||
}
|
||||
|
||||
fn main() -> CommandError {
|
||||
pretty_env_logger::init();
|
||||
info!("rust-script-tool started");
|
||||
|
||||
let commands: Vec<Box<dyn Command>> = vec![
|
||||
Box::new(CommandNew{}),
|
||||
];
|
||||
@@ -42,6 +40,9 @@ fn main() -> CommandError {
|
||||
}
|
||||
|
||||
let matches = app.get_matches();
|
||||
init_log(&matches);
|
||||
info!("rust-script-tool started");
|
||||
|
||||
for command in &commands {
|
||||
if let Some(sub_cmd_matches) = matches.subcommand_matches(command.name()) {
|
||||
info!("matched subcommand: {}", command.name());
|
||||
@@ -52,3 +53,19 @@ fn main() -> CommandError {
|
||||
info!("matched default subcommand");
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user