dmesg: change Record timestamp_us data type to i64.

This commit is contained in:
Fuad Ismail
2024-11-27 16:05:31 +07:00
parent c4944f4c85
commit dff0a8ea0b
3 changed files with 11 additions and 14 deletions

View File

@@ -158,13 +158,10 @@ impl Dmesg<'_> {
for record in records { for record in records {
match self.time_format { match self.time_format {
TimeFormat::Delta => { TimeFormat::Delta => {
print!("[{}] ", delta_formatter.format(record.timestamp_us as i64)) print!("[{}] ", delta_formatter.format(record.timestamp_us))
} }
TimeFormat::Reltime => { TimeFormat::Reltime => {
print!( print!("[{}] ", reltime_formatter.format(record.timestamp_us))
"[{}] ",
reltime_formatter.format(record.timestamp_us as i64)
)
} }
TimeFormat::Ctime => { TimeFormat::Ctime => {
print!("[{}] ", time_formatter::ctime(record.timestamp_us)) print!("[{}] ", time_formatter::ctime(record.timestamp_us))
@@ -200,7 +197,7 @@ enum TimeFormat {
struct Record { struct Record {
priority_facility: u32, priority_facility: u32,
_sequence: u64, _sequence: u64,
timestamp_us: u64, timestamp_us: i64,
message: String, message: String,
} }

View File

@@ -23,7 +23,7 @@ struct Dmesg<'a> {
#[derive(serde::Serialize)] #[derive(serde::Serialize)]
struct Record<'a> { struct Record<'a> {
pri: u32, pri: u32,
time: u64, time: i64,
msg: &'a str, msg: &'a str,
} }
@@ -129,11 +129,11 @@ impl serde_json::ser::Formatter for DmesgFormatter {
} }
} }
fn write_u64<W>(&mut self, writer: &mut W, value: u64) -> io::Result<()> fn write_i64<W>(&mut self, writer: &mut W, value: i64) -> io::Result<()>
where where
W: ?Sized + io::Write, W: ?Sized + io::Write,
{ {
// The only u64 field in Dmesg is time, which requires a specific format // The only i64 field in Dmesg is time, which requires a specific format
let seconds = value / 1000000; let seconds = value / 1000000;
let sub_seconds = value % 1000000; let sub_seconds = value % 1000000;
let repr = format!("{:>5}.{:0>6}", seconds, sub_seconds); let repr = format!("{:>5}.{:0>6}", seconds, sub_seconds);

View File

@@ -3,22 +3,22 @@ use chrono::{DateTime, FixedOffset, TimeDelta};
use chrono::{NaiveDate, NaiveTime}; use chrono::{NaiveDate, NaiveTime};
use std::sync::OnceLock; use std::sync::OnceLock;
pub fn raw(timestamp_us: u64) -> String { pub fn raw(timestamp_us: i64) -> String {
let seconds = timestamp_us / 1000000; let seconds = timestamp_us / 1000000;
let sub_seconds = timestamp_us % 1000000; let sub_seconds = timestamp_us % 1000000;
format!("{:>5}.{:0>6}", seconds, sub_seconds) format!("{:>5}.{:0>6}", seconds, sub_seconds)
} }
pub fn ctime(timestamp_us: u64) -> String { pub fn ctime(timestamp_us: i64) -> String {
let date_time = boot_time() let date_time = boot_time()
.checked_add_signed(TimeDelta::microseconds(timestamp_us as i64)) .checked_add_signed(TimeDelta::microseconds(timestamp_us))
.unwrap(); .unwrap();
date_time.format("%a %b %d %H:%M:%S %Y").to_string() date_time.format("%a %b %d %H:%M:%S %Y").to_string()
} }
pub fn iso(timestamp_us: u64) -> String { pub fn iso(timestamp_us: i64) -> String {
let date_time = boot_time() let date_time = boot_time()
.checked_add_signed(TimeDelta::microseconds(timestamp_us as i64)) .checked_add_signed(TimeDelta::microseconds(timestamp_us))
.unwrap(); .unwrap();
date_time.format("%Y-%m-%dT%H:%M:%S,%6f%:z").to_string() date_time.format("%Y-%m-%dT%H:%M:%S,%6f%:z").to_string()
} }