diff options
author | Tomasz Kramkowski <tomasz@kramkow.ski> | 2025-07-10 00:20:08 +0100 |
---|---|---|
committer | Tomasz Kramkowski <tomasz@kramkow.ski> | 2025-07-10 00:20:08 +0100 |
commit | 03e50ca399b44a26fefafe325ea40fa453b7d6ea (patch) | |
tree | 7efefb4eafdbff5adfb42b5013d2ea9fa6f06f31 /src/config.rs | |
parent | b37432b1f435f88e18eb7779fecfc33a6260cef3 (diff) | |
download | mqttr-03e50ca399b44a26fefafe325ea40fa453b7d6ea.tar.gz mqttr-03e50ca399b44a26fefafe325ea40fa453b7d6ea.tar.xz mqttr-03e50ca399b44a26fefafe325ea40fa453b7d6ea.zip |
Configurable logging
Would be good to maybe log more things, and maybe play around with log
levels. But this is okay for now.
stderrlog probably will go at some point, too many unnecessary transient
dependencies
Diffstat (limited to 'src/config.rs')
-rw-r--r-- | src/config.rs | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs index 2ba10ec..c110825 100644 --- a/src/config.rs +++ b/src/config.rs @@ -7,6 +7,7 @@ use std::{ }; use anyhow::bail; +use log::LevelFilter; use rumqttc::{AsyncClient, EventLoop, MqttOptions, QoS}; use serde::{ de::{self, Visitor}, @@ -15,6 +16,23 @@ use serde::{ use crate::PROGRAM; +#[derive(Deserialize, Debug, PartialEq)] +pub struct Logging { + #[serde(default = "default_level_filter")] + pub level: LevelFilter, + #[serde(default)] // Off + pub timestamps: bool, +} + +impl Default for Logging { + fn default() -> Self { + Self { + level: default_level_filter(), + timestamps: bool::default(), + } + } +} + #[derive(Deserialize, Debug)] pub struct Credentials { pub username: String, @@ -41,6 +59,10 @@ fn default_timeout() -> Duration { Duration::from_secs(60) } +fn default_level_filter() -> LevelFilter { + LevelFilter::Info +} + #[allow(clippy::enum_variant_names)] #[derive(Deserialize, Debug)] #[serde(remote = "QoS", rename_all = "kebab-case")] @@ -248,6 +270,8 @@ pub struct Config { pub qos: QoS, #[serde(default = "default_timeout", deserialize_with = "deserialize_timeout")] pub timeout: Duration, + #[serde(default)] + pub log: Logging, pub credentials: Option<Credentials>, #[serde(default = "default_id")] pub id: String, @@ -317,6 +341,10 @@ mod tests { username = "testuser" password = "testpassword" + [log] + level = "trace" + timestamps = true + [routes] "topic/map" = { programs = [ ["/bin/program1"], @@ -341,6 +369,9 @@ mod tests { assert_eq!(creds.username, "testuser"); assert_eq!(creds.password, "testpassword"); + assert_eq!(config.log.level, LevelFilter::Trace); + assert_eq!(config.log.timestamps, true); + assert_eq!(config.routes.len(), 2); let route_map = config.routes.get("topic/map").unwrap(); @@ -377,6 +408,7 @@ mod tests { assert_eq!(config.id, default_id()); assert_eq!(config.timeout, default_timeout()); assert!(config.credentials.is_none()); + assert_eq!(config.log, Logging::default()); assert!(config.routes.is_empty()); } |