3

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)

Roflo
  • 235
  • 1
  • 9
  • 27
  • 2
    Answer can be found in this other question: https://stackoverflow.com/questions/5825270/printflike-modifier – Steve Summit Sep 22 '17 at 15:56
  • Thanks @SteveSummit, that Q also lead me to this related question for MSVC: [__attribute__((format(printf, 1, 2))) for MSVC?](https://stackoverflow.com/questions/2354784/attribute-formatprintf-1-2-for-msvc) – Roflo Sep 22 '17 at 16:22

1 Answers1

2

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

__attribute__((format(printf, 1, 2)))
void debugLog(const char * s, ...) {
    ...
}
P.P
  • 117,907
  • 20
  • 175
  • 238