@@ -16,8 +16,7 @@ use std::env;
1616use std:: net:: Ipv4Addr ;
1717use std:: path:: PathBuf ;
1818
19- use clap:: { Parser , Subcommand } ;
20- use clap_verbosity_flag:: { InfoLevel , Verbosity } ;
19+ use clap:: { ArgAction , Args , Parser , Subcommand } ;
2120use tracing:: Level ;
2221use url:: Url ;
2322
@@ -26,7 +25,7 @@ pub struct Cli {
2625 #[ clap( short, long, default_value = "numtracker.db" , env = "NUMTRACKER_DB" ) ]
2726 pub ( crate ) db : PathBuf ,
2827 #[ clap( flatten, next_help_heading = "Logging/Debug" ) ]
29- verbose : Verbosity < InfoLevel > ,
28+ verbose : Verbosity ,
3029 #[ clap( flatten, next_help_heading = "Tracing and Logging" ) ]
3130 tracing : TracingOptions ,
3231 #[ clap( subcommand) ]
@@ -61,38 +60,50 @@ pub struct ServeOptions {
6160 port : u16 ,
6261}
6362
63+ #[ derive( Debug , Args ) ]
64+ struct Verbosity {
65+ /// Increase the level of logs written to stderr
66+ #[ clap( short, long, global = true , action = ArgAction :: Count ) ]
67+ verbose : u8 ,
68+ /// Disable all output to stderr/stdout
69+ #[ clap( short, long, global = true , conflicts_with = "verbose" ) ]
70+ quiet : bool ,
71+ }
72+
6473impl Cli {
6574 pub fn init ( ) -> Self {
6675 Self :: parse ( )
6776 }
68- pub fn log_level ( & self ) -> Option < Level > {
69- use clap_verbosity_flag:: Level as ClapLevel ;
70- match self . verbose . log_level ( ) {
71- Some ( lvl) => Some ( match lvl {
72- ClapLevel :: Error => Level :: ERROR ,
73- ClapLevel :: Warn => Level :: WARN ,
74- ClapLevel :: Info => Level :: INFO ,
75- ClapLevel :: Debug => Level :: DEBUG ,
76- ClapLevel :: Trace => Level :: TRACE ,
77- } ) ,
78- None => Some (
79- match env:: var ( "NUMTRACKER_LOG_LEVEL" )
80- . map ( |lvl| lvl. to_ascii_lowercase ( ) )
81- . as_deref ( )
82- {
83- Ok ( "info" ) => Level :: INFO ,
84- Ok ( "debug" ) => Level :: DEBUG ,
85- Ok ( "trace" ) => Level :: TRACE ,
86- Ok ( "warn" ) => Level :: WARN ,
87- Ok ( "error" ) => Level :: ERROR ,
88- _ => return None ,
89- } ,
90- ) ,
91- }
92- }
9377 pub fn tracing ( & self ) -> & TracingOptions {
9478 & self . tracing
9579 }
80+ pub fn log_level ( & self ) -> Option < Level > {
81+ self . verbose . log_level ( )
82+ }
83+ }
84+ impl Verbosity {
85+ pub fn log_level ( & self ) -> Option < Level > {
86+ if self . quiet {
87+ return None ;
88+ }
89+ match self . verbose {
90+ 0 => match env:: var ( "NUMTRACKER_LOG_LEVEL" )
91+ . map ( |lvl| lvl. to_ascii_lowercase ( ) )
92+ . as_deref ( )
93+ {
94+ Ok ( "info" ) => Level :: INFO ,
95+ Ok ( "debug" ) => Level :: DEBUG ,
96+ Ok ( "trace" ) => Level :: TRACE ,
97+ Ok ( "warn" ) => Level :: WARN ,
98+ Ok ( "error" ) => Level :: ERROR ,
99+ _ => Level :: ERROR ,
100+ } ,
101+ 1 => Level :: INFO ,
102+ 2 => Level :: DEBUG ,
103+ _ => Level :: TRACE ,
104+ }
105+ . into ( )
106+ }
96107}
97108
98109impl ServeOptions {
0 commit comments