Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for reading from the system journal #40

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
57 changes: 57 additions & 0 deletions src/journal.rs
Original file line number Diff line number Diff line change
@@ -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<File>,
}

impl SdJournal {
pub fn open_journal<P: AsRef<Path>>(path: P) -> Result<Self> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is a bit of API mismatch here. To the best of my knowledge, the C API never works on a single file in isolation. I think the closest to this is sd_journal_open_files, which would take a slice of paths instead.

Perhaps we should start with an implementation of sd_journal_open_files_fd instead, and build the other ones of top of that?

let file = File::open(path)?;

let journal = Journal::new(file)?;

let sdjournal = SdJournal {
inner: journal,
};

Ok(sdjournal)
}

pub fn iter(&self) -> EntryIter<File> {
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();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this file a bit smaller, just containing a few entry objects?


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);
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Binary file added tests/user-1000.journal
Binary file not shown.