Add arguments to mxb_assert_message

The macro can now be used to print runtime information in a printf-like
manner. This makes it easier to see exactly why an assertion has failed.
This commit is contained in:
Markus Mäkelä
2019-06-11 13:29:12 +03:00
parent 74f61c233d
commit 6166da76ea
3 changed files with 9 additions and 5 deletions

View File

@ -31,9 +31,11 @@ MXB_BEGIN_DECLS
fprintf(stderr, "debug assert at %s:%d failed: %s\n", (char*)__FILE__, __LINE__, debug_expr); \
raise(SIGABRT);}} while (false)
#define mxb_assert_message(exp, message) \
#define mxb_assert_message(exp, fmt, ...) \
do {if (exp) {} else { \
const char* debug_expr = #exp; \
char message[1024]; \
snprintf(message, sizeof(message), fmt, ##__VA_ARGS__); \
MXB_ERROR("debug assert at %s:%d failed: %s (%s)\n", \
(char*)__FILE__, \
__LINE__, \

View File

@ -1966,7 +1966,8 @@ const
string value = get_string(key);
milliseconds duration {0};
MXB_AT_DEBUG(bool rval = ) get_suffixed_duration(value.c_str(), interpretation, &duration);
mxb_assert(rval); // When this function is called, the validity of the value should have been checked.
// When this function is called, the validity of the value should have been checked.
mxb_assert_message(rval, "Invalid value for '%s': %s", key.c_str(), value.c_str());
return duration;
}

View File

@ -285,19 +285,20 @@ void test_multiple_sql_packets1()
mxb_assert_message(complete && head, "Both buffers should have data");
mxb_assert_message(gwbuf_length(complete) + gwbuf_length(head) + gwbuf_length(quarter)
+ gwbuf_length(half) == sizeof(resultset),
"25% of data should be available");
"a quarter of data should be available");
quarter = gwbuf_append(gwbuf_append(complete, head), quarter);
complete = modutil_get_complete_packets(&quarter);
mxb_assert_message(gwbuf_length(complete) + gwbuf_length(quarter)
+ gwbuf_length(half) == sizeof(resultset),
"50% of data should be available");
"half of data should be available");
half = gwbuf_append(gwbuf_append(complete, quarter), half);
complete = modutil_get_complete_packets(&half);
mxb_assert_message(complete, "Complete should not be NULL");
mxb_assert_message(half == NULL, "Old buffer should be NULL");
mxb_assert_message(gwbuf_length(complete) == sizeof(resultset), "Complete should contain 100% of data");
mxb_assert_message(gwbuf_length(complete) == sizeof(resultset),
"Complete should contain all of the data");
completelen = gwbuf_length(complete);
mxb_assert_message(gwbuf_copy_data(complete, 0, completelen, databuf) == completelen,