[desktop] Add tauri logs

This commit is contained in:
Vladimir Stoilov
2024-07-26 15:58:27 +03:00
parent 22a288c66b
commit e92da53703
5 changed files with 65 additions and 9 deletions

View File

@@ -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",
]

View File

@@ -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"

View File

@@ -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<String>,
@@ -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<String>,
#[arg(long)]
log: Option<String>,
}
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();

View File

@@ -29,6 +29,7 @@ pub fn create_main_window(app: &AppHandle) -> Result<WebviewWindow> {
.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<WebviewWindow> {
match app.get_webview_window("main") {
Some(win) => {
app.portmaster().show_window();
let _ = win.set_focus();
Ok(win)
}
None => {

View File

@@ -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."
}
},
]
}
},