8 #ifndef MUTILS_EXCEPTION_H 9 #define MUTILS_EXCEPTION_H 24 #define MU_THROW(ex, src, msg, ...) do { \ 26 bool db = (mutils::Log::level >= mutils::Log::DEBUG); \ 27 mutils::cxxsnprintf(_mu_buf, sizeof(_mu_buf), "%s:%0*d: " msg "\n", \ 28 (db ? (src) : ""), (db ? 1 : 4), __LINE__, ## __VA_ARGS__); \ 29 if (mutils::Exception::errors) { \ 30 mutils::Log::error(_mu_buf); \ 32 (ex).message(_mu_buf); \ 44 #define MU_RETHROW(ex, src, msg, ...) do { \ 45 (ex).print(); MU_THROW(ex, src, msg, ## __VA_ARGS__); \ 56 #define MU_THROW_IF(cond, ex, src, msg, ...) do { \ 57 if (cond) { MU_THROW(ex, src, msg, ## __VA_ARGS__); } \ 69 #define MU_THROW_ERRNO(ex, src, msg, ...) \ 70 MU_THROW(ex, src, msg ": %s", ## __VA_ARGS__, strerror(errno)) 81 #define MU_THROW_ERRNO_IF(cond, ex, src, msg, ...) \ 82 MU_THROW_IF(cond, ex, src, msg ": %s", ## __VA_ARGS__, strerror(errno)) 90 #define MU_EXCEPTION(src, msg, ...) do { \ 91 mutils::Exception _mu_ex; \ 92 MU_THROW(_mu_ex, src, msg, ## __VA_ARGS__); \ 101 #define MU_EXCEPTION_IF(cond, src, msg, ...) do { \ 102 mutils::Exception _mu_ex; \ 103 MU_THROW_IF(cond, _mu_ex, src, msg, ## __VA_ARGS__); \ 112 #define MU_EXCEPTION_ERRNO(src, msg, ...) do { \ 113 mutils::Exception _mu_ex; \ 114 MU_THROW_ERRNO(_mu_ex, src, msg, ## __VA_ARGS__); \ 124 #define MU_EXCEPTION_ERRNO_IF(cond, src, msg, ...) do { \ 125 mutils::Exception _mu_ex; \ 126 MU_THROW_ERRNO_IF(cond, _mu_ex, src, msg, ## __VA_ARGS__); \ 196 auto mex = dynamic_cast<mutils::Exception *>(&ex);
197 if (mex) { mex->print(); }
198 else { fprintf(stderr,
"%s\n", ex.
what()); }
211 fprintf(stderr,
"Exception caught (Log::file = %s); exiting...\n",
218 #endif // MUTILS_EXCEPTION_H static bool errors
Whether to print error messages (else throw exceptions).
static std::string file
Message log file name.
static FILE * stream
Message log file stream.
MathUtils exception (base) class.
Exception(int level=Log::ERROR)
Default constructor.
static const char * name(int lev=mutils::Log::level)
Standard logging level name string.
std::string msg_
Error message.
Declares diagnostic message macros.
virtual ~Exception() noexcept
Destructor.
virtual const char * what() const noexcept
Returns error string.
void print() const
Prints error message to log.
void exitException(std::exception &ex, int status=EXIT_FAILURE)
Exits program with exception message. Exits the program (with status) after displaying the exception ...
static int level
Message logging level for output to stream.
static constexpr int ERROR
Error condition.
void message(const char *msg)
Assigns error message.
int level
Condition log level.
void printException(std::exception &ex)
Prints exception message.