Skip to content

Commit 986a239

Browse files
committed
feat: 统一日志文件目录
1 parent fa3e14a commit 986a239

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

src-tauri/capabilities/default.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@
1111
{
1212
"identifier": "opener:allow-open-path",
1313
"allow": [
14+
{
15+
"path": "$HOME/.codeforge/**"
16+
},
17+
{
18+
"path": "$APPDATA/**"
19+
},
20+
{
21+
"path": "$LOCALAPPDATA/**"
22+
},
1423
{
1524
"path": "**"
1625
}
@@ -20,9 +29,19 @@
2029
"shell:allow-open",
2130
"dialog:default",
2231
"fs:allow-read-text-file",
32+
"fs:allow-read-dir",
33+
"fs:allow-read-file",
34+
"fs:allow-write-file",
35+
"fs:allow-write-text-file",
36+
"fs:allow-remove",
37+
"fs:allow-mkdir",
38+
"fs:allow-exists",
2339
{
2440
"identifier": "fs:scope",
2541
"allow": [
42+
{
43+
"path": "$HOME/.codeforge/**"
44+
},
2645
{
2746
"path": "/var/folders/**"
2847
},
@@ -32,6 +51,12 @@
3251
{
3352
"path": "/private/var/folders/**"
3453
},
54+
{
55+
"path": "$APPDATA/**"
56+
},
57+
{
58+
"path": "$LOCALAPPDATA/**"
59+
},
3560
{
3661
"path": "**"
3762
}

src-tauri/src/logger.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,24 @@ fn get_effective_log_directory(app: &AppHandle) -> PathBuf {
107107
}
108108

109109
// 获取默认日志目录
110-
fn get_default_log_directory(app: &AppHandle) -> PathBuf {
111-
app.path()
112-
.app_data_dir()
113-
.expect("Failed to get app data dir")
114-
.join("logs")
110+
fn get_default_log_directory(_app: &AppHandle) -> PathBuf {
111+
let home_dir = dirs::home_dir().expect("Failed to get home directory");
112+
let log_dir = home_dir.join(".codeforge").join("logs");
113+
114+
// 确保目录存在
115+
if !log_dir.exists() {
116+
if let Err(e) = fs::create_dir_all(&log_dir) {
117+
eprintln!("Failed to create log directory: {}", e);
118+
// 如果创建失败,回退到应用数据目录
119+
return _app
120+
.path()
121+
.app_data_dir()
122+
.expect("Failed to get app data dir")
123+
.join("logs");
124+
}
125+
}
126+
127+
log_dir
115128
}
116129

117130
// 公共函数,供其他模块调用

src/composables/useLogDirectory.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ref } from 'vue'
22
import { invoke } from '@tauri-apps/api/core'
33
import { open as openDialog } from '@tauri-apps/plugin-dialog'
44
import { openPath } from '@tauri-apps/plugin-opener'
5+
import { join } from '@tauri-apps/api/path'
56
import { useToast } from '../plugins/toast'
67

78
export function useLogDirectory(emit: any)
@@ -97,7 +98,7 @@ export function useLogDirectory(emit: any)
9798

9899
const openLogFile = async (filename: string) => {
99100
try {
100-
const logPath = `${ currentLogDir.value }/${ filename }`
101+
const logPath = await join(currentLogDir.value, filename)
101102
await openPath(logPath)
102103
}
103104
catch (error) {

0 commit comments

Comments
 (0)