mirror of
https://github.com/matrix-construct/construct
synced 2024-11-25 08:12:37 +01:00
ircd: Make ircd::terminate / ircd::assertion funcjects.
This commit is contained in:
parent
589858b412
commit
9299b0df9f
3 changed files with 47 additions and 42 deletions
|
@ -21,19 +21,9 @@ namespace boost::system
|
|||
|
||||
namespace ircd
|
||||
{
|
||||
// Root exception
|
||||
struct 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);
|
||||
struct terminate;
|
||||
struct assertion;
|
||||
struct exception; // Root exception
|
||||
|
||||
// util
|
||||
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
|
||||
///
|
||||
/// If you want to create your own exception type, you have found the right
|
||||
|
|
|
@ -177,20 +177,16 @@ noexcept
|
|||
return size;
|
||||
}
|
||||
|
||||
void
|
||||
ircd::assertion()
|
||||
ircd::assertion::assertion()
|
||||
noexcept(RB_DEBUG_LEVEL)
|
||||
:assertion
|
||||
{
|
||||
"without exception"_sv
|
||||
}
|
||||
{
|
||||
static const auto &default_message
|
||||
{
|
||||
"without exception"_sv
|
||||
};
|
||||
|
||||
assertion(default_message);
|
||||
}
|
||||
|
||||
void
|
||||
ircd::assertion(const string_view &msg)
|
||||
ircd::assertion::assertion(const string_view &msg)
|
||||
noexcept(RB_DEBUG_LEVEL)
|
||||
{
|
||||
log::critical
|
||||
|
@ -199,7 +195,10 @@ noexcept(RB_DEBUG_LEVEL)
|
|||
};
|
||||
|
||||
if(std::uncaught_exceptions())
|
||||
assertion(std::current_exception());
|
||||
assertion
|
||||
{
|
||||
std::current_exception()
|
||||
};
|
||||
|
||||
assert(0);
|
||||
throw assertive
|
||||
|
@ -208,23 +207,21 @@ noexcept(RB_DEBUG_LEVEL)
|
|||
};
|
||||
}
|
||||
|
||||
void
|
||||
ircd::assertion(std::exception_ptr eptr)
|
||||
ircd::assertion::assertion(std::exception_ptr eptr)
|
||||
noexcept(RB_DEBUG_LEVEL) try
|
||||
{
|
||||
std::rethrow_exception(eptr);
|
||||
}
|
||||
catch(const std::exception &e)
|
||||
{
|
||||
assertion(e);
|
||||
assertion{e};
|
||||
}
|
||||
|
||||
void
|
||||
ircd::assertion(const std::exception &e)
|
||||
ircd::assertion::assertion(const std::exception &e)
|
||||
noexcept(RB_DEBUG_LEVEL)
|
||||
{
|
||||
#ifdef RB_DEBUG
|
||||
terminate(e);
|
||||
terminate{e};
|
||||
#else
|
||||
log::critical
|
||||
{
|
||||
|
@ -235,15 +232,16 @@ noexcept(RB_DEBUG_LEVEL)
|
|||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
ircd::terminate()
|
||||
ircd::terminate::terminate()
|
||||
noexcept
|
||||
:terminate
|
||||
{
|
||||
std::current_exception()
|
||||
}
|
||||
{
|
||||
terminate(std::current_exception());
|
||||
}
|
||||
|
||||
void
|
||||
ircd::terminate(std::exception_ptr eptr)
|
||||
ircd::terminate::terminate(std::exception_ptr eptr)
|
||||
noexcept
|
||||
{
|
||||
if(eptr) try
|
||||
|
@ -252,7 +250,7 @@ noexcept
|
|||
}
|
||||
catch(const std::exception &e)
|
||||
{
|
||||
terminate(e);
|
||||
terminate{e};
|
||||
}
|
||||
|
||||
log::critical
|
||||
|
@ -267,8 +265,7 @@ noexcept
|
|||
std::terminate();
|
||||
}
|
||||
|
||||
void
|
||||
ircd::terminate(const std::exception &e)
|
||||
ircd::terminate::terminate(const std::exception &e)
|
||||
noexcept
|
||||
{
|
||||
log::critical
|
||||
|
@ -277,8 +274,7 @@ noexcept
|
|||
};
|
||||
|
||||
fprintf(stderr, "\nIRCd Terminated: %s\n", e.what());
|
||||
|
||||
::fflush(stdout);
|
||||
::fflush(stderr);
|
||||
::fflush(stdout);
|
||||
std::terminate();
|
||||
}
|
||||
|
|
|
@ -306,8 +306,7 @@ try
|
|||
}
|
||||
catch(const std::exception &e)
|
||||
{
|
||||
ircd::assertion(e);
|
||||
return;
|
||||
ircd::assertion{e};
|
||||
}
|
||||
|
||||
bool
|
||||
|
|
Loading…
Reference in a new issue