Skip to content

Commit

Permalink
Now deserializing loglevel at config and added default
Browse files Browse the repository at this point in the history
  • Loading branch information
cikzh authored and davidv1992 committed Oct 26, 2023
1 parent 7aefe0f commit afae15f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
26 changes: 21 additions & 5 deletions statime-linux/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fs::read_to_string, os::unix::fs::PermissionsExt, path::Path};
use std::{fs::read_to_string, os::unix::fs::PermissionsExt, path::Path, str::FromStr};

use log::warn;
use serde::{Deserialize, Deserializer};
Expand All @@ -8,7 +8,11 @@ use timestamped_socket::interface::InterfaceName;
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct Config {
pub loglevel: String,
#[serde(
default = "default_loglevel",
deserialize_with = "deserialize_loglevel"
)]
pub loglevel: log::LevelFilter,
#[serde(default = "default_sdo_id")]
pub sdo_id: u16,
#[serde(default = "default_domain")]
Expand Down Expand Up @@ -46,6 +50,16 @@ pub struct PortConfig {
pub delay_mechanism: i8,
}

fn deserialize_loglevel<'de, D>(deserializer: D) -> Result<log::LevelFilter, D::Error>
where
D: Deserializer<'de>,
{
use serde::de::Error;
let raw: &str = Deserialize::deserialize(deserializer)?;
log::LevelFilter::from_str(raw)
.map_err(|e| D::Error::custom(format!("Invalid loglevel: {}", e)))
}

fn deserialize_acceptable_master_list<'de, D>(
deserializer: D,
) -> Result<Option<Vec<ClockIdentity>>, D::Error>
Expand Down Expand Up @@ -149,6 +163,10 @@ impl std::fmt::Display for ConfigError {

impl std::error::Error for ConfigError {}

fn default_loglevel() -> log::LevelFilter {
log::LevelFilter::Info
}

fn default_domain() -> u8 {
0
}
Expand Down Expand Up @@ -195,8 +213,6 @@ mod tests {
#[test]
fn minimal_config() {
const MINIMAL_CONFIG: &str = r#"
loglevel = "info" # Other values include trace, debug, warn and error
[[port]]
interface = "enp0s31f6"
"#;
Expand All @@ -215,7 +231,7 @@ interface = "enp0s31f6"
};

let expected = crate::config::Config {
loglevel: "info".to_string(),
loglevel: log::LevelFilter::Info,
sdo_id: 0x000,
domain: 0,
identity: None,
Expand Down
4 changes: 1 addition & 3 deletions statime-linux/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::{
future::Future,
path::PathBuf,
pin::{pin, Pin},
str::FromStr,
};

use clap::Parser;
Expand Down Expand Up @@ -258,8 +257,7 @@ async fn actual_main() {
)
.unwrap_or_else(|e| panic!("error loading config: {e}"));

let log_level = log::LevelFilter::from_str(&config.loglevel).unwrap();
setup_logger(log_level).expect("could not setup logging");
setup_logger(config.loglevel).expect("could not setup logging");

let clock_identity = config.identity.unwrap_or(ClockIdentity(
get_clock_id().expect("could not get clock identity"),
Expand Down

0 comments on commit afae15f

Please sign in to comment.