C语言 如何在宏定义中使用可变参数
有一个记录日志的函数,想用宏定义进行包装,简化调用方法,但是xlc报如下的错误:
void _log(const int level, char* file, int line, const char* fmt, ...){ va_list ap; int count; time_t t; struct tm* st; char str_time[255]; memset(str_time, 0x00, sizeof(str_time)); time(&t); st= localtime(&t); switch(level){ case DEBUG: fprintf(stdout, "[DEBUG] "); break; case INFO: fprintf(stdout, "[INFO] "); break; case WARNING: fprintf(stdout, "[WARNING] "); break; case ERROR: fprintf(stdout, "[ERROR] "); break; default: fprintf(stdout, "[INFO] "); break; } strftime( str_time, sizeof(str_time), "%Y-%m-%d %H:%M:%S", st); fprintf(stdout, "[%s] LINE:[%04d] [%s] ", file, line, str_time); va_start(ap, fmt); count = vfprintf(stdout, fmt, ap); fprintf(stdout, "\n"); fflush(stdout); va_end(ap); } #define log_info(...) _log((INFO), (__FILE__), (__LINE__), "", (#__VA_ARGS__)) #define log_err(...) _log((ERROR), (__FILE__), (__LINE__), "", (#__VA_ARGS__))
Syntax error at line 71, column 18, file RProCrminfo.cp:
Error at line 71, column 18 in file RProCrminfo.cp
- define log_info((aaa), ...) _log((INFO), (__FILE__), (__LINE__), "", (#__VA_ARG
S__))
.................1
PCC-S-02014, Encountered the symbol "(" when expecting one of the following:
) ... an identifier, define, elif, else, endif, error, if,
ifdef, ifndef, include, line, pragma, undef, exec, sql,
begin, end, var, type, oracle,
an immediate preprocessor command, a C token, create,
function, package, procedure, trigger, or, replace,
The symbol ")" was substituted for "(" to continue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
早先一个项目woj-land里用到的,供参考