3
votes

For debugging purposes, I've created a custom logging function. For all practical purposes, let's say it's defined like this:

void debugLog(const char * s, ...) {
    va_list args;
    va_start(args, s);
    if(NULL!=logfp) {
        vfprintf(logfp, s, args);
    }
    va_end(args);
}

The problem is that format warnings are skipped for my custom function.

If I write, for example:

fprintf(logfp, "Received error: %d.\n");

I'll get a warning like this:

warning: format '%d' expects a matching 'int' argument [-Wformat]

But I won't get any warning if I call:

debugLog("Received error: %d.\n");

Is there a way to enable such a warning for my function?

Maybe I can tell mingw-gcc to treat debugLog the way it would treat printf? Or are this kind of warnings hard-coded into the compiler? (ie: gcc simply knows the *printf* family but we can't expect it to know my function)

1
Answer can be found in this other question: stackoverflow.com/questions/5825270/printflike-modifier - Steve Summit
Thanks @SteveSummit, that Q also lead me to this related question for MSVC: __attribute__((format(printf, 1, 2))) for MSVC? - Roflo

1 Answers

2
votes

At least with gcc, you can use the function attribute:

__attribute__((format(printf, 1, 2)))
void debugLog(const char * s, ...) {
    ...
}