1
0
mirror of https://github.com/lencx/ChatGPT.git synced 2024-10-01 01:06:13 -04:00

chore: add log

This commit is contained in:
lencx 2022-12-18 11:50:34 +08:00
parent 4366b8ee8a
commit c07fd1e0b8
6 changed files with 55 additions and 21 deletions

View File

@ -19,6 +19,12 @@ serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.2.2", features = ["api-all", "devtools", "system-tray", "updater"] }
tauri-plugin-positioner = { version = "1.0.4", features = ["system-tray"] }
log = "0.4.17"
[dependencies.tauri-plugin-log]
git = "https://github.com/tauri-apps/tauri-plugin-log"
branch = "dev"
features = ["colored"]
[features]
# by default Tauri runs in production mode

View File

@ -70,4 +70,4 @@ pub fn get_chat_model() -> serde_json::Value {
let path = utils::chat_root().join("chat.model.json");
let content = fs::read_to_string(path).unwrap_or_else(|_| r#"{"data":[]}"#.to_string());
serde_json::from_str(&content).unwrap()
}
}

View File

@ -4,7 +4,7 @@ use crate::{
};
use tauri::{
AboutMetadata, AppHandle, CustomMenuItem, Manager, Menu, MenuItem, Submenu, SystemTray,
SystemTrayEvent, SystemTrayMenu, WindowMenuEvent, SystemTrayMenuItem,
SystemTrayEvent, SystemTrayMenu, SystemTrayMenuItem, WindowMenuEvent,
};
use tauri_plugin_positioner::{on_tray_event, Position, WindowExt};
@ -138,6 +138,10 @@ pub fn init() -> Menu {
let help_menu = Submenu::new(
"Help",
Menu::new()
.add_item(CustomMenuItem::new(
"chatgpt_log".to_string(),
"ChatGPT Log",
))
.add_item(CustomMenuItem::new("update_log".to_string(), "Update Log"))
.add_item(CustomMenuItem::new("report_bug".to_string(), "Report Bug"))
.add_item(
@ -226,6 +230,7 @@ pub fn menu_handler(event: WindowMenuEvent<tauri::Wry>) {
)
.unwrap(),
// Help
"chatgpt_log" => utils::open_file(utils::chat_root().join("chatgpt.log")),
"update_log" => open(&app, conf::UPDATE_LOG_URL.to_string()),
"report_bug" => open(&app, conf::ISSUES_URL.to_string()),
"dev_tools" => {
@ -240,11 +245,20 @@ pub fn menu_handler(event: WindowMenuEvent<tauri::Wry>) {
pub fn tray_menu() -> SystemTray {
SystemTray::new().with_menu(
SystemTrayMenu::new()
.add_item(CustomMenuItem::new("control_center".to_string(), "Control Center"))
.add_item(CustomMenuItem::new("show_dock_icon".to_string(), "Show Dock Icon"))
.add_item(CustomMenuItem::new("hide_dock_icon".to_string(), "Hide Dock Icon"))
.add_item(CustomMenuItem::new(
"control_center".to_string(),
"Control Center",
))
.add_item(CustomMenuItem::new(
"show_dock_icon".to_string(),
"Show Dock Icon",
))
.add_item(CustomMenuItem::new(
"hide_dock_icon".to_string(),
"Hide Dock Icon",
))
.add_native_item(SystemTrayMenuItem::Separator)
.add_item(CustomMenuItem::new("quit".to_string(), "Quit ChatGPT"))
.add_item(CustomMenuItem::new("quit".to_string(), "Quit ChatGPT")),
)
}
@ -276,25 +290,19 @@ pub fn tray_handler(handle: &AppHandle, event: SystemTrayEvent) {
"control_center" => app.get_window("main").unwrap().show().unwrap(),
"restart" => tauri::api::process::restart(&handle.env()),
"show_dock_icon" => {
ChatConfJson::amend(
&serde_json::json!({ "hide_dock_icon": false }),
Some(app),
)
.unwrap();
},
ChatConfJson::amend(&serde_json::json!({ "hide_dock_icon": false }), Some(app))
.unwrap();
}
"hide_dock_icon" => {
let chat_conf = conf::ChatConfJson::get_chat_conf();
if !chat_conf.hide_dock_icon {
ChatConfJson::amend(
&serde_json::json!({ "hide_dock_icon": true }),
Some(app),
)
.unwrap();
ChatConfJson::amend(&serde_json::json!({ "hide_dock_icon": true }), Some(app))
.unwrap();
}
},
}
"quit" => std::process::exit(0),
_ => (),
}
},
_ => (),
}
}

View File

@ -34,7 +34,8 @@ pub fn init(app: &mut App) -> std::result::Result<(), Box<dyn std::error::Error>
.initialization_script(include_str!("../assets/export.js"))
.initialization_script(include_str!("../assets/cmd.js"))
.user_agent(&chat_conf.ua_window)
.build().unwrap();
.build()
.unwrap();
#[cfg(not(target_os = "macos"))]
WindowBuilder::new(&app, "core", WindowUrl::App(url.into()))
@ -51,7 +52,8 @@ pub fn init(app: &mut App) -> std::result::Result<(), Box<dyn std::error::Error>
.initialization_script(include_str!("../assets/export.js"))
.initialization_script(include_str!("../assets/cmd.js"))
.user_agent(&chat_conf.ua_window)
.build().unwrap();
.build()
.unwrap();
});
}

View File

@ -9,13 +9,29 @@ mod utils;
use app::{cmd, menu, setup};
use conf::{ChatConfJson, ChatState};
use tauri::api::path;
use tauri_plugin_log::{fern::colors::ColoredLevelConfig, LogTarget, LoggerBuilder};
fn main() {
ChatConfJson::init();
let chat_conf = ChatConfJson::get_chat_conf();
let context = tauri::generate_context!();
let colors = ColoredLevelConfig::default();
tauri::Builder::default()
// https://github.com/tauri-apps/tauri/pull/2736
.plugin(
LoggerBuilder::new()
.with_colors(colors)
.targets([
// LogTarget::LogDir,
// LOG PATH: ~/.chatgpt/ChatGPT.log
LogTarget::Folder(path::home_dir().unwrap().join(".chatgpt")),
LogTarget::Stdout,
LogTarget::Webview,
])
.build(),
)
.manage(ChatState::default(chat_conf))
.invoke_handler(tauri::generate_handler![
cmd::drag_window,

View File

@ -1,4 +1,5 @@
use anyhow::Result;
use log::info;
use std::{
fs::{self, File},
path::{Path, PathBuf},
@ -48,6 +49,7 @@ pub fn user_script() -> String {
}
pub fn open_file(path: PathBuf) {
info!("open_file: {}", path.to_string_lossy());
#[cfg(target_os = "macos")]
Command::new("open").arg("-R").arg(path).spawn().unwrap();