#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#define GlobalDebugFlag 1
#define DEBUG_PRINT_WITH_TIME(fmt, args...) { \
do \
{ \
if (GlobalDebugFlag == 1 ) \
{ \
time_t timep = {0}; \
struct tm *p = NULL; \
time(&timep); \
p=localtime(&timep); \
fprintf(stdout, "[%4d-%02d-%02d %02d:%02d:%02d %s %s %d] ", \
(1900+p->tm_year), \
(1+p->tm_mon), \
p->tm_mday, \
p->tm_hour, \
p->tm_min, \
p->tm_sec, \
__FILE__, \
__FUNCTION__, \
__LINE__); \
fprintf(stdout, fmt "\n", ##args); \
} \
} \
while(0); \
}
C语言添加DEBUG,可以显示时间、文件、函数、行对于调试时比较方便
最新推荐文章于 2023-11-01 10:17:08 发布
该代码段展示了在C语言中如何使用宏定义DEBUG_PRINT_WITH_TIME来实现条件性的日志打印,当全局调试标志GlobalDebugFlag为1时,函数会输出带有时间戳、文件名、函数名和行号的信息,便于调试和追踪程序执行流程。
摘要由CSDN通过智能技术生成