For cleaner error handling I use a macro (it uses C99 and GCC extensions); the behavior is like standard assert
:
#define A(cond, msg, ...) ({ \
if (!(cond)) { \
if (msg) \
say(msg, ##__VA_ARGS__); \
else \
say("Error at file %s, function %s, line %u: %s", \
__FILE__, __func__, __LINE__, #cond); \
A_RETURN(); \
} \
})
where say
is a formatted output. And use it like that:
#undef A_RETURN
#define A_RETURN() ({ fclose(f); free(p); return false; })
A(foo() != FOO_ERROR, 0);
A(bar() != BAR_ERROR, "bar failed");
When I don't have a specific error message, I have to write A(cond, 0)
. But I want just write A(cond)
in this case. How to modify my A
macro for this behavior? I.e. I need a way to check if msg
argument isn't passed to the macro.
From the help of suggested question I came to the point that you can modify your macro like this.
so your new
A_NEW
macro call will be expanded toA(cond, 0)
if you don't passmsg
.Variadic macro is explained nicely at this blog.