Skip to content

Commit

Permalink
improve the ergonomics of ERROR and ASSERT
Browse files Browse the repository at this point in the history
  • Loading branch information
mabruzzo committed Sep 15, 2023
1 parent f8db7c8 commit 0b25556
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/global/global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void Set_Gammas(Real gamma_in)
{
// set gamma
gama = gamma_in;
ASSERT(gama > 1.0, "Set_Gammas", "Gamma must be greater than one.");
ASSERT(gama > 1.0, "Gamma must be greater than one.");
}

/*! \fn double get_time(void)
Expand Down
8 changes: 4 additions & 4 deletions src/io/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2788,10 +2788,10 @@ void Ensure_Outdir_Exists(std::string outdir)
// to a directory (it's unclear from docs whether err-code is set in that
// case)
if (err_code or not std::filesystem::is_directory(without_file_prefix)) {
ERROR("Ensure_Outdir_Exists",
"something went wrong while trying to create the path to the "
"output-dir: %s",
outdir.c_str());
ERROR(
"something went wrong while trying to create the path to the "
"output-dir: %s",
outdir.c_str());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/error_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void Check_Configuration(parameters const& P)
// Check the boundary conditions
auto Check_Boundary = [](int const& boundary, std::string const& direction) {
bool is_allowed_bc = boundary >= 0 and boundary <= 4;
ASSERT(is_allowed_bc, "Check_Configuration",
ASSERT(is_allowed_bc,
"WARNING: Possibly invalid boundary conditions for direction: %s flag: %d. "
"Must select between 0 (no boundary), 1 (periodic), 2 (reflective), 3 (transmissive), 4 (custom), 5 (mpi).",
direction.c_str(), boundary);
Expand Down
32 changes: 22 additions & 10 deletions src/utils/error_handling.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,53 @@ void Check_Configuration(parameters const& P);
*/
[[noreturn]] void Abort_With_Err_(const char* func_name, const char* file_name, int line_num, const char* msg, ...);

/* __CHOLLA_PRETTY_FUNC__ is a magic constant like __LINE__ or __FILE__ that
* provides the name of the current function.
* - The C++11 standard requires that __func__ is provided on all platforms, but
* that only provides limited information (just the name of the function).
* - Where available, we prefer to use compiler-specific features that provide
* more information about the function (like the scope of the function & the
* the function signature).
*/
#ifdef __GNUG__
#define __CHOLLA_PRETTY_FUNC__ __PRETTY_FUNCTION__
#else
#define __CHOLLA_PRETTY_FUNC__ __func__
#endif

/*!
* \brief print an error-message (with printf formatting) & abort the program.
*
* This macro should be treated as a function with the signature:
* [[noreturn]] void ERROR(const char* func_name, const char* msg, ...);
* [[noreturn]] void ERROR(const char* msg, ...);
*
* - The 1st arg is the name of the function where it's called
* - The 2nd arg is printf-style format argument specifying the error message
* - The 1st arg is printf-style format argument specifying the error message
* - The remaining args arguments are used to format error message
*
* \note
* the ``msg`` string is part of the variadic args so that there is always
* at least 1 variadic argument (even in cases when ``msg`` doesn't format
* any arguments). There is no way around this until C++ 20.
*/
#define ERROR(func_name, ...) Abort_With_Err_(func_name, __FILE__, __LINE__, __VA_ARGS__)
#define ERROR(...) Abort_With_Err_(__CHOLLA_PRETTY_FUNC__, __FILE__, __LINE__, __VA_ARGS__)

/*!
* \brief if the condition is false, print an error-message (with printf
* formatting) & abort the program.
*
* This macro should be treated as a function with the signature:
* [[noreturn]] void ASSERT(bool cond, const char* func_name, const char* msg, ...);
* [[noreturn]] void ASSERT(bool cond, const char* msg, ...);
*
* - The 1st arg is a boolean condition. When true, this does noth
* - The 2nd arg is the name of the function where it's called
* - The 3rd arg is printf-style format argument specifying the error message
* - The 2nd arg is printf-style format argument specifying the error message
* - The remaining args arguments are used to format error message
*
* \note
* the behavior is independent of the ``NDEBUG`` macro
*/
#define ASSERT(cond, func_name, ...) \
if (not(cond)) { \
Abort_With_Err_(func_name, __FILE__, __LINE__, __VA_ARGS__); \
#define ASSERT(cond, ...) \
if (not(cond)) { \
Abort_With_Err_(__CHOLLA_PRETTY_FUNC__, __FILE__, __LINE__, __VA_ARGS__); \
}

#endif /*ERROR_HANDLING_CHOLLA_H*/

0 comments on commit 0b25556

Please sign in to comment.