#ifndef _MACRO_H_
#define _MACRO_H_
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#define MAX_LOG_SIZE (1024*1024) //1M
#define DEBUG
#ifdef DEBUG
#define DBG(format, ...) do\
{\
fflush(stdout);\
fprintf(stdout, "DEBUG, pid: %4d, %20s: %4d: %20s\t"format"", getpid(),\
__FILE__,\
__LINE__,\
__FUNCTION__,\
##__VA_ARGS__);\
}while(0)
#else
#define DBG(format, ...)
#endif
#define ERR(format, ...) do\
{\
fflush(stderr);\
fprintf(stderr, "ERROR, pid: %4d, %20s: %4d: %20s, error: %s, "format"", getpid(),\
__FILE__,\
__LINE__,\
__FUNCTION__,\
strerror(errno),\
##__VA_ARGS__);\
}while(0)
#define ASSERT(expr) do\
{\
if (!(expr))\
{\
fflush(stderr);\
fprintf(stderr, "%20s: %d, ASSERT %s failed\n",\
__FILE__,\
__LINE__,\
#expr);\
abort();\
}\
}while(0)
#define FATAL(format, ...) do\
{\
fflush(stderr);\
fprintf(stderr, "FATAL, %20s: %4d: error: %s, "format"",\
__FILE__,\
__LINE__,\
strerror(errno),\
##__VA_ARGS__);\
abort();\
}while(0)
/*文件的打开方式应该用"w+"来打开文件, 否则限制文件大小不起作用*/
#define LOG(fp, format, ...) do\
{\
ASSERT(fp != NULL);\
time_t t = time(NULL);\
struct tm *time_info = localtime(&t);\
char buf_t[20];\
memset(buf_t, 0, sizeof(buf_t));\
strftime(buf_t, sizeof(buf_t), "%Y-%m-%d %H:%M:%S", time_info);\
if (ftell(fp) > MAX_LOG_SIZE)\
{\
fseek(fp, 0, SEEK_SET);\
}\
fprintf(fp, "LOG, %s, %s: %d: error: %s, "format"",\
buf_t,\
__FILE__,\
__LINE__,\
strerror(errno),\
##__VA_ARGS__);\
}while(0)
#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
#define SUCCESS 0
#define FAILURE -1
#endif
#include "macro.h"
int main()
{
FILE *fp = fopen("./log.txt", "w+");
ASSERT(NULL != fp);
while(1)
{
LOG(fp, "+++++++++++++++++++++++++++\n");
LOG(fp, "---------------------\n");
LOG(fp, "@@@@@@@@@@@@@@@@\n");
LOG(fp, "#############\n");
LOG(fp, "$$$$$$$$\n");
LOG(fp, "******\n");
LOG(fp, "^^\n");
usleep(100*1000);
}
return 0;
}