From 39aeae7a69cc475666a7136e30a9c8c9d8bd7ced Mon Sep 17 00:00:00 2001 From: mat Date: Mon, 15 Apr 2024 19:19:09 -0500 Subject: [PATCH] add basic logging --- Cargo.lock | 87 ++++++++++++++++++++++++++++++++ Cargo.toml | 2 + src/config.rs | 3 +- src/engines/answer/numbat.rs | 3 +- src/engines/answer/thesaurus.rs | 3 +- src/engines/answer/timezone.rs | 8 +-- src/engines/mod.rs | 8 ++- src/engines/search/marginalia.rs | 3 +- src/main.rs | 5 +- src/normalize.rs | 3 +- src/web/autocomplete.rs | 3 +- src/web/mod.rs | 3 +- src/web/search.rs | 2 +- 13 files changed, 115 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3138434..b7ca2ff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -987,6 +987,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.153" @@ -1083,6 +1089,8 @@ dependencies = [ "tokio", "tokio-stream", "toml", + "tracing", + "tracing-subscriber", "url", "urlencoding", ] @@ -1119,6 +1127,16 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54" +[[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi", +] + [[package]] name = "num-bigint" version = "0.4.4" @@ -1228,6 +1246,12 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + [[package]] name = "parking_lot" version = "0.12.1" @@ -1837,6 +1861,15 @@ dependencies = [ "digest", ] +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + [[package]] name = "shellexpand" version = "3.1.0" @@ -2016,6 +2049,16 @@ dependencies = [ "syn 2.0.52", ] +[[package]] +name = "thread_local" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" +dependencies = [ + "cfg-if", + "once_cell", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -2161,9 +2204,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" dependencies = [ "pin-project-lite", + "tracing-attributes", "tracing-core", ] +[[package]] +name = "tracing-attributes" +version = "0.1.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.52", +] + [[package]] name = "tracing-core" version = "0.1.32" @@ -2171,6 +2226,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" dependencies = [ "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" +dependencies = [ + "nu-ansi-term", + "sharded-slab", + "smallvec", + "thread_local", + "tracing-core", + "tracing-log", ] [[package]] @@ -2262,6 +2343,12 @@ version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "86bd8d4e895da8537e5315b8254664e6b769c4ff3db18321b297a1e7004392e3" +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "version_check" version = "0.9.4" diff --git a/Cargo.toml b/Cargo.toml index a5abf8e..93c6812 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,5 +39,7 @@ serde_json = "1.0.114" tokio = { version = "1.36.0", features = ["rt", "macros"] } tokio-stream = "0.1.15" toml = { version = "0.8.12", default-features = false, features = ["parse"] } +tracing = "0.1.40" +tracing-subscriber = "0.3.18" url = "2.5.0" urlencoding = "2.1.3" diff --git a/src/config.rs b/src/config.rs index 11240e0..5cf575a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,6 +2,7 @@ use std::{collections::HashMap, fs, net::SocketAddr, path::Path}; use once_cell::sync::Lazy; use serde::Deserialize; +use tracing::info; use crate::engines::Engine; @@ -22,7 +23,7 @@ impl Config { config.update(given_config); Ok(config) } else { - println!("No config found, creating one at {config_path:?}"); + info!("No config found, creating one at {config_path:?}"); fs::write(config_path, default_config_str)?; Ok(config) } diff --git a/src/engines/answer/numbat.rs b/src/engines/answer/numbat.rs index 661a29c..2d352d9 100644 --- a/src/engines/answer/numbat.rs +++ b/src/engines/answer/numbat.rs @@ -8,6 +8,7 @@ use numbat::{ InterpreterResult, InterpreterSettings, Statement, }; use once_cell::sync::Lazy; +use tracing::debug; use crate::engines::EngineResponse; @@ -94,7 +95,7 @@ fn interpret(query: &str) -> Option<(Statement, Markup)> { ) { Ok(r) => r, Err(err) => { - eprintln!("numbat error: {}", err); + debug!("numbat error: {err}"); return None; } }; diff --git a/src/engines/answer/thesaurus.rs b/src/engines/answer/thesaurus.rs index 84bf885..139dfd6 100644 --- a/src/engines/answer/thesaurus.rs +++ b/src/engines/answer/thesaurus.rs @@ -1,6 +1,7 @@ use eyre::eyre; use scraper::{Html, Selector}; use serde::Deserialize; +use tracing::error; use url::Url; use crate::engines::{EngineResponse, RequestResponse, CLIENT}; @@ -134,7 +135,7 @@ fn parse_thesaurus_com_item( weak_matches = matches; } _ => { - eprintln!("Unknown thesaurus match type: {match_type}"); + error!("Unknown thesaurus match type: {match_type}"); } } } diff --git a/src/engines/answer/timezone.rs b/src/engines/answer/timezone.rs index ea1d2a0..a845336 100644 --- a/src/engines/answer/timezone.rs +++ b/src/engines/answer/timezone.rs @@ -1,5 +1,5 @@ use chrono::{DateTime, TimeZone}; -use chrono_tz::{OffsetComponents, OffsetName, Tz}; +use chrono_tz::{OffsetComponents, Tz}; use crate::engines::EngineResponse; @@ -77,12 +77,6 @@ fn evaluate(query: &str) -> Option { let source_offset = source_timezone.offset_from_utc_date(¤t_date); let target_offset = target_timezone.offset_from_utc_date(¤t_date); - println!( - "source_offset: {source_offset:?} {:?}", - source_offset.tz_id() - ); - println!("target_offset: {target_offset:?}"); - let source_time_naive = current_date.and_hms_opt( if ampm == "pm" && hour != 12 { hour + 12 diff --git a/src/engines/mod.rs b/src/engines/mod.rs index 9cdf41d..715ef14 100644 --- a/src/engines/mod.rs +++ b/src/engines/mod.rs @@ -13,6 +13,7 @@ use once_cell::sync::Lazy; use reqwest::header::HeaderMap; use serde::{Deserialize, Deserializer}; use tokio::sync::mpsc; +use tracing::{error, info}; mod macros; use crate::{ @@ -246,12 +247,15 @@ impl ProgressUpdate { } } +#[tracing::instrument(fields(query = %query.query), skip(progress_tx))] pub async fn search( query: &SearchQuery, progress_tx: mpsc::UnboundedSender, ) -> eyre::Result<()> { let start_time = Instant::now(); + info!("Doing search"); + let progress_tx = &progress_tx; let mut requests = Vec::new(); @@ -303,7 +307,7 @@ pub async fn search( let response = match engine.parse_response(&http_response) { Ok(response) => response, Err(e) => { - eprintln!("parse error: {e}"); + error!("parse error: {e}"); EngineResponse::new() } }; @@ -368,7 +372,7 @@ pub async fn search( engine.postsearch_parse_response(&http_response) } Err(e) => { - eprintln!("postsearch request error: {e}"); + error!("postsearch request error: {e}"); None } }; diff --git a/src/engines/search/marginalia.rs b/src/engines/search/marginalia.rs index f91b483..ff9cbf8 100644 --- a/src/engines/search/marginalia.rs +++ b/src/engines/search/marginalia.rs @@ -1,5 +1,6 @@ use reqwest::Url; use serde::Deserialize; +use tracing::error; use crate::{ engines::{Engine, EngineResponse, RequestResponse, SearchQuery, CLIENT}, @@ -29,7 +30,7 @@ pub fn request(query: &SearchQuery) -> RequestResponse { let config: MarginaliaConfig = match toml::Value::Table(config_toml).try_into() { Ok(args) => args, Err(err) => { - eprintln!("Failed to parse Marginalia config: {err}"); + error!("Failed to parse Marginalia config: {err}"); return RequestResponse::None; } }; diff --git a/src/main.rs b/src/main.rs index 37ad7d1..8248ad2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use config::Config; +use tracing::error; pub mod config; pub mod engines; @@ -8,10 +9,12 @@ pub mod web; #[tokio::main(flavor = "current_thread")] async fn main() { + tracing_subscriber::fmt::init(); + let config = match Config::read_or_create() { Ok(config) => config, Err(err) => { - eprintln!("Couldn't parse config:\n{err}"); + error!("Couldn't parse config:\n{err}"); return; } }; diff --git a/src/normalize.rs b/src/normalize.rs index 1a6c1de..17e3dc3 100644 --- a/src/normalize.rs +++ b/src/normalize.rs @@ -1,3 +1,4 @@ +use tracing::error; use url::Url; pub fn normalize_url(url: &str) -> eyre::Result { @@ -7,7 +8,7 @@ pub fn normalize_url(url: &str) -> eyre::Result { } let Ok(mut url) = Url::parse(url) else { - eprintln!("failed to parse url: {url}"); + error!("failed to parse url: {url}"); return Ok(url.to_string()); }; diff --git a/src/web/autocomplete.rs b/src/web/autocomplete.rs index 93277d4..efad3cb 100644 --- a/src/web/autocomplete.rs +++ b/src/web/autocomplete.rs @@ -6,6 +6,7 @@ use axum::{ response::IntoResponse, Json, }; +use tracing::error; use crate::{config::Config, engines}; @@ -22,7 +23,7 @@ pub async fn route( let res = match engines::autocomplete(&config, &query).await { Ok(res) => res, Err(err) => { - eprintln!("Autocomplete error for {query}: {err}"); + error!("Autocomplete error for {query}: {err}"); return (StatusCode::INTERNAL_SERVER_ERROR, Json((query, vec![]))); } }; diff --git a/src/web/mod.rs b/src/web/mod.rs index 20b63c2..7ecdfb0 100644 --- a/src/web/mod.rs +++ b/src/web/mod.rs @@ -5,6 +5,7 @@ pub mod search; use std::{net::SocketAddr, sync::Arc}; use axum::{http::header, routing::get, Router}; +use tracing::info; use crate::config::Config; @@ -53,7 +54,7 @@ pub async fn run(config: Config) { .route("/autocomplete", get(autocomplete::route)) .with_state(Arc::new(config)); - println!("Listening on {bind_addr}"); + info!("Listening on {bind_addr}"); let listener = tokio::net::TcpListener::bind(bind_addr).await.unwrap(); axum::serve( diff --git a/src/web/search.rs b/src/web/search.rs index ca84a4a..a1aab5c 100644 --- a/src/web/search.rs +++ b/src/web/search.rs @@ -207,7 +207,7 @@ pub async fn route( let (progress_tx, mut progress_rx) = tokio::sync::mpsc::unbounded_channel(); - let search_future = tokio::spawn(async move { engines::search( &query, progress_tx).await }); + let search_future = tokio::spawn(async move { engines::search(&query, progress_tx).await }); while let Some(progress_update) = progress_rx.recv().await { match progress_update.data {