scraper: Use logger, add --quiet option

This commit is contained in:
cohenarthur 2020-04-18 15:52:36 +02:00
parent debe1eca64
commit 2b84cb165a
3 changed files with 14 additions and 1 deletions

View file

@ -19,6 +19,9 @@ pub struct Args {
#[structopt(short, long, default_value = "20")]
pub tries: usize,
#[structopt(short, long)]
pub quiet: bool,
}
impl Args {

View file

@ -11,6 +11,9 @@ static TIME_FORMAT: &str = "%Y-%m-%dT%H:%M:%S";
pub struct Logger {
}
// Allow the dead code in case we don't use all the macros. Using it here means
// it doesn't affect the other modules in the crate
#[allow(dead_code)]
impl Logger {
/// Get the current formatted timestamp
fn get_timestamp() -> String {
@ -36,16 +39,19 @@ impl Logger {
}
#[macro_export]
/// Print an info message on stdout
macro_rules! info {
($($arg: tt)*) => ($crate::logger::Logger::info(format!($($arg)*)));
}
#[macro_export]
/// Print a warning message on stdout
macro_rules! warn {
($($arg: tt)*) => ($crate::logger::Logger::warn(format!($($arg)*)));
}
#[macro_export]
/// Print an error message on stderr
macro_rules! error {
($($arg: tt)*) => ($crate::logger::Logger::error(format!($($arg)*)));
}

View file

@ -14,6 +14,8 @@ use super::args;
use super::disk;
use super::dom;
use crate::info;
/// Maximum number of empty recv() from the channel
static MAX_EMPTY_RECEIVES: usize = 10;
@ -108,7 +110,9 @@ impl Scraper {
scraper.visited_urls.lock().unwrap().insert(url.to_string());
println!("{} has been downloaded", url);
if !scraper.args.quiet {
info!("Downloaded {}", url);
}
}
/// Run through the channel and complete it