add basic logging

This commit is contained in:
mat 2024-04-15 19:19:09 -05:00
parent 25760146a3
commit 39aeae7a69
13 changed files with 115 additions and 18 deletions

87
Cargo.lock generated
View File

@ -987,6 +987,12 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.153" version = "0.2.153"
@ -1083,6 +1089,8 @@ dependencies = [
"tokio", "tokio",
"tokio-stream", "tokio-stream",
"toml", "toml",
"tracing",
"tracing-subscriber",
"url", "url",
"urlencoding", "urlencoding",
] ]
@ -1119,6 +1127,16 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54" 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]] [[package]]
name = "num-bigint" name = "num-bigint"
version = "0.4.4" version = "0.4.4"
@ -1228,6 +1246,12 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
[[package]]
name = "overload"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39"
[[package]] [[package]]
name = "parking_lot" name = "parking_lot"
version = "0.12.1" version = "0.12.1"
@ -1837,6 +1861,15 @@ dependencies = [
"digest", "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]] [[package]]
name = "shellexpand" name = "shellexpand"
version = "3.1.0" version = "3.1.0"
@ -2016,6 +2049,16 @@ dependencies = [
"syn 2.0.52", "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]] [[package]]
name = "tinyvec" name = "tinyvec"
version = "1.6.0" version = "1.6.0"
@ -2161,9 +2204,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef"
dependencies = [ dependencies = [
"pin-project-lite", "pin-project-lite",
"tracing-attributes",
"tracing-core", "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]] [[package]]
name = "tracing-core" name = "tracing-core"
version = "0.1.32" version = "0.1.32"
@ -2171,6 +2226,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54"
dependencies = [ dependencies = [
"once_cell", "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]] [[package]]
@ -2262,6 +2343,12 @@ version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86bd8d4e895da8537e5315b8254664e6b769c4ff3db18321b297a1e7004392e3" checksum = "86bd8d4e895da8537e5315b8254664e6b769c4ff3db18321b297a1e7004392e3"
[[package]]
name = "valuable"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.9.4" version = "0.9.4"

View File

@ -39,5 +39,7 @@ serde_json = "1.0.114"
tokio = { version = "1.36.0", features = ["rt", "macros"] } tokio = { version = "1.36.0", features = ["rt", "macros"] }
tokio-stream = "0.1.15" tokio-stream = "0.1.15"
toml = { version = "0.8.12", default-features = false, features = ["parse"] } toml = { version = "0.8.12", default-features = false, features = ["parse"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
url = "2.5.0" url = "2.5.0"
urlencoding = "2.1.3" urlencoding = "2.1.3"

View File

@ -2,6 +2,7 @@ use std::{collections::HashMap, fs, net::SocketAddr, path::Path};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use serde::Deserialize; use serde::Deserialize;
use tracing::info;
use crate::engines::Engine; use crate::engines::Engine;
@ -22,7 +23,7 @@ impl Config {
config.update(given_config); config.update(given_config);
Ok(config) Ok(config)
} else { } 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)?; fs::write(config_path, default_config_str)?;
Ok(config) Ok(config)
} }

View File

@ -8,6 +8,7 @@ use numbat::{
InterpreterResult, InterpreterSettings, Statement, InterpreterResult, InterpreterSettings, Statement,
}; };
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use tracing::debug;
use crate::engines::EngineResponse; use crate::engines::EngineResponse;
@ -94,7 +95,7 @@ fn interpret(query: &str) -> Option<(Statement, Markup)> {
) { ) {
Ok(r) => r, Ok(r) => r,
Err(err) => { Err(err) => {
eprintln!("numbat error: {}", err); debug!("numbat error: {err}");
return None; return None;
} }
}; };

View File

@ -1,6 +1,7 @@
use eyre::eyre; use eyre::eyre;
use scraper::{Html, Selector}; use scraper::{Html, Selector};
use serde::Deserialize; use serde::Deserialize;
use tracing::error;
use url::Url; use url::Url;
use crate::engines::{EngineResponse, RequestResponse, CLIENT}; use crate::engines::{EngineResponse, RequestResponse, CLIENT};
@ -134,7 +135,7 @@ fn parse_thesaurus_com_item(
weak_matches = matches; weak_matches = matches;
} }
_ => { _ => {
eprintln!("Unknown thesaurus match type: {match_type}"); error!("Unknown thesaurus match type: {match_type}");
} }
} }
} }

View File

