dmesg: parse since/until option values.

This commit is contained in:
Fuad Ismail 2024-12-11 11:32:45 +07:00
parent 99693490b0
commit fb77a356ca
3 changed files with 63 additions and 2 deletions

28
Cargo.lock generated

@ -405,6 +405,12 @@ version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]]
name = "minimal-lexical"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
[[package]]
name = "nix"
version = "0.29.0"
@ -417,6 +423,16 @@ dependencies = [
"libc",
]
[[package]]
name = "nom"
version = "7.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
dependencies = [
"memchr",
"minimal-lexical",
]
[[package]]
name = "ntapi"
version = "0.4.1"
@ -482,6 +498,17 @@ dependencies = [
"unicode-width",
]
[[package]]
name = "parse_datetime"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8720474e3dd4af20cea8716703498b9f3b690f318fa9d9d9e2e38eaf44b96d0"
dependencies = [
"chrono",
"nom",
"regex",
]
[[package]]
name = "phf"
version = "0.11.2"
@ -1008,6 +1035,7 @@ version = "0.0.1"
dependencies = [
"chrono",
"clap",
"parse_datetime",
"regex",
"serde",
"serde_json",

@ -17,6 +17,7 @@ regex = { workspace = true }
serde_json = { workspace = true }
serde = { workspace = true }
chrono = "0.4.38"
parse_datetime = "0.6.0"
[features]
fixed-boot-time = []

@ -105,8 +105,28 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
}
dmesg.level_filters = Some(level_filters);
}
if let Some(_since) = matches.get_one::<String>(options::SINCE) {}
if let Some(_until) = matches.get_one::<String>(options::UNTIL) {}
if let Some(since) = matches.get_one::<String>(options::SINCE) {
let since = remove_enclosing_quotes(since);
if let Ok(since) = parse_datetime::parse_datetime(since) {
dmesg.since_filter = Some(since);
} else {
return Err(USimpleError::new(
1,
format!("invalid time value \"{since}\""),
));
}
}
if let Some(until) = matches.get_one::<String>(options::UNTIL) {
let until = remove_enclosing_quotes(until);
if let Ok(until) = parse_datetime::parse_datetime(until) {
dmesg.until_filter = Some(until);
} else {
return Err(USimpleError::new(
1,
format!("invalid time value \"{until}\""),
));
}
}
dmesg.print()?;
Ok(())
}
@ -183,6 +203,8 @@ struct Dmesg<'a> {
time_format: TimeFormat,
facility_filters: Option<HashSet<Facility>>,
level_filters: Option<HashSet<Level>>,
since_filter: Option<chrono::DateTime<chrono::FixedOffset>>,
until_filter: Option<chrono::DateTime<chrono::FixedOffset>>,
}
impl Dmesg<'_> {
@ -193,6 +215,8 @@ impl Dmesg<'_> {
time_format: TimeFormat::Raw,
facility_filters: None,
level_filters: None,
since_filter: None,
until_filter: None,
}
}
@ -458,3 +482,11 @@ impl TryFrom<u32> for Facility {
}
}
}
fn remove_enclosing_quotes(value: &str) -> &str {
if value.starts_with('"') && value.ends_with('"') {
&value[1..value.len() - 1]
} else {
value
}
}