fix: tail-logs subcommand follows log rollovers and sleeps to minimize idle CPU loops
This commit is contained in:
+35
-8
@@ -104,23 +104,50 @@ pub async fn tail_logs(no_color: bool) -> Result<()> {
|
|||||||
.seek(SeekFrom::End(0))
|
.seek(SeekFrom::End(0))
|
||||||
.with_context(|| "Unable to tail log file")?;
|
.with_context(|| "Unable to tail log file")?;
|
||||||
|
|
||||||
let mut lines = reader.lines();
|
|
||||||
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
loop {
|
loop {
|
||||||
if let Some(Ok(line)) = lines.next() {
|
let mut line_buf = String::new();
|
||||||
if no_color {
|
match reader.read_line(&mut line_buf) {
|
||||||
println!("{line}");
|
Ok(0) => {
|
||||||
} else {
|
if was_log_rotated(&file_path, &mut reader) {
|
||||||
let colored_line = colorize_log_line(&line, &re);
|
continue;
|
||||||
println!("{colored_line}");
|
}
|
||||||
|
|
||||||
|
tokio::time::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}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
line_buf.clear();
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn was_log_rotated(file_path: &PathBuf, reader: &mut BufReader<File>) -> bool {
|
||||||
|
let current_pos = reader.stream_position().unwrap_or(0);
|
||||||
|
let file_len = fs::metadata(file_path).map(|m| m.len()).unwrap_or(0);
|
||||||
|
|
||||||
|
if file_len < current_pos
|
||||||
|
&& let Ok(new_file) = File::open(file_path)
|
||||||
|
{
|
||||||
|
*reader = BufReader::new(new_file);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
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) {
|
if let Some(caps) = re.captures(line) {
|
||||||
let level = &caps["level"];
|
let level = &caps["level"];
|
||||||
|
|||||||
Reference in New Issue
Block a user