Added logging for open with mac

This commit is contained in:
Connor Yoh 2025-07-15 17:08:46 +01:00
parent a3555d882a
commit 30b8fa0f36
5 changed files with 57 additions and 8 deletions

View File

@ -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<String> = 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| {

View File

@ -76,3 +76,20 @@ fn write_to_log_file(log_entry: &str) {
}
}
}
// Get current logs for debugging
pub fn get_logs() -> Vec<String> {
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<Vec<String>, 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")
}

View File

@ -1,3 +1,3 @@
pub mod logging;
pub use logging::add_log;
pub use logging::{add_log, get_tauri_logs};

View File

@ -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);

View File

@ -3,15 +3,18 @@ import { invoke } from '@tauri-apps/api/core';
export interface FileOpenService {
getOpenedFile(): Promise<string | null>;
readFileAsArrayBuffer(filePath: string): Promise<{ fileName: string; arrayBuffer: ArrayBuffer } | null>;
clearOpenedFile(): Promise<void>;
}
class TauriFileOpenService implements FileOpenService {
async getOpenedFile(): Promise<string | null> {
try {
console.log('🔍 Calling invoke(get_opened_file)...');
const result = await invoke<string | null>('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<void> {
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<void> {
// In web mode, no file clearing needed
}
}
// Export the appropriate service based on environment