feat: --tail-logs can track log rollovers and incoporates a sleep timer to minimize idle CPU cycles
This commit is contained in:
+33
-8
@@ -1,9 +1,11 @@
|
|||||||
use crate::config::paths;
|
use crate::config::paths;
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
use fancy_regex::Regex;
|
use fancy_regex::Regex;
|
||||||
use std::fs::File;
|
use std::fs::{self, File};
|
||||||
use std::io::{BufRead, BufReader, Seek, SeekFrom};
|
use std::io::{BufRead, BufReader, Seek, SeekFrom};
|
||||||
use std::process;
|
use std::process;
|
||||||
|
use std::time::Duration;
|
||||||
|
use tokio::time::sleep;
|
||||||
|
|
||||||
pub async fn tail_logs(no_color: bool) {
|
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();
|
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,20 +18,43 @@ pub async fn tail_logs(no_color: bool) {
|
|||||||
process::exit(1);
|
process::exit(1);
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut lines = reader.lines();
|
let mut line_buf = String::new();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
if let Some(Ok(line)) = lines.next() {
|
match reader.read_line(&mut line_buf) {
|
||||||
if no_color {
|
Ok(0) => {
|
||||||
println!("{line}");
|
if file_was_rotated(&file_path, &mut reader) {
|
||||||
} else {
|
let file = File::open(&file_path).expect("Cannot open file");
|
||||||
let colored_line = colorize_log_line(&line, &re);
|
reader = BufReader::new(file);
|
||||||
println!("{colored_line}");
|
}
|
||||||
|
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);
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn colorize_log_line(line: &str, re: &Regex) -> String {
|
fn colorize_log_line(line: &str, re: &Regex) -> String {
|
||||||
if let Some(caps) = re.captures(line).expect("Failed to capture log line") {
|
if let Some(caps) = re.captures(line).expect("Failed to capture log line") {
|
||||||
let level = &caps["level"];
|
let level = &caps["level"];
|
||||||
|
|||||||
Reference in New Issue
Block a user