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,26 +31,29 @@ 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() {
match UserDirs::new() { if app_config.is_debug() {
Some(user_dirs) => { match UserDirs::new() {
let home_dir = user_dirs.home_dir(); Some(user_dirs) => {
let paperoni_dir = home_dir.join(".paperoni"); let home_dir = user_dirs.home_dir();
let log_dir = paperoni_dir.join("logs"); let paperoni_dir = home_dir.join(".paperoni");
if !paperoni_dir.is_dir() || !log_dir.is_dir() { let log_dir = paperoni_dir.join("logs");
std::fs::create_dir_all(&log_dir) if !paperoni_dir.is_dir() || !log_dir.is_dir() {
.expect("Unable to create paperoni directories on home directory for logging purposes"); 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") None => eprintln!("Unable to get user directories for logging purposes"),
.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"),
};
download(app_config); download(app_config);
} }
} }