Skip to content

Commit

Permalink
[desktop] Add tauri logs
Browse files Browse the repository at this point in the history
  • Loading branch information
vlabo committed Jul 26, 2024
1 parent 22a288c commit e92da53
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 9 deletions.
6 changes: 4 additions & 2 deletions desktop/tauri/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion desktop/tauri/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
55 changes: 51 additions & 4 deletions desktop/tauri/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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};

Expand All @@ -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>,
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion desktop/tauri/src-tauri/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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 => {
Expand Down
7 changes: 6 additions & 1 deletion desktop/tauri/src-tauri/tauri.conf.json5
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@
"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."
},
{
"name": "with-prompts",
"description": "Enable experimental prompt support via Tauri. Replaces the notifier app."
}
},
]
}
},
Expand Down

0 comments on commit e92da53

Please sign in to comment.