From 37531507dbbe3ee8b472315507b687b42841b1fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8B=82=E7=94=B7=E9=A3=8E?= Date: Sat, 27 Dec 2025 20:04:18 +0800 Subject: [PATCH] fix(mobile): logs unreachable on android (#1710) --- easytier-gui/src-tauri/src/lib.rs | 25 ++++++++++++++++++++++++- easytier-gui/src/pages/index.vue | 13 +++++++++---- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/easytier-gui/src-tauri/src/lib.rs b/easytier-gui/src-tauri/src/lib.rs index 33cff00a..653006c7 100644 --- a/easytier-gui/src-tauri/src/lib.rs +++ b/easytier-gui/src-tauri/src/lib.rs @@ -425,6 +425,28 @@ async fn is_web_client_connected() -> Result { } } +// 获取日志目录的辅助函数 +fn get_log_dir(app: &tauri::AppHandle) -> Result { + if cfg!(target_os = "android") { + // Android: cache_dir + logs 子目录 + app.path().cache_dir().map(|p| p.join("logs")) + } else { + // 其他平台: 默认日志目录 + app.path().app_log_dir() + } +} + +#[tauri::command] +async fn get_log_dir_path(app: tauri::AppHandle) -> Result { + match get_log_dir(&app) { + Ok(log_dir) => { + std::fs::create_dir_all(&log_dir).ok(); + Ok(log_dir.to_string_lossy().to_string()) + } + Err(e) => Err(format!("Failed to get log directory: {}", e)), + } +} + #[cfg(not(target_os = "android"))] fn toggle_window_visibility(app: &tauri::AppHandle) { if let Some(window) = app.get_webview_window("main") { @@ -1015,7 +1037,7 @@ pub fn run_gui() -> std::process::ExitCode { let app = builder .setup(|app| { // for logging config - let Ok(log_dir) = app.path().app_log_dir() else { + let Ok(log_dir) = get_log_dir(app.app_handle()) else { return Ok(()); }; let config = LoggingConfigBuilder::default() @@ -1079,6 +1101,7 @@ pub fn run_gui() -> std::process::ExitCode { is_client_running, init_web_client, is_web_client_connected, + get_log_dir_path, ]) .on_window_event(|_win, event| match event { #[cfg(not(target_os = "android"))] diff --git a/easytier-gui/src/pages/index.vue b/easytier-gui/src/pages/index.vue index 75abfb64..c7994159 100644 --- a/easytier-gui/src/pages/index.vue +++ b/easytier-gui/src/pages/index.vue @@ -2,7 +2,7 @@ import { type } from '@tauri-apps/plugin-os' -import { appLogDir } from '@tauri-apps/api/path' +import { invoke } from '@tauri-apps/api/core' import { writeText } from '@tauri-apps/plugin-clipboard-manager' import { exit } from '@tauri-apps/plugin-process' import { I18nUtils, RemoteManagement, Utils } from "easytier-frontend-lib" @@ -231,6 +231,11 @@ onMounted(async () => { let current_log_level = 'off' const log_menu = ref() +// 从后端获取正确的日志路径 +async function getLogDirPath(): Promise { + return await invoke('get_log_dir_path') +} + const log_menu_items_popup: Ref = ref([ ...['off', 'warn', 'info', 'debug', 'trace'].map(level => ({ label: () => t(`logging_level_${level}`) + (current_log_level === level ? ' ✓' : ''), @@ -246,15 +251,15 @@ const log_menu_items_popup: Ref = ref([ label: () => t('logging_open_dir'), icon: 'pi pi-folder-open', command: async () => { - // console.log('open log dir', await appLogDir()) - await open(await appLogDir()) + // console.log('open log dir', await getLogDirPath()) + await open(await getLogDirPath()) }, }, { label: () => t('logging_copy_dir'), icon: 'pi pi-tablet', command: async () => { - await writeText(await appLogDir()) + await writeText(await getLogDirPath()) }, }, ])