我可以为自定义日志记录功能启用格式警告吗?

时间:2017-09-22 15:51:09

标签: c mingw compiler-warnings

出于调试目的,我创建了自定义日志记录功能。出于所有实际目的,让我们说它的定义如下:

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

问题是我的自定义功能会跳过格式警告。

如果我写,例如:

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

我会收到这样的警告:

  

警告:格式'%d'期待匹配的' int'参数[-Wformat]

但如果我打电话,我就不会收到任何警告:

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

有没有办法为我的功能启用这样的警告?

也许我可以告诉mingw-gcc以对待printf的方式处理debugLog? 或者这种警告硬编码到编译器中? (即:gcc只是知道 * printf *系列,但我们不能指望它知道我的功能)

1 个答案:

答案 0 :(得分:1)

至少使用gcc,您可以使用function attribute

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