Chinaunix首页 | 论坛 | 博客
  • 博客访问: 186544
  • 博文数量: 30
  • 博客积分: 2500
  • 博客等级: 少校
  • 技术积分: 526
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-19 10:07
文章分类

全部博文(30)

文章存档

2009年(1)

2008年(29)

我的朋友

分类: C/C++

2008-05-08 10:02:48

 

#ifndef _LOG_H_
#define _LOG_H_

#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>

#ifdef DEBUG
    #define GENPRINTF(format,...) debug_print(__FILE__, __LINE__, __FUNCTION__, format, ##__VA_ARGS__)
#else
    #define GENPRINTF(format,...)
#endif

#define GENPRINTF_1(...) printf(__VA_ARGS__);
#define GENPRINTF_2(format,...) printf(format, __VA_ARGS__);
#define GENPRINTF_3(format,...) printf(format, ##__VA_ARGS__);
#define GENPRINTF_4(level,format,...) printf("%d : %s : %s : %d @ "format, level, __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__);


void debug_print(const char* file, const int line, const char* fun, const char* format,...)
{
    char buf[1024];
    
    time_t seconds;
    struct tm* ptime;
    
    seconds = time((time_t*)(NULL));
    ptime = localtime(&seconds);

    va_list ap;
    va_start(ap, format);
    
    //FILE *fp;
    //fp = fopen("log","a+");
    //if(NULL == fp) {
    //    perror("fopen");
    //}
    
    fprintf(stdout,"%04d-%02d-%02d %02d:%02d:%02d--%s[%04d]: %s :<<",
            ptime->tm_year + 1900, ptime->tm_mon + 1, ptime->tm_mday, ptime->tm_hour, ptime->tm_min, ptime->tm_sec,
            file, line, fun);
    
    vsprintf(buf, format, ap);
    
    fprintf(stdout, "%s>>\n", buf);
    
    va_end(ap);
    //fclose(fp);
}

#endif

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