feat: file logging on client, logs command to fetch last N lines

This commit is contained in:
Helios Agent 2026-03-03 17:09:23 +01:00
parent 23bbb5b603
commit db3fa9f416
No known key found for this signature in database
GPG key ID: C8259547CD8309B5
6 changed files with 130 additions and 9 deletions

View file

@ -0,0 +1,59 @@
/// File logger — writes structured log lines alongside the pretty terminal output.
use std::fs::{File, OpenOptions};
use std::io::Write;
use std::sync::{Mutex, OnceLock};
static LOG_FILE: OnceLock<Mutex<File>> = OnceLock::new();
static LOG_PATH: OnceLock<String> = OnceLock::new();
pub fn init() {
let path = log_path();
// Create parent dir if needed
if let Some(parent) = std::path::Path::new(&path).parent() {
let _ = std::fs::create_dir_all(parent);
}
match OpenOptions::new().create(true).append(true).open(&path) {
Ok(f) => {
LOG_PATH.set(path.clone()).ok();
LOG_FILE.set(Mutex::new(f)).ok();
write_line("INFO", "helios-remote started");
}
Err(e) => eprintln!("[logger] Failed to open log file {path}: {e}"),
}
}
fn log_path() -> String {
#[cfg(windows)]
{
let base = std::env::var("LOCALAPPDATA")
.unwrap_or_else(|_| "C:\\Temp".to_string());
format!("{base}\\helios-remote\\helios-remote.log")
}
#[cfg(not(windows))]
{
"/tmp/helios-remote.log".to_string()
}
}
pub fn get_log_path() -> String {
LOG_PATH.get().cloned().unwrap_or_else(log_path)
}
pub fn write_line(level: &str, msg: &str) {
let now = chrono::Local::now().format("%Y-%m-%d %H:%M:%S");
let line = format!("{now} [{level:<5}] {msg}\n");
if let Some(mutex) = LOG_FILE.get() {
if let Ok(mut f) = mutex.lock() {
let _ = f.write_all(line.as_bytes());
}
}
}
/// Read the last `n` lines from the log file.
pub fn tail(n: u32) -> String {
let path = get_log_path();
let content = std::fs::read_to_string(&path).unwrap_or_default();
let lines: Vec<&str> = content.lines().collect();
let start = lines.len().saturating_sub(n as usize);
lines[start..].join("\n")
}

View file

@ -13,6 +13,7 @@ use base64::Engine;
use helios_common::{ClientMessage, ServerMessage};
use uuid::Uuid;
mod logger;
mod shell;
mod screenshot;
mod input;
@ -76,21 +77,27 @@ macro_rules! log_status {
}
macro_rules! log_ok {
($($arg:tt)*) => {
println!(" {}\t{}", "", format!($($arg)*));
};
($($arg:tt)*) => {{
let msg = format!($($arg)*);
println!(" {}\t{}", "", msg);
logger::write_line("OK", &msg);
}};
}
macro_rules! log_err {
($($arg:tt)*) => {
println!(" {}\t{}", "", format!($($arg)*));
};
($($arg:tt)*) => {{
let msg = format!($($arg)*);
println!(" {}\t{}", "", msg);
logger::write_line("ERROR", &msg);
}};
}
macro_rules! log_cmd {
($emoji:expr, $($arg:tt)*) => {
println!(" {}\t{}", $emoji, format!($($arg)*));
};
($emoji:expr, $($arg:tt)*) => {{
let msg = format!($($arg)*);
println!(" {}\t{}", $emoji, msg);
logger::write_line("CMD", &msg);
}};
}
// ────────────────────────────────────────────────────────────────────────────
@ -173,6 +180,7 @@ async fn main() {
// Enable ANSI color codes on Windows (required when running as admin)
#[cfg(windows)]
enable_ansi();
logger::init();
// Suppress tracing output by default
if std::env::var("RUST_LOG").is_err() {
@ -558,6 +566,13 @@ async fn handle_message(
}
}
ServerMessage::LogsRequest { request_id, lines } => {
log_cmd!("📜", "logs (last {lines} lines)");
let content = logger::tail(lines);
let log_path = logger::get_log_path();
ClientMessage::LogsResponse { request_id, content, log_path }
}
ServerMessage::UploadRequest { request_id, path, content_base64 } => {
log_cmd!("", "upload → {}", path);
match (|| -> Result<(), String> {