Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
systemd: Add module for logging to the journal
Lorenz Leutgeb committed 9 months ago
commit c7f3c47f9adf967f2886d066cfd0fb9e607864eb
parent 2776e003dc2f7e585c205cfa966ffb90f2c43ff5
4 files changed +46 -1
modified Cargo.lock
@@ -2782,6 +2782,10 @@ dependencies = [
[[package]]
name = "radicle-systemd"
version = "0.9.0"
+
dependencies = [
+
 "log",
+
 "systemd-journal-logger",
+
]

[[package]]
name = "radicle-term"
@@ -3433,6 +3437,16 @@ dependencies = [
]

[[package]]
+
name = "systemd-journal-logger"
+
version = "2.2.2"
+
source = "registry+https://github.com/rust-lang/crates.io-index"
+
checksum = "7266304d24ca5a4b230545fc558c80e18bd3e1d2eb1be149b6bcd04398d3e79c"
+
dependencies = [
+
 "log",
+
 "rustix 1.0.7",
+
]
+

+
[[package]]
name = "tar"
version = "0.4.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
modified crates/radicle-systemd/Cargo.toml
@@ -8,6 +8,11 @@ edition.workspace = true
version = "0.9.0"
rust-version.workspace = true

+
[dependencies]
+
log = { workspace = true, optional = true }
+
systemd-journal-logger = { version = "2.2.2", optional = true }
+

[features]
-
default = ["listen"]
+
default = ["journal", "listen"]
+
journal = ["dep:log", "dep:systemd-journal-logger"]
listen = []

\ No newline at end of file
added crates/radicle-systemd/src/journal.rs
@@ -0,0 +1,23 @@
+
use systemd_journal_logger::{connected_to_journal, current_exe_identifier, JournalLog};
+

+
/// If the current process is directly connected to the systemd journal,
+
/// return a logger that will write to it.
+
pub fn logger<K, V, I>(
+
    default_identifier: String,
+
    extra_fields: I,
+
) -> std::io::Result<Option<Box<dyn log::Log>>>
+
where
+
    I: IntoIterator<Item = (K, V)>,
+
    K: AsRef<str>,
+
    V: AsRef<[u8]>,
+
{
+
    if !connected_to_journal() {
+
        return Ok(None);
+
    }
+

+
    Ok(Some(Box::new(
+
        JournalLog::new()?
+
            .with_syslog_identifier(current_exe_identifier().unwrap_or(default_identifier))
+
            .with_extra_fields(extra_fields),
+
    )))
+
}
modified crates/radicle-systemd/src/lib.rs
@@ -1,4 +1,7 @@
//! Library for interaction with systemd, specialized for Radicle.

+
#[cfg(feature = "journal")]
+
pub mod journal;
+

#[cfg(feature = "listen")]
pub mod listen;