wordchipper_cli_util/
logging.rs1use stderrlog::Timestamp;
2
3#[derive(clap::Args, Debug)]
5pub struct LogArgs {
6 #[clap(short, long)]
8 pub quiet: bool,
9
10 #[arg(short, long, action = clap::ArgAction::Count, default_value = None)]
12 verbose: Option<u8>,
13
14 #[clap(short, long)]
16 pub ts: bool,
17}
18
19impl LogArgs {
20 pub fn setup_logging(
21 &self,
22 default: u8,
23 ) -> Result<(), Box<dyn std::error::Error>> {
24 let level = if let Some(verbose) = self.verbose
25 && verbose > 0
26 {
27 verbose
28 } else {
29 default
30 };
31
32 let log_level = match level {
33 0 => stderrlog::LogLevelNum::Off,
34 1 => stderrlog::LogLevelNum::Error,
35 2 => stderrlog::LogLevelNum::Warn,
36 3 => stderrlog::LogLevelNum::Info,
37 4 => stderrlog::LogLevelNum::Debug,
38 _ => stderrlog::LogLevelNum::Trace,
39 };
40
41 stderrlog::new()
42 .quiet(self.quiet)
43 .verbosity(log_level)
44 .timestamp(if self.ts {
45 Timestamp::Second
46 } else {
47 Timestamp::Off
48 })
49 .init()?;
50
51 Ok(())
52 }
53}