From edd8fa5b5581186a088b724f09fd007f24dbc869 Mon Sep 17 00:00:00 2001 From: Hikari Date: Sat, 7 Feb 2026 13:34:43 -0800 Subject: [PATCH] refactor: remove Discord RPC file logging Removes file-based logging from Discord RPC manager in favour of using the tracing framework exclusively. All Discord RPC logs now appear in the Debug Console with proper log levels. Changes: - Remove log_path field and file logging methods from DiscordRpcManager - Replace all self.log() calls with tracing macros (debug/info/error) - Remove log_discord_rpc command and its registration - Remove set_app_handle() call from main setup Benefits: - Reduces disk usage (no unbounded log file growth) - Eliminates maintenance burden of managing log files - Better log levels and integration with existing tracing system Closes: #129 --- src-tauri/src/commands.rs | 9 --- src-tauri/src/discord_rpc.rs | 110 +++++++++++------------------------ src-tauri/src/lib.rs | 4 -- 3 files changed, 35 insertions(+), 88 deletions(-) diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index 8dd6082..41ea7b7 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -1145,15 +1145,6 @@ pub async fn stop_discord_rpc( discord_rpc.stop() } -#[tauri::command] -pub async fn log_discord_rpc( - discord_rpc: State<'_, std::sync::Arc>, - message: String, -) -> Result<(), String> { - discord_rpc.log(&message); - Ok(()) -} - #[tauri::command] pub async fn close_application(app_handle: AppHandle) -> Result<(), String> { // Get the main window diff --git a/src-tauri/src/discord_rpc.rs b/src-tauri/src/discord_rpc.rs index ffdfa3c..a3b6bda 100644 --- a/src-tauri/src/discord_rpc.rs +++ b/src-tauri/src/discord_rpc.rs @@ -1,18 +1,13 @@ use discord_rich_presence::activity::{Activity, Assets, Timestamps}; use discord_rich_presence::{DiscordIpc, DiscordIpcClient}; use parking_lot::RwLock; -use std::fs::OpenOptions; -use std::io::Write; -use std::path::PathBuf; use std::sync::Arc; -use tauri::{AppHandle, Manager}; pub struct DiscordRpcManager { client: Arc>>, session_name: Arc>, model: Arc>, started_at: Arc>, - log_path: Arc>>, } impl DiscordRpcManager { @@ -22,82 +17,47 @@ impl DiscordRpcManager { session_name: Arc::new(RwLock::new(String::new())), model: Arc::new(RwLock::new(String::new())), started_at: Arc::new(RwLock::new(0)), - log_path: Arc::new(RwLock::new(None)), - } - } - - pub fn set_app_handle(&self, app_handle: &AppHandle) { - if let Ok(app_data_dir) = app_handle.path().app_data_dir() { - // Ensure the directory exists - if let Err(e) = std::fs::create_dir_all(&app_data_dir) { - tracing::error!("Failed to create app data directory: {}", e); - return; - } - let log_path = app_data_dir.join("hikari_discord_rpc.log"); - *self.log_path.write() = Some(log_path.clone()); - self.log(&format!( - "Log file initialised at: {}", - log_path.display() - )); - } - } - - pub fn log(&self, message: &str) { - let log_path_guard = self.log_path.read(); - let path = match log_path_guard.as_ref() { - Some(p) => p.clone(), - None => PathBuf::from("hikari_discord_rpc.log"), - }; - drop(log_path_guard); - - if let Ok(mut file) = OpenOptions::new() - .create(true) - .append(true) - .open(&path) - { - let timestamp = chrono::Local::now().format("%Y-%m-%d %H:%M:%S"); - let _ = writeln!(file, "[{}] {}", timestamp, message); } } pub fn init(&self, initial_session_name: String, initial_model: String, started_at: i64) -> Result<(), String> { - self.log("Attempting to initialize Discord RPC..."); - self.log("DEBUG: Application ID: 1391117878182281316"); - self.log(&format!("DEBUG: Initial session: '{}', model: '{}', timestamp: {}", - initial_session_name, initial_model, started_at)); + tracing::debug!("Attempting to initialize Discord RPC..."); + tracing::debug!("Application ID: 1391117878182281316"); + tracing::debug!("Initial session: '{}', model: '{}', timestamp: {}", + initial_session_name, initial_model, started_at); let mut client = DiscordIpcClient::new("1391117878182281316") .map_err(|e| { let error_msg = format!("Failed to create Discord RPC client: {} (is Discord running?)", e); - self.log(&format!("ERROR: {}", error_msg)); + tracing::error!("{}", error_msg); error_msg })?; - self.log("DEBUG: DiscordIpcClient created successfully"); + tracing::debug!("DiscordIpcClient created successfully"); client .connect() .map_err(|e| { let error_msg = format!("Failed to connect to Discord RPC: {} (ensure Discord is running)", e); - self.log(&format!("ERROR: {}", error_msg)); + tracing::error!("{}", error_msg); error_msg })?; - self.log("DEBUG: Connected to Discord IPC socket"); + tracing::debug!("Connected to Discord IPC socket"); // Set initial activity immediately after connecting - self.log("DEBUG: Building initial activity..."); + tracing::debug!("Building initial activity..."); let state_text = format!("Model: {}", initial_model); let assets = Assets::new() .large_image("hikari") .large_text("Hikari - Claude Code Assistant"); - self.log("DEBUG: Assets created - large_image: 'hikari', large_text: 'Hikari - Claude Code Assistant'"); + tracing::debug!("Assets created - large_image: 'hikari', large_text: 'Hikari - Claude Code Assistant'"); let timestamps = Timestamps::new() .start(started_at); - self.log(&format!("DEBUG: Timestamps created - start: {}", started_at)); + tracing::debug!("Timestamps created - start: {}", started_at); let activity = Activity::new() .details(initial_session_name.as_str()) @@ -105,19 +65,19 @@ impl DiscordRpcManager { .assets(assets) .timestamps(timestamps); - self.log(&format!("DEBUG: Activity created - details: '{}', state: '{}'", - initial_session_name, state_text)); + tracing::debug!("Activity created - details: '{}', state: '{}'", + initial_session_name, state_text); - self.log("DEBUG: Attempting to set initial activity..."); + tracing::debug!("Attempting to set initial activity..."); client .set_activity(activity) .map_err(|e| { let error_msg = format!("Failed to set initial Discord RPC activity: {}", e); - self.log(&format!("ERROR: {}", error_msg)); + tracing::error!("{}", error_msg); error_msg })?; - self.log("DEBUG: Initial activity set successfully!"); + tracing::debug!("Initial activity set successfully!"); // Store the client and initial state *self.client.write() = Some(client); @@ -125,8 +85,8 @@ impl DiscordRpcManager { *self.model.write() = initial_model.clone(); *self.started_at.write() = started_at; - self.log(&format!("Discord RPC connected successfully with initial activity: session='{}', model='{}'", - initial_session_name, initial_model)); + tracing::info!("Discord RPC connected successfully with initial activity: session='{}', model='{}'", + initial_session_name, initial_model); Ok(()) } @@ -136,37 +96,37 @@ impl DiscordRpcManager { model: String, started_at: i64, ) -> Result<(), String> { - self.log(&format!("DEBUG: update() called with session='{}', model='{}', timestamp={}", - session_name, model, started_at)); + tracing::debug!("update() called with session='{}', model='{}', timestamp={}", + session_name, model, started_at); *self.session_name.write() = session_name.clone(); *self.model.write() = model.clone(); *self.started_at.write() = started_at; - self.log("DEBUG: State variables updated"); + tracing::debug!("State variables updated"); let mut client_guard = self.client.write(); let client = client_guard .as_mut() .ok_or_else(|| { let error_msg = "Discord RPC client not initialized".to_string(); - self.log(&format!("ERROR: {}", error_msg)); + tracing::error!("{}", error_msg); error_msg })?; - self.log("DEBUG: Client lock acquired"); + tracing::debug!("Client lock acquired"); let state_text = format!("Model: {}", model); let assets = Assets::new() .large_image("hikari") .large_text("Hikari - Claude Code Assistant"); - self.log("DEBUG: Assets created - large_image: 'hikari', large_text: 'Hikari - Claude Code Assistant'"); + tracing::debug!("Assets created - large_image: 'hikari', large_text: 'Hikari - Claude Code Assistant'"); let timestamps = Timestamps::new() .start(started_at); - self.log(&format!("DEBUG: Timestamps created - start: {}", started_at)); + tracing::debug!("Timestamps created - start: {}", started_at); let activity = Activity::new() .details(session_name.as_str()) @@ -174,38 +134,38 @@ impl DiscordRpcManager { .assets(assets) .timestamps(timestamps); - self.log(&format!("DEBUG: Activity created - details: '{}', state: '{}'", - session_name, state_text)); + tracing::debug!("Activity created - details: '{}', state: '{}'", + session_name, state_text); - self.log("DEBUG: Attempting to set activity..."); + tracing::debug!("Attempting to set activity..."); client .set_activity(activity) .map_err(|e| { let error_msg = format!("Failed to update Discord RPC: {}", e); - self.log(&format!("ERROR: {}", error_msg)); + tracing::error!("{}", error_msg); error_msg })?; - self.log(&format!("Updated Discord RPC: session='{}', model='{}'", session_name, model)); + tracing::info!("Updated Discord RPC: session='{}', model='{}'", session_name, model); Ok(()) } pub fn stop(&self) -> Result<(), String> { - self.log("DEBUG: stop() called"); + tracing::debug!("stop() called"); let mut client_guard = self.client.write(); if let Some(mut client) = client_guard.take() { - self.log("DEBUG: Client found, attempting to close..."); + tracing::debug!("Client found, attempting to close..."); client .close() .map_err(|e| { let error_msg = format!("Failed to close Discord RPC: {}", e); - self.log(&format!("ERROR: {}", error_msg)); + tracing::error!("{}", error_msg); error_msg })?; - self.log("Discord RPC stopped successfully"); + tracing::info!("Discord RPC stopped successfully"); } else { - self.log("DEBUG: No client to stop (already stopped or never initialized)"); + tracing::debug!("No client to stop (already stopped or never initialized)"); } Ok(()) } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 1f7d9a3..8f31c0b 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -73,9 +73,6 @@ pub fn run() { // Initialize the app handle in the bridge manager bridge_manager.lock().set_app_handle(app.handle().clone()); - // Initialize the app handle in the Discord RPC manager for logging - discord_rpc.set_app_handle(app.handle()); - // Clean up any orphaned temp files from previous sessions if let Ok(count) = temp_manager.lock().cleanup_orphaned_files() { if count > 0 { @@ -195,7 +192,6 @@ pub fn run() { init_discord_rpc, update_discord_rpc, stop_discord_rpc, - log_discord_rpc, close_application, list_memory_files, ])