Skip to content

Commit a910c00

Browse files
committed
Welcome to the 21st century
println -> tracing
1 parent 6dfe0db commit a910c00

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ rumqttc = "0.24.0"
1313
serde = { version = "1.0.215", features = ["derive"] }
1414
serde_json = "1.0.133"
1515
tokio = { version = "1.41.1", features = ["full"] }
16+
tracing = "0.1.40"
17+
tracing-subscriber = "0.3.18"
1618
url = { version = "2.5.4", features = ["serde"] }
1719

1820
[dev-dependencies]

src/main.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
use ha_mitaffald::settings::Settings;
22
use ha_mitaffald::sync_data;
3+
use tracing::{error, info, Level};
4+
use tracing_subscriber::FmtSubscriber;
35

46
#[tokio::main]
57
async fn main() {
8+
let subscriber = FmtSubscriber::builder()
9+
.with_max_level(Level::INFO)
10+
.finish();
11+
12+
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
613
loop {
7-
println!("Starting data synchronization");
14+
info!("Starting data synchronization");
815

916
let settings = Settings::new().expect("Failed to read settings");
1017
let update_interval =
@@ -13,14 +20,14 @@ async fn main() {
1320
let report = sync_data(settings).await;
1421

1522
match report {
16-
Ok(_) => println!("Data synchronization completed"),
17-
Err(x) => eprintln!(
23+
Ok(_) => info!("Data synchronization completed"),
24+
Err(x) => error!(
1825
"Data synchronization failed (some entities may have been updated), error: {}",
1926
x
2027
),
2128
}
2229

23-
println!(
30+
info!(
2431
"Next synchronization scheduled at {}",
2532
(chrono::Local::now() + update_interval).format("%Y-%m-%d %H:%M:%S")
2633
);

src/mitaffald/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub mod settings;
33
use chrono::{DateTime, Utc};
44
use serde::Deserialize;
55
use settings::{Address, AffaldVarmeConfig};
6+
use tracing::info;
67
use url::Url;
78

89
use self::settings::{AddressId, TraditionalAddress};
@@ -25,7 +26,7 @@ pub async fn get_containers(config: AffaldVarmeConfig) -> Result<Vec<Container>,
2526
.next()
2627
.ok_or_else(|| "No data found".to_string())
2728
.map(|response| {
28-
println!("Received information for stand: {}", response.stand_name);
29+
info!("Received information for stand: {}", response.stand_name);
2930
response.into()
3031
})
3132
})

tests/mqtt/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::{
66
};
77

88
use rumqttc::{Client, Event, Packet, Publish, QoS};
9+
use tracing::info;
910

1011
pub struct CollectingClient {
1112
received_messages: std::sync::Arc<Mutex<Vec<Publish>>>,
@@ -37,7 +38,7 @@ impl CollectingClient {
3738

3839
loop {
3940
let message = connection.recv_timeout(Duration::from_secs(1));
40-
println!("Received message: {:?}", &message);
41+
info!("Received message: {:?}", &message);
4142
match message {
4243
Ok(Ok(Event::Incoming(Packet::SubAck(_)))) => {
4344
tx.send(()).expect("Cannot report ready to main thread")
@@ -49,7 +50,7 @@ impl CollectingClient {
4950
}
5051

5152
if stopping_flag.load(std::sync::atomic::Ordering::Relaxed) {
52-
println!("Thread is terminating");
53+
info!("Thread is terminating");
5354
break;
5455
}
5556
}
@@ -83,7 +84,7 @@ impl CollectingClient {
8384
std::thread::sleep(std::time::Duration::from_millis(500));
8485
}
8586

86-
println!("Joining worker thread...");
87+
info!("Joining worker thread...");
8788
if let Some(handle) = self.join_handle {
8889
handle.join().unwrap();
8990
}

0 commit comments

Comments
 (0)