Chinaunix首页 | 论坛 | 博客
  • 博客访问: 204910
  • 博文数量: 38
  • 博客积分: 2060
  • 博客等级: 大尉
  • 技术积分: 388
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-17 10:10
文章分类

全部博文(38)

文章存档

2011年(1)

2009年(37)

我的朋友

分类: LINUX

2009-11-09 11:51:14

贴在这记录下,免得总是printf
 
c文件
 

#include "debug.h"

#define DBG_LEVEL_CRITICAL 0
#define DBG_LEVEL_ERR 1
#define DBG_LEVEL_WARNING 2
#define DBG_LEVEL_MSG 3
#define DBG_LEVEL_TRACE 4

const char *msg_level[] = {
 "CRI",
 "ERR",
 "WAR",
 "MSG",
 "TRA"
};

int dbg_comm(const char *file, const int line, unsigned int level, const char *fmt, va_list arglist)
{
 FILE *tg = NULL;

 if(level == DBG_LEVEL_CRITICAL || level == DBG_LEVEL_ERR)
 {
  tg = stderr;
 }
 else
 {
  tg = stdout;
 }
 #ifdef OUTPUT_TIME
    struct tm *time_info;
    struct timeval tv = {0};

 if (gettimeofday(&tv, NULL) < 0)
 {
  perror("gettimeofday");
  return -1;
    }
    time_info = localtime(&(tv.tv_sec));
 fprintf(tg, "%02d:%02d:%02d:%06d-", time_info->tm_hour, time_info->tm_min, time_info->tm_sec, (int)tv.tv_usec);
 #endif

 #ifdef OUTPUT_PID
 pid_t pid;
 pid = getpid();
 fprintf(tg, "%05u-", pid);
 #endif

 #ifdef OUTPUT_TID
 pid_t tid;
 if((tid = syscall(SYS_gettid)) == -1)
 {
  perror("syscall");
  tid = 65535;
 }
 fprintf(tg, "%05u-", tid);
 #endif

 fprintf(tg, "%s-->%s:%d: ", msg_level[level], file, line);
 vfprintf(tg, fmt, arglist);
 return 0;
}

int dbg_trace(const char *file, const int line, const char *fmt, ...)
{
 int ret;
 va_list arglist;
 va_start(arglist, fmt);
 ret = dbg_comm(file, line, DBG_LEVEL_TRACE, fmt, arglist);
 return ret;
}

int dbg_msg(const char *file, const int line, const char *fmt, ...)
{
 int ret;
 va_list arglist;
 va_start(arglist, fmt);
 ret = dbg_comm(file, line, DBG_LEVEL_MSG, fmt, arglist);
 return ret;
}

int dbg_warning(const char *file, const int line, const char *fmt, ...)
{
 int ret;
 va_list arglist;
 va_start(arglist, fmt);
 ret = dbg_comm(file, line, DBG_LEVEL_WARNING, fmt, arglist);
 return ret;
}

int dbg_err(const char *file, const int line, const char *fmt, ...)
{
 int ret;
 va_list arglist;
 va_start(arglist, fmt);
 ret = dbg_comm(file, line, DBG_LEVEL_ERR, fmt, arglist);
 return ret;
}

int dbg_critical(const char *file, const int line, const char *fmt, ...)
{
 int ret;
 va_list arglist;
 va_start(arglist, fmt);
 ret = dbg_comm(file, line, DBG_LEVEL_CRITICAL, fmt, arglist);
 return ret;
}

 

 

h文件


 

/*****************************************
 *
 *
 *
 * ***************************************/

#ifndef __DDB_DEBUG_H__
#define __DDB_DEBUG_H__

#ifndef _GUN_SOURCE
#define _GNU_SOURCE
#endif

#include
#include
#include
#include
#include
#include
#include

#ifdef __cplusplus
extern "C" {
#endif

/****************************************/
 /*config debug options here*/
//#define OUTPUT_TIME
//#define OUTPUT_PID
//#define OUTPUT_TID
#define DBG_TRA(...) dbg_trace(__FILE__, __LINE__, __VA_ARGS__)
#define DBG_MSG(...) dbg_msg(__FILE__, __LINE__, __VA_ARGS__)
#define DBG_WAR(...) dbg_warning(__FILE__, __LINE__, __VA_ARGS__)
#define DBG_ERR(...) dbg_err(__FILE__, __LINE__, __VA_ARGS__)
#define DBG_CRI(...) dbg_critical(__FILE__, __LINE__, __VA_ARGS__)
/****************************************/

int dbg_trace(const char *file, const int line, const char *fmt, ...);
int dbg_msg(const char *file, const int line, const char *fmt, ...);
int dbg_warning(const char *file, const int line, const char *fmt, ...);
int dbg_err(const char *file, const int line, const char *fmt, ...);
int dbg_critical(const char *file, const int line, const char *fmt, ...);

#ifdef __cplusplus
}
#endif

#endif

 

测试文件main.c


 

#include "debug.h"

int main()
{
    DBG_TRA("%s %d\n", "dbg_trace", 0);
    DBG_MSG("%s %d\n", "dbg_msg", 1);
    DBG_WAR("%s %d\n", "dbg_warning", 0);
    DBG_ERR("%s %d\n", "dbg_err", 0);
    DBG_CRI("%s %d\n", "dbg_critical", 0);
    return 0;
}


阅读(1961) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~