From 31638f3cf6cf8266edc0c8b30f44407229f79637 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Fri, 15 May 2026 22:36:02 +0800 Subject: Add option to silence panic messages in stdout settings --- CHANGELOG.md | 1 + mingling_core/src/program.rs | 20 ++++++++++++++++++++ mingling_core/src/program/config.rs | 4 ++++ 3 files changed, 25 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 89c0ba5..7fec970 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -79,6 +79,7 @@ this::().modify_res::(|code| { ``` 11. **\[core\]** Added panic catch for program execution. +12. **\[core\]** Added the `stdout_setting.silence_panic` option, which is disabled by default. When enabled, `Program`'s `panic!` will not output to the console #### **BREAKING CHANGES**: diff --git a/mingling_core/src/program.rs b/mingling_core/src/program.rs index e7e9ec0..a00b2c7 100644 --- a/mingling_core/src/program.rs +++ b/mingling_core/src/program.rs @@ -193,6 +193,16 @@ where .unwrap() .downcast_ref::>() .unwrap(); + + #[cfg(not(panic = "abort"))] + if program.stdout_setting.silence_panic { + std::panic::set_hook(Box::new(|_| {})); + } + + #[cfg(panic = "abort")] + return Ok(f(program)); + + #[cfg(not(panic = "abort"))] match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| f(program))) { Ok(fut) => Ok(fut.await), Err(panic_info) => { @@ -294,6 +304,16 @@ where .unwrap() .downcast_ref::>() .unwrap(); + + #[cfg(not(panic = "abort"))] + if program.stdout_setting.silence_panic { + std::panic::set_hook(Box::new(|_| {})); + } + + #[cfg(panic = "abort")] + return Ok(f(program)); + + #[cfg(not(panic = "abort"))] match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| f(program))) { Ok(result) => Ok(result), Err(panic_info) => { diff --git a/mingling_core/src/program/config.rs b/mingling_core/src/program/config.rs index 35b9392..c3b1b0e 100644 --- a/mingling_core/src/program/config.rs +++ b/mingling_core/src/program/config.rs @@ -7,6 +7,9 @@ pub struct ProgramStdoutSetting { /// Render results and output pub render_output: bool, + /// Silence panic messages + pub silence_panic: bool, + #[cfg(feature = "clap")] /// Behavior when Clap Dispatcher outputs help information pub clap_help_print_behaviour: ClapHelpPrintBehaviour, @@ -28,6 +31,7 @@ impl Default for ProgramStdoutSetting { ProgramStdoutSetting { error_output: true, render_output: true, + silence_panic: false, #[cfg(feature = "clap")] clap_help_print_behaviour: ClapHelpPrintBehaviour::default(), } -- cgit