dmesg: implement since/until filter.

This commit is contained in:
Fuad Ismail 2024-12-11 14:48:45 +07:00
parent c7ff9cd553
commit a35c00b0e4
2 changed files with 36 additions and 1 deletions

@ -3,6 +3,7 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
use chrono::{DateTime, FixedOffset};
use clap::{crate_version, Arg, ArgAction, Command};
use regex::Regex;
use std::{
@ -265,7 +266,9 @@ impl Dmesg<'_> {
Ok(self
.try_iter()?
.filter(Self::is_record_in_set(&self.facility_filters))
.filter(Self::is_record_in_set(&self.level_filters)))
.filter(Self::is_record_in_set(&self.level_filters))
.filter(Self::is_record_since(&self.since_filter))
.filter(Self::is_record_until(&self.until_filter)))
}
fn try_iter(&self) -> UResult<RecordIterator> {
@ -289,6 +292,32 @@ impl Dmesg<'_> {
_ => true,
}
}
fn is_record_since(
since: &Option<DateTime<FixedOffset>>,
) -> impl Fn(&UResult<Record>) -> bool + '_ {
move |record: &UResult<Record>| match (record, since) {
(Ok(record), Some(since)) => {
let time =
time_formatter::datetime_from_microseconds_since_boot(record.timestamp_us);
time >= *since
}
_ => true,
}
}
fn is_record_until(
until: &Option<DateTime<FixedOffset>>,
) -> impl Fn(&UResult<Record>) -> bool + '_ {
move |record: &UResult<Record>| match (record, until) {
(Ok(record), Some(until)) => {
let time =
time_formatter::datetime_from_microseconds_since_boot(record.timestamp_us);
time <= *until
}
_ => true,
}
}
}
enum OutputFormat {

@ -116,6 +116,12 @@ impl DeltaFormatter {
}
}
pub fn datetime_from_microseconds_since_boot(microseconds: i64) -> DateTime<FixedOffset> {
boot_time()
.checked_add_signed(TimeDelta::microseconds(microseconds))
.unwrap()
}
static BOOT_TIME: OnceLock<DateTime<FixedOffset>> = OnceLock::new();
#[cfg(feature = "fixed-boot-time")]