1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
extern crate env_logger;
extern crate failure;
extern crate log;
#[macro_use]
extern crate structopt;
use env_logger::Builder as LoggerBuilder;
use failure::Error;
use log::Level;
#[derive(StructOpt, Debug, Clone)]
pub struct Verbosity {
#[structopt(long = "verbosity", short = "v", parse(from_occurrences))]
verbosity: u8,
}
impl Verbosity {
pub fn log_level(&self) -> Level {
match self.verbosity {
0 => Level::Error,
1 => Level::Warn,
2 => Level::Info,
3 => Level::Debug,
_ => Level::Trace,
}
}
pub fn setup_env_logger(&self, own_pkg_name: &str) -> Result<(), Error> {
let level_filter = self.log_level().to_level_filter();
LoggerBuilder::new()
.filter(Some(&own_pkg_name.replace("-", "_")), level_filter)
.filter(None, Level::Warn.to_level_filter())
.try_init()?;
Ok(())
}
}
use std::fmt;
impl fmt::Display for Verbosity {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.verbosity)
}
}