Good day, I would like to ask if you could give me an example of how to effectively write a panic message to a file? I would need it for my project because it will not be possible to write it in windows cmd there.
Judging from this link found in this SO question, both PowerShell and Windows
CMD support the
> redirection operator. The easiest solution would be to execute your program like this:
your_program.exe 2> file_with_error_message.txt
2> file_with_error_message.txt takes the stderr output from the previous command and writes it to
EDIT: My bad, I didn't read your question properly. You aren't calling your program from
CMD or PowerShell. You can use the
catch_unwind function to programmatically catch panics, convert them into a
Result, write it to file if it is an
Err and only after that unwind your program. Be aware though that
catch_unwind may not catch all panics.
Be also aware that an uncaught panic while inside a panic handler leads to an immediate abort, so for maximum reliability you need to think about a bit how to handle any I/O errors that may occur while logging the panic message.
Edit: note that there’s also
std::panic::set_hook that is run whether or not panics are configured to be unwinding or not. The default hook is what is responsible for writing the standard message to stdout. You cannot stop a panic in a panic hook, unlike in
catch_unwind, but as that’s not required in your use case,
set_hook seems to be a good fit.
problem is that my rust code is added to dll converted and added to my
old project so cmd not possible and catch unwind must be then added to
every function also hard to do that becauase there is many functions
2023-01-07 14:46 GMT+01:00, Johannes Dahlström via The Rust
Programming Language Forum
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.