diff --git a/Cargo.lock b/Cargo.lock index b74d3d1..b165ddd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -844,6 +844,7 @@ dependencies = [ "tokio-util", "toml", "tracing", + "tracing-journald", "tracing-subscriber", "users", "uucore", @@ -1219,6 +1220,17 @@ dependencies = [ "valuable", ] +[[package]] +name = "tracing-journald" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d3a81ed245bfb62592b1e2bc153e77656d94ee6a0497683a65a12ccaf2438d0" +dependencies = [ + "libc", + "tracing-core", + "tracing-subscriber", +] + [[package]] name = "tracing-log" version = "0.2.0" diff --git a/Cargo.toml b/Cargo.toml index 4f732c1..617ca82 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,6 +37,7 @@ itertools = "0.14.0" tokio-util = "0.7.18" caps = "0.5.6" users = { version = "0.11.0", default-features = false } +tracing-journald = "0.3.2" [features] default = ["systemd"] diff --git a/nix/module.nix b/nix/module.nix index 79975de..0b829ca 100644 --- a/nix/module.nix +++ b/nix/module.nix @@ -11,6 +11,12 @@ in { type = lib.types.submodule { freeformType = format.type; options = { + log_level = lib.mkOption { + type = lib.types.enum [ "info" "debug" "trace" ]; + default = "info"; + description = "Log level for the roowho2 daemon."; + }; + rwhod = { enable = lib.mkEnableOption "the rwhod service" // { default = true; @@ -30,12 +36,6 @@ in { default = { }; description = "Configuration settings for Roowho2."; }; - - logLevel = lib.mkOption { - type = lib.types.enum [ "quiet" "info" "debug" "trace" ]; - default = "info"; - description = "Log level for the roowho2 daemon."; - }; }; config = lib.mkIf cfg.enable { @@ -61,7 +61,6 @@ in { }; systemd.services.roowho2 = { - environment.RUST_LOG = cfg.logLevel; serviceConfig = { Type = "notify"; ExecStart = "${lib.getExe' cfg.package "roowhod"} --config ${format.generate "roowho2-config.toml" cfg.settings}"; diff --git a/nix/vm.nix b/nix/vm.nix index c3cb7e0..aecf309 100644 --- a/nix/vm.nix +++ b/nix/vm.nix @@ -48,7 +48,7 @@ nixpkgs.lib.nixosSystem { services.roowho2 = { enable = true; - logLevel = "trace"; + settings.log_level = "trace"; }; programs.vim = { diff --git a/src/bin/roowhod.rs b/src/bin/roowhod.rs index d9ad977..dbec2e3 100644 --- a/src/bin/roowhod.rs +++ b/src/bin/roowhod.rs @@ -9,10 +9,11 @@ use anyhow::Context; use clap::Parser; use tokio::{net::UdpSocket, sync::RwLock}; use tokio_util::sync::CancellationToken; -use tracing_subscriber::{EnvFilter, fmt, layer::SubscriberExt, util::SubscriberInitExt}; +use tracing::level_filters::LevelFilter; +use tracing_subscriber::layer::SubscriberExt; use roowho2_lib::server::{ - config::DEFAULT_CONFIG_PATH, + config::{DEFAULT_CONFIG_PATH, LogLevel}, rwhod::{RwhodStatusStore, rwhod_packet_receiver_task, rwhod_packet_sender_task}, varlink_api::varlink_client_server_task, }; @@ -38,11 +39,6 @@ struct Args { async fn main() -> anyhow::Result<()> { let args = Args::parse(); - tracing_subscriber::registry() - .with(fmt::layer()) - .with(EnvFilter::from_default_env()) - .init(); - let config = toml::from_str::( &std::fs::read_to_string(&args.config_path).context(format!( "Failed to read configuration file {:?}", @@ -50,6 +46,19 @@ async fn main() -> anyhow::Result<()> { ))?, )?; + let log_filter = match config.log_level.unwrap_or(LogLevel::Info) { + LogLevel::Info => LevelFilter::INFO, + LogLevel::Debug => LevelFilter::DEBUG, + LogLevel::Trace => LevelFilter::TRACE, + }; + + let subscriber = tracing_subscriber::registry() + .with(log_filter) + .with(tracing_journald::layer()?); + + tracing::subscriber::set_global_default(subscriber) + .context("Failed to set global default tracing subscriber")?; + let fd_map: HashMap = HashMap::from_iter(sd_notify::listen_fds_with_names()?.map(|(fd_num, name)| { ( diff --git a/src/server/config.rs b/src/server/config.rs index 1201305..3953702 100644 --- a/src/server/config.rs +++ b/src/server/config.rs @@ -8,6 +8,9 @@ pub const DEFAULT_CLIENT_SOCKET_PATH: &str = "/run/roowho2/server_client.sock"; #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct Config { + /// Logging level for the daemon. + pub log_level: Option, + /// Configuration for the rwhod server. pub rwhod: RwhodConfig, @@ -17,6 +20,14 @@ pub struct Config { pub client_socket_path: Option, } +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum LogLevel { + Info, + Debug, + Trace, +} + #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct RwhodConfig { /// Enable or disable the rwhod server functionality.