Skip to content

Commit aaeaaf9

Browse files
committed
migrate logging to tracing
1 parent fd891bd commit aaeaaf9

File tree

13 files changed

+112
-66
lines changed

13 files changed

+112
-66
lines changed

Cargo.lock

Lines changed: 72 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,7 @@ confy = { version = "0.6.1", default-features = false, features = [
3535
"yaml_conf",
3636
] }
3737
homedir = { version = "0.3.4", default-features = false }
38-
log = { version = "0.4.27", features = ["std", "max_level_debug"] }
3938
serde = { version = "1.0.219", features = ["derive"] }
40-
simple_logger = { version = "5.0.0", default-features = false, features = [
41-
"colors",
42-
"colored",
43-
"stderr",
44-
"timestamps",
45-
] }
4639
thiserror = "2.0.12"
4740
time = { version = "0.3.37", default-features = false, features = ["macros"] }
4841
users = { version = "0.11.0", default-features = false }
@@ -53,3 +46,6 @@ libc = { version = "0.2.172", default-features = false }
5346
semaphore = "0.4.0"
5447
circular-queue = "0.2.7"
5548
iana-time-zone = "0.1.63"
49+
tracing-subscriber = "0.3.19"
50+
tracing = "0.1.41"
51+
chrono = "0.4.41"

src/dbmgr/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
server::{config::Config, db::DatabaseClient},
44
};
55
use color_print::cprintln;
6-
use log::{debug, error, info};
6+
use tracing::{debug, error, info};
77
use std::{io::stdin, process::exit};
88

99
pub async fn main(cmd: DatabaseCommand, config: &Config) {

src/error.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::{io, num::TryFromIntError};
2+
use tracing::subscriber::SetGlobalDefaultError;
23

34
#[derive(Debug, thiserror::Error)]
45
pub enum Error {
@@ -24,7 +25,7 @@ pub enum Error {
2425

2526
/// Failed to set up the logger.
2627
#[error("Failed to set global logger")]
27-
LogInit(#[from] log::SetLoggerError),
28+
LogInit(#[from] SetGlobalDefaultError),
2829

2930
/// Integer conversion error.
3031
#[error("Integer conversion error: {0}")]

src/logging.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use crate::error::Error;
2+
use std::time::SystemTime;
3+
use tracing_subscriber::fmt::{format::Writer, time::FormatTime};
4+
5+
struct DateTimeFormatter;
6+
7+
impl FormatTime for DateTimeFormatter {
8+
fn format_time(&self, w: &mut Writer<'_>) -> std::fmt::Result {
9+
let now = SystemTime::now();
10+
let datetime: chrono::DateTime<chrono::Local> = now.into();
11+
write!(w, "{}", datetime.format("%d.%m.%Y/%H:%M:%S"))
12+
}
13+
}
14+
15+
pub fn setup() -> Result<(), Error> {
16+
let subscriber = tracing_subscriber::fmt()
17+
.compact()
18+
.with_timer(DateTimeFormatter)
19+
.with_target(false)
20+
.finish();
21+
tracing::subscriber::set_global_default(subscriber)?;
22+
23+
Ok(())
24+
}

src/main.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
use clap::Parser;
22
use cli::Command;
3-
use log::{debug, error, info};
43
use server::config::Config;
5-
use simple_logger::SimpleLogger;
64
use sqlx::migrate::Migrator;
75
use std::process::exit;
8-
use time::macros::format_description;
6+
use tracing::{debug, error, info};
97

108
mod cli;
119
mod dbmgr;
1210
mod error;
11+
mod logging;
1312
mod server;
1413
mod svcmgr;
1514
mod tester;
@@ -20,18 +19,7 @@ pub static MIGRATOR: Migrator = sqlx::migrate!();
2019
async fn main() -> Result<(), error::Error> {
2120
let args = cli::Cli::parse();
2221

23-
let logger = SimpleLogger::new().with_timestamp_format(format_description!(
24-
"[year]-[month]-[day] [hour]:[minute]:[second]"
25-
));
26-
27-
let log_level = if args.debug || cfg!(debug_assertions) {
28-
log::LevelFilter::Debug
29-
} else {
30-
log::LevelFilter::Info
31-
};
32-
33-
let logger = logger.with_level(log_level);
34-
logger.init()?;
22+
logging::setup()?;
3523

3624
info!("PixelWeather Server v{}", env!("CARGO_PKG_VERSION"));
3725
debug!("Arguments: {args:?}");

src/server/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use super::db::{DatabaseClient, FirmwareBlob, MeasurementId, NodeId};
22
use crate::error::Error;
33
use arrayref::array_ref;
44
use circular_queue::CircularQueue;
5-
use log::{debug, error, warn};
65
use pwmp_client::pwmp_msg::{
76
Message, MsgId, mac::Mac, request::Request, response::Response, version::Version,
87
};
@@ -11,6 +10,7 @@ use tokio::{
1110
io::{AsyncReadExt, AsyncWriteExt},
1211
net::TcpStream,
1312
};
13+
use tracing::{debug, error, warn};
1414

1515
const RCV_BUFFER_SIZE: usize = 1024;
1616
const ID_CACHE_SIZE: usize = 32;

src/server/client_handle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::{
55
rate_limit::RateLimiter,
66
};
77
use crate::error::Error;
8-
use log::{debug, error, warn};
8+
use tracing::{debug, error, warn};
99
use pwmp_client::pwmp_msg::{request::Request, response::Response};
1010
use std::{io::Read, net::SocketAddr, sync::Arc, time::Duration};
1111
use tokio::{net::TcpStream, time::timeout};

src/server/handle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::{config::Config, db::DatabaseClient, rate_limit::RateLimiter};
22
use crate::server::client_handle::handle_client;
3-
use log::{debug, error, info, warn};
43
use semaphore::Semaphore;
54
use std::{net::SocketAddr, panic, sync::Arc, time::Duration};
65
use tokio::{
@@ -9,6 +8,7 @@ use tokio::{
98
select,
109
signal::unix::Signal,
1110
};
11+
use tracing::{debug, error, info, warn};
1212

1313
#[allow(clippy::needless_pass_by_value)]
1414
pub async fn server_loop(

src/server/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use crate::server::{db::DatabaseClient, handle::server_loop};
22
use config::Config;
33
use libc::{IPPROTO_TCP, SO_KEEPALIVE, SO_LINGER, SOL_SOCKET, TCP_NODELAY, linger, socklen_t};
4-
use log::{debug, error, info, warn};
54
use std::{ffi::c_int, io, mem, os::fd::AsRawFd, process::exit, sync::Arc};
65
use tokio::{
76
net::TcpListener,
87
signal::unix::{Signal, SignalKind, signal},
98
};
9+
use tracing::{debug, error, info, warn};
1010

1111
mod client;
1212
mod client_handle;

0 commit comments

Comments
 (0)