From: Mira Limbeck <m.limbeck@proxmox.com>
To: pmg-devel@lists.proxmox.com
Subject: [pmg-devel] [PATCH log-tracker 1/2] add support for bookworm syslog time format
Date: Mon, 26 Jun 2023 12:41:41 +0200 [thread overview]
Message-ID: <20230626104142.519755-1-m.limbeck@proxmox.com> (raw)
Adds `proxmox-time` as dependency to parse the timestamp
Since parse_rfc3339 can't handle microseconds, we try to remove the dot
followed by 6 digits of microseconds before passing it to parse_rfc3339.
A fallback to the previous format is used when when it fails to parse
the new format.
Signed-off-by: Mira Limbeck <m.limbeck@proxmox.com>
---
Cargo.toml | 1 +
src/main.rs | 36 ++++++++++++++++++++++++++++++++++++
2 files changed, 37 insertions(+)
diff --git a/Cargo.toml b/Cargo.toml
index b484137..6d92253 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -14,3 +14,4 @@ anyhow = "1"
clap = { version = "3.2.23", features = ["cargo"] }
flate2 = "1.0"
libc = "0.2"
+proxmox-time = "1.1"
diff --git a/src/main.rs b/src/main.rs
index 20d8404..e7bffd8 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2236,6 +2236,42 @@ fn parse_number(data: &[u8], max_digits: usize) -> Option<(usize, &[u8])> {
/// Parse time. Returns a tuple of (parsed_time, remaining_text) or None.
fn parse_time(data: &'_ [u8], cur_year: i64, cur_month: i64) -> Option<(time_t, &'_ [u8])> {
+ parse_time_with_year(data).or_else(|| parse_time_no_year(data, cur_year, cur_month))
+}
+
+fn parse_time_with_year(data: &'_ [u8]) -> Option<(time_t, &'_ [u8])> {
+ let mut timestamp_buffer = [0u8; 25];
+
+ let count = data.iter().take_while(|b| **b != b' ').count();
+ if count != 27 && count != 32 {
+ return None;
+ }
+ let (timestamp, data) = data.split_at(count);
+ // remove whitespace
+ let data = &data[1..];
+
+ // microseconds: .123456 -> 7 bytes
+ let microseconds_idx = timestamp.iter().take_while(|b| **b != b'.').count();
+
+ // YYYY-MM-DDTHH:MM:SS
+ let year_time = ×tamp[0..microseconds_idx];
+ let year_time_len = year_time.len();
+ // Z | +HH:MM | -HH:MM
+ let timezone = ×tamp[microseconds_idx + 7..];
+ let timezone_len = timezone.len();
+ let timestamp_len = year_time_len + timezone_len;
+ timestamp_buffer[0..year_time_len].copy_from_slice(year_time);
+ timestamp_buffer[year_time_len..timestamp_len].copy_from_slice(timezone);
+
+ match proxmox_time::parse_rfc3339(unsafe {
+ std::str::from_utf8_unchecked(×tamp_buffer[0..timestamp_len])
+ }) {
+ Ok(ltime) => Some((ltime, data)),
+ Err(_err) => None,
+ }
+}
+
+fn parse_time_no_year(data: &'_ [u8], cur_year: i64, cur_month: i64) -> Option<(time_t, &'_ [u8])> {
if data.len() < 15 {
return None;
}
--
2.30.2
next reply other threads:[~2023-06-26 10:41 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-26 10:41 Mira Limbeck [this message]
2023-06-26 10:41 ` [pmg-devel] [PATCH log-tracker 2/2] tests: add rfc3339 timestamp test Mira Limbeck
2023-06-26 13:58 ` [pmg-devel] [PATCH log-tracker 1/2] add support for bookworm syslog time format Dominik Csapak
2023-06-26 20:00 ` [pmg-devel] applied: " Thomas Lamprecht
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230626104142.519755-1-m.limbeck@proxmox.com \
--to=m.limbeck@proxmox.com \
--cc=pmg-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox