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")
.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<String> = 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<String>,
max_conn: usize,
merged: Option<String>,
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<String>) {
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
}
}

View file

@ -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);
}
}