From 088699b2c362bfad1d4b86188204f7186e9a3f40 Mon Sep 17 00:00:00 2001 From: Kenneth Gitere Date: Sat, 24 Apr 2021 15:50:43 +0300 Subject: [PATCH] Add debug flag --- src/cli.rs | 20 +++++++++++++++++++- src/main.rs | 41 ++++++++++++++++++++++------------------- 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index a8701bc..9e4b62e 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -38,7 +38,12 @@ It takes a url and downloads the article content from it and saves it to an epub .long("max_conn") .help("The maximum number of concurrent HTTP connections when downloading articles. Default is 8") .long_help("The maximum number of concurrent HTTP connections when downloading articles. Default is 8.\nNOTE: It is advised to use as few connections as needed i.e between 1 and 50. Using more connections can end up overloading your network card with too many concurrent requests.") - .takes_value(true)); + .takes_value(true)) + .arg( + Arg::with_name("debug") + .long("debug") + .help("Enable logging of events for debugging") + .takes_value(false)); let arg_matches = app.get_matches(); let mut urls: Vec = match arg_matches.value_of("file") { Some(file_name) => { @@ -84,6 +89,9 @@ It takes a url and downloads the article content from it and saves it to an epub }; app_config.set_merged(file_name); } + if arg_matches.is_present("debug") { + app_config.toggle_debug(true); + } app_config } @@ -91,6 +99,7 @@ pub struct AppConfig { urls: Vec, max_conn: usize, merged: Option, + is_debug: bool, } impl AppConfig { @@ -99,9 +108,14 @@ impl AppConfig { urls: vec![], max_conn, merged: None, + is_debug: false, } } + fn toggle_debug(&mut self, is_debug: bool) { + self.is_debug = is_debug; + } + fn set_urls(&mut self, urls: Vec) { self.urls.extend(urls); } @@ -120,4 +134,8 @@ impl AppConfig { pub fn merged(&self) -> Option<&String> { self.merged.as_ref() } + + pub fn is_debug(&self) -> bool { + self.is_debug + } } diff --git a/src/main.rs b/src/main.rs index 7fa1ddb..ebf2dca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,26 +31,29 @@ fn main() { let app_config = cli::cli_init(); if !app_config.urls().is_empty() { - match UserDirs::new() { - Some(user_dirs) => { - let home_dir = user_dirs.home_dir(); - let paperoni_dir = home_dir.join(".paperoni"); - let log_dir = paperoni_dir.join("logs"); - if !paperoni_dir.is_dir() || !log_dir.is_dir() { - std::fs::create_dir_all(&log_dir) - .expect("Unable to create paperoni directories on home directory for logging purposes"); + if app_config.is_debug() { + match UserDirs::new() { + Some(user_dirs) => { + let home_dir = user_dirs.home_dir(); + let paperoni_dir = home_dir.join(".paperoni"); + let log_dir = paperoni_dir.join("logs"); + if !paperoni_dir.is_dir() || !log_dir.is_dir() { + std::fs::create_dir_all(&log_dir) + .expect("Unable to create paperoni directories on home directory for logging purposes"); + } + match flexi_logger::Logger::with_str("paperoni=debug") + .directory(log_dir) + .log_to_file() + .print_message() + .start() + { + Ok(_) => (), + Err(e) => eprintln!("Unable to start logger!\n{}", e), + } } - match flexi_logger::Logger::with_str("paperoni=debug") - .directory(log_dir) - .log_to_file() - .start() - { - Ok(_) => (), - Err(e) => eprintln!("Unable to start logger!\n{}", e), - } - } - None => eprintln!("Unable to get user directories for logging purposes"), - }; + None => eprintln!("Unable to get user directories for logging purposes"), + }; + } download(app_config); } }