mirror of
https://github.com/clash-verge-rev/clash-verge-rev
synced 2025-05-05 06:53:44 +08:00
feat: log panic
This commit is contained in:
parent
1fc54e49d9
commit
2396a6b35a
40
src-tauri/src/utils/error.rs
Normal file
40
src-tauri/src/utils/error.rs
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
use crate::log_err;
|
||||||
|
use anyhow;
|
||||||
|
use std::{
|
||||||
|
backtrace::{Backtrace, BacktraceStatus},
|
||||||
|
thread,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn redirect_panic_to_log() {
|
||||||
|
std::panic::set_hook(Box::new(move |panic_info| {
|
||||||
|
let thread = thread::current();
|
||||||
|
let thread_name = thread.name().unwrap_or("<unnamed>");
|
||||||
|
let payload = panic_info.payload();
|
||||||
|
|
||||||
|
let payload = if let Some(s) = payload.downcast_ref::<&str>() {
|
||||||
|
&**s
|
||||||
|
} else if let Some(s) = payload.downcast_ref::<String>() {
|
||||||
|
s
|
||||||
|
} else {
|
||||||
|
&format!("{:?}", payload)
|
||||||
|
};
|
||||||
|
|
||||||
|
let location = panic_info
|
||||||
|
.location()
|
||||||
|
.map(|l| l.to_string())
|
||||||
|
.unwrap_or("unknown location".to_string());
|
||||||
|
|
||||||
|
let backtrace = Backtrace::capture();
|
||||||
|
let backtrace = if backtrace.status() == BacktraceStatus::Captured {
|
||||||
|
&format!("stack backtrace:\n{}", backtrace)
|
||||||
|
} else {
|
||||||
|
"note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace"
|
||||||
|
};
|
||||||
|
|
||||||
|
let err: Result<(), anyhow::Error> = Err(anyhow::anyhow!(format!(
|
||||||
|
"thread '{}' panicked at {}:\n{}\n{}",
|
||||||
|
thread_name, location, payload, backtrace
|
||||||
|
)));
|
||||||
|
log_err!(err);
|
||||||
|
}));
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
pub mod dirs;
|
pub mod dirs;
|
||||||
|
pub mod error;
|
||||||
pub mod help;
|
pub mod help;
|
||||||
pub mod init;
|
pub mod init;
|
||||||
pub mod resolve;
|
pub mod resolve;
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use crate::cmds::import_profile;
|
use crate::cmds::import_profile;
|
||||||
use crate::config::IVerge;
|
use crate::config::IVerge;
|
||||||
|
use crate::utils::error;
|
||||||
use crate::{config::Config, core::*, utils::init, utils::server};
|
use crate::{config::Config, core::*, utils::init, utils::server};
|
||||||
use crate::{log_err, trace_err};
|
use crate::{log_err, trace_err};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
@ -32,6 +33,7 @@ pub fn find_unused_port() -> Result<u16> {
|
|||||||
|
|
||||||
/// handle something when start app
|
/// handle something when start app
|
||||||
pub async fn resolve_setup(app: &mut App) {
|
pub async fn resolve_setup(app: &mut App) {
|
||||||
|
error::redirect_panic_to_log();
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
app.set_activation_policy(tauri::ActivationPolicy::Accessory);
|
app.set_activation_policy(tauri::ActivationPolicy::Accessory);
|
||||||
let version = app.package_info().version.to_string();
|
let version = app.package_info().version.to_string();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user