Move initializing logger to logs module

This commit is contained in:
Kenneth Gitere 2021-04-28 07:47:45 +03:00
parent 36c3eb65c6
commit 00d704fdd6
4 changed files with 29 additions and 25 deletions

1
Cargo.lock generated
View file

@ -1485,7 +1485,6 @@ name = "paperoni"
version = "0.3.0-alpha1" version = "0.3.0-alpha1"
dependencies = [ dependencies = [
"async-std", "async-std",
"atty",
"clap", "clap",
"colored", "colored",
"comfy-table", "comfy-table",

View file

@ -13,7 +13,7 @@ readme = "README.md"
[dependencies] [dependencies]
async-std = "1.9.0" async-std = "1.9.0"
atty = "0.2.14" # atty = "0.2.14"
clap = "2.33.3" clap = "2.33.3"
colored = "2.0.0" colored = "2.0.0"
comfy-table = "2.1.0" comfy-table = "2.1.0"

View file

@ -1,6 +1,7 @@
use colored::*; use colored::*;
use comfy_table::presets::UTF8_HORIZONTAL_BORDERS_ONLY; use comfy_table::presets::UTF8_HORIZONTAL_BORDERS_ONLY;
use comfy_table::{Cell, CellAlignment, ContentArrangement, Table}; use comfy_table::{Cell, CellAlignment, ContentArrangement, Table};
use directories::UserDirs;
use log::error; use log::error;
use crate::errors::PaperoniError; use crate::errors::PaperoniError;
@ -75,6 +76,31 @@ fn short_summary(initial_count: usize, successful_count: usize, failed_count: us
} }
} }
pub fn init_logger() {
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),
}
}
None => eprintln!("Unable to get user directories for logging purposes"),
};
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::short_summary; use super::short_summary;

View file

@ -5,7 +5,6 @@ use async_std::stream;
use async_std::task; use async_std::task;
use comfy_table::presets::{UTF8_FULL, UTF8_HORIZONTAL_BORDERS_ONLY}; use comfy_table::presets::{UTF8_FULL, UTF8_HORIZONTAL_BORDERS_ONLY};
use comfy_table::{ContentArrangement, Table}; use comfy_table::{ContentArrangement, Table};
use directories::UserDirs;
use futures::stream::StreamExt; use futures::stream::StreamExt;
use indicatif::{ProgressBar, ProgressStyle}; use indicatif::{ProgressBar, ProgressStyle};
use log::{debug, warn}; use log::{debug, warn};
@ -25,34 +24,14 @@ use cli::AppConfig;
use epub::generate_epubs; use epub::generate_epubs;
use extractor::Extractor; use extractor::Extractor;
use http::{download_images, fetch_html}; use http::{download_images, fetch_html};
use logs::display_summary; use logs::{display_summary, init_logger};
fn main() { 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() { if app_config.is_debug() {
match UserDirs::new() { init_logger();
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),
}
}
None => eprintln!("Unable to get user directories for logging purposes"),
};
} }
download(app_config); download(app_config);
} }