From 30b8fa0f364431c88099381189ed693ca5487385 Mon Sep 17 00:00:00 2001 From: Connor Yoh Date: Tue, 15 Jul 2025 17:08:46 +0100 Subject: [PATCH] Added logging for open with mac --- frontend/src-tauri/src/lib.rs | 18 +++++++++++++----- frontend/src-tauri/src/utils/logging.rs | 17 +++++++++++++++++ frontend/src-tauri/src/utils/mod.rs | 2 +- frontend/src/hooks/useOpenedFile.ts | 9 ++++++++- frontend/src/services/fileOpenService.ts | 19 ++++++++++++++++++- 5 files changed, 57 insertions(+), 8 deletions(-) diff --git a/frontend/src-tauri/src/lib.rs b/frontend/src-tauri/src/lib.rs index 159d0def3..05eac2403 100644 --- a/frontend/src-tauri/src/lib.rs +++ b/frontend/src-tauri/src/lib.rs @@ -4,7 +4,7 @@ mod utils; mod commands; use commands::{start_backend, check_backend_health, get_opened_file, clear_opened_file, cleanup_backend, set_opened_file}; -use utils::add_log; +use utils::{add_log, get_tauri_logs}; #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { @@ -12,18 +12,26 @@ pub fn run() { .plugin(tauri_plugin_shell::init()) .plugin(tauri_plugin_fs::init()) .setup(|_| { - // Check command line arguments at startup for macOS file opening + add_log("🚀 Tauri app setup started".to_string()); + + // Log all command line arguments for debugging let args: Vec = std::env::args().collect(); - for arg in args.iter().skip(1) { - if arg.ends_with(".pdf") && std::path::Path::new(arg).exists() { + add_log(format!("🔍 DEBUG: All command line args: {:?}", args)); + + // Check command line arguments at startup for macOS file opening + for (i, arg) in args.iter().enumerate() { + add_log(format!("🔍 DEBUG: Arg {}: {}", i, arg)); + if i > 0 && arg.ends_with(".pdf") && std::path::Path::new(arg).exists() { add_log(format!("📂 File argument detected at startup: {}", arg)); set_opened_file(arg.clone()); break; // Only handle the first PDF file } } + + add_log("🔍 DEBUG: Setup completed, checking for opened file...".to_string()); Ok(()) }) - .invoke_handler(tauri::generate_handler![start_backend, check_backend_health, get_opened_file, clear_opened_file]) + .invoke_handler(tauri::generate_handler![start_backend, check_backend_health, get_opened_file, clear_opened_file, get_tauri_logs]) .build(tauri::generate_context!()) .expect("error while building tauri application") .run(|app_handle, event| { diff --git a/frontend/src-tauri/src/utils/logging.rs b/frontend/src-tauri/src/utils/logging.rs index 4d4779d6c..513be751f 100644 --- a/frontend/src-tauri/src/utils/logging.rs +++ b/frontend/src-tauri/src/utils/logging.rs @@ -76,3 +76,20 @@ fn write_to_log_file(log_entry: &str) { } } } + +// Get current logs for debugging +pub fn get_logs() -> Vec { + let logs = BACKEND_LOGS.lock().unwrap(); + logs.iter().cloned().collect() +} + +// Command to get logs from frontend +#[tauri::command] +pub async fn get_tauri_logs() -> Result, String> { + Ok(get_logs()) +} + +// Get log file path for external access +pub fn get_log_file_path() -> PathBuf { + get_log_directory().join("tauri-backend.log") +} diff --git a/frontend/src-tauri/src/utils/mod.rs b/frontend/src-tauri/src/utils/mod.rs index 4f7f3c38d..258efca6c 100644 --- a/frontend/src-tauri/src/utils/mod.rs +++ b/frontend/src-tauri/src/utils/mod.rs @@ -1,3 +1,3 @@ pub mod logging; -pub use logging::add_log; \ No newline at end of file +pub use logging::{add_log, get_tauri_logs}; \ No newline at end of file diff --git a/frontend/src/hooks/useOpenedFile.ts b/frontend/src/hooks/useOpenedFile.ts index 55343e489..0a2921d83 100644 --- a/frontend/src/hooks/useOpenedFile.ts +++ b/frontend/src/hooks/useOpenedFile.ts @@ -7,13 +7,20 @@ export function useOpenedFile() { useEffect(() => { const checkForOpenedFile = async () => { + console.log('🔍 Checking for opened file...'); try { const filePath = await fileOpenService.getOpenedFile(); + console.log('🔍 fileOpenService.getOpenedFile() returned:', filePath); if (filePath) { console.log('✅ App opened with file:', filePath); setOpenedFilePath(filePath); - } + + // Clear the file from Tauri state after consuming it + await fileOpenService.clearOpenedFile(); + } else { + console.log('â„šī¸ No file was opened with the app'); + } } catch (error) { console.error('❌ Failed to check for opened file:', error); diff --git a/frontend/src/services/fileOpenService.ts b/frontend/src/services/fileOpenService.ts index 2ee43a7b7..02b8729b7 100644 --- a/frontend/src/services/fileOpenService.ts +++ b/frontend/src/services/fileOpenService.ts @@ -3,15 +3,18 @@ import { invoke } from '@tauri-apps/api/core'; export interface FileOpenService { getOpenedFile(): Promise; readFileAsArrayBuffer(filePath: string): Promise<{ fileName: string; arrayBuffer: ArrayBuffer } | null>; + clearOpenedFile(): Promise; } class TauriFileOpenService implements FileOpenService { async getOpenedFile(): Promise { try { + console.log('🔍 Calling invoke(get_opened_file)...'); const result = await invoke('get_opened_file'); + console.log('🔍 invoke(get_opened_file) returned:', result); return result; } catch (error) { - console.error('Failed to get opened file:', error); + console.error('❌ Failed to get opened file:', error); return null; } } @@ -32,6 +35,16 @@ class TauriFileOpenService implements FileOpenService { return null; } } + + async clearOpenedFile(): Promise { + try { + console.log('🔍 Calling invoke(clear_opened_file)...'); + await invoke('clear_opened_file'); + console.log('✅ Successfully cleared opened file'); + } catch (error) { + console.error('❌ Failed to clear opened file:', error); + } + } } class WebFileOpenService implements FileOpenService { @@ -44,6 +57,10 @@ class WebFileOpenService implements FileOpenService { // In web mode, cannot read arbitrary file paths return null; } + + async clearOpenedFile(): Promise { + // In web mode, no file clearing needed + } } // Export the appropriate service based on environment