feat: --tail-logs can track log rollovers and incoporates a sleep timer to minimize idle CPU cycles
This commit is contained in:
+29
-4
@@ -1,9 +1,11 @@
|
||||
use crate::config::paths;
|
||||
use colored::Colorize;
|
||||
use fancy_regex::Regex;
|
||||
use std::fs::File;
|
||||
use std::fs::{self, File};
|
||||
use std::io::{BufRead, BufReader, Seek, SeekFrom};
|
||||
use std::process;
|
||||
use std::time::Duration;
|
||||
use tokio::time::sleep;
|
||||
|
||||
pub async fn tail_logs(no_color: bool) {
|
||||
let re = Regex::new(r"^(?P<timestamp>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3})\s+<(?P<opid>[^\s>]+)>\s+\[(?P<level>[A-Z]+)\]\s+(?P<logger>[^:]+):(?P<line>\d+)\s+-\s+(?P<message>.*)$").unwrap();
|
||||
@@ -16,17 +18,40 @@ pub async fn tail_logs(no_color: bool) {
|
||||
process::exit(1);
|
||||
};
|
||||
|
||||
let mut lines = reader.lines();
|
||||
let mut line_buf = String::new();
|
||||
|
||||
loop {
|
||||
if let Some(Ok(line)) = lines.next() {
|
||||
match reader.read_line(&mut line_buf) {
|
||||
Ok(0) => {
|
||||
if file_was_rotated(&file_path, &mut reader) {
|
||||
let file = File::open(&file_path).expect("Cannot open file");
|
||||
reader = BufReader::new(file);
|
||||
}
|
||||
sleep(Duration::from_millis(100)).await;
|
||||
}
|
||||
Ok(_) => {
|
||||
let line = line_buf.trim_end();
|
||||
if no_color {
|
||||
println!("{line}");
|
||||
} else {
|
||||
let colored_line = colorize_log_line(&line, &re);
|
||||
let colored_line = colorize_log_line(line, &re);
|
||||
println!("{colored_line}");
|
||||
}
|
||||
line_buf.clear();
|
||||
}
|
||||
Err(_) => {
|
||||
line_buf.clear();
|
||||
sleep(Duration::from_millis(100)).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn file_was_rotated(path: &std::path::Path, reader: &mut BufReader<File>) -> bool {
|
||||
let current_pos = reader.stream_position().unwrap_or(0);
|
||||
match fs::metadata(path) {
|
||||
Ok(metadata) => metadata.len() < current_pos,
|
||||
Err(_) => true,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user