feat: record-daemon logs to a file

This commit is contained in:
2026-05-30 10:41:40 +02:00
parent 7f54e959b8
commit 598c5a2391
5 changed files with 108 additions and 7 deletions
+39
View File
@@ -314,6 +314,20 @@ fn find_daemon_binary() -> Option<PathBuf> {
None
}
/// Get the log file path for the daemon.
///
/// Returns a path in the app's data directory.
fn get_log_file_path() -> Result<PathBuf, String> {
let data_dir = directories::ProjectDirs::from("com", "leaguerecorder", "record-daemon")
.ok_or_else(|| "Failed to get app data directory".to_string())?;
let log_dir = data_dir.data_dir().join("logs");
std::fs::create_dir_all(&log_dir)
.map_err(|e| format!("Failed to create log directory: {}", e))?;
Ok(log_dir.join("daemon.log"))
}
/// Spawn the record-daemon as a detached background process.
///
/// Returns `Ok(())` if the process was successfully spawned.
@@ -321,10 +335,13 @@ fn spawn_daemon() -> Result<(), String> {
let binary = find_daemon_binary()
.ok_or_else(|| "record-daemon binary not found".to_string())?;
let log_file = get_log_file_path()?;
eprintln!("[daemon] Spawning record-daemon: {}", binary.display());
eprintln!("[daemon] Log file: {}", log_file.display());
let mut cmd = std::process::Command::new(&binary);
cmd.arg("--foreground");
cmd.arg("--log-file").arg(&log_file);
// Set the working directory to the daemon binary's directory so it can
// find its bundled resources (OBS plugins, etc.) relative to itself.
@@ -536,3 +553,25 @@ pub async fn daemon_shutdown() -> Result<serde_json::Value, String> {
Err(response.error.unwrap_or_else(|| "Unknown error".to_string()))
}
}
/// Get the daemon log file path.
#[tauri::command]
pub fn daemon_get_log_path() -> Result<String, String> {
get_log_file_path()
.map(|p| p.to_string_lossy().to_string())
}
/// Read the daemon log file.
///
/// Returns the last `lines` lines of the log file (default 100, max 1000).
#[tauri::command]
pub fn daemon_read_logs(lines: Option<usize>) -> Result<String, String> {
let log_path = get_log_file_path()?;
let max_lines = lines.unwrap_or(100).min(1000);
let content = std::fs::read_to_string(&log_path)
.map_err(|e| format!("Failed to read log file: {}", e))?;
let lines: Vec<&str> = content.lines().rev().take(max_lines).collect();
Ok(lines.into_iter().rev().collect::<Vec<_>>().join("\n"))
}
+2
View File
@@ -439,6 +439,8 @@ pub fn run() {
daemon_ipc::daemon_start_recording,
daemon_ipc::daemon_stop_recording,
daemon_ipc::daemon_shutdown,
daemon_ipc::daemon_get_log_path,
daemon_ipc::daemon_read_logs,
])
.run(tauri::generate_context!())
.expect("error while running tauri application");