Skip to content

Commit 0a2b894

Browse files
committed
Switch to an actual, standard logging system
1 parent 77ed9e3 commit 0a2b894

File tree

6 files changed

+145
-36
lines changed

6 files changed

+145
-36
lines changed

Cargo.lock

Lines changed: 101 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ categories = ["command-line-utilities", "text-processing"]
1414

1515
[dependencies]
1616
clap = "2"
17-
colored = "2"
1817
regex = "1"
18+
log = "0.4"
19+
simplelog = "0.10"
1920

2021

2122
[package.metadata.rpm.cargo]

src/cmd_line.rs

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

8+
use log::info;
9+
810
/// Define the command-line arguments and return them as the `clap::ArgMatches` struct.
911
pub fn get_args() -> ArgMatches<'static> {
1012
// Define command-line options
11-
App::new(crate_name!())
13+
let matches = App::new(crate_name!())
1214
.version(crate_version!())
1315
.author(crate_authors!())
1416
.about(crate_description!())
@@ -102,5 +104,11 @@ pub fn get_args() -> ArgMatches<'static> {
102104
.value_name("directory")
103105
.help("Save the generated files in this directory"),
104106
)
105-
.get_matches()
107+
.get_matches();
108+
109+
if matches.is_present("detect-directory") {
110+
info!("The `--detect-directory` (`-D`) option is now enabled by default.");
111+
}
112+
113+
matches
106114
}

src/logging.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use simplelog::{ColorChoice, ConfigBuilder, LevelFilter, TerminalMode, TermLogger};
2+
3+
/// This function initializes the `simplelog` logging system, which plugs into the `log`
4+
/// infrastructure. The function returns nothing. It only affects the global state when it runs.
5+
pub fn initialize_logger() {
6+
let config = ConfigBuilder::new()
7+
// Display a time stamp only for the debug and more verbose levels.
8+
.set_time_level(LevelFilter::Debug)
9+
.build();
10+
11+
TermLogger::init(
12+
// The default verbosity level is Info because newdoc displays the include statement
13+
// at that level.
14+
LevelFilter::Info,
15+
config,
16+
// Mixed mode prints errors to stderr and info to stdout. Not sure about the other levels.
17+
TerminalMode::Mixed,
18+
// Try to use color if possible.
19+
ColorChoice::Auto
20+
).expect("Failed to configure the terminal logging.");
21+
}

src/main.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod cmd_line;
2+
mod logging;
23
mod module;
34
mod write;
45

@@ -16,11 +17,9 @@ pub struct Options {
1617
}
1718

1819
fn main() {
19-
let cmdline_args = cmd_line::get_args();
20+
logging::initialize_logger();
2021

21-
if cmdline_args.is_present("detect-directory") {
22-
eprintln!("I: The `--detect-directory` (`-D`) option is now enabled by default.");
23-
}
22+
let cmdline_args = cmd_line::get_args();
2423

2524
// Set current options based on the command-line options
2625
let options = Options {

src/write.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::fs;
22
use std::io::{self, Write};
33
use std::path::PathBuf;
44

5-
use colored::*;
5+
use log::{error, info, warn};
66

77
use crate::module::Module;
88
use crate::Options;
@@ -17,11 +17,8 @@ impl Module {
1717
// If the target file already exists, just print out an error
1818
if full_path.exists() {
1919
// A prompt enabling the user to overwrite the existing file
20-
eprintln!(
21-
"{}",
22-
format!("W: File already exists: {}", full_path.display()).yellow()
23-
);
24-
eprint!(" Do you want to overwrite it? [y/N] ");
20+
warn!("File already exists: {}", full_path.display());
21+
warn!("Do you want to overwrite it? [y/N] ");
2522
// We must manually flush the buffer or else the printed string doesn't appear.
2623
// The buffer otherwise waits for a newline.
2724
io::stdout().flush().unwrap();
@@ -34,10 +31,10 @@ impl Module {
3431

3532
match answer.trim().to_lowercase().as_str() {
3633
"y" | "yes" => {
37-
eprintln!(" → Rewriting the file.");
34+
warn!("→ Rewriting the file.");
3835
}
3936
_ => {
40-
eprintln!(" → Preserving the existing file.");
37+
info!("→ Preserving the existing file.");
4138
// Break from generating this particular module.
4239
// Other modules that might be in the queue will be generated on next iteration.
4340
return;
@@ -50,12 +47,12 @@ impl Module {
5047
match result {
5148
// If the write succeeds, print the include statement
5249
Ok(()) => {
53-
eprintln!("‣ File generated: {}", full_path.display());
54-
eprintln!(" {}", self.include_statement);
50+
info!("‣ File generated: {}", full_path.display());
51+
info!(" {}", self.include_statement);
5552
}
5653
// If the write fails, print why it failed
5754
Err(e) => {
58-
eprintln!("{}", format!("E: Failed to write the file: {}", e).red());
55+
error!("Failed to write the file: {}", e);
5956
}
6057
}
6158
}

0 commit comments

Comments
 (0)