#include <sys/time.h>
#include <stdio.h>
#include <time.h>
/* debug level define */
int g_dbg_level;
FILE *g_log_fp;
/* debug level */
#define DBG_INFOR 0x01 // call information
#define DBG_WARNING 0x02 // paramters invalid,
#define DBG_ERROR 0x04 // process error, leading to one call fails
#define DBG_CRITICAL 0x08 // process error, leading to voip process can't run exactly or exit
/* debug macro */
#define DBG(level, fmt, para...) do \
{ \
time_t t = time(NULL); \
struct tm *tmm = localtime(&t); \
if(g_dbg_level & level) \
printf("[%d-%02d-%02d %02d:%02d:%02d][%s][%s:%d]" fmt "\n",tmm->tm_year+1900,tmm->tm_mon+1,tmm->tm_mday,tmm->tm_hour,tmm->tm_min,tmm->tm_sec,__FUNCTION__,__FILE__,__LINE__,##para); \
if((g_log_fp != NULL) && (level >= DBG_CRITICAL)) \
fprintf(g_log_fp, "[%d-%02d-%02d %02d:%02d:%02d][%s][%s:%d]" fmt "\n",tmm->tm_year+1900,tmm->tm_mon+1,tmm->tm_mday,tmm->tm_hour,tmm->tm_min,tmm->tm_sec,__FUNCTION__,__FILE__,__LINE__,##para); \
} while(0)
int main()
{
g_dbg_level = 15;
g_log_fp = fopen("my_program.log","a+");
if (g_log_fp == NULL)
{
printf("open log file error\n");
}
DBG(DBG_INFOR,"hello");
DBG(DBG_CRITICAL,"hello 222");
}C语言简单写日志函数
C日志系统实现
最新推荐文章于 2024-03-29 21:29:31 发布
本文介绍了一个简单的C语言日志系统实现方法。该系统能够根据不同级别打印日志,并且可以将关键级别的日志记录到文件中。文章展示了如何定义日志级别、创建宏来格式化并打印日志,以及如何在主函数中进行测试。
4211

被折叠的 条评论
为什么被折叠?



