C语言简单写日志函数
2014-12-11 09:52
246 查看
#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语言实现的日志函数
- C语言中三角函数的简单应用——HDU2080
- C语言封装自己的日志函数
- 简单的程序日志打印函数WriteLog,适用于vc等,含宽字符。
- 20140629:简单的打印函数执行日志的函数代码WriteLog函数(适用于c++builder等,未加载lib文件)
- 【学习ios之路:C语言】函数及递归的简单应用
- Linux下简单的日志功能实现(C语言)
- C语言中函数指针、指针函数、回调函数的简单含义
- C语言10的n次方pow函数不好用,自己写一个简单的
- c语言中字符搜索函数的简单总结
- C语言可变参数日志函数
- c语言两简单的函数------字符输入输出------【getchar、putchar(parm)】
- 用C语言的可变参函数实现一个简单的printf函数
- C语言实现简单的分级别写日志程序
- linux下C语言之调用简单函数
- tc2.0环境下的C语言研究-简单使用寄存器并打印函数的偏移地址
- C语言 函数的简单使用(库函数和自定义函数)
- C/C++程序员:排序算法之标准C语言qsort函数简单用法介绍
- zlog 纯C的日志函数库的简单使用方法