#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 (5*1024*1024) // 5M
extern FILE *plog;
extern const char *log_path;
int InitLogFile(const char *path);
void CloseLog();
#define PRF(format, ...) do\
{\
fflush(stdout);\
const char *find_ = rindex(__FILE__, '/');\
if (NULL != find_)\
find_++;\
else\
find_ = __FILE__;\
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);\
fprintf(stdout, "DEBUG | %s|%-20.20s: %-5d| %-20.20s| "format"", buf_t,\
find_,\
__LINE__,\
__FUNCTION__,\
##__VA_ARGS__);\
}while(0)
#define LOG(format, ...) do\
{\
ASSERT(plog != NULL);\
ASSERT(log_path != 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(plog) > MAX_LOG_SIZE)\
{\
fclose(plog);\
fclose(fopen(log_path, "w+"));\
plog = fopen(log_path, "a+");\
}\
const char *find_ = rindex(__FILE__, '/');\
if (NULL != find_)\
find_++;\
else\
find_ = __FILE__;\
fprintf(plog, "LOG | %s|%-20.20s: %-5d| %-20.20s| "format"", buf_t,\
find_,\
__LINE__,\
__FUNCTION__,\
##__VA_ARGS__);\
}while(0)
#ifdef _DEBUG_STDOUT_
#define DBG(format, ...) do\
{\
PRF(""format"", ##__VA_ARGS__);\
}while(0)
#define ERR(format, ...) do\
{\
char err[50];bzero(err, sizeof(err));\
strncpy(err, strerror(errno), sizeof(err));\
PRF("%s, "format"", err, ##__VA_ARGS__);\
}while(0)
#elif defined _DEBUG_LOG_
#define DBG(format, ...) do\
{\
LOG(""format"", ##__VA_ARGS__);\
}while(0)
#define ERR(format, ...) do\
{\
char err[50];bzero(err, sizeof(err));\
strncpy(err, strerror(errno), sizeof(err));\
LOG("%s, "format"", err, ##__VA_ARGS__);\
}while(0)
#elif defined _DEBUG_STDOUT_LOG_
#define DBG(format, ...) do\
{\
PRF(""format"", ##__VA_ARGS__);\
LOG(""format"", ##__VA_ARGS__);\
}while(0)
#define ERR(format, ...) do\
{\
char err[50];bzero(err, sizeof(err));\
strncpy(err, strerror(errno), sizeof(err));\
PRF("%s, "format"", err, ##__VA_ARGS__);\
LOG("%s, "format"", err, ##__VA_ARGS__);\
}while(0)
#else
#define DBG(format, ...)
#define ERR(format, ...)
#endif
#define ASSERT(expr) do\
{\
if (!(expr))\
{\
fflush(stderr);\
const char *find_ = rindex(__FILE__, '/');\
if (NULL != find_)\
find_++;\
else\
find_ = __FILE__;\
fprintf(stderr, "%-20.20s: %-5d, ASSERT %s failed\n",\
find_,\
__LINE__,\
#expr);\
abort();\
}\
}while(0)
#define FATAL(format, ...) do\
{\
fflush(stderr);\
const char *find_ = rindex(__FILE__, '/');\
if (NULL != find_)\
find_++;\
else\
find_ = __FILE__;\
fprintf(stderr, "FATAL, %-20.20s: %-5d: error: %-20.20s, "format"",\
find_,\
__LINE__,\
strerror(errno),\
##__VA_ARGS__);\
abort();\
}while(0)
#define PROTO_DEBUG
#ifdef PROTO_DEBUG
#define CALL(func) do\
{\
(func);\
}while(0)
#else
#define CALL(func)
#endif
typedef unsigned int u_int32_t;
typedef unsigned long ul_int32_t;
typedef unsigned long long ull_int64_t;
#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
#define SUCCESS 0
#define FAILURE -1
#endif
#include "macro.h"
FILE *plog = NULL;
const char *log_path = NULL;
int InitLogFile(const char *path)
{
int ret = 0;
log_path = path;
plog = fopen(path, "a+");
if (!plog)
ret = -1;
DBG("init log file completed.\n");
return ret;
}
void CloseLog()
{
fclose(plog);
DBG("close log file completed.\n");
}