From 7a3fd3f2ca898402e32685cc1574ee1b0b9de1ea Mon Sep 17 00:00:00 2001 From: xenofem Date: Sat, 16 Apr 2022 00:03:09 -0400 Subject: [PATCH] add basic logging --- Cargo.lock | 37 +++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/fetch.rs | 8 ++++++++ src/main.rs | 8 +++++++- 4 files changed, 54 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 5b46e70..e156b06 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -288,6 +288,17 @@ version = "0.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "619743e34b5ba4e9703bba34deac3427c72507c7159f5fd030aea8cac0cfe341" +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -425,6 +436,17 @@ dependencies = [ "generic-array", ] +[[package]] +name = "colored" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3616f750b84d8f0de8a58bda93e08e2a81ad3f523089b05f1dffecab48c6cbd" +dependencies = [ + "atty", + "lazy_static", + "winapi", +] + [[package]] name = "convert_case" version = "0.4.0" @@ -1389,10 +1411,12 @@ dependencies = [ "futures", "json", "lazy_static", + "log", "pdf", "regex", "reqwest", "scraper", + "simple_logger", "thiserror", "time 0.3.9", "tokio", @@ -1764,6 +1788,19 @@ dependencies = [ "libc", ] +[[package]] +name = "simple_logger" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c75a9723083573ace81ad0cdfc50b858aa3c366c48636edb4109d73122a0c0ea" +dependencies = [ + "atty", + "colored", + "log", + "time 0.3.9", + "winapi", +] + [[package]] name = "siphasher" version = "0.3.10" diff --git a/Cargo.toml b/Cargo.toml index 61d15cf..2521017 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,10 +14,12 @@ bytes = "1.1" futures = "0.3" json = "0.12.4" lazy_static = "1.4" +log = "0.4.16" pdf = "0.7.2" regex = "1.5.5" reqwest = { version = "0.11", features = ["rustls-tls", "stream"], default-features = false } scraper = "0.12" +simple_logger = { version = "2.1.0", features = ["stderr"] } thiserror = "1" time = { version = "0.3.9", features = ["formatting", "macros", "parsing"] } tokio = { version = "1", features = ["full"] } diff --git a/src/fetch.rs b/src/fetch.rs index 74925e0..0018500 100644 --- a/src/fetch.rs +++ b/src/fetch.rs @@ -2,6 +2,7 @@ use std::time::{Duration, Instant, SystemTime}; use futures::{sink::SinkExt, TryStreamExt}; use lazy_static::lazy_static; +use log::info; use reqwest::Url; use scraper::Selector; use time::PrimitiveDateTime; @@ -50,6 +51,8 @@ impl PdfFetcher { } pub async fn fetch(&mut self) -> Result>, Error> { + info!("Fetching data PDF"); + let cache_modtime = match tokio::fs::File::open(CACHED_PDF_PATH).await { Ok(file) => Some( file.metadata() @@ -65,6 +68,7 @@ impl PdfFetcher { if let Some(instant) = self.last_checked { if now - instant < *MIN_CHECK_INTERVAL { return if cache_modtime.is_some() { + info!("Already checked origin recently, not rechecking"); self.cached_pdf() } else { Err(Error::TooSoon) @@ -111,6 +115,8 @@ impl PdfFetcher { .map_or(true, |(cache, origin)| origin > cache); if outdated { + info!("Cached PDF is outdated, downloading latest version"); + let mut pdf_stream = self .client .get(pdf_url) @@ -124,6 +130,8 @@ impl PdfFetcher { sink.send_all(&mut pdf_stream).await?; + Unpin>::close(&mut sink) .await?; + } else { + info!("Cached PDF is already up to date"); } self.last_checked = Some(now); self.cached_pdf() diff --git a/src/main.rs b/src/main.rs index b84da58..f16b3bc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,9 @@ use std::{sync::Arc, time::Duration}; -use actix_web::{get, http::header::ContentType, web, App, HttpResponse, HttpServer, Responder}; +use actix_web::{ + get, http::header::ContentType, middleware::Logger, web, App, HttpResponse, HttpServer, + Responder, +}; use lazy_static::lazy_static; use tokio::sync::RwLock; @@ -60,11 +63,14 @@ async fn start_updater() -> Result, Error> { #[actix_web::main] async fn main() -> std::io::Result<()> { + simple_logger::init_with_level(log::Level::Info).unwrap(); + let state = start_updater().await.expect("Failed to initialize state"); HttpServer::new(move || { App::new() .app_data(state.clone()) + .wrap(Logger::default()) .service(csv) .service(json) .service(actix_files::Files::new("/", "./static/").index_file("index.html"))