generated from nhcarrigan/template
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
This commit is contained in:
@@ -1145,15 +1145,6 @@ pub async fn stop_discord_rpc(
|
|||||||
discord_rpc.stop()
|
discord_rpc.stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
|
||||||
pub async fn log_discord_rpc(
|
|
||||||
discord_rpc: State<'_, std::sync::Arc<crate::discord_rpc::DiscordRpcManager>>,
|
|
||||||
message: String,
|
|
||||||
) -> Result<(), String> {
|
|
||||||
discord_rpc.log(&message);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn close_application(app_handle: AppHandle) -> Result<(), String> {
|
pub async fn close_application(app_handle: AppHandle) -> Result<(), String> {
|
||||||
// Get the main window
|
// Get the main window
|
||||||
|
|||||||
@@ -1,18 +1,13 @@
|
|||||||
use discord_rich_presence::activity::{Activity, Assets, Timestamps};
|
use discord_rich_presence::activity::{Activity, Assets, Timestamps};
|
||||||
use discord_rich_presence::{DiscordIpc, DiscordIpcClient};
|
use discord_rich_presence::{DiscordIpc, DiscordIpcClient};
|
||||||
use parking_lot::RwLock;
|
use parking_lot::RwLock;
|
||||||
use std::fs::OpenOptions;
|
|
||||||
use std::io::Write;
|
|
||||||
use std::path::PathBuf;
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tauri::{AppHandle, Manager};
|
|
||||||
|
|
||||||
pub struct DiscordRpcManager {
|
pub struct DiscordRpcManager {
|
||||||
client: Arc<RwLock<Option<DiscordIpcClient>>>,
|
client: Arc<RwLock<Option<DiscordIpcClient>>>,
|
||||||
session_name: Arc<RwLock<String>>,
|
session_name: Arc<RwLock<String>>,
|
||||||
model: Arc<RwLock<String>>,
|
model: Arc<RwLock<String>>,
|
||||||
started_at: Arc<RwLock<i64>>,
|
started_at: Arc<RwLock<i64>>,
|
||||||
log_path: Arc<RwLock<Option<PathBuf>>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DiscordRpcManager {
|
impl DiscordRpcManager {
|
||||||
@@ -22,82 +17,47 @@ impl DiscordRpcManager {
|
|||||||
session_name: Arc::new(RwLock::new(String::new())),
|
session_name: Arc::new(RwLock::new(String::new())),
|
||||||
model: Arc::new(RwLock::new(String::new())),
|
model: Arc::new(RwLock::new(String::new())),
|
||||||
started_at: Arc::new(RwLock::new(0)),
|
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> {
|
pub fn init(&self, initial_session_name: String, initial_model: String, started_at: i64) -> Result<(), String> {
|
||||||
self.log("Attempting to initialize Discord RPC...");
|
tracing::debug!("Attempting to initialize Discord RPC...");
|
||||||
self.log("DEBUG: Application ID: 1391117878182281316");
|
tracing::debug!("Application ID: 1391117878182281316");
|
||||||
self.log(&format!("DEBUG: Initial session: '{}', model: '{}', timestamp: {}",
|
tracing::debug!("Initial session: '{}', model: '{}', timestamp: {}",
|
||||||
initial_session_name, initial_model, started_at));
|
initial_session_name, initial_model, started_at);
|
||||||
|
|
||||||
let mut client = DiscordIpcClient::new("1391117878182281316")
|
let mut client = DiscordIpcClient::new("1391117878182281316")
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
let error_msg = format!("Failed to create Discord RPC client: {} (is Discord running?)", 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
|
error_msg
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
self.log("DEBUG: DiscordIpcClient created successfully");
|
tracing::debug!("DiscordIpcClient created successfully");
|
||||||
|
|
||||||
client
|
client
|
||||||
.connect()
|
.connect()
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
let error_msg = format!("Failed to connect to Discord RPC: {} (ensure Discord is running)", 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
|
error_msg
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
self.log("DEBUG: Connected to Discord IPC socket");
|
tracing::debug!("Connected to Discord IPC socket");
|
||||||
|
|
||||||
// Set initial activity immediately after connecting
|
// 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 state_text = format!("Model: {}", initial_model);
|
||||||
let assets = Assets::new()
|
let assets = Assets::new()
|
||||||
.large_image("hikari")
|
.large_image("hikari")
|
||||||
.large_text("Hikari - Claude Code Assistant");
|
.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()
|
let timestamps = Timestamps::new()
|
||||||
.start(started_at);
|
.start(started_at);
|
||||||
|
|
||||||
self.log(&format!("DEBUG: Timestamps created - start: {}", started_at));
|
tracing::debug!("Timestamps created - start: {}", started_at);
|
||||||
|
|
||||||
let activity = Activity::new()
|
let activity = Activity::new()
|
||||||
.details(initial_session_name.as_str())
|
.details(initial_session_name.as_str())
|
||||||
@@ -105,19 +65,19 @@ impl DiscordRpcManager {
|
|||||||
.assets(assets)
|
.assets(assets)
|
||||||
.timestamps(timestamps);
|
.timestamps(timestamps);
|
||||||
|
|
||||||
self.log(&format!("DEBUG: Activity created - details: '{}', state: '{}'",
|
tracing::debug!("Activity created - details: '{}', state: '{}'",
|
||||||
initial_session_name, state_text));
|
initial_session_name, state_text);
|
||||||
|
|
||||||
self.log("DEBUG: Attempting to set initial activity...");
|
tracing::debug!("Attempting to set initial activity...");
|
||||||
client
|
client
|
||||||
.set_activity(activity)
|
.set_activity(activity)
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
let error_msg = format!("Failed to set initial Discord RPC activity: {}", 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
|
error_msg
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
self.log("DEBUG: Initial activity set successfully!");
|
tracing::debug!("Initial activity set successfully!");
|
||||||
|
|
||||||
// Store the client and initial state
|
// Store the client and initial state
|
||||||
*self.client.write() = Some(client);
|
*self.client.write() = Some(client);
|
||||||
@@ -125,8 +85,8 @@ impl DiscordRpcManager {
|
|||||||
*self.model.write() = initial_model.clone();
|
*self.model.write() = initial_model.clone();
|
||||||
*self.started_at.write() = started_at;
|
*self.started_at.write() = started_at;
|
||||||
|
|
||||||
self.log(&format!("Discord RPC connected successfully with initial activity: session='{}', model='{}'",
|
tracing::info!("Discord RPC connected successfully with initial activity: session='{}', model='{}'",
|
||||||
initial_session_name, initial_model));
|
initial_session_name, initial_model);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,37 +96,37 @@ impl DiscordRpcManager {
|
|||||||
model: String,
|
model: String,
|
||||||
started_at: i64,
|
started_at: i64,
|
||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
self.log(&format!("DEBUG: update() called with session='{}', model='{}', timestamp={}",
|
tracing::debug!("update() called with session='{}', model='{}', timestamp={}",
|
||||||
session_name, model, started_at));
|
session_name, model, started_at);
|
||||||
|
|
||||||
*self.session_name.write() = session_name.clone();
|
*self.session_name.write() = session_name.clone();
|
||||||
*self.model.write() = model.clone();
|
*self.model.write() = model.clone();
|
||||||
*self.started_at.write() = started_at;
|
*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 mut client_guard = self.client.write();
|
||||||
let client = client_guard
|
let client = client_guard
|
||||||
.as_mut()
|
.as_mut()
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
let error_msg = "Discord RPC client not initialized".to_string();
|
let error_msg = "Discord RPC client not initialized".to_string();
|
||||||
self.log(&format!("ERROR: {}", error_msg));
|
tracing::error!("{}", error_msg);
|
||||||
error_msg
|
error_msg
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
self.log("DEBUG: Client lock acquired");
|
tracing::debug!("Client lock acquired");
|
||||||
|
|
||||||
let state_text = format!("Model: {}", model);
|
let state_text = format!("Model: {}", model);
|
||||||
let assets = Assets::new()
|
let assets = Assets::new()
|
||||||
.large_image("hikari")
|
.large_image("hikari")
|
||||||
.large_text("Hikari - Claude Code Assistant");
|
.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()
|
let timestamps = Timestamps::new()
|
||||||
.start(started_at);
|
.start(started_at);
|
||||||
|
|
||||||
self.log(&format!("DEBUG: Timestamps created - start: {}", started_at));
|
tracing::debug!("Timestamps created - start: {}", started_at);
|
||||||
|
|
||||||
let activity = Activity::new()
|
let activity = Activity::new()
|
||||||
.details(session_name.as_str())
|
.details(session_name.as_str())
|
||||||
@@ -174,38 +134,38 @@ impl DiscordRpcManager {
|
|||||||
.assets(assets)
|
.assets(assets)
|
||||||
.timestamps(timestamps);
|
.timestamps(timestamps);
|
||||||
|
|
||||||
self.log(&format!("DEBUG: Activity created - details: '{}', state: '{}'",
|
tracing::debug!("Activity created - details: '{}', state: '{}'",
|
||||||
session_name, state_text));
|
session_name, state_text);
|
||||||
|
|
||||||
self.log("DEBUG: Attempting to set activity...");
|
tracing::debug!("Attempting to set activity...");
|
||||||
client
|
client
|
||||||
.set_activity(activity)
|
.set_activity(activity)
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
let error_msg = format!("Failed to update Discord RPC: {}", e);
|
let error_msg = format!("Failed to update Discord RPC: {}", e);
|
||||||
self.log(&format!("ERROR: {}", error_msg));
|
tracing::error!("{}", error_msg);
|
||||||
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn stop(&self) -> Result<(), String> {
|
pub fn stop(&self) -> Result<(), String> {
|
||||||
self.log("DEBUG: stop() called");
|
tracing::debug!("stop() called");
|
||||||
|
|
||||||
let mut client_guard = self.client.write();
|
let mut client_guard = self.client.write();
|
||||||
if let Some(mut client) = client_guard.take() {
|
if let Some(mut client) = client_guard.take() {
|
||||||
self.log("DEBUG: Client found, attempting to close...");
|
tracing::debug!("Client found, attempting to close...");
|
||||||
client
|
client
|
||||||
.close()
|
.close()
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
let error_msg = format!("Failed to close Discord RPC: {}", e);
|
let error_msg = format!("Failed to close Discord RPC: {}", e);
|
||||||
self.log(&format!("ERROR: {}", error_msg));
|
tracing::error!("{}", error_msg);
|
||||||
error_msg
|
error_msg
|
||||||
})?;
|
})?;
|
||||||
self.log("Discord RPC stopped successfully");
|
tracing::info!("Discord RPC stopped successfully");
|
||||||
} else {
|
} 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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -73,9 +73,6 @@ pub fn run() {
|
|||||||
// Initialize the app handle in the bridge manager
|
// Initialize the app handle in the bridge manager
|
||||||
bridge_manager.lock().set_app_handle(app.handle().clone());
|
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
|
// Clean up any orphaned temp files from previous sessions
|
||||||
if let Ok(count) = temp_manager.lock().cleanup_orphaned_files() {
|
if let Ok(count) = temp_manager.lock().cleanup_orphaned_files() {
|
||||||
if count > 0 {
|
if count > 0 {
|
||||||
@@ -195,7 +192,6 @@ pub fn run() {
|
|||||||
init_discord_rpc,
|
init_discord_rpc,
|
||||||
update_discord_rpc,
|
update_discord_rpc,
|
||||||
stop_discord_rpc,
|
stop_discord_rpc,
|
||||||
log_discord_rpc,
|
|
||||||
close_application,
|
close_application,
|
||||||
list_memory_files,
|
list_memory_files,
|
||||||
])
|
])
|
||||||
|
|||||||
Reference in New Issue
Block a user