Files
skyg-linux-tracker/examples/journal_tail.rs

56 lines
1.9 KiB
Rust

//! Usage: journal_tail [--unit X] [--priority err] [--lines N] [--follow]
use skyg_linux_tracker::config::Config;
use skyg_linux_tracker::sources::journal::SdJournal;
use skyg_linux_tracker::sources::{JournalQuery, JournalSource, Priority};
use tokio_util::sync::CancellationToken;
#[tokio::main]
async fn main() {
let mut q = JournalQuery::default();
let mut follow = false;
let mut args = std::env::args().skip(1);
while let Some(a) = args.next() {
match a.as_str() {
"--unit" => q.unit = args.next(),
"--priority" => {
let v = args.next().unwrap_or_default();
match v.parse::<Priority>() {
Ok(p) => q.priority = Some(p),
Err(e) => die(&format!("bad --priority {v:?}: {e}")),
}
}
"--lines" => match args.next().and_then(|v| v.parse().ok()) {
Some(n) => q.lines = Some(n),
None => die("--lines needs a number"),
},
"--follow" => follow = true,
other => die(&format!("unknown argument {other:?}")),
}
}
let token = CancellationToken::new();
let src = SdJournal::new(&Config::default(), token.clone());
if follow {
let mut rx = src.follow(&q).await.unwrap_or_else(|e| fail(e));
while let Some(e) = rx.recv().await {
println!("{}", serde_json::to_string(&e).unwrap());
}
} else {
for e in src.query(&q).await.unwrap_or_else(|e| fail(e)) {
println!("{}", serde_json::to_string(&e).unwrap());
}
}
}
fn fail(e: skyg_linux_tracker::error::AppError) -> ! {
die(&format!(
"{}\nhint: reading the full system journal needs the `systemd-journal` group (or root); \
otherwise only your own entries are visible.",
e.message()
))
}
fn die(msg: &str) -> ! {
eprintln!("journal_tail: {msg}");
std::process::exit(2);
}