欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Rust捕獲全局panic并記錄進程退出日志的方法

 更新時間:2024年04月24日 09:25:00   作者:會編程的大白熊  
本文提供了捕獲全局panic并記錄進程退出日志的方法,首先使用 panic::set_hook 注冊異常處理及panic 觸發(fā)異常,結合實例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧

本文提供了捕獲全局panic并記錄進程退出日志的方法。

1. 使用 panic::set_hook 注冊異常處理

use human_panic::setup_panic;
use log::error;
use std::{boxed::Box, panic};
fn hook(panic_info: &panic::PanicInfo) {
    if cfg!(debug_assertions) {
        let err_message = format!("panic occurred {:?}", panic_info);
        error!("{}", err_message);
    } else {
        let err_message = match panic_info.payload().downcast_ref::<&str>() {
            Option::Some(&str) => {
                let err_message = format!(
                    "panic info: {:?}, occurred in {:?}",
                    str,
                    panic_info.location()
                );
                err_message
            }
            Option::None => {
                let err_message =
                    format!("panic occurred in {:?}", panic_info.location());
                err_message
            }
        };
        error!("{}", err_message);
    }
}
/// 注冊異常處理函數(shù)
/// 在panic發(fā)出后,在panic運行時之前,觸發(fā)鉤子函數(shù)去處理這個panic信息。
/// panic信息被保存在PanicInfo結構體中。
pub fn register_panic_hook() {
    panic::set_hook(Box::new(|panic_info| {
        hook(panic_info);
    }));
    // setup_panic!();
}

2. panic 觸發(fā)異常

use core_utils::panic::register_panic_hook;
use core_utils::panic::register_panic_hook;
use env_logger;
use log::LevelFilter;
use std::thread;
use std::time::Duration;
#[test]
fn test_panic_set_hook() {
    let _ = env_logger::builder()
        .is_test(true)
        .filter(None, LevelFilter::Debug)
        .try_init();
    register_panic_hook();
    thread::spawn(|| {
        panic!("child thread panic");
    });
    thread::sleep(Duration::from_millis(100));
}

日志如下

debug模式

[2024-04-20T05:32:30Z ERROR core_utils::panic] panic occurred PanicInfo { payload: Any { .. }, message: Some(child thread panic), location: Location { file: "core_utils/tests/test_panic.rs", line: 16, col: 9 }, can_unwind: true, force_no_backtrace: false }

release模式

[2024-04-20T05:41:06Z ERROR core_utils::panic] panic info: "child thread panic", occurred in Some(Location { file: "core_utils/tests/test_panic.rs", line: 17, col: 9 })

3. unwrap 觸發(fā)異常

#[test]
fn test_panic_unwrap() {
    let _ = env_logger::builder()
        .is_test(true)
        .filter(None, LevelFilter::Debug)
        .try_init();
    register_panic_hook();
    thread::spawn(|| {
        let _ = "abc".parse::<i32>().unwrap();
    });
    thread::sleep(Duration::from_millis(100));
}

日志如下

debug模式

[2024-04-20T05:38:22Z ERROR core_utils::panic] panic occurred PanicInfo { payload: Any { .. }, message: Some(called `Result::unwrap()` on an `Err` value: ParseIntError { kind: InvalidDigit }), location: Location { file: "core_utils/tests/test_panic.rs", line: 33, col: 38 }, can_unwind: true, force_no_backtrace: false }

release模式

注意:unwrap觸發(fā)的異常會導致 panic_info.payload().downcast_ref::<&str>()返回結果為 None

[2024-04-20T05:42:34Z ERROR core_utils::panic] panic occurred in Some(Location { file: "core_utils/tests/test_panic.rs", line: 33, col: 38 })

4. 使用 human_panic

human_panic只能在非debug模式且環(huán)境變量RUST_BACKTRACE未設置的情況下才會生效。

注冊 hook

use human_panic::setup_panic;
pub fn register_panic_hook() {
    setup_panic!();
}

模擬release環(huán)境異常

use core_utils::panic::register_panic_hook;
use env_logger;
use human_panic::setup_panic;
use log::error;
use std::thread;
use std::time::Duration;
fn main() {
    env_logger::init();
    register_panic_hook();
    thread::spawn(|| {
        panic!("error");
        // let _ = "abc".parse::<i32>().unwrap();
    });
    thread::sleep(Duration::from_secs(1));
}
cargo run --bin human_panic --release

panic發(fā)生時會在在臨時文件夾下面創(chuàng)建一個報告文件

