|
| 1 | +// This file is part of the uutils util-linux package. |
| 2 | +// |
| 3 | +// For the full copyright and license information, please view the LICENSE |
| 4 | +// file that was distributed with this source code. |
| 5 | + |
| 6 | +use clap::{crate_version, Arg, ArgAction, Command}; |
| 7 | +use regex::Regex; |
| 8 | +use std::fs; |
| 9 | +use uucore::{ |
| 10 | + error::{FromIo, UResult, USimpleError}, |
| 11 | + format_usage, help_about, help_usage, |
| 12 | +}; |
| 13 | + |
| 14 | +mod json; |
| 15 | + |
| 16 | +const ABOUT: &str = help_about!("dmesg.md"); |
| 17 | +const USAGE: &str = help_usage!("dmesg.md"); |
| 18 | + |
| 19 | +#[uucore::main] |
| 20 | +pub fn uumain(args: impl uucore::Args) -> UResult<()> { |
| 21 | + let mut dmesg = Dmesg::new(); |
| 22 | + let matches: clap::ArgMatches = uu_app().try_get_matches_from(args)?; |
| 23 | + if let Some(kmsg_file) = matches.get_one::<String>(options::KMSG_FILE) { |
| 24 | + dmesg.kmsg_file = kmsg_file; |
| 25 | + } |
| 26 | + if matches.get_flag(options::JSON) { |
| 27 | + dmesg.output_format = OutputFormat::Json; |
| 28 | + } |
| 29 | + dmesg.parse()?.print(); |
| 30 | + Ok(()) |
| 31 | +} |
| 32 | + |
| 33 | +pub fn uu_app() -> Command { |
| 34 | + Command::new(uucore::util_name()) |
| 35 | + .override_usage(format_usage(USAGE)) |
| 36 | + .about(ABOUT) |
| 37 | + .version(crate_version!()) |
| 38 | + .arg( |
| 39 | + Arg::new(options::KMSG_FILE) |
| 40 | + .short('K') |
| 41 | + .long("kmsg-file") |
| 42 | + .help("use the file in kmsg format") |
| 43 | + .action(ArgAction::Set), |
| 44 | + ) |
| 45 | + .arg( |
| 46 | + Arg::new(options::JSON) |
| 47 | + .short('J') |
| 48 | + .long("json") |
| 49 | + .help("use JSON output format") |
| 50 | + .action(ArgAction::SetTrue), |
| 51 | + ) |
| 52 | +} |
| 53 | + |
| 54 | +mod options { |
| 55 | + pub const KMSG_FILE: &str = "kmsg-file"; |
| 56 | + pub const JSON: &str = "json"; |
| 57 | +} |
| 58 | + |
| 59 | +struct Dmesg<'a> { |
| 60 | + kmsg_file: &'a str, |
| 61 | + output_format: OutputFormat, |
| 62 | + records: Option<Vec<Record>>, |
| 63 | +} |
| 64 | + |
| 65 | +impl Dmesg<'_> { |
| 66 | + fn new() -> Self { |
| 67 | + Dmesg { |
| 68 | + kmsg_file: "/dev/kmsg", |
| 69 | + output_format: OutputFormat::Normal, |
| 70 | + records: None, |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + fn parse(mut self) -> UResult<Self> { |
| 75 | + let mut records = vec![]; |
| 76 | + let re = Self::record_regex(); |
| 77 | + let lines = self.read_lines_from_kmsg_file()?; |
| 78 | + for line in lines { |
| 79 | + for (_, [pri_fac, seq, time, msg]) in re.captures_iter(&line).map(|c| c.extract()) { |
| 80 | + records.push(Record::from_str_fields( |
| 81 | + pri_fac, |
| 82 | + seq, |
| 83 | + time, |
| 84 | + msg.to_string(), |
| 85 | + )?); |
| 86 | + } |
| 87 | + } |
| 88 | + self.records = Some(records); |
| 89 | + Ok(self) |
| 90 | + } |
| 91 | + |
| 92 | + fn record_regex() -> Regex { |
| 93 | + let valid_number_pattern = "0|[1-9][0-9]*"; |
| 94 | + let additional_fields_pattern = ",^[,;]*"; |
| 95 | + let record_pattern = format!( |
| 96 | + "(?m)^({0}),({0}),({0}),.(?:{1})*;(.*)$", |
| 97 | + valid_number_pattern, additional_fields_pattern |
| 98 | + ); |
| 99 | + Regex::new(&record_pattern).expect("invalid regex.") |
| 100 | + } |
| 101 | + |
| 102 | + fn read_lines_from_kmsg_file(&self) -> UResult<Vec<String>> { |
| 103 | + let kmsg_bytes = fs::read(self.kmsg_file) |
| 104 | + .map_err_context(|| format!("cannot open {}", self.kmsg_file))?; |
| 105 | + let lines = kmsg_bytes |
| 106 | + .split(|&byte| byte == 0) |
| 107 | + .map(|line| String::from_utf8_lossy(line).to_string()) |
| 108 | + .collect(); |
| 109 | + Ok(lines) |
| 110 | + } |
| 111 | + |
| 112 | + fn print(&self) { |
| 113 | + match self.output_format { |
| 114 | + OutputFormat::Json => self.print_json(), |
| 115 | + OutputFormat::Normal => unimplemented!(), |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + fn print_json(&self) { |
| 120 | + if let Some(records) = &self.records { |
| 121 | + println!("{}", json::serialize_records(records)); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +enum OutputFormat { |
| 127 | + Normal, |
| 128 | + Json, |
| 129 | +} |
| 130 | + |
| 131 | +struct Record { |
| 132 | + priority_facility: u32, |
| 133 | + _sequence: u64, |
| 134 | + timestamp_us: u64, |
| 135 | + message: String, |
| 136 | +} |
| 137 | + |
| 138 | +impl Record { |
| 139 | + fn from_str_fields(pri_fac: &str, seq: &str, time: &str, msg: String) -> UResult<Record> { |
| 140 | + let pri_fac = str::parse(pri_fac); |
| 141 | + let seq = str::parse(seq); |
| 142 | + let time = str::parse(time); |
| 143 | + match (pri_fac, seq, time) { |
| 144 | + (Ok(pri_fac), Ok(seq), Ok(time)) => Ok(Record { |
| 145 | + priority_facility: pri_fac, |
| 146 | + _sequence: seq, |
| 147 | + timestamp_us: time, |
| 148 | + message: msg, |
| 149 | + }), |
| 150 | + _ => Err(USimpleError::new(1, "Failed to parse record field(s)")), |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments