diff options
| -rw-r--r-- | CHANGELOG.md | 1 | ||||
| -rw-r--r-- | mingling_core/src/program.rs | 20 | ||||
| -rw-r--r-- | mingling_core/src/program/config.rs | 4 |
3 files changed, 25 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 89c0ba5..7fec970 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -79,6 +79,7 @@ this::<ThisProgram>().modify_res::<ExitCode>(|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::<Program<C>>() .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::<Program<C>>() .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(), } |
