C/C++ 打印信息控制,包括日志

#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");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值