Hone logo
Hone
Problems

Implementing a Custom Panic Handler in Rust

Rust's panic system provides a mechanism for handling unexpected errors. While Rust has a default panic handler, you can customize it to perform specific actions like logging, cleanup, or providing more informative error messages before the program terminates. This challenge asks you to implement a custom panic handler that logs the panic information to the console and then unwinds the stack.

Problem Description

You are tasked with creating a custom panic handler in Rust. The panic handler should:

  1. Log Panic Information: When a panic occurs, the handler should print the panic message and location (file and line number) to the standard error stream (stderr).
  2. Unwind the Stack: After logging the information, the handler must unwind the stack. This ensures that Drop implementations are called for any resources held by the stack frames, preventing resource leaks.
  3. No Return: The panic handler should not return. It's designed to terminate the program gracefully (as gracefully as possible after a panic).

The goal is to demonstrate your understanding of Rust's panic system and how to intercept and handle panics in a controlled manner.

Examples

Example 1:

Input: A program that panics with the message "Something went wrong!".
Output:
[02/23/2024 10:00:00] PANIC: Something went wrong!
File: src/main.rs:10:5
Explanation: The program panics, the custom handler logs the message and location to stderr, and the program terminates after unwinding the stack. The exact timestamp will vary.

Example 2:

Input: A program that panics within a function that holds a resource (e.g., a `MutexGuard`).
Output:
[02/23/2024 10:00:00] PANIC: Another error occurred.
File: src/main.rs:15:9
Explanation: The program panics, the custom handler logs the message and location. The `MutexGuard` (or any other `Drop` implementation) is automatically dropped during stack unwinding, preventing a potential deadlock or resource leak. The exact timestamp will vary.

Example 3: (Edge Case - No file/line information)

Input: A program that panics with a message that doesn't include file/line information.
Output:
[02/23/2024 10:00:00] PANIC: Generic panic message.
File: <unknown>:0
Explanation: The program panics, the custom handler logs the message and location (using `<unknown>:0` as a default). The program terminates after unwinding the stack. The exact timestamp will vary.

Constraints

  • The panic handler must be implemented using the panic::set_hook function.
  • The handler must accept a &PanicInfo argument.
  • The handler must print the panic message and location to stderr.
  • The handler must unwind the stack. Failure to unwind will result in incorrect behavior and potential resource leaks.
  • The program should compile and run without errors.
  • The panic handler should be simple and efficient. Avoid unnecessary overhead.

Notes

  • The PanicInfo struct contains information about the panic, including the message and location.
  • Consider using the eprintln! macro for printing to stderr.
  • Remember that panics are intended for unrecoverable errors. Use Result for recoverable errors.
  • The std::thread module can be useful for testing your panic handler in a multi-threaded environment (though not strictly required for the basic challenge).
  • The exact format of the output is not strictly enforced, but it should be clear and informative. A timestamp is a good addition.
Loading editor...
rust