Files
clash-verge-rev/src-tauri/src/utils/startup.rs
T
renovate[bot]renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>Tunglies
a76818326d chore(deps): update tauri (#8764)
* chore(deps): update tauri

* fix(installer): align NSIS locale variables with Tauri 2.13

* chore(tray): migrate template icons to Tauri 2.13 APIs

* fix(core): resolve the bundled core for validation fingerprints

* fix(deps): resolve Tauri 2.13 with the patched WebDriver

* fix(tray): wake the main window from Linux tray clicks

Use StatusNotifierItem activation to wake the dashboard, including lightweight mode.

Closes #8517

* fix(tray): gate template icon handling to macOS at compile time

* fix(deps): select GTK3 for Linux dialogs

* fix(linux): return when the startup dialog cannot initialize GTK

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Tunglies <77394545+Tunglies@users.noreply.github.com>
2026-12-03 08:07:50 +00:00

89 lines
2.6 KiB
Rust

use crate::utils::dirs;
use anyhow::{Context as _, Result};
use chrono::Local;
use std::{
fs::OpenOptions,
io::Write as _,
path::{Path, PathBuf},
};
const STARTUP_LOG_FILE: &str = "startup.log";
pub(crate) fn report_error(error: &anyhow::Error) {
let detail = format!("{error:#}");
eprintln!("[clash-verge] startup failed: {detail}");
let log_result = startup_log_path().and_then(|path| {
append_error(&path, &detail).with_context(|| format!("startup log {}", path.display()))?;
Ok(path)
});
let message = match log_result {
Ok(path) => format!(
"Clash Verge could not start.\n\n{detail}\n\nDiagnostic log:\n{}",
path.display()
),
Err(log_error) => {
eprintln!("[clash-verge] failed to write startup log: {log_error:#}");
format!(
"Clash Verge could not start.\n\n{detail}\n\nThe diagnostic log could not be written:\n{log_error:#}"
)
}
};
show_error_dialog(&message);
}
#[cfg(target_os = "linux")]
fn show_error_dialog(message: &str) {
use gtk::prelude::DialogExt as _;
// rfd's GTK3 MessageDialog waits indefinitely if its GTK worker fails to initialize.
if gtk::init().is_err() {
return;
}
gtk::MessageDialog::builder()
.message_type(gtk::MessageType::Error)
.title("Clash Verge startup failed")
.text(message)
.buttons(gtk::ButtonsType::Ok)
.build()
.run();
}
#[cfg(not(target_os = "linux"))]
fn show_error_dialog(message: &str) {
let _ = rfd::MessageDialog::new()
.set_level(rfd::MessageLevel::Error)
.set_title("Clash Verge startup failed")
.set_description(message)
.set_buttons(rfd::MessageButtons::Ok)
.show();
}
fn startup_log_path() -> Result<PathBuf> {
Ok(dirs::preinit_app_data_dir()?.join("logs").join(STARTUP_LOG_FILE))
}
fn append_error(path: &Path, detail: &str) -> Result<()> {
let parent = path.parent().context("startup log has no parent directory")?;
std::fs::create_dir_all(parent).context("failed to create startup log directory")?;
let mut options = OpenOptions::new();
options.create(true).append(true);
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt as _;
options.mode(0o600);
}
let mut file = options.open(path).context("failed to open startup log")?;
writeln!(
file,
"[{}] ERROR [Startup] {detail}",
Local::now().format("%Y-%m-%d %H:%M:%S%.3f")
)
.context("failed to write startup log")?;
file.flush().context("failed to flush startup log")?;
Ok(())
}