我可以为我的自定义日志记录功能启用格式警告吗?
Can I enable format warnings for my custom logging function?
出于调试目的,我创建了一个自定义日志记录函数。出于所有实际目的,假设它是这样定义的:
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");
我会收到这样的警告:
warning: format '%d' expects a matching 'int' argument [-Wformat]
但是如果我调用:
我不会得到任何警告
debugLog("Received error: %d.\n");
有没有办法为我的函数启用这样的警告?
也许我可以告诉 mingw-gcc 像对待 printf 一样对待 debugLog?
或者这种警告是硬编码到编译器中的吗? (即:gcc 只是 知道 *printf* 系列,但我们不能指望它知道我的功能)
至少对于 gcc,你可以使用 function attribute:
__attribute__((format(printf, 1, 2)))
void debugLog(const char * s, ...) {
...
}
出于调试目的,我创建了一个自定义日志记录函数。出于所有实际目的,假设它是这样定义的:
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");
我会收到这样的警告:
warning: format '%d' expects a matching 'int' argument [-Wformat]
但是如果我调用:
我不会得到任何警告debugLog("Received error: %d.\n");
有没有办法为我的函数启用这样的警告?
也许我可以告诉 mingw-gcc 像对待 printf 一样对待 debugLog? 或者这种警告是硬编码到编译器中的吗? (即:gcc 只是 知道 *printf* 系列,但我们不能指望它知道我的功能)
至少对于 gcc,你可以使用 function attribute:
__attribute__((format(printf, 1, 2)))
void debugLog(const char * s, ...) {
...
}