Skip to content
Merged
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
16 changes: 7 additions & 9 deletions nvx/src/guest/kernel_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ pub(crate) struct SyslogLine<'a> {
}

impl<'a> SyslogLine<'a> {
/// Parses one line. A timestamp is spaces, then decimal seconds with a decimal fraction,
/// between brackets. A line whose brackets hold anything else keeps them in its message.
/// Parses one line. A timestamp is spaces, then decimal seconds with a decimal fraction, each
/// of one or more digits, between brackets, such as the kernel's `[%5lu.%06lu]`. A line whose
/// brackets hold anything else, such as `[.]` or `[5.]`, keeps them in its message.
pub(crate) fn parse(line: &'a [u8]) -> Self {
let line = line
.strip_prefix(b"<")
Expand All @@ -91,22 +92,19 @@ impl<'a> SyslogLine<'a> {
}
}

/// Parses a log timestamp, spaces then `seconds.fraction` in decimal, into microseconds.
/// Parses a log timestamp, spaces then `seconds.fraction`, each of one or more decimal digits,
/// into microseconds.
fn microseconds(stamp: &[u8]) -> Option<u64> {
/// Microseconds in a second.
const PER_SECOND: u64 = 1_000_000;
/// Decimal digits of a microsecond fraction of a second.
const DIGITS: usize = 6;
let stamp = std::str::from_utf8(stamp).ok()?.trim_start_matches(' ');
let (seconds, fraction) = stamp.split_once('.')?;
if !(seconds.bytes().chain(fraction.bytes())).all(|byte| byte.is_ascii_digit()) {
let whole = parse_decimal(seconds.as_bytes())?;
if fraction.is_empty() || !fraction.bytes().all(|byte| byte.is_ascii_digit()) {
return None;
}
let whole = if seconds.is_empty() {
0
} else {
seconds.parse::<u64>().ok()?
};
let micros = fraction
.bytes()
.chain(std::iter::repeat(b'0'))
Expand Down
17 changes: 17 additions & 0 deletions nvx/src/guest/kernel_text/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,23 @@ fn syslog_lines_strip_only_well_formed_timestamps() {
assert_eq!((line.timestamp_us, line.message), (None, &b"plain"[..]));
}

#[test]
fn syslog_brackets_without_seconds_or_fraction_digits_stay_in_the_message() {
for line in [
&b"<6>[.] message"[..],
b"<6>[5.] message",
b"<6>[ .5] message",
] {
let parsed = SyslogLine::parse(line);
assert_eq!(
(parsed.timestamp_us, parsed.message),
(None, &line[b"<6>".len()..]),
"{}",
line.escape_ascii()
);
}
}

#[test]
fn cpu_lists_are_plain_ranges() {
assert_eq!(parse_cpu_list(b"0-3,6"), Some(vec![0..=3, 6..=6]));
Expand Down
Loading