Well, this is embarrassing.
core_utils had a problem and crashed. To help us diagnose the problem you can send us a crash report.
We have generated a report file at "/var/folders/gx/hn6l2rd56cx0lcwnkblxqvmr0000gn/T/report-93547ab5-9341-4212-a9af-6d2f17d6311d.toml". Submit an issue or email with the subject of "core_utils Crash Report" and include the report as an attachment.
We take privacy seriously, and do not perform any automated error collection. In order to improve the software, we rely on people to submit reports.
Thank you kindly!

報告內(nèi)容如下

"name" = "core_utils"
"operating_system" = "Mac OS 14.1.1 [64-bit]"
"crate_version" = "0.1.0"
"explanation" = """
Panic occurred in file 'core_utils/src/bin/human_panic.rs' at line 14
"""
"cause" = "error"
"method" = "Panic"
"backtrace" = """
   0: 0x105b840a5 - core::panicking::panic_fmt::h2aac8cf45f7ae617
   1: 0x1059fd7c6 - std::sys_common::backtrace::__rust_begin_short_backtrace::h4bae865db206eae3
   2: 0x1059fe2fd - core::ops::function::FnOnce::call_once{{vtable.shim}}::ha8d441119e8b7a5a
   3: 0x105b5a819 - std::sys::pal::unix::thread::Thread::new::thread_start::h679ffa03f8a73496
   4: 0x7ff801993202 - __pthread_start"""

到此這篇關于Rust捕獲全局panic并記錄進程退出日志的文章就介紹到這了,更多相關rust 捕獲全局panic內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Rust中的&和ref使用解讀

    Rust中的&和ref使用解讀

    在Rust中,`&`和`ref`都可以用來定義指針,但它們的使用位置不同,`&`通常放在等號右邊,而`ref`放在左邊,`&`主要用于函數(shù)參數(shù)和模式匹配中,而`ref`主要用于模式匹配中,Rust通過`&`和`ref`提供了靈活的指針操作,使得代碼更加安全和高效
    2025-02-02
  • 關于Rust?使用?dotenv?來設置環(huán)境變量的問題

    關于Rust?使用?dotenv?來設置環(huán)境變量的問題

    在項目中,我們通常需要設置一些環(huán)境變量,用來保存一些憑證或其它數(shù)據(jù),這時我們可以使用dotenv這個crate,接下來通過本文給大家介紹Rust?使用dotenv來設置環(huán)境變量的問題,感興趣的朋友一起看看吧
    2022-01-01
  • Rust讀取配置文件的實現(xiàn)

    Rust讀取配置文件的實現(xiàn)

    本文主要介紹了Rust讀取配置文件的實現(xiàn),主要讀取Cargo.toml文件,讀取.env文件和讀取自定義toml文件這三種,具有一定的參考價值,感興趣的可以了解一下
    2024-03-03
  • 淺析Rust多線程中如何安全的使用變量

    淺析Rust多線程中如何安全的使用變量

    這篇文章主要為大家詳細介紹了Rust如何在線程的閉包中安全的使用變量,包括共享變量和修改變量,文中的示例代碼講解詳細,有需要的小伙伴可以參考下
    2025-01-01
  • Rust?Atomics?and?Locks內(nèi)存序Memory?Ordering詳解

    Rust?Atomics?and?Locks內(nèi)存序Memory?Ordering詳解

    這篇文章主要為大家介紹了Rust?Atomics?and?Locks內(nèi)存序Memory?Ordering詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • Rust中的Copy和Clone對比分析

    Rust中的Copy和Clone對比分析

    這篇文章主要介紹了Rust中的Copy和Clone及區(qū)別對比分析,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-04-04
  • Rust Atomics and Locks并發(fā)基礎理解

    Rust Atomics and Locks并發(fā)基礎理解

    這篇文章主要為大家介紹了Rust Atomics and Locks并發(fā)基礎理解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • 深入了解Rust?結構體的使用

    深入了解Rust?結構體的使用

    結構體是一種自定義的數(shù)據(jù)類型,它允許我們將多個不同的類型組合成一個整體。下面我們就來學習如何定義和使用結構體,并對比元組與結構體之間的異同,需要的可以參考一下
    2022-11-11
  • Rust調用函數(shù)操作符?.?和?::?的區(qū)別詳解

    Rust調用函數(shù)操作符?.?和?::?的區(qū)別詳解

    在Rust中,.和::操作符都可以用來調用方法,但它們的用法有所不同,所以本文就將詳細的給大家介紹一下.和::操作符的區(qū)別,感興趣的同學跟著小編一起來學習吧
    2023-07-07
  • vscode搭建rust開發(fā)環(huán)境的圖文教程

    vscode搭建rust開發(fā)環(huán)境的圖文教程

    本文主要介紹了vscode搭建rust開發(fā)環(huán)境的圖文教程,文中通過圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-08-08

最新評論