Add relative timestamp to syslog messages

This commit is contained in:
pjht 2024-11-28 14:06:11 -06:00
parent f4ad6d177a
commit 262b6f74a4
Signed by: pjht
GPG Key ID: CA239FC6934E6F3A

View File

@ -9,21 +9,24 @@ use std::os::mikros::syscalls::{get_initrd, new_process};
use std::os::mikros::{ipc, syscalls};
use std::path::Path;
use std::process::Command;
use std::time::{Duration, Instant};
use syslog_msg_ipc::Message;
use tar_no_std::TarArchiveRef;
struct SyslogMessageHandler {
registered_devices: Vec<String>,
waiting_text_msgs: Vec<Message>,
waiting_text_msgs: Vec<(Duration, Message)>,
msg_output_file: Option<File>,
start_time: Instant,
}
impl SyslogMessageHandler {
fn new() -> Self {
fn new(start_time: Instant) -> Self {
Self {
registered_devices: Vec::new(),
waiting_text_msgs: Vec::new(),
msg_output_file: None,
start_time,
}
}
@ -50,17 +53,18 @@ impl SyslogMessageHandler {
);
}
} else if let Some(text) = msg.text.as_ref() {
let dur = Instant::now() - self.start_time;
if let Some(file) = self.msg_output_file.as_mut() {
writeln!(file, "[{}] {}", msg.from, text).unwrap();
writeln!(file, "[{} {:?}] {}", msg.from, dur, text).unwrap();
} else {
self.waiting_text_msgs.push(msg.clone());
self.waiting_text_msgs.push((dur, msg.clone()));
}
}
}
fn set_output(&mut self, mut file: File) {
for msg in &self.waiting_text_msgs {
writeln!(file, "[{}] {}", msg.from, msg.text.as_ref().unwrap()).unwrap();
for (dur, msg) in &self.waiting_text_msgs {
writeln!(file, "[{} {:?}] {}", msg.from, dur, msg.text.as_ref().unwrap()).unwrap();
}
self.msg_output_file = Some(file);
self.waiting_text_msgs.clear();
@ -68,6 +72,7 @@ impl SyslogMessageHandler {
}
fn main() {
let start_time = Instant::now();
syslog_msg_ipc::register_callback();
let initrd = TarArchiveRef::new(get_initrd()).unwrap();
let syslog_pid = run_initrd_proc(&initrd, "bin/syslog");
@ -76,7 +81,7 @@ fn main() {
syslog_client.subscribe_to_binary("devfs".to_string(), vec![0, 1]);
syslog_client.subscribe_to_binary("tarfs".to_string(), vec![0]);
syslog_client.subscribe_to_binary("tmpfs".to_string(), vec![0]);
let mut msg_handler = SyslogMessageHandler::new();
let mut msg_handler = SyslogMessageHandler::new(start_time);
let proc_man_pid = run_initrd_proc(&initrd, "bin/proc_man");
let proc_man_client = proc_man_rpc::Client::new(proc_man_pid);
proc_man_client.new_proc(0, None).unwrap();