I like to do it this way in C. This allows calling assert
the following ways:
assert(expression);
assert(expression, "reason");
assert(file, "cannot open %s", filename);
Here is the code:
#define assert(cond, ...) \
if (!(cond)) \
_assert(#cond, __FILE__, __LINE__, #__VA_ARGS__ __VA_OPT__(,) ##__VA_ARGS__)
void _assert (const char* snippet, const char* file, int line, const char* message, ...)
{
print("assert failed %s:%d %s\n", file, line, snippet);
if (*message)
{
va_list arg;
va_start(arg, message);
char* data = va_arg(arg, char*);
vprintf(data, arg);
}
}