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