开发者社区> 问答> 正文

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.

展开
收起
a123456678 2016-06-03 19:32:05 3048 0
1 条回答
写回答
取消 提交回答
  • #define FM_LOG_DEBUG(x...)   log_write(LOG_DEBUG, __FILE__, __LINE__, ##x)
    
    static void log_write(int level, const char *file,
            const int line, const char *fmt, ...)
    {
        if (log_opened == 0)
        {
            fprintf(stderr, "log_open not called yet\n");
            exit(1);
        }
        static char buffer[log_buffer_size];
        static char datetime[100];
        static char line_str[20];
        static time_t now;
        now = time(NULL);
    
        strftime(datetime, 99, "%Y-%m-%d %H:%M:%S", localtime(&now));
        snprintf(line_str, 19, "%d", line);
        va_list ap;
        va_start(ap, fmt);   
        vsnprintf(log_buffer, log_buffer_size, fmt, ap);   
        va_end(ap);   
    
        size_t count = snprintf(buffer, log_buffer_size,
                "%s\x7 [%s]\x7 [%s:%d]%s\x7 %s\x7\n", 
                LOG_LEVEL_NOTE[level], datetime, file, line, log_extra_info, log_buffer);
        int log_fd = log_fp->_fileno;
        //puts(buffer);
        if (flock(log_fd, LOCK_EX) == 0)
        {
            if (write(log_fd, buffer, count) < 0)
            {
                perror("write error");
                exit(1);
            }
            flock(log_fd, LOCK_UN);
        }
        else
        {
            perror("flock error");
            exit(1);
        }
    }
    2019-07-17 19:27:10
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载