ostree_ext/
logging.rs

1use std::collections::HashMap;
2use std::sync::atomic::{AtomicBool, Ordering};
3
4/// Set to true if we failed to write to the journal once
5static EMITTED_JOURNAL_ERROR: AtomicBool = AtomicBool::new(false);
6
7/// Wrapper for systemd structured logging which only emits a message
8/// if we're targeting the system repository, and it's booted.
9pub(crate) fn system_repo_journal_send<K, V>(
10    repo: &ostree::Repo,
11    priority: libsystemd::logging::Priority,
12    msg: &str,
13    vars: impl Iterator<Item = (K, V)>,
14) where
15    K: AsRef<str>,
16    V: AsRef<str>,
17{
18    if !libsystemd::daemon::booted() {
19        return;
20    }
21    if !repo.is_system() {
22        return;
23    }
24    if let Err(e) = libsystemd::logging::journal_send(priority, msg, vars) {
25        if !EMITTED_JOURNAL_ERROR.swap(true, Ordering::SeqCst) {
26            eprintln!("failed to write to journal: {e}");
27        }
28    }
29}
30
31/// Wrapper for systemd structured logging which only emits a message
32/// if we're targeting the system repository, and it's booted.
33pub(crate) fn system_repo_journal_print(
34    repo: &ostree::Repo,
35    priority: libsystemd::logging::Priority,
36    msg: &str,
37) {
38    let vars: HashMap<&str, &str> = HashMap::new();
39    system_repo_journal_send(repo, priority, msg, vars.into_iter())
40}