diff options
author | Ben Bridle <ben@derelict.engineering> | 2025-02-03 09:55:11 +1300 |
---|---|---|
committer | Ben Bridle <ben@derelict.engineering> | 2025-02-03 09:55:11 +1300 |
commit | 582b9c3408f391f7dbba85bb5909c3deb7091f4b (patch) | |
tree | 8b2d190d1235d67307306ee7d6c88d1e2152a687 /src/main.rs | |
parent | 05147de4a071917865982c90e0699657efcf14ea (diff) | |
download | toaster-582b9c3408f391f7dbba85bb5909c3deb7091f4b.zip |
Use log crate for printing log messages
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 41 |
1 files changed, 8 insertions, 33 deletions
diff --git a/src/main.rs b/src/main.rs index 25d1528..82814e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,33 +11,8 @@ use vagabond::*; use std::collections::HashSet; use std::time::SystemTime; +use log::{info, warn, error, fatal}; -const NORMAL: &str = "\x1b[0m"; -const BOLD: &str = "\x1b[1m"; -const WHITE: &str = "\x1b[37m"; -const RED: &str = "\x1b[31m"; -const YELLOW: &str = "\x1b[33m"; -const BLUE: &str = "\x1b[34m"; - -static mut VERBOSE: bool = false; -#[macro_export] macro_rules! verbose { - ($($tokens:tt)*) => { if unsafe { VERBOSE } { - eprint!("{BOLD}{BLUE}[INFO]{NORMAL}: "); eprint!($($tokens)*); - eprintln!("{NORMAL}"); - } }; -} -#[macro_export] macro_rules! warn { - ($($tokens:tt)*) => {{ - eprint!("{BOLD}{YELLOW}[WARNING]{NORMAL}{WHITE}: "); eprint!($($tokens)*); - eprintln!("{NORMAL}"); - }}; -} -#[macro_export] macro_rules! error { - ($($tokens:tt)*) => {{ - eprint!("{BOLD}{RED}[ERROR]{WHITE}: "); eprint!($($tokens)*); - eprintln!("{NORMAL}"); std::process::exit(1); - }}; -} fn main() { let args = Arguments::from_env_or_exit(); @@ -47,14 +22,14 @@ fn main() { std::process::exit(0); } if args.verbose { - unsafe { VERBOSE = true; } + log::set_log_level(log::LogLevel::Info); } if args.source.is_none() || args.destination.is_none() { - error!("Provide a source directory and a destination directory.") + fatal!("Provide a source directory and a destination directory.") } let source_directory = match args.source.as_ref().unwrap().canonicalize() { Ok(source_directory) => source_directory, - Err(err) => error!("{:?}: {err}", args.source.unwrap()), + Err(err) => fatal!("{:?}: {err}", args.source.unwrap()), }; let destination_directory = args.destination.unwrap(); @@ -84,7 +59,7 @@ fn main() { destination.push(make_url_safe(&website.name)); if args.delete && Entry::from_path(&destination).is_ok() { - verbose!("Deleting existing destination directory {destination:?}"); + info!("Deleting existing destination directory {destination:?}"); remove(&destination).unwrap_or_else(|_| error!("Failed to delete existing destination directory {destination:?}")); } @@ -100,7 +75,7 @@ fn main() { } // Copy original markdown file. destination.add_extension("md"); - verbose!("Copying original markdown file to {destination:?}"); + info!("Copying original markdown file to {destination:?}"); copy(&page.source_path, &destination).unwrap_or_else(|_| error!("Failed to copy original markdown file {:?} to {:?}", page.source_path, destination)); @@ -109,7 +84,7 @@ fn main() { for static_file in &website.static_files { let mut destination = destination.clone(); destination.push(&static_file.full_url); - verbose!("Copying static file to {destination:?}"); + info!("Copying static file to {destination:?}"); make_parent_directory(&destination).unwrap(); copy(&static_file.source_path, &destination).unwrap_or_else(|_| error!("Failed to copy static file {:?} to {:?}", @@ -141,7 +116,7 @@ fn main() { pub fn write_file(text: &str, destination: &PathBuf, ext: &str, last_modified: Option<SystemTime>) { let mut destination = destination.clone(); destination.add_extension(ext); - verbose!("Generating {destination:?}"); + info!("Generating {destination:?}"); make_parent_directory(&destination).unwrap_or_else(|_| error!("Failed to create parent directories for {destination:?}")); write_to_file(&destination, text).unwrap_or_else(|_| |