bin/roowhod: configure loglevel via config file, use journald protocol

This commit is contained in:
2026-04-29 06:03:11 +09:00
parent 448ce6e500
commit a3aae2b28b
6 changed files with 47 additions and 15 deletions
Generated
+12
View File
@@ -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"
+1
View File
@@ -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"]
+6 -7
View File
@@ -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}";
+1 -1
View File
@@ -48,7 +48,7 @@ nixpkgs.lib.nixosSystem {
services.roowho2 = {
enable = true;
logLevel = "trace";
settings.log_level = "trace";
};
programs.vim = {
+16 -7
View File
@@ -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::<roowho2_lib::server::config::Config>(
&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<String, OwnedFd> =
HashMap::from_iter(sd_notify::listen_fds_with_names()?.map(|(fd_num, name)| {
(
+11
View File
@@ -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<LogLevel>,
/// Configuration for the rwhod server.
pub rwhod: RwhodConfig,
@@ -17,6 +20,14 @@ pub struct Config {
pub client_socket_path: Option<String>,
}
#[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.