0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-29 02:02:38 +01:00

ircd: Make ircd::terminate / ircd::assertion funcjects.

This commit is contained in:
Jason Volk 2018-11-04 17:18:00 -08:00
parent 589858b412
commit 9299b0df9f
3 changed files with 47 additions and 42 deletions

View file

@ -21,19 +21,9 @@ namespace boost::system
namespace ircd namespace ircd
{ {
// Root exception struct terminate;
struct exception; struct assertion;
struct exception; // Root exception
// Prefer ircd::terminate() to std::terminate() if possible.
[[noreturn]] void terminate(const std::exception &) noexcept;
[[noreturn]] void terminate(std::exception_ptr) noexcept;
[[noreturn]] void terminate() noexcept;
// Terminates in debug mode; throws in release mode; always logs critical.
[[noreturn]] void assertion(const std::exception &) noexcept(RB_DEBUG_LEVEL);
[[noreturn]] void assertion(std::exception_ptr) noexcept(RB_DEBUG_LEVEL);
[[noreturn]] void assertion(const string_view &) noexcept(RB_DEBUG_LEVEL);
[[noreturn]] void assertion() noexcept(RB_DEBUG_LEVEL);
// util // util
std::error_code make_error_code(const int &code = errno); std::error_code make_error_code(const int &code = errno);
@ -110,6 +100,26 @@ struct ircd::exception
} }
}; };
/// Terminates in debug mode; throws in release mode; always logs critical.
/// Not a replacement for a standard assert() macro. Used specifically when
/// the assert should also be hit in release-mode when NDEBUG is set. This
/// is basically the project's soft assert for now.
struct ircd::assertion
{
[[noreturn]] assertion(const std::exception &) noexcept(RB_DEBUG_LEVEL);
[[noreturn]] assertion(std::exception_ptr) noexcept(RB_DEBUG_LEVEL);
[[noreturn]] assertion(const string_view &) noexcept(RB_DEBUG_LEVEL);
[[noreturn]] assertion() noexcept(RB_DEBUG_LEVEL);
};
/// Always prefer ircd::terminate() to std::terminate() for all project code.
struct ircd::terminate
{
[[noreturn]] terminate(const std::exception &) noexcept;
[[noreturn]] terminate(std::exception_ptr) noexcept;
[[noreturn]] terminate() noexcept;
};
/// Exception generator convenience macro /// Exception generator convenience macro
/// ///
/// If you want to create your own exception type, you have found the right /// If you want to create your own exception type, you have found the right

View file

@ -177,20 +177,16 @@ noexcept
return size; return size;
} }
void ircd::assertion::assertion()
ircd::assertion()
noexcept(RB_DEBUG_LEVEL) noexcept(RB_DEBUG_LEVEL)
{ :assertion
static const auto &default_message
{ {
"without exception"_sv "without exception"_sv
}; }
{
assertion(default_message);
} }
void ircd::assertion::assertion(const string_view &msg)
ircd::assertion(const string_view &msg)
noexcept(RB_DEBUG_LEVEL) noexcept(RB_DEBUG_LEVEL)
{ {
log::critical log::critical
@ -199,7 +195,10 @@ noexcept(RB_DEBUG_LEVEL)
}; };
if(std::uncaught_exceptions()) if(std::uncaught_exceptions())
assertion(std::current_exception()); assertion
{
std::current_exception()
};
assert(0); assert(0);
throw assertive throw assertive
@ -208,23 +207,21 @@ noexcept(RB_DEBUG_LEVEL)
}; };
} }
void ircd::assertion::assertion(std::exception_ptr eptr)
ircd::assertion(std::exception_ptr eptr)
noexcept(RB_DEBUG_LEVEL) try noexcept(RB_DEBUG_LEVEL) try
{ {
std::rethrow_exception(eptr); std::rethrow_exception(eptr);
} }
catch(const std::exception &e) catch(const std::exception &e)
{ {
assertion(e); assertion{e};
} }
void ircd::assertion::assertion(const std::exception &e)
ircd::assertion(const std::exception &e)
noexcept(RB_DEBUG_LEVEL) noexcept(RB_DEBUG_LEVEL)
{ {
#ifdef RB_DEBUG #ifdef RB_DEBUG
terminate(e); terminate{e};
#else #else
log::critical log::critical
{ {
@ -235,15 +232,16 @@ noexcept(RB_DEBUG_LEVEL)
#endif #endif
} }
void ircd::terminate::terminate()
ircd::terminate()
noexcept noexcept
:terminate
{
std::current_exception()
}
{ {
terminate(std::current_exception());
} }
void ircd::terminate::terminate(std::exception_ptr eptr)
ircd::terminate(std::exception_ptr eptr)
noexcept noexcept
{ {
if(eptr) try if(eptr) try
@ -252,7 +250,7 @@ noexcept
} }
catch(const std::exception &e) catch(const std::exception &e)
{ {
terminate(e); terminate{e};
} }
log::critical log::critical
@ -267,8 +265,7 @@ noexcept
std::terminate(); std::terminate();
} }
void ircd::terminate::terminate(const std::exception &e)
ircd::terminate(const std::exception &e)
noexcept noexcept
{ {
log::critical log::critical
@ -277,8 +274,7 @@ noexcept
}; };
fprintf(stderr, "\nIRCd Terminated: %s\n", e.what()); fprintf(stderr, "\nIRCd Terminated: %s\n", e.what());
::fflush(stdout);
::fflush(stderr); ::fflush(stderr);
::fflush(stdout);
std::terminate(); std::terminate();
} }

View file

@ -306,8 +306,7 @@ try
} }
catch(const std::exception &e) catch(const std::exception &e)
{ {
ircd::assertion(e); ircd::assertion{e};
return;
} }
bool bool