I have created a printf-like function which takes the following arguments:
- One mandatory argument (custom error code)
- Optional format string
- Variable count of format arguments
The function prototype looks like:
int my_printf(int err_code, ...);
err_code also includes information whether the format string (and possible format tags) are given.
If they are given, I can extract the format string using the va_arg function and pass it to vfprintf together with the rest of the arguments.
The calls look like:
my_printf(-ERR_SIMPLE);
my_printf(-ERR_COMPLICATED, "Error: problem with %d", 123);
Unfortunately, I am unable to use the GCC attribute for format type-checking since it needs a string-index:
format (archetype, string-index, first-to-check)
Is it still possible to do the type-checking anyhow? Solutions using helper macros, helper functions, modifying the optional format string part etc are acceptable.
Update:
I am unsure whether passing a modified va_list to vfprintf-like functions is OK. See comments.
If possible, it would be better to avoid this and use macros or something else for the optional format string part.