GUI: support change log level when running (#153)

This commit is contained in:
Sijie.Sun
2024-07-06 17:18:13 +08:00
committed by GitHub
parent 571d4a6e8f
commit 7cfa850d4c
10 changed files with 444 additions and 40 deletions

View File

@@ -11,7 +11,7 @@ edition = "2021"
tauri-build = { version = "1", features = [] }
[dependencies]
tauri = { version = "1", features = [
tauri = { version = "1", features = [ "clipboard-all", "path-all",
"process-exit",
"system-tray",
"shell-open",

View File

@@ -8,9 +8,11 @@ use auto_launch::AutoLaunchBuilder;
use dashmap::DashMap;
use easytier::{
common::config::{
ConfigLoader, NetworkIdentity, PeerConfig, TomlConfigLoader, VpnPortalConfig,
ConfigLoader, FileLoggerConfig, NetworkIdentity, PeerConfig, TomlConfigLoader,
VpnPortalConfig,
},
launcher::{NetworkInstance, NetworkInstanceRunningInfo},
utils::{self, NewFilterSender},
};
use serde::{Deserialize, Serialize};
@@ -165,6 +167,9 @@ impl NetworkConfig {
static INSTANCE_MAP: once_cell::sync::Lazy<DashMap<String, NetworkInstance>> =
once_cell::sync::Lazy::new(DashMap::new);
static mut LOGGER_LEVEL_SENDER: once_cell::sync::Lazy<Option<NewFilterSender>> =
once_cell::sync::Lazy::new(Default::default);
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
#[tauri::command]
fn parse_network_config(cfg: NetworkConfig) -> Result<String, String> {
@@ -222,6 +227,13 @@ fn set_auto_launch_status(app_handle: tauri::AppHandle, enable: bool) -> Result<
Ok(init_launch(&app_handle, enable).map_err(|e| e.to_string())?)
}
#[tauri::command]
fn set_logging_level(level: String) -> Result<(), String> {
let sender = unsafe { LOGGER_LEVEL_SENDER.as_ref().unwrap() };
sender.send(level).map_err(|e| e.to_string())?;
Ok(())
}
fn toggle_window_visibility(window: &Window) {
if window.is_visible().unwrap() {
window.hide().unwrap();
@@ -306,6 +318,7 @@ fn main() {
if !check_sudo() {
process::exit(0);
}
let quit = CustomMenuItem::new("quit".to_string(), "退出 Quit");
let hide = CustomMenuItem::new("hide".to_string(), "显示 Show / 隐藏 Hide");
let tray_menu = SystemTrayMenu::new()
@@ -314,13 +327,31 @@ fn main() {
.add_item(hide);
tauri::Builder::default()
.setup(|app| {
let Some(log_dir) = app.path_resolver().app_log_dir() else {
return Ok(());
};
let config = TomlConfigLoader::default();
config.set_file_logger_config(FileLoggerConfig {
dir: Some(log_dir.to_string_lossy().to_string()),
level: None,
file: None,
});
let Ok(Some(logger_reinit)) = utils::init_logger(config, true) else {
return Ok(());
};
unsafe { LOGGER_LEVEL_SENDER.replace(logger_reinit) };
Ok(())
})
.invoke_handler(tauri::generate_handler![
parse_network_config,
run_network_instance,
retain_network_instance,
collect_network_infos,
get_os_hostname,
set_auto_launch_status
set_auto_launch_status,
set_logging_level
])
.system_tray(SystemTray::new().with_menu(tray_menu))
.on_system_tray_event(|app, event| match event {

View File

@@ -14,10 +14,18 @@
"all": false,
"shell": {
"all": false,
"open": true
"open": ".*"
},
"process": {
"exit": true
},
"path": {
"all": true
},
"clipboard": {
"all": true,
"writeText": true,
"readText": true
}
},
"windows": [