diff --git a/include/dwarfs/error.h b/include/dwarfs/error.h index 4b65ccaf..efddf7d7 100644 --- a/include/dwarfs/error.h +++ b/include/dwarfs/error.h @@ -74,16 +74,22 @@ class system_error : public std::system_error { #define DWARFS_CHECK(expr, message) \ do { \ if (!(expr)) { \ - assertion_failed(#expr, message, std::source_location::current()); \ + ::dwarfs::assertion_failed(#expr, message, \ + std::source_location::current()); \ } \ - } while (0) + } while (false) + +#define DWARFS_PANIC(message) \ + do { \ + ::dwarfs::handle_panic(message, std::source_location::current()); \ + } while (false) #define DWARFS_NOTHROW(expr) \ [&]() -> decltype(expr) { \ try { \ return expr; \ } catch (...) { \ - handle_nothrow(#expr, std::source_location::current()); \ + ::dwarfs::handle_nothrow(#expr, std::source_location::current()); \ } \ }() @@ -96,4 +102,7 @@ handle_nothrow(std::string_view expr, std::source_location loc); assertion_failed(std::string_view expr, std::string_view message, std::source_location loc); +[[noreturn]] void +handle_panic(std::string_view message, std::source_location loc); + } // namespace dwarfs diff --git a/src/error.cpp b/src/error.cpp index faf4269b..a2adb351 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -93,4 +93,10 @@ void assertion_failed(std::string_view expr, std::string_view msg, do_terminate(); } +void handle_panic(std::string_view msg, std::source_location loc) { + std::cerr << "Panic: " << msg << " in " << loc.file_name() << "(" + << loc.line() << ")\n"; + do_terminate(); +} + } // namespace dwarfs