From a910c009d6dd033922dce339484dc2974366d001 Mon Sep 17 00:00:00 2001 From: Cosmin Constantin Lazar Date: Sat, 23 Nov 2024 07:18:43 +0100 Subject: [PATCH] Welcome to the 21st century println -> tracing --- Cargo.toml | 2 ++ src/main.rs | 15 +++++++++++---- src/mitaffald/mod.rs | 3 ++- tests/mqtt/mod.rs | 7 ++++--- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6b2e295..07cb771 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,8 @@ rumqttc = "0.24.0" serde = { version = "1.0.215", features = ["derive"] } serde_json = "1.0.133" tokio = { version = "1.41.1", features = ["full"] } +tracing = "0.1.40" +tracing-subscriber = "0.3.18" url = { version = "2.5.4", features = ["serde"] } [dev-dependencies] diff --git a/src/main.rs b/src/main.rs index 54492e3..2d85035 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,17 @@ use ha_mitaffald::settings::Settings; use ha_mitaffald::sync_data; +use tracing::{error, info, Level}; +use tracing_subscriber::FmtSubscriber; #[tokio::main] async fn main() { + let subscriber = FmtSubscriber::builder() + .with_max_level(Level::INFO) + .finish(); + + tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed"); loop { - println!("Starting data synchronization"); + info!("Starting data synchronization"); let settings = Settings::new().expect("Failed to read settings"); let update_interval = @@ -13,14 +20,14 @@ async fn main() { let report = sync_data(settings).await; match report { - Ok(_) => println!("Data synchronization completed"), - Err(x) => eprintln!( + Ok(_) => info!("Data synchronization completed"), + Err(x) => error!( "Data synchronization failed (some entities may have been updated), error: {}", x ), } - println!( + info!( "Next synchronization scheduled at {}", (chrono::Local::now() + update_interval).format("%Y-%m-%d %H:%M:%S") ); diff --git a/src/mitaffald/mod.rs b/src/mitaffald/mod.rs index 5c51a27..9525677 100644 --- a/src/mitaffald/mod.rs +++ b/src/mitaffald/mod.rs @@ -3,6 +3,7 @@ pub mod settings; use chrono::{DateTime, Utc}; use serde::Deserialize; use settings::{Address, AffaldVarmeConfig}; +use tracing::info; use url::Url; use self::settings::{AddressId, TraditionalAddress}; @@ -25,7 +26,7 @@ pub async fn get_containers(config: AffaldVarmeConfig) -> Result, .next() .ok_or_else(|| "No data found".to_string()) .map(|response| { - println!("Received information for stand: {}", response.stand_name); + info!("Received information for stand: {}", response.stand_name); response.into() }) }) diff --git a/tests/mqtt/mod.rs b/tests/mqtt/mod.rs index b36a7f6..442e86b 100644 --- a/tests/mqtt/mod.rs +++ b/tests/mqtt/mod.rs @@ -6,6 +6,7 @@ use std::{ }; use rumqttc::{Client, Event, Packet, Publish, QoS}; +use tracing::info; pub struct CollectingClient { received_messages: std::sync::Arc>>, @@ -37,7 +38,7 @@ impl CollectingClient { loop { let message = connection.recv_timeout(Duration::from_secs(1)); - println!("Received message: {:?}", &message); + info!("Received message: {:?}", &message); match message { Ok(Ok(Event::Incoming(Packet::SubAck(_)))) => { tx.send(()).expect("Cannot report ready to main thread") @@ -49,7 +50,7 @@ impl CollectingClient { } if stopping_flag.load(std::sync::atomic::Ordering::Relaxed) { - println!("Thread is terminating"); + info!("Thread is terminating"); break; } } @@ -83,7 +84,7 @@ impl CollectingClient { std::thread::sleep(std::time::Duration::from_millis(500)); } - println!("Joining worker thread..."); + info!("Joining worker thread..."); if let Some(handle) = self.join_handle { handle.join().unwrap(); }