feat(cli): add logs mode with tail/follow and completion support (#337)

Introduce a dedicated --logs mode for CLI log viewing with --tail <lines> and
-f/--follow streaming from the normal/debug log channel, keep user-facing CLI
output free of timestamped log formatting, and update bash/zsh completions for
the new logs flags.
This commit is contained in:
cucadmuh
2026-04-27 20:21:41 +03:00
committed by GitHub
parent 8967ca825d
commit bd2242e4f3
5 changed files with 242 additions and 62 deletions
+20 -1
View File
@@ -1,6 +1,7 @@
#[cfg(unix)]
use libc;
use std::collections::VecDeque;
use std::io::Write;
use std::sync::{Mutex, OnceLock};
use std::sync::atomic::{AtomicU8, Ordering};
@@ -20,6 +21,16 @@ fn log_buffer() -> &'static Mutex<VecDeque<String>> {
LOG_BUFFER.get_or_init(|| Mutex::new(VecDeque::with_capacity(LOG_BUFFER_MAX_LINES)))
}
/// Shared runtime file used by CLI `--tail` to read normal/debug log channel.
pub fn cli_log_channel_path() -> std::path::PathBuf {
if let Ok(dir) = std::env::var("XDG_RUNTIME_DIR") {
if !dir.is_empty() {
return std::path::PathBuf::from(dir).join("psysonic-cli.log");
}
}
std::env::temp_dir().join("psysonic-cli.log")
}
fn parse_logging_mode(mode: &str) -> Option<LoggingMode> {
match mode.trim().to_ascii_lowercase().as_str() {
"off" => Some(LoggingMode::Off),
@@ -57,7 +68,15 @@ pub fn append_log_line(line: String) {
if buf.len() >= LOG_BUFFER_MAX_LINES {
buf.pop_front();
}
buf.push_back(line);
buf.push_back(line.clone());
drop(buf);
let path = cli_log_channel_path();
if let Some(parent) = path.parent() {
let _ = std::fs::create_dir_all(parent);
}
if let Ok(mut f) = std::fs::OpenOptions::new().create(true).append(true).open(path) {
let _ = writeln!(f, "{}", line);
}
}
pub fn export_logs_to_file(path: &str) -> Result<usize, String> {