|
@@ -2,6 +2,7 @@ use serde::{Deserialize, Serialize};
|
|
|
use std::fs;
|
|
use std::fs;
|
|
|
use std::path::PathBuf;
|
|
use std::path::PathBuf;
|
|
|
use chrono::Local;
|
|
use chrono::Local;
|
|
|
|
|
+use tauri::{AppHandle, Manager};
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
|
pub struct LogEntry {
|
|
pub struct LogEntry {
|
|
@@ -32,37 +33,36 @@ pub struct LogPaths {
|
|
|
pub install_log: String,
|
|
pub install_log: String,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-fn get_log_dir() -> PathBuf {
|
|
|
|
|
- dirs::data_local_dir()
|
|
|
|
|
- .unwrap_or_else(|| PathBuf::from("."))
|
|
|
|
|
- .join("claude-ai-installer")
|
|
|
|
|
- .join("logs")
|
|
|
|
|
|
|
+fn get_log_dir(app: &AppHandle) -> PathBuf {
|
|
|
|
|
+ app.path()
|
|
|
|
|
+ .app_log_dir()
|
|
|
|
|
+ .unwrap_or_else(|_| PathBuf::from("."))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-fn ensure_log_dir() -> std::io::Result<PathBuf> {
|
|
|
|
|
- let dir = get_log_dir();
|
|
|
|
|
|
|
+fn ensure_log_dir(app: &AppHandle) -> std::io::Result<PathBuf> {
|
|
|
|
|
+ let dir = get_log_dir(app);
|
|
|
if !dir.exists() {
|
|
if !dir.exists() {
|
|
|
fs::create_dir_all(&dir)?;
|
|
fs::create_dir_all(&dir)?;
|
|
|
}
|
|
}
|
|
|
Ok(dir)
|
|
Ok(dir)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-fn get_app_log_path() -> PathBuf {
|
|
|
|
|
- get_log_dir().join("app.log")
|
|
|
|
|
|
|
+fn get_app_log_path(app: &AppHandle) -> PathBuf {
|
|
|
|
|
+ get_log_dir(app).join("app.log")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-fn get_install_log_path() -> PathBuf {
|
|
|
|
|
- get_log_dir().join("install.log")
|
|
|
|
|
|
|
+fn get_install_log_path(app: &AppHandle) -> PathBuf {
|
|
|
|
|
+ get_log_dir(app).join("install.log")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-fn get_history_path() -> PathBuf {
|
|
|
|
|
- get_log_dir().join("history.json")
|
|
|
|
|
|
|
+fn get_history_path(app: &AppHandle) -> PathBuf {
|
|
|
|
|
+ get_log_dir(app).join("history.json")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// 获取日志
|
|
/// 获取日志
|
|
|
#[tauri::command]
|
|
#[tauri::command]
|
|
|
-pub async fn get_logs() -> Vec<LogEntry> {
|
|
|
|
|
- let path = get_app_log_path();
|
|
|
|
|
|
|
+pub async fn get_logs(app: AppHandle) -> Vec<LogEntry> {
|
|
|
|
|
+ let path = get_app_log_path(&app);
|
|
|
|
|
|
|
|
if let Ok(content) = fs::read_to_string(&path) {
|
|
if let Ok(content) = fs::read_to_string(&path) {
|
|
|
content
|
|
content
|
|
@@ -76,9 +76,9 @@ pub async fn get_logs() -> Vec<LogEntry> {
|
|
|
|
|
|
|
|
/// 写入安装日志
|
|
/// 写入安装日志
|
|
|
#[tauri::command]
|
|
#[tauri::command]
|
|
|
-pub async fn write_install_log(message: String, level: Option<String>) -> Result<(), String> {
|
|
|
|
|
- let _ = ensure_log_dir().map_err(|e| e.to_string())?;
|
|
|
|
|
- let path = get_install_log_path();
|
|
|
|
|
|
|
+pub async fn write_install_log(app: AppHandle, message: String, level: Option<String>) -> Result<(), String> {
|
|
|
|
|
+ let _ = ensure_log_dir(&app).map_err(|e| e.to_string())?;
|
|
|
|
|
+ let path = get_install_log_path(&app);
|
|
|
|
|
|
|
|
let entry = LogEntry {
|
|
let entry = LogEntry {
|
|
|
level: level.unwrap_or_else(|| "INFO".to_string()),
|
|
level: level.unwrap_or_else(|| "INFO".to_string()),
|
|
@@ -101,17 +101,17 @@ pub async fn write_install_log(message: String, level: Option<String>) -> Result
|
|
|
|
|
|
|
|
/// 获取日志文件路径
|
|
/// 获取日志文件路径
|
|
|
#[tauri::command]
|
|
#[tauri::command]
|
|
|
-pub async fn get_log_paths() -> LogPaths {
|
|
|
|
|
|
|
+pub async fn get_log_paths(app: AppHandle) -> LogPaths {
|
|
|
LogPaths {
|
|
LogPaths {
|
|
|
- app_log: get_app_log_path().to_string_lossy().to_string(),
|
|
|
|
|
- install_log: get_install_log_path().to_string_lossy().to_string(),
|
|
|
|
|
|
|
+ app_log: get_app_log_path(&app).to_string_lossy().to_string(),
|
|
|
|
|
+ install_log: get_install_log_path(&app).to_string_lossy().to_string(),
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// 获取安装历史
|
|
/// 获取安装历史
|
|
|
#[tauri::command]
|
|
#[tauri::command]
|
|
|
-pub async fn get_install_history(limit: Option<usize>) -> Vec<InstallHistoryItem> {
|
|
|
|
|
- let path = get_history_path();
|
|
|
|
|
|
|
+pub async fn get_install_history(app: AppHandle, limit: Option<usize>) -> Vec<InstallHistoryItem> {
|
|
|
|
|
+ let path = get_history_path(&app);
|
|
|
|
|
|
|
|
if let Ok(content) = fs::read_to_string(&path) {
|
|
if let Ok(content) = fs::read_to_string(&path) {
|
|
|
if let Ok(mut history) = serde_json::from_str::<Vec<InstallHistoryItem>>(&content) {
|
|
if let Ok(mut history) = serde_json::from_str::<Vec<InstallHistoryItem>>(&content) {
|