From ab5e187212dca58f38109f62b66e3c3cd66c945b Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Tue, 4 Feb 2025 23:57:02 -0800 Subject: [PATCH] Use tracing library --- Cargo.lock | 2 ++ Cargo.toml | 2 ++ src/demo.rs | 3 ++- src/feed_utils.rs | 12 +++++++----- src/feeds.rs | 3 ++- src/main.rs | 20 ++++++++++++++++++++ src/poll.rs | 19 +++++++++++-------- src/user.rs | 5 +++-- 8 files changed, 49 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2f796ad..4d431ba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2677,6 +2677,8 @@ dependencies = [ "sqlx", "time", "tokio", + "tracing", + "tracing-subscriber", "url", "uuid", ] diff --git a/Cargo.toml b/Cargo.toml index c7b92c5..56802a4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,3 +22,5 @@ tokio = "1.43.0" time = "0.3.37" base64 = "0.21" getrandom = "0.2" +tracing = "0.1" +tracing-subscriber = { version = "0.3", features = ["env-filter"] } diff --git a/src/demo.rs b/src/demo.rs index 2872314..926474c 100644 --- a/src/demo.rs +++ b/src/demo.rs @@ -1,5 +1,6 @@ use crate::feeds::Feed; use crate::user::User; +use tracing::info; struct DemoFeed { name: &'static str, @@ -75,5 +76,5 @@ pub async fn setup_demo_data(pool: &sqlx::SqlitePool) { .expect("Failed to create demo feed"); } - println!("Successfully set up demo data"); + info!("Successfully set up demo data"); } diff --git a/src/feed_utils.rs b/src/feed_utils.rs index d37db18..52359eb 100644 --- a/src/feed_utils.rs +++ b/src/feed_utils.rs @@ -1,29 +1,31 @@ +use feed_rs; use url::Url; +use tracing::{error, info}; #[derive(Debug)] pub struct FeedError; pub async fn fetch_feed(url: &Url) -> Result { - println!("Making a request to fetch feed `{url}`"); + info!("Making a request to fetch feed `{url}`"); // Fetch the feed content let response = reqwest::get(url.as_ref()).await.map_err(|e| { - eprintln!("Failed to fetch feed: {}", e); + error!("Failed to fetch feed: {}", e); FeedError })?; let content = response.text().await.map_err(|e| { - eprintln!("Failed to read response body: {}", e); + error!("Failed to read response body: {}", e); FeedError })?; // Parse the feed let feed_data = feed_rs::parser::parse(content.as_bytes()).map_err(|e| { - eprintln!("Failed to parse feed content: {}", e); + error!("Failed to parse feed content: {}", e); FeedError })?; - println!("Fetched feed: {}", url.as_ref()); + info!("Fetched feed: {}", url.as_ref()); Ok(feed_data) } diff --git a/src/feeds.rs b/src/feeds.rs index 09576b3..4c7beb5 100644 --- a/src/feeds.rs +++ b/src/feeds.rs @@ -3,6 +3,7 @@ use rocket::serde::{self, json::Json, Deserialize, Serialize}; use rocket_db_pools::Connection; use sqlx::types::JsonValue; use sqlx::Executor; +use tracing::error; use url::Url; use uuid::Uuid; @@ -42,7 +43,7 @@ impl Feed { { // Convert categorization to JSON value let categorization_json = serde::json::to_value(&self.categorization).map_err(|e| { - eprintln!("Failed to serialize categorization: {}", e); + error!("Failed to serialize categorization: {}", e); sqlx::Error::Decode(Box::new(e)) })?; diff --git a/src/main.rs b/src/main.rs index d4286fa..70bc6ad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,8 @@ extern crate rocket; use clap::Parser; +use tracing::{info, Level}; +use tracing_subscriber::FmtSubscriber; mod demo; mod feed_utils; @@ -89,8 +91,24 @@ async fn setup_database(demo: bool, rocket: Rocket) -> fairing::Result { #[launch] fn rocket() -> _ { + // Initialize the tracing subscriber + FmtSubscriber::builder() + .with_max_level(Level::INFO) + .with_target(false) + .with_thread_ids(true) + .with_file(true) + .with_line_number(true) + .with_thread_names(true) + .pretty() + .init(); + let args = Args::parse(); + info!("Starting RSS Reader"); + if args.demo { + info!("Running in demo mode"); + } + let db_url = if args.demo { // Use a temporary file for demo mode instead of in-memory database let temp_db = "/tmp/rss-reader-temp-db.sqlite"; @@ -100,6 +118,7 @@ fn rocket() -> _ { } // Create the new database file std::fs::File::create(temp_db).expect("Failed to create temporary database file"); + info!("Created temporary database at {}", temp_db); format!("sqlite:{}", temp_db) } else { let database = args @@ -109,6 +128,7 @@ fn rocket() -> _ { if !std::path::Path::new(&database).exists() { use std::fs::File; File::create(&database).expect("Failed to create database file"); + info!("Created new database at {}", database); } format!("sqlite:{}", database) }; diff --git a/src/poll.rs b/src/poll.rs index 45e0e01..5d4ac46 100644 --- a/src/poll.rs +++ b/src/poll.rs @@ -7,6 +7,7 @@ use rocket::serde::uuid::Uuid; use rocket::serde::{self, json::Json, Serialize}; use rocket_db_pools::Connection; use sqlx::{Acquire, SqliteConnection}; +use tracing::{error, info}; use url::Url; const POLLING_INTERVAL: Duration = Duration::minutes(20); @@ -39,7 +40,7 @@ async fn update_entry_db( ) -> Result<(), Status> { // Start a transaction for batch update let mut tx = db.begin().await.map_err(|e| { - eprintln!("Failed to start transaction: {}", e); + error!("Failed to start transaction: {}", e); Status::InternalServerError })?; @@ -52,7 +53,7 @@ async fn update_entry_db( .execute(&mut *tx) .await .map_err(|e| { - eprintln!("Failed to update feed last_checked_time: {}", e); + error!("Failed to update feed last_checked_time: {}", e); Status::InternalServerError })?; @@ -95,9 +96,9 @@ async fn update_entry_db( .await; if let Err(e) = result { - eprintln!("Failed to save feed entry: {}", e); + error!("Failed to save feed entry: {}", e); tx.rollback().await.map_err(|e| { - eprintln!("Failed to rollback transaction: {}", e); + error!("Failed to rollback transaction: {}", e); Status::InternalServerError })?; return Err(Status::InternalServerError); @@ -106,7 +107,7 @@ async fn update_entry_db( // Commit the transaction tx.commit().await.map_err(|e| { - eprintln!("Failed to commit transaction: {}", e); + error!("Failed to commit transaction: {}", e); Status::InternalServerError })?; @@ -136,7 +137,7 @@ async fn read_entries(feed_id: &str, db: &mut SqliteConnection) -> Result Ok(Json(user)), Err(e) => { - eprintln!("Database error: {}", e); + error!("Database error: {}", e); match e { sqlx::Error::Database(db_err) if db_err.is_unique_violation() => { Err(Status::Conflict) @@ -360,7 +361,7 @@ pub async fn setup( match user.write_to_database(&mut **db).await { Ok(_) => Ok(Status::Created), Err(e) => { - eprintln!("Database error: {}", e); + error!("Database error: {}", e); match e { sqlx::Error::Database(db_err) if db_err.is_unique_violation() => { Err(Json(SetupError {