@ -1,5 +1,5 @@
use chrono::{DateTime, TimeZone}; use chrono::{DateTime, TimeZone};
use chrono_tz::{OffsetComponents, OffsetName, Tz}; use chrono_tz::{OffsetComponents, Tz};
use crate::engines::EngineResponse; use crate::engines::EngineResponse;
@ -77,12 +77,6 @@ fn evaluate(query: &str) -> Option<TimeResponse> {
let source_offset = source_timezone.offset_from_utc_date(&current_date); let source_offset = source_timezone.offset_from_utc_date(&current_date);
let target_offset = target_timezone.offset_from_utc_date(&current_date); let target_offset = target_timezone.offset_from_utc_date(&current_date);
println!(
"source_offset: {source_offset:?} {:?}",
source_offset.tz_id()
);
println!("target_offset: {target_offset:?}");
let source_time_naive = current_date.and_hms_opt( let source_time_naive = current_date.and_hms_opt(
if ampm == "pm" && hour != 12 { if ampm == "pm" && hour != 12 {
hour + 12 hour + 12

View File

@ -13,6 +13,7 @@ use once_cell::sync::Lazy;
use reqwest::header::HeaderMap; use reqwest::header::HeaderMap;
use serde::{Deserialize, Deserializer}; use serde::{Deserialize, Deserializer};
use tokio::sync::mpsc; use tokio::sync::mpsc;
use tracing::{error, info};
mod macros; mod macros;
use crate::{ use crate::{
@ -246,12 +247,15 @@ impl ProgressUpdate {
} }
} }
#[tracing::instrument(fields(query = %query.query), skip(progress_tx))]
pub async fn search( pub async fn search(
query: &SearchQuery, query: &SearchQuery,
progress_tx: mpsc::UnboundedSender<ProgressUpdate>, progress_tx: mpsc::UnboundedSender<ProgressUpdate>,
) -> eyre::Result<()> { ) -> eyre::Result<()> {
let start_time = Instant::now(); let start_time = Instant::now();
info!("Doing search");
let progress_tx = &progress_tx; let progress_tx = &progress_tx;
let mut requests = Vec::new(); let mut requests = Vec::new();
@ -303,7 +307,7 @@ pub async fn search(
let response = match engine.parse_response(&http_response) { let response = match engine.parse_response(&http_response) {
Ok(response) => response, Ok(response) => response,
Err(e) => { Err(e) => {
eprintln!("parse error: {e}"); error!("parse error: {e}");
EngineResponse::new() EngineResponse::new()
} }
}; };
@ -368,7 +372,7 @@ pub async fn search(
engine.postsearch_parse_response(&http_response) engine.postsearch_parse_response(&http_response)
} }
Err(e) => { Err(e) => {
eprintln!("postsearch request error: {e}"); error!("postsearch request error: {e}");
None None
} }
}; };

View File

@ -1,5 +1,6 @@
use reqwest::Url; use reqwest::Url;
use serde::Deserialize; use serde::Deserialize;
use tracing::error;
use crate::{ use crate::{
engines::{Engine, EngineResponse, RequestResponse, SearchQuery, CLIENT}, 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() { let config: MarginaliaConfig = match toml::Value::Table(config_toml).try_into() {
Ok(args) => args, Ok(args) => args,
Err(err) => { Err(err) => {
eprintln!("Failed to parse Marginalia config: {err}"); error!("Failed to parse Marginalia config: {err}");
return RequestResponse::None; return RequestResponse::None;
} }
}; };

View File

@ -1,4 +1,5 @@
use config::Config; use config::Config;
use tracing::error;
pub mod config; pub mod config;
pub mod engines; pub mod engines;
@ -8,10 +9,12 @@ pub mod web;
#[tokio::main(flavor = "current_thread")] #[tokio::main(flavor = "current_thread")]
async fn main() { async fn main() {
tracing_subscriber::fmt::init();
let config = match Config::read_or_create() { let config = match Config::read_or_create() {
Ok(config) => config, Ok(config) => config,
Err(err) => { Err(err) => {
eprintln!("Couldn't parse config:\n{err}"); error!("Couldn't parse config:\n{err}");
return; return;
} }
}; };

View File

@ -1,3 +1,4 @@
use tracing::error;
use url::Url; use url::Url;
pub fn normalize_url(url: &str) -> eyre::Result<String> { pub fn normalize_url(url: &str) -> eyre::Result<String> {
@ -7,7 +8,7 @@ pub fn normalize_url(url: &str) -> eyre::Result<String> {
} }
let Ok(mut url) = Url::parse(url) else { 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()); return Ok(url.to_string());
}; };

View File

@ -6,6 +6,7 @@ use axum::{
response::IntoResponse, response::IntoResponse,
Json, Json,
}; };
use tracing::error;
use crate::{config::Config, engines}; use crate::{config::Config, engines};
@ -22,7 +23,7 @@ pub async fn route(
let res = match engines::autocomplete(&config, &query).await { let res = match engines::autocomplete(&config, &query).await {
Ok(res) => res, Ok(res) => res,
Err(err) => { Err(err) => {
eprintln!("Autocomplete error for {query}: {err}"); error!("Autocomplete error for {query}: {err}");
return (StatusCode::INTERNAL_SERVER_ERROR, Json((query, vec![]))); return (StatusCode::INTERNAL_SERVER_ERROR, Json((query, vec![])));
} }
}; };

View File

@ -5,6 +5,7 @@ pub mod search;
use std::{net::SocketAddr, sync::Arc}; use std::{net::SocketAddr, sync::Arc};
use axum::{http::header, routing::get, Router}; use axum::{http::header, routing::get, Router};
use tracing::info;
use crate::config::Config; use crate::config::Config;
@ -53,7 +54,7 @@ pub async fn run(config: Config) {
.route("/autocomplete", get(autocomplete::route)) .route("/autocomplete", get(autocomplete::route))
.with_state(Arc::new(config)); .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(); let listener = tokio::net::TcpListener::bind(bind_addr).await.unwrap();
axum::serve( axum::serve(

View File

@ -207,7 +207,7 @@ pub async fn route(
let (progress_tx, mut progress_rx) = tokio::sync::mpsc::unbounded_channel(); 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 { while let Some(progress_update) = progress_rx.recv().await {
match progress_update.data { match progress_update.data {