C++日志之获取函数的名字,行号,文件名

ANSI C标准中有几个标准预定义宏(也是常用的):

LINE:在源代码中插入当前源代码行号;

FILE:在源文件中插入当前源文件名;

DATE:在源文件中插入当前的编译日期

TIME:在源文件中插入当前编译时间;

STDC:当要求程序严格遵循ANSI C标准时该标识被赋值为1;

__cplusplus:当编写C++程序时该标识符被定义。

代码:

#define LOG(level, format, ...) /
    do { /
        fprintf(stderr, "[%s|%s@%s,%d] " format "/n", /
            level, __func__, __FILE__, __LINE__, ##__VA_ARGS__ ); /
    } while (0)
 
int main()
{
    LOG(LOG_DEBUG, "a=%d", 10);
    return 0;
}

运行结果:

[DEBUG|main@a.c,17] a=10

ref

posted @ 2024-04-07 17:31  guanyubo  阅读(241)  评论(0)    收藏  举报