From e92da537034c6ceed8d886073d90d1abe6c4d1b1 Mon Sep 17 00:00:00 2001 From: Vladimir Stoilov Date: Fri, 26 Jul 2024 15:58:27 +0300 Subject: [PATCH] [desktop] Add tauri logs --- desktop/tauri/src-tauri/Cargo.lock | 6 ++- desktop/tauri/src-tauri/Cargo.toml | 3 +- desktop/tauri/src-tauri/src/main.rs | 55 ++++++++++++++++++++++-- desktop/tauri/src-tauri/src/window.rs | 3 +- desktop/tauri/src-tauri/tauri.conf.json5 | 7 ++- 5 files changed, 65 insertions(+), 9 deletions(-) diff --git a/desktop/tauri/src-tauri/Cargo.lock b/desktop/tauri/src-tauri/Cargo.lock index 78940eec..d169adc6 100644 --- a/desktop/tauri/src-tauri/Cargo.lock +++ b/desktop/tauri/src-tauri/Cargo.lock @@ -209,6 +209,7 @@ version = "0.1.0" dependencies = [ "assert_matches", "cached", + "clap 4.5.9", "ctor", "dark-light", "dataurl", @@ -7679,9 +7680,9 @@ dependencies = [ [[package]] name = "tauri-plugin-log" -version = "2.0.0-beta.8" +version = "2.0.0-beta.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4cc0912ad72022b4d931c909582f06edb0c5e696cb945f16189ed6abf48a0ebd" +checksum = "80f80d78a6e8102acf05a1e735f006991a2abfc71566d4e484f820b7495cd52c" dependencies = [ "android_logger", "byte-unit", @@ -7695,6 +7696,7 @@ dependencies = [ "swift-rs", "tauri", "tauri-plugin", + "thiserror", "time", ] diff --git a/desktop/tauri/src-tauri/Cargo.toml b/desktop/tauri/src-tauri/Cargo.toml index aef1abe3..416ab2d5 100644 --- a/desktop/tauri/src-tauri/Cargo.toml +++ b/desktop/tauri/src-tauri/Cargo.toml @@ -25,9 +25,10 @@ tauri-plugin-single-instance = "2.0.0-beta" tauri-plugin-cli = "2.0.0-beta" tauri-plugin-notification = "2.0.0-beta" tauri-plugin-log = "2.0.0-beta" -tauri-plugin-window-state = "2.0.0-beta.11" +tauri-plugin-window-state = "2.0.0-beta" tauri-cli = "2.0.0-beta.21" +clap = { version = "4", features = [ "derive" ] } # General serde_json = "1.0" diff --git a/desktop/tauri/src-tauri/src/main.rs b/desktop/tauri/src-tauri/src/main.rs index 584326e1..c1ca3ac4 100644 --- a/desktop/tauri/src-tauri/src/main.rs +++ b/desktop/tauri/src-tauri/src/main.rs @@ -1,8 +1,9 @@ // Prevents additional console window on Windows in release, DO NOT REMOVE!! #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] -use std::time::Duration; +use std::{env, path::Path, time::Duration}; +use clap::{command, Parser}; use tauri::{AppHandle, Emitter, Listener, Manager, RunEvent, WindowEvent}; use tauri_plugin_cli::CliExt; @@ -18,8 +19,9 @@ mod portmaster; mod traymenu; mod window; -use log::{debug, error, info}; +use log::{debug, error, info, LevelFilter}; use portmaster::PortmasterExt; +use tauri_plugin_log::RotationStrategy; use traymenu::setup_tray_menu; use window::{close_splash_window, create_main_window}; @@ -28,6 +30,12 @@ extern crate lazy_static; const FALLBACK_TO_OLD_UI_EXIT_CODE: i32 = 77; +#[cfg(not(debug_assertions))] +const LOG_LEVEL: LevelFilter = LevelFilter::Warn; + +#[cfg(debug_assertions)] +const LOG_LEVEL: LevelFilter = LevelFilter::Debug; + #[derive(Clone, serde::Serialize)] struct Payload { args: Vec, @@ -41,6 +49,16 @@ struct WsHandler { is_first_connect: bool, } +#[derive(Parser, Debug)] +#[command(version, about, long_about = None)] +struct BasicCli { + #[arg(long)] + data: Option, + + #[arg(long)] + log: Option, +} + impl portmaster::Handler for WsHandler { fn name(&self) -> String { "main-handler".to_string() @@ -114,11 +132,41 @@ fn main() { std::process::exit(show_webview_not_installed_dialog()); } + let mut target = tauri_plugin_log::Target::new(tauri_plugin_log::TargetKind::Stdout); + + let cli = BasicCli::parse(); + if let Some(data_dir) = cli.data { + target = tauri_plugin_log::Target::new(tauri_plugin_log::TargetKind::Folder { + path: Path::new(&format!("{}/logs/app2", data_dir)).into(), + file_name: None, + }); + } + + let mut log_level = LOG_LEVEL; + if let Some(level) = cli.log { + match level.as_str() { + "off" => log_level = LevelFilter::Off, + "error" => log_level = LevelFilter::Error, + "warn" => log_level = LevelFilter::Warn, + "info" => log_level = LevelFilter::Info, + "debug" => log_level = LevelFilter::Debug, + "trace" => log_level = LevelFilter::Trace, + _ => {} + } + } + let app = tauri::Builder::default() // Shell plugin for open_external support .plugin(tauri_plugin_shell::init()) // Initialize Logging plugin. - .plugin(tauri_plugin_log::Builder::default().build()) + .plugin( + tauri_plugin_log::Builder::default() + .level(log_level) + .rotation_strategy(RotationStrategy::KeepAll) + .clear_targets() + .target(target) + .build(), + ) // Clipboard support .plugin(tauri_plugin_clipboard_manager::init()) // Dialog (Save/Open) support @@ -148,7 +196,6 @@ fn main() { .setup(|app| { setup_tray_menu(app)?; portmaster::setup(app.handle().clone()); - // Setup the single-instance event listener that will create/focus the main window // or the splash-screen. let handle = app.handle().clone(); diff --git a/desktop/tauri/src-tauri/src/window.rs b/desktop/tauri/src-tauri/src/window.rs index 6e078d63..69eafaf4 100644 --- a/desktop/tauri/src-tauri/src/window.rs +++ b/desktop/tauri/src-tauri/src/window.rs @@ -29,6 +29,7 @@ pub fn create_main_window(app: &AppHandle) -> Result { .title("Portmaster") .visible(false) .inner_size(1200.0, 700.0) + .min_inner_size(800.0, 600.0) .theme(Some(Theme::Dark)) .build(); @@ -114,7 +115,7 @@ pub fn open_window(app: &AppHandle) -> Result { match app.get_webview_window("main") { Some(win) => { app.portmaster().show_window(); - + let _ = win.set_focus(); Ok(win) } None => { diff --git a/desktop/tauri/src-tauri/tauri.conf.json5 b/desktop/tauri/src-tauri/tauri.conf.json5 index a7ea5f8f..8ec0538f 100644 --- a/desktop/tauri/src-tauri/tauri.conf.json5 +++ b/desktop/tauri/src-tauri/tauri.conf.json5 @@ -22,6 +22,11 @@ "name": "background", "description": "Start in the background without opening a window" }, + { + "name": "log", + "description": "Log level to use: off, error, warn, info, debug, trace", + "takesValue": true + }, { "name": "with-notifications", "description": "Enable experimental notifications via Tauri. Replaces the notifier app." @@ -29,7 +34,7 @@ { "name": "with-prompts", "description": "Enable experimental prompt support via Tauri. Replaces the notifier app." - } + }, ] } },