简单的LOG公共操作类设计

#ifndef OPP_LOGING_H
#define OPP_LOGING_H

#define LOG_ERR(fmt,...)\
    Logger::instance()->Log(Logger::Error, fmt, __VA_ARGS__);

#define  LOG_WARN(fmt, ...)\
    Logger::instance()->Log(Logger::Warn, fmt, __VA_ARGS__);

#define  LOG_INFO(fmt, ...)\
    Logger::instance()->Log(Logger::Info, fmt, __VA_ARGS__);

#define  LOG_TRACE(fmt, ...)\
    Logger::instance()->Log(Logger::Trace, fmt, __VA_ARGS__);

#define  LOG_DEBUG(fmt, ...)\
    Logger::instance()->Log(Logger::Debug, fmt, __VA_ARGS__);

class Logger
{
public:
    static const int Error = 0;
    static const int Warn = 1;
    static const int Info = 2;
    static const int Trace = 3;
    static const int Debug = 4;

    static const char * const LOG_LEVEL_NAME[5];
    static int const LOG_SUFFIX[7];

    virtual ~Logger();
    static Logger * instance();

    void SetLevel(int nLevel);

    void Log(int nLevel, const char * pszFmt, ...);

    void LogToFile(const char * pFileName, const char * pLog, const size_t nSize, bool bTruncate = false);

    void LogToStdout(const char * pLog, const size_t nSize);
private:
    Logger();
private:
    int m_nFile;//File handle
    int m_nLevel;//Log level
};


#endif
#include "StdAfx.h"
#include "Logger.h"
#include <time.h>
#include <direct.h>
#include <stdarg.h>
#include <Windows.h>

#define  MAX_LOG_SIZE 1024

const char * const Logger::LOG_LEVEL_NAME[5]={
    "Error",
    "Warn",
    "Info",
    "Trace",
    "Debug"
};

int const Logger::LOG_SUFFIX[7] = {1,2,3,1,2,3,2};

Logger * Logger::instance()
{
    static Logger log;
    return &log;
}

Logger::Logger()
{
    m_nFile = -1;
    m_nLevel = Trace;
}

Logger::~Logger()
{
    //logic code
}

void Logger::SetLevel(int nLevel)
{
    m_nLevel = nLevel;
}

void Logger::Log(
                 int nLevel, 
                 const char * pszFmt, ...
                 )
{
    static int lastLogSuffix = -1;
    if ((nLevel < 0) || (nLevel > m_nLevel))
    {
        return;
    }
    time_t nNow = time(NULL);
    struct tm * pLocalTime = localtime(&nNow);
    unsigned short year = pLocalTime->tm_year+1900;
    unsigned short month = pLocalTime->tm_mon+1;
    unsigned short day = pLocalTime->tm_mday;
    unsigned short hour = pLocalTime->tm_hour;
    unsigned short minute = pLocalTime->tm_min;
    unsigned short second = pLocalTime->tm_sec;
    unsigned short weekDay = pLocalTime->tm_wday;
    
    int tempLogSuffix = LOG_SUFFIX[weekDay];//1,2,3,1,2,3,2
    char szLogFile[255] = {0};
    char szTheLog[MAX_LOG_SIZE+2] = {0};//2 for \n \0
    _mkdir("log");
    //Log file Name
    sprintf(szLogFile, "log\\log_%d.log", tempLogSuffix);
    //Log content
    int nBytesWrite = 0;
    int ret = sprintf(szTheLog, "[%s][%d-%02d-%02d %02d:%02d:%02d]",
        LOG_LEVEL_NAME[nLevel], year, month, day, hour, minute, second);
    if (ret < 0)
    {
        return;
    }
    nBytesWrite += ret;
    va_list ap;
    va_start(ap,pszFmt);

    ret = vsnprintf_s(szTheLog+nBytesWrite,
        MAX_LOG_SIZE-nBytesWrite,
        MAX_LOG_SIZE-nBytesWrite,
        pszFmt, ap);
    if (ret <= 0)
    {
        return;
    }
    va_end(ap);
    nBytesWrite +=ret;
    if (nBytesWrite == MAX_LOG_SIZE)
    {
        szTheLog[MAX_LOG_SIZE] = '\n';
        szTheLog[MAX_LOG_SIZE+1] = '\0';
        nBytesWrite += 2;
    }
    else
    {
        szTheLog[nBytesWrite] = '\n';
        szTheLog[nBytesWrite+1] = '\0';
        nBytesWrite += 2;
    }

    if (lastLogSuffix != tempLogSuffix)
    {
        lastLogSuffix = tempLogSuffix;
        LogToFile(szLogFile, szTheLog, nBytesWrite, true);
    }
    else
    {
        LogToFile(szLogFile, szTheLog, nBytesWrite);
    }

    LogToStdout(szTheLog, nBytesWrite);
    return;
}

void Logger::LogToFile(
                       const char * pFileName, 
                       const char * pLog, 
                       const size_t nSize, 
                       bool bTruncate /* = false */
                       )
{
    FILE * pFile = NULL;
    if (bTruncate)
    {
        pFile = fopen(pFileName, "w");
    }
    else
    {
        pFile = fopen(pFileName, "a");
    }
    if (pFile != NULL)
    {
        fwrite(pLog, 1, nSize, pFile);
    }
    fclose(pFile);
    return ;
}

void Logger::LogToStdout(const char *pLog, const size_t nSize)
{
    printf("log to stdout:%s\n", pLog);
}

日志保留三天.

转载于:https://www.cnblogs.com/kingdom_0/articles/2805072.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值