一个跨平台的c++日志模块实现

     文件log.h
#ifndef __GUARD_LOG_H
#define __GUARD_LOG_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
    #include <windows.h>
#else
    #include <stdarg.h>
    #include <unistd.h>
    #include <sys/time.h>
    #include <pthread.h>
    #define  CRITICAL_SECTION   pthread_mutex_t
#endif//WIN32
#ifndef MAX_PATH
#define MAX_PATH 256
#endif
const int LOG_HIT = 0;//提示
const int LOG_WARN = 1;//警告
const int LOG_ERROR = 2;//错误
const int LOG_CRITICAL = 3;//严重错误

class AutoLock
{
    public:
        AutoLock()
        {
#ifdef WIN32
            ::InitializeCriticalSection(&section);
#endif

        }
        virtual ~AutoLock()
        {
#ifdef WIN32
            ::DeleteCriticalSection(&secion);
#endif
        }
    public:
       bool Lock()
       {
#ifdef WIN32
           ::EnterCriticalSection(&section);
#else
           pthread_mutex_lock(§ion);
#endif
           return true;
       }
       bool Unlock()
       {
#ifdef WIN32
           ::LeaveCriticalSection(&section);
#else
           pthread_mutex_unlock(&section);
#endif
           return true;
       }
    private:
    CRITICAL_SECTION section;
};



class LogFile
{
public:
    LogFile();
    virtual ~LogFile();
public:
    void Start(const char* pszFilename);
    void Write(int nErrorLevel, const char* pFormatStr, ...);
    void WriteLine(int nErrorLevel, const char* pFormatStr, ...);
    void Close();
    bool CreateDir(const char * pszLogDir);
protected:
    void CreateFile(char* pszFileName);
protected:
    char file_name_[MAX_PATH*2];
    char file_title_[MAX_PATH*2];
    FILE *pLog_;
    AutoLock lock_;
};
#endif//__GUARD_LOG_H

文件log.cc:


#include "log.h"
#include <time.h>

LogFile::LogFile()
{
    pLog_ = NULL;
    memset(file_name_, 0, MAX_PATH * 2);
    memset(file_title_, 0, MAX_PATH * 2);
}
LogFile::~LogFile()
{
    if(pLog_){
        fclose(pLog_);
        pLog_ = NULL;
    }
}
void LogFile::Start(const char* pszFilename)
{
    if (strlen(pszFilename) <= 0){
        return;
    }
    lock_.Lock();
    strcpy(file_title_, pszFilename);
    this->CreateFile(file_title_);
    lock_.Unlock();
}
void LogFile::Write(int nErrorLevel, const char* pFormatStr, ...)
{
    this->CreateFile(file_title_);
    lock_.Lock();
    if (pLog_ == NULL){
        return;
    }
    const int nBufSize = 50 * 1024;
    char Buf[nBufSize];
    const int nLevelSize = 32;
    char Level[nLevelSize];

    if (nErrorLevel == LOG_HIT){
        strcpy(Level, "HIT");
    }
    else if (nErrorLevel == LOG_WARN){
        strcpy(Level, "WARN");
    }
    else if (nErrorLevel == LOG_ERROR){
        strcpy(Level, "ERROR");
    }
    else if (nErrorLevel == LOG_CRITICAL){
        strcpy(Level, "CRITICAL");
    }
    else{
        strcpy(Level, "UNKNOW");
    }

    //输出时间
    time_t timer;
    time(&timer);
    struct tm *today = localtime(&timer);
    //[2005-03-25 11:29:16]
    sprintf(Buf,"[%04d-%02d-%02d %02d:%02d:%02d LEVEL = %s] ",
            today->tm_year + 1900,
            today->tm_mon + 1,
            today->tm_mday,
            today->tm_hour,
            today->tm_min,
            today->tm_sec,
            Level);

    //写到LOG文件.
    fwrite(Buf,strlen(Buf), 1, pLog_);
    memset(Buf, 0, nBufSize);

    //分析输入可变参数.
    va_list args;
    va_start(args, pFormatStr);
    vsprintf(Buf, pFormatStr, args);
    va_end(args);

    //写到LOG文件
    fwrite(Buf, strlen(Buf), 1, pLog_);
    //缓冲区写到文件
    fflush(pLog_);
    lock_.Unlock();
}
void LogFile::WriteLine(int nErrorLevel, const char* pFormatStr, ...)
{
    lock_.Lock();
    this->CreateFile(file_title_);
    if (pLog_ == NULL){
        return;
    }

    const int nBufSize = 50 * 1024;
    char Buf[nBufSize];

    const int nLevelSize = 32;
    char Level[nLevelSize];
    if (nErrorLevel == LOG_HIT){
        strcpy(Level, "HIT");
    }
    else if (nErrorLevel == LOG_WARN){
        strcpy(Level, "WARN");
    }
    else if (nErrorLevel == LOG_ERROR){
        strcpy(Level, "ERROR");
    }
    else if (nErrorLevel == LOG_CRITICAL){
        strcpy(Level, "CRITICAL");
    }
    else{
        strcpy(Level, "UNKNOW");
    }

    //输出时间
    time_t timer;
    time(&timer);
    struct tm *today = localtime(&timer);
    //[2005-03-25 11:29:16]
    sprintf(Buf,"[%04d-%02d-%02d %02d:%02d:%02d LEVEL = %s] ",
            today->tm_year + 1900,
            today->tm_mon + 1,
            today->tm_mday,
            today->tm_hour,
            today->tm_min,
            today->tm_sec,
            Level);

    //写到LOG文件.
    fprintf(pLog_, "%s", Buf);
    memset(Buf, 0, nBufSize);
    //分析输入可变参数.
    va_list args;
    va_start(args, pFormatStr);
    vsprintf(Buf, pFormatStr, args);
    va_end(args);

    //写到LOG文件.
    fwrite(Buf, strlen(Buf), 1, pLog_);

    //缓冲区写到文件.
    fflush(pLog_);
    lock_.Unlock();
}
void LogFile::Close(void)
{
    lock_.Lock();
    if (pLog_){
        fclose(pLog_);
        pLog_ = NULL;
    }
    lock_.Unlock();
}
void LogFile::CreateFile(char* pszFileName)
{
    lock_.Lock();
    //输出时间.
    time_t timer;
    time(&timer);
    struct tm *today = localtime(&timer);
    char chBuf[MAX_PATH] = {0};
    sprintf(chBuf, "%s%04d%02d%02d%s",
            pszFileName,
            today->tm_year + 1900,
            today->tm_mon + 1,
            today->tm_mday,
            ".log");

    if (memcmp(file_name_, chBuf, strlen(chBuf)) != 0){
        if (pLog_){
            this->Close();
            pLog_ = NULL;
        }

        memcpy(file_name_, chBuf, strlen(chBuf));
        pLog_ = fopen(file_name_, "a");
    }
    lock_.Unlock();
}






评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值