Skip to content

Commit

Permalink
Welcome to the 21st century
Browse files Browse the repository at this point in the history
println -> tracing
  • Loading branch information
CosminLazar committed Nov 23, 2024
1 parent 6dfe0db commit a910c00
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
15 changes: 11 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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 =
Expand All @@ -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")
);
Expand Down
3 changes: 2 additions & 1 deletion src/mitaffald/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -25,7 +26,7 @@ pub async fn get_containers(config: AffaldVarmeConfig) -> Result<Vec<Container>,
.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()
})
})
Expand Down
7 changes: 4 additions & 3 deletions tests/mqtt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
};

use rumqttc::{Client, Event, Packet, Publish, QoS};
use tracing::info;

pub struct CollectingClient {
received_messages: std::sync::Arc<Mutex<Vec<Publish>>>,
Expand Down Expand Up @@ -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")
Expand All @@ -49,7 +50,7 @@ impl CollectingClient {
}

if stopping_flag.load(std::sync::atomic::Ordering::Relaxed) {
println!("Thread is terminating");
info!("Thread is terminating");
break;
}
}
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit a910c00

Please sign in to comment.