Add debug flag

This commit is contained in:
Kenneth Gitere 2021-04-24 15:50:43 +03:00
parent a9787d7b5a
commit 088699b2c3
2 changed files with 41 additions and 20 deletions

View file

@ -38,7 +38,12 @@ It takes a url and downloads the article content from it and saves it to an epub
.long("max_conn") .long("max_conn")
.help("The maximum number of concurrent HTTP connections when downloading articles. Default is 8") .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.") .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 arg_matches = app.get_matches();
let mut urls: Vec<String> = match arg_matches.value_of("file") { let mut urls: Vec<String> = match arg_matches.value_of("file") {
Some(file_name) => { 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); app_config.set_merged(file_name);
} }
if arg_matches.is_present("debug") {
app_config.toggle_debug(true);
}
app_config app_config
} }
@ -91,6 +99,7 @@ pub struct AppConfig {
urls: Vec<String>, urls: Vec<String>,
max_conn: usize, max_conn: usize,
merged: Option<String>, merged: Option<String>,
is_debug: bool,
} }
impl AppConfig { impl AppConfig {
@ -99,9 +108,14 @@ impl AppConfig {
urls: vec![], urls: vec![],
max_conn, max_conn,
merged: None, 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<String>) { fn set_urls(&mut self, urls: Vec<String>) {
self.urls.extend(urls); self.urls.extend(urls);
} }
@ -120,4 +134,8 @@ impl AppConfig {
pub fn merged(&self) -> Option<&String> { pub fn merged(&self) -> Option<&String> {
self.merged.as_ref() self.merged.as_ref()
} }
pub fn is_debug(&self) -> bool {
self.is_debug
}
} }

View file

@ -31,6 +31,7 @@ fn main() {
let app_config = cli::cli_init(); let app_config = cli::cli_init();
if !app_config.urls().is_empty() { if !app_config.urls().is_empty() {
if app_config.is_debug() {
match UserDirs::new() { match UserDirs::new() {
Some(user_dirs) => { Some(user_dirs) => {
let home_dir = user_dirs.home_dir(); let home_dir = user_dirs.home_dir();
@ -43,6 +44,7 @@ fn main() {
match flexi_logger::Logger::with_str("paperoni=debug") match flexi_logger::Logger::with_str("paperoni=debug")
.directory(log_dir) .directory(log_dir)
.log_to_file() .log_to_file()
.print_message()
.start() .start()
{ {
Ok(_) => (), Ok(_) => (),
@ -51,6 +53,7 @@ fn main() {
} }
None => eprintln!("Unable to get user directories for logging purposes"), None => eprintln!("Unable to get user directories for logging purposes"),
}; };
}
download(app_config); download(app_config);
} }
} }