diff --git a/Cargo.toml b/Cargo.toml index 530f6c6..198feb8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ error-chain = { version = "^0.12.1", default-features = false } hmac = "0.7" libc = "0.2" nix = "0.15" +sdjournal = { git = "https://github.com/jabedude/sdjournal-rs" } serde = { version = "^1.0.91", features = ["derive"] } sha2 = "0.8" uuid = { version = "^0.8.1", features = ["serde"] } diff --git a/src/journal.rs b/src/journal.rs new file mode 100644 index 0000000..3d31400 --- /dev/null +++ b/src/journal.rs @@ -0,0 +1,57 @@ +use std::fs::File; +use std::path::Path; + +use sdjournal::journal::*; +use sdjournal::iter::EntryIter; + +use crate::errors::*; + +#[derive(Debug)] +pub struct SdJournal { + inner: Journal, +} + +impl SdJournal { + pub fn open_journal>(path: P) -> Result { + let file = File::open(path)?; + + let journal = Journal::new(file)?; + + let sdjournal = SdJournal { + inner: journal, + }; + + Ok(sdjournal) + } + + pub fn iter(&self) -> EntryIter { + self.inner.iter_entries() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_sdjournal_read_simple() { + let sd = SdJournal::open_journal("./tests/user-1000.journal"); + assert!(sd.is_ok()); + } + + #[test] + fn test_sdjournal_iter_simple() { + let sd = SdJournal::open_journal("./tests/user-1000.journal").unwrap(); + + let mut counter = 0; + + let iter = sd.iter(); + for _obj in iter { + counter += 1; + //eprintln!("obj: {}", obj.realtime); + } + + // journalctl --header --file tests/user-1000.journal | grep "Entry Objects" == 645 + assert_eq!(counter, 645); + } +} diff --git a/src/lib.rs b/src/lib.rs index 504d489..23e76e7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,6 +27,9 @@ pub mod activation; /// Interfaces for systemd-aware daemons. pub mod daemon; +/// Interfaces for reading from the system journal. +pub mod journal; + /// Error handling. pub mod errors; diff --git a/tests/user-1000.journal b/tests/user-1000.journal new file mode 100644 index 0000000..1082308 Binary files /dev/null and b/tests/user-1000.journal differ