Skip to content

Commit 69e26e6

Browse files
Replaced a bunch of println!()'s with proper log macros
1 parent e7693bc commit 69e26e6

File tree

6 files changed

+29
-12
lines changed

6 files changed

+29
-12
lines changed

src/bin/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
3030
book.build()?;
3131

3232
if args.is_present("open") {
33+
// FIXME: What's the right behaviour if we don't use the HTML renderer?
3334
open(book.build_dir_for("html").join("index.html"));
3435
}
3536

src/bin/init.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
2626
// Skip this if `--force` is present
2727
if !args.is_present("force") {
2828
// Print warning
29-
print!("\nCopying the default theme to {}", builder.config().book.src.display());
29+
println!();
30+
println!(
31+
"Copying the default theme to {}",
32+
builder.config().book.src.display()
33+
);
3034
println!("This could potentially overwrite files already present in that directory.");
3135
print!("\nAre you sure you want to continue? (y/n) ");
3236

src/bin/mdbook.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use chrono::Local;
1717
use log::LevelFilter;
1818
use env_logger::Builder;
1919
use error_chain::ChainedError;
20+
use mdbook::utils;
2021

2122
pub mod build;
2223
pub mod init;
@@ -64,7 +65,7 @@ fn main() {
6465
};
6566

6667
if let Err(e) = res {
67-
eprintln!("{}", e.display_chain());
68+
utils::log_backtrace(&e);
6869

6970
::std::process::exit(101);
7071
}
@@ -101,12 +102,12 @@ fn get_book_dir(args: &ArgMatches) -> PathBuf {
101102
p.to_path_buf()
102103
}
103104
} else {
104-
env::current_dir().unwrap()
105+
env::current_dir().expect("Unable to determine the current directory")
105106
}
106107
}
107108

108109
fn open<P: AsRef<OsStr>>(path: P) {
109110
if let Err(e) = open::that(path) {
110-
println!("Error opening web browser: {}", e);
111+
error!("Error opening web browser: {}", e);
111112
}
112113
}

src/bin/serve.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use self::iron::{status, AfterMiddleware, Chain, Iron, IronError, IronResult, Re
77
Set};
88
use clap::{App, ArgMatches, SubCommand};
99
use mdbook::MDBook;
10+
use mdbook::utils;
1011
use mdbook::errors::*;
1112
use {get_book_dir, open};
1213
#[cfg(feature = "watch")]
@@ -80,7 +81,9 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
8081

8182
#[cfg(feature = "watch")]
8283
watch::trigger_on_change(&mut book, move |path, book_dir| {
83-
println!("File changed: {:?}\nBuilding book...\n", path);
84+
info!("File changed: {:?}", path);
85+
info!("Building book...");
86+
8487
// FIXME: This area is really ugly because we need to re-set livereload :(
8588

8689
let livereload_url = livereload_url.clone();
@@ -94,10 +97,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
9497

9598
if let Err(e) = result {
9699
error!("Unable to load the book");
97-
error!("Error: {}", e);
98-
for cause in e.iter().skip(1) {
99-
error!("\tCaused By: {}", cause);
100-
}
100+
utils::log_backtrace(&e);
101101
} else {
102102
let _ = broadcaster.send("reload");
103103
}

src/bin/watch.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::time::Duration;
66
use std::sync::mpsc::channel;
77
use clap::{App, ArgMatches, SubCommand};
88
use mdbook::MDBook;
9+
use mdbook::utils;
910
use mdbook::errors::Result;
1011
use {get_book_dir, open};
1112

@@ -30,13 +31,13 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
3031
}
3132

3233
trigger_on_change(&book, |path, book_dir| {
33-
println!("File changed: {:?}\nBuilding book...\n", path);
34+
info!("File changed: {:?}\nBuilding book...\n", path);
3435
let result = MDBook::load(&book_dir).and_then(|b| b.build());
3536

3637
if let Err(e) = result {
37-
println!("Error while building: {}", e);
38+
error!("Unable to build the book");
39+
utils::log_backtrace(&e);
3840
}
39-
println!();
4041
});
4142

4243
Ok(())

src/utils/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod fs;
22
mod string;
3+
use errors::Error;
34

45
use pulldown_cmark::{html, Event, Options, Parser, Tag, OPTION_ENABLE_FOOTNOTES,
56
OPTION_ENABLE_TABLES};
@@ -105,6 +106,15 @@ fn convert_quotes_to_curly(original_text: &str) -> String {
105106
.collect()
106107
}
107108

109+
/// Prints a "backtrace" of some `Error`.
110+
pub fn log_backtrace(e: &Error) {
111+
error!("Error: {}", e);
112+
113+
for cause in e.iter().skip(1) {
114+
error!("\tCaused By: {}", cause);
115+
}
116+
}
117+
108118
#[cfg(test)]
109119
mod tests {
110120
mod render_markdown {

0 commit comments

Comments
 (0)