Skip to content

Commit 761c6b4

Browse files
committed
Let the verbosity options actually do something
1 parent 26681e4 commit 761c6b4

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

src/cmd_line.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ use clap::{
55
ArgMatches,
66
};
77

8-
use log::info;
9-
108
/// Define the command-line arguments and return them as the `clap::ArgMatches` struct.
119
pub fn get_args() -> ArgMatches<'static> {
1210
// Define command-line options
@@ -120,9 +118,5 @@ pub fn get_args() -> ArgMatches<'static> {
120118
)
121119
.get_matches();
122120

123-
if matches.is_present("detect-directory") {
124-
info!("The `--detect-directory` (`-D`) option is now enabled by default.");
125-
}
126-
127121
matches
128122
}

src/logging.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,26 @@ use simplelog::{ColorChoice, ConfigBuilder, LevelFilter, TerminalMode, TermLogge
22

33
/// This function initializes the `simplelog` logging system, which plugs into the `log`
44
/// infrastructure. The function returns nothing. It only affects the global state when it runs.
5-
pub fn initialize_logger() {
5+
pub fn initialize_logger(verbose: bool, quiet: bool) {
6+
// Set the verbosity level based on the command-line options.
7+
// Our `clap` configuration ensures that `verbose` and `quiet` can never be both true.
8+
let verbosity = if verbose {
9+
LevelFilter::Debug
10+
} else if quiet {
11+
LevelFilter::Warn
12+
} else {
13+
// The default verbosity level is Info because newdoc displays the include statement
14+
// at that level.
15+
LevelFilter::Info
16+
};
17+
618
let config = ConfigBuilder::new()
719
// Display a time stamp only for the debug and more verbose levels.
820
.set_time_level(LevelFilter::Debug)
921
.build();
1022

1123
TermLogger::init(
12-
// The default verbosity level is Info because newdoc displays the include statement
13-
// at that level.
14-
LevelFilter::Info,
24+
verbosity,
1525
config,
1626
// Mixed mode prints errors to stderr and info to stdout. Not sure about the other levels.
1727
TerminalMode::Mixed,

src/main.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use log::info;
2+
13
mod cmd_line;
24
mod logging;
35
mod module;
@@ -17,9 +19,17 @@ pub struct Options {
1719
}
1820

1921
fn main() {
20-
logging::initialize_logger();
21-
22+
// Parse the command-line options
2223
let cmdline_args = cmd_line::get_args();
24+
// Determine the configured verbosity level
25+
let verbose = cmdline_args.is_present("verbose");
26+
let quiet = cmdline_args.is_present("quiet");
27+
// Initialize the logging system based on the set verbosity
28+
logging::initialize_logger(verbose, quiet);
29+
30+
if cmdline_args.is_present("detect-directory") {
31+
info!("The `--detect-directory` (`-D`) option is now enabled by default.");
32+
}
2333

2434
// Set current options based on the command-line options
2535
let options = Options {

0 commit comments

Comments
 (0)