dmesg: support normal print with raw time format.

This commit is contained in:
Fuad Ismail 2024-11-25 13:03:54 +07:00
parent 7dc7924915
commit c893226fe8
2 changed files with 31 additions and 1 deletions

@ -12,6 +12,7 @@ use uucore::{
};
mod json;
mod time_formatter;
const ABOUT: &str = help_about!("dmesg.md");
const USAGE: &str = help_usage!("dmesg.md");
@ -69,6 +70,7 @@ mod options {
struct Dmesg<'a> {
kmsg_file: &'a str,
output_format: OutputFormat,
time_format: TimeFormat,
records: Option<Vec<Record>>,
}
@ -77,6 +79,7 @@ impl Dmesg<'_> {
Dmesg {
kmsg_file: "/dev/kmsg",
output_format: OutputFormat::Normal,
time_format: TimeFormat::Raw,
records: None,
}
}
@ -122,7 +125,7 @@ impl Dmesg<'_> {
fn print(&self) {
match self.output_format {
OutputFormat::Json => self.print_json(),
OutputFormat::Normal => unimplemented!(),
OutputFormat::Normal => self.print_normal(),
}
}
@ -131,6 +134,20 @@ impl Dmesg<'_> {
println!("{}", json::serialize_records(records));
}
}
fn print_normal(&self) {
if let Some(records) = &self.records {
for record in records {
match self.time_format {
TimeFormat::Raw => {
print!("[{}] ", time_formatter::raw(record.timestamp_us))
}
_ => unimplemented!(),
}
println!("{}", record.message);
}
}
}
}
enum OutputFormat {
@ -138,6 +155,14 @@ enum OutputFormat {
Json,
}
enum TimeFormat {
Delta,
Reltime,
Ctime,
Iso,
Raw,
}
struct Record {
priority_facility: u32,
_sequence: u64,

@ -0,0 +1,5 @@
pub fn raw(timestamp_us: u64) -> String {
let seconds = timestamp_us / 1000000;
let sub_seconds = timestamp_us % 1000000;
format!("{:>5}.{:0>6}", seconds, sub_seconds)
}