diff --git a/include/utils.hpp b/include/utils.hpp index 5c2926f..a41ce7b 100644 --- a/include/utils.hpp +++ b/include/utils.hpp @@ -23,7 +23,7 @@ /** @def DEBUG(msg) * @brief Writes a debug message * - * This function writes a debug message that includes the filename, + * This macro writes a debug message that includes the filename, * line number, and a custom message. The function is wrapped in an ifdef * that checks if DBG is defined, so one can choose to display the debug * messages by adding the -DDBG flag when compiling. @@ -35,7 +35,14 @@ #define DEBUG(msg) #endif -#define ASSERT(expr) m_assert(expr, #expr, __FUNCTION__, __FILE__, __LINE__) +/** @def ASSERT(expr) + * @brief A prettier assertion function. + * + * This macro calls the m_assert function which is a more informative + * assertion function than the regular assert function from cassert. + * */ +#define ASSERT(expr, msg) m_assert(expr, #expr, __FUNCTION__, __FILE__, \ + __LINE__, msg) /** Code stolen from https://github.com/anderkve/FYS3150 * Header: https://github.com/anderkve/FYS3150/blob/master/code_examples/compilation_linking/example_1/include/utils.hpp @@ -64,6 +71,23 @@ std::string scientific_format(const std::vector& v, int width=20, int prec=10); -void m_assert(bool expr, const char* expr_str, const char* func, const char* file, int line); +/** @brief Test an expression, confirm that test is ok, or abort execution. + * + * This function takes in an expression and prints an OK message if it's + * true, or it prints a fail message and aborts execution if it fails. + * + * @param expr The expression to be evaluated + * @param expr_str The stringified version of the expression + * @param func The function name of the caller + * @param file The file of the caller + * @param line The line number where this function is called from + * @param msg The message to be displayed + * */ +void m_assert(bool expr, + const char* expr_str, + const char* func, + const char* file, + int line, + const char* msg); #endif diff --git a/src/utils.cpp b/src/utils.cpp index 3c35a9d..f326b19 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -11,7 +11,9 @@ * */ #include "utils.hpp" #include +#include #include +#include std::string scientific_format(double d, int width, int prec) { @@ -29,19 +31,35 @@ std::string scientific_format(const std::vector& v, int width, int prec) return ss.str(); } +static void print_message(const char* msg) +{ + if (strlen(msg) > 0) { + std::cout << "message: " << msg << "\n\n"; + } + else { + std::cout << "\n"; + } +} + void m_assert(bool expr, const char* expr_str, const char* f, const char* file, - int line) + int line, + const char* msg) { - printf("%s: ", f); + std::string new_assert(strlen(f) + (expr ? 4 : 6), '-'); + std::cout << "\x1B[36m" << new_assert << "\033[0m\n"; + std::cout << f << ": "; if (expr) { - printf("\x1B[32mOK\033[0m\n\n"); + std::cout << "\x1B[32mOK\033[0m\n"; + print_message(msg); } else { - printf("\x1B[31mFAIL\033[0m\n\n"); - printf("%s %d: Assertion (%s) Failed\n\n", file, line, expr_str); + std::cout << "\x1B[31mFAIL\033[0m\n"; + print_message(msg); + std::cout << file << " " << line << ": Assertion \"" + << expr_str << "\" Failed\n\n"; abort(); } }