diff --git a/Cargo.lock b/Cargo.lock index 0bac9fa..707b366 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7,6 +7,7 @@ name = "acmed" version = "0.25.0-dev" dependencies = [ "clap", + "syslog-tracing", "tokio", "tracing", "tracing-subscriber", @@ -268,6 +269,17 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "syslog-tracing" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d349bc2df408b4bf656709a29643641cef7f1795d708f88b105c626a8f64f6e4" +dependencies = [ + "libc", + "tracing-core", + "tracing-subscriber", +] + [[package]] name = "thread_local" version = "1.1.8" @@ -305,6 +317,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c" dependencies = [ "once_cell", + "valuable", ] [[package]] @@ -331,6 +344,12 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index d2b7544..c23753d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,6 +23,7 @@ ed448 = [] [dependencies] clap = { version = "4.5.23", default-features = false, features = ["color", "derive", "help", "std"] } +syslog-tracing = { version = "0.3.1", default-features = false } tokio = { version = "1.42.0", default-features = false, features = ["rt", "rt-multi-thread"] } tracing = { version = "0.1.41", default-features = false, features = ["std"] } tracing-subscriber = { version = "0.3.19", default-features = false, features = ["ansi", "fmt", "std"] } diff --git a/src/log.rs b/src/log.rs index 466cd12..45bfe50 100644 --- a/src/log.rs +++ b/src/log.rs @@ -23,8 +23,17 @@ impl Level { } pub fn init(level: Level, is_syslog: bool) { - let subscriber = FmtSubscriber::builder() - .with_max_level(level.tracing()) - .finish(); - tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed"); + if is_syslog { + let identity = std::ffi::CStr::from_bytes_with_nul(crate::APP_IDENTITY).unwrap(); + let (options, facility) = Default::default(); + let syslog = syslog_tracing::Syslog::new(identity, options, facility) + .expect("building syslog subscriber failed"); + tracing_subscriber::fmt().with_writer(syslog).init(); + } else { + let subscriber = FmtSubscriber::builder() + .with_max_level(level.tracing()) + .finish(); + tracing::subscriber::set_global_default(subscriber) + .expect("setting default subscriber failed"); + } } diff --git a/src/main.rs b/src/main.rs index fc2af2f..df53e3e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ mod log; use clap::Parser; +pub const APP_IDENTITY: &[u8] = b"acmed\0"; pub const APP_THREAD_NAME: &str = "acmed-runtime"; pub const DEFAULT_CONFIG_PATH: &str = "/etc/acmed/acmed.toml"; pub const DEFAULT_LOG_LEVEL: log::Level = log::Level::Warn;