【问题标题】:Can I enable format warnings for my custom logging function?我可以为我的自定义日志记录功能启用格式警告吗?
【发布时间】:2017-09-22 15:51:09
【问题描述】:

出于调试目的,我创建了一个自定义日志记录功能。出于所有实际目的,假设它是这样定义的:

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* 系列,但我们不能指望它知道我的功能)

【问题讨论】:

标签: c mingw compiler-warnings


【解决方案1】:

至少使用 gcc,你可以使用function attribute:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-02
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多