From ec17af924767a2ae3c5180d9ea25193d071e5661 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Mon, 17 Jun 2024 12:01:22 +0200 Subject: Closes #20. Use chrono and seperate timers in logging --- Cargo.lock | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 +- README.md | 1 - src/config.rs | 1 - src/main.rs | 27 +++++++++++---------- 5 files changed, 91 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0654c77..dcdec98 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -26,6 +26,21 @@ dependencies = [ "memchr", ] +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "arbitrary" version = "1.3.2" @@ -203,6 +218,18 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" +[[package]] +name = "chrono" +version = "0.4.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "num-traits", + "windows-targets 0.52.5", +] + [[package]] name = "color-eyre" version = "0.6.3" @@ -279,6 +306,12 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "core-foundation-sys" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" + [[package]] name = "cpufeatures" version = "0.2.12" @@ -654,6 +687,29 @@ dependencies = [ "tracing", ] +[[package]] +name = "iana-time-zone" +version = "0.1.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + [[package]] name = "icu_collections" version = "1.5.0" @@ -1010,6 +1066,15 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + [[package]] name = "num_cpus" version = "1.16.0" @@ -2048,6 +2113,7 @@ version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" dependencies = [ + "chrono", "matchers", "nu-ansi-term", "once_cell", @@ -2385,6 +2451,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets 0.52.5", +] + [[package]] name = "windows-sys" version = "0.48.0" diff --git a/Cargo.toml b/Cargo.toml index 7b10baa..c633f57 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ edition = "2021" axum = { version = "0.7", features = ["ws"] } tokio = { version = "1.38", features = ["macros", "rt-multi-thread"] } tracing = "0.1" -tracing-subscriber = { version = "0.3", features = ["env-filter", "local-time", "time"] } +tracing-subscriber = { version = "0.3", features = ["env-filter", "local-time", "chrono"] } time = { version = "0.3", features = ["macros"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" diff --git a/README.md b/README.md index 8c2c8c6..790f9c2 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,6 @@ Default `config.toml`: serveraddr = "0.0.0.0:7229" # String pingtimeout = 10 # i64 pingthreshold = 1 # u64 -timeoffset = 0 # i8 [auth] method = "none" # "none"|"key" diff --git a/src/config.rs b/src/config.rs index 7c91c08..18c3bab 100644 --- a/src/config.rs +++ b/src/config.rs @@ -8,7 +8,6 @@ pub struct Config { pub serveraddr: String, pub pingtimeout: i64, pub pingthreshold: u64, - pub timeoffset: i8, pub auth: Auth, } diff --git a/src/main.rs b/src/main.rs index 204c318..b550dd8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,8 @@ use crate::{ - config::Config, routes::{device, start, status}, services::ping::{BroadcastCommand, StatusMap}, storage::Device + config::Config, + routes::{device, start, status}, + services::ping::{BroadcastCommand, StatusMap}, + storage::Device, }; use axum::{ middleware::from_fn_with_state, @@ -8,14 +11,9 @@ use axum::{ }; use dashmap::DashMap; use std::{env, sync::Arc}; -use time::UtcOffset; use tokio::sync::broadcast::{channel, Sender}; use tracing::{info, level_filters::LevelFilter, trace}; -use tracing_subscriber::{ - fmt::{self, time::OffsetTime}, - prelude::*, - EnvFilter, -}; +use tracing_subscriber::{fmt, prelude::*, EnvFilter}; use utoipa::{ openapi::security::{ApiKey, ApiKeyValue, SecurityScheme}, Modify, OpenApi, @@ -24,10 +22,10 @@ use utoipa_swagger_ui::SwaggerUi; mod auth; mod config; -mod storage; mod error; mod routes; mod services; +mod storage; mod wol; #[derive(OpenApi)] @@ -73,15 +71,20 @@ async fn main() -> color_eyre::eyre::Result<()> { let config = Config::load()?; - let time_format = - time::macros::format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"); - let time = OffsetTime::new(UtcOffset::from_hms(config.timeoffset, 0, 0)?, time_format); + let writer_time = + tracing_subscriber::fmt::time::ChronoLocal::new("%Y-%m-%d %H:%M:%S%.6f%:z".to_string()); + let time = tracing_subscriber::fmt::time::ChronoLocal::new("%Y-%m-%d %H:%M:%S%:z".to_string()); let file_appender = tracing_appender::rolling::daily("logs", "webol.log"); let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender); tracing_subscriber::registry() - .with(fmt::layer().with_writer(non_blocking).with_ansi(false)) + .with( + fmt::layer() + .with_timer(writer_time) + .with_writer(non_blocking) + .with_ansi(false), + ) .with(fmt::layer().with_timer(time)) .with( EnvFilter::builder() -- cgit v1.2.3