Color Log

#ifndef COLORLOG_H
#define COLORLOG_H

#include <stdio.h>
#include <time.h>

#ifdef WIN32
#include "windows.h"
#endif

//############################
#ifdef WIN32
//============================
#define FC_BLACK        0x0000
#define FC_DARK_BLUE    0x0001
#define FC_DARK_GREEN   0x0002
#define FC_DARK_CRAY    0x0003
#define FC_DARK_RED     0x0004
#define FC_DARK_MAGENTA 0x0005
#define FC_DARK_YELLOW  0x0006
#define FC_GRAY         0x0007
#define FC_DARK_GRAY    0x0008
#define FC_BLUE         0x0009
#define FC_GREEN        0x000A
#define FC_CRAY         0x000B
#define FC_RED          0x000C
#define FC_MAGENTA      0x000D
#define FC_YELLOW       0x000E
#define FC_WHITE        0x000F
//============================
#define BC_BLACK        0x0000
#define BC_DARK_BLUE    0x0010
#define BC_DARK_GREEN   0x0020
#define BC_DARK_CRAY    0x0030
#define BC_DARK_RED     0x0040
#define BC_DARK_MAGENTA 0x0050
#define BC_DARK_YELLOW  0x0060
#define BC_GRAY         0x0070
#define BC_DARK_GRAY    0x0080
#define BC_BLUE         0x0090
#define BC_GREEN        0x00A0
#define BC_CRAY         0x00B0
#define BC_RED          0x00C0
#define BC_MAGENTA      0x00D0
#define BC_YELLOW       0x00E0
#define BC_WHITE        0x00F0
//============================
#else
//============================
#define FC_BLACK        30
#define FC_DARK_BLUE    34
#define FC_DARK_GREEN   32
#define FC_DARK_CRAY    36
#define FC_DARK_RED     31
#define FC_DARK_MAGENTA 35
#define FC_DARK_YELLOW  33
#define FC_GRAY         37
#define FC_DARK_GRAY    90
#define FC_BLUE         94
#define FC_GREEN        92
#define FC_CRAY         96
#define FC_RED          91
#define FC_MAGENTA      95
#define FC_YELLOW       93
#define FC_WHITE        97
//============================
#define BC_BLACK        40
#define BC_DARK_BLUE    44
#define BC_DARK_GREEN   42
#define BC_DARK_CRAY    46
#define BC_DARK_RED     41
#define BC_DARK_MAGENTA 45
#define BC_DARK_YELLOW  43
#define BC_GRAY         47
#define BC_DARK_GRAY    100
#define BC_BLUE         104
#define BC_GREEN        102
#define BC_CRAY         106
#define BC_RED          104
#define BC_MAGENTA      105
#define BC_YELLOW       103
#define BC_WHITE        107
//============================
#endif
//############################

namespace AppParkServer
{

class kColorLog
{
public:
    kColorLog();
    ~kColorLog();

    bool Init(const char* folder, const char* file);
    void Uninit();

    void SetLogTime(bool val);

    void Debug(const char * format, ...);
    void Info(const char * format, ...);
    void Warn(const char * format, ...);
    void Error(const char * format, ...);
    void Fatal(const char * format, ...);

    void Log(const char* label, const char * format, ...);
    void ColorLog(unsigned int fc, unsigned int bc);
    void ColorLog(const char* label, unsigned int fc, unsigned int bc, const char * format, ...);

    void Sample();

private:
    kColorLog(const kColorLog&);
    kColorLog& operator=(const kColorLog&);
    kColorLog* operator&();
    const kColorLog operator&() const;

    static bool IsFolderExist(const char* path);
    static bool CreateFolder(const char* path);

    enum
    {
        //GMT_8 = 28800,
        LOG_FILE_NAME_LEN = 256,
        LOG_MESSAGE_LEN = 16384
    };

#ifdef WIN32
    tm local;
#else
    tm *local;
#endif
    time_t t;

    bool mLogTime;
    char mLogMsg[LOG_MESSAGE_LEN];
    char mFolderName[LOG_FILE_NAME_LEN];
    char mFileName[LOG_FILE_NAME_LEN];
    FILE* mFile;

#ifdef WIN32
    HANDLE m_hConsole;
    CONSOLE_SCREEN_BUFFER_INFO m_ScreenInfo;
#endif
};

};

#endif

  

#include "colorlog.h"
#include <memory.h>
#include <stdarg.h>
#include <string.h>

#ifdef WIN32
#include <direct.h>
#include <io.h>
#else
#include <stdarg.h>
#include <sys/stat.h>
#include <unistd.h>
#endif

#ifdef WIN32
#define ACCESS _access
#define MKDIR(a) _mkdir((a))
#else
#define ACCESS access
#define MKDIR(a) mkdir((a), 0755)
#endif

namespace AppParkServer
{
#ifdef WIN32
#define COLORLOG(LABEL) \
    memset(mLogMsg, 0, LOG_MESSAGE_LEN); \
    \
    if(mLogTime) \
    { \
        t = time(NULL); \
        localtime_s(&local, &t); \
        sprintf_s(mLogMsg, LOG_MESSAGE_LEN, "[%s][%04d-%02d-%02d %02d:%02d:%02d]: ", LABEL, \
        local.tm_year+1900,local.tm_mon+1, local.tm_mday, \
        local.tm_hour,local.tm_min, local.tm_sec); \
        nLen = (unsigned int)strlen(mLogMsg); \
    } \
    \
    vsprintf_s(mLogMsg+nLen, LOG_MESSAGE_LEN, format, arglist); \
    \
    if(mFile) \
    { \
        nLen = (unsigned int)strlen(mLogMsg); \
        fwrite(mLogMsg, sizeof(char), nLen, mFile); \
        fflush(mFile); \
    }
#else
#define COLORLOG(LABEL) \
    memset(mLogMsg, 0, LOG_MESSAGE_LEN); \
    \
    if(mLogTime) \
    { \
        t = time(NULL); \
        local = localtime(&t); \
        sprintf(mLogMsg, "[%s][%04d-%02d-%02d %02d:%02d:%02d]: ", LABEL, \
        local->tm_year+1900,local->tm_mon+1, local->tm_mday, \
        local->tm_hour,local->tm_min, local->tm_sec); \
        nLen = (unsigned int)strlen(mLogMsg); \
    } \
    \
    vsprintf(mLogMsg+nLen, format, arglist); \
    \
    if(mFile) \
    { \
        nLen = (unsigned int)strlen(mLogMsg); \
        fwrite(mLogMsg, sizeof(char), nLen, mFile); \
        fflush(mFile); \
    }
#endif

kColorLog::kColorLog()
{
    mLogTime = false;
    memset(mLogMsg, 0, LOG_MESSAGE_LEN);
    memset(mFolderName, 0, LOG_FILE_NAME_LEN);
    memset(mFileName, 0, LOG_FILE_NAME_LEN);
    mFile = NULL;
}

kColorLog::~kColorLog()
{
    Uninit();
}

bool kColorLog::Init(const char* folder, const char* file)
{
#ifdef WIN32
    m_hConsole= GetStdHandle(STD_OUTPUT_HANDLE);
    if(!m_hConsole){ return false; }
#endif

    if(!IsFolderExist(folder))
    {
        if(!CreateFolder(folder))
        {
            printf("Create folder \"%s\" failed.", folder);
            return false;
        }
    }

    t = time(NULL);
#ifdef WIN32
    localtime_s(&local, &t);
    sprintf_s(mFileName, LOG_FILE_NAME_LEN, "%s/%s[%04d-%02d-%02d].log",
        folder, file,
        local.tm_year+1900,local.tm_mon+1, local.tm_mday);
    if(0 != fopen_s(&mFile, mFileName, "at"))

#else
    local = localtime(&t);
    sprintf(mFileName, "%s/%s[%04d-%02d-%02d].log",
        folder, file,
        local->tm_year+1900,local->tm_mon+1, local->tm_mday);
    if(0 == (mFile=fopen(mFileName,"at")))
#endif
    {
        Fatal("Create log file \"%s\" failed.", mFileName);
        return false;
    }

    return true;
}

void kColorLog::Uninit()
{
#ifdef WIN32
    if(m_hConsole){ FreeConsole(); m_hConsole=0; }
#endif

    if(NULL != mFile)
    {
        fclose(mFile);
        mFile = NULL;
    }

    memset(mLogMsg, 0, LOG_MESSAGE_LEN);
    memset(mFolderName, 0, LOG_FILE_NAME_LEN);
    memset(mFileName, 0, LOG_FILE_NAME_LEN);
}

void kColorLog::SetLogTime(bool val)
{
    mLogTime = true;
}

void kColorLog::Debug(const char * format, ...)
{
    unsigned int nLen = 0;
    va_list arglist = 0;
    va_start(arglist, format);

    COLORLOG("DEBUG");

    ColorLog(FC_DARK_GREEN, FC_BLACK);

    va_end(arglist);
}

void kColorLog::Info(const char * format, ...)
{
    unsigned int nLen = 0;
    va_list arglist = 0;
    va_start(arglist, format);

    COLORLOG("INFO");

    memset(mLogMsg, 0, LOG_MESSAGE_LEN);

    printf("%s", mLogMsg);

    va_end(arglist);
}

void kColorLog::Warn(const char * format, ...)
{
    unsigned int nLen = 0;
    va_list arglist = 0;
    va_start(arglist, format);

    COLORLOG("WARN");

    ColorLog(FC_YELLOW, FC_BLACK);

    va_end(arglist);
}

void kColorLog::Error(const char * format, ...)
{
    unsigned int nLen = 0;
    va_list arglist = 0;
    va_start(arglist, format);

    COLORLOG("ERROR");

    ColorLog(FC_MAGENTA, FC_BLACK);

    va_end(arglist);
}

void kColorLog::Fatal(const char * format, ...)
{
    unsigned int nLen = 0;
    va_list arglist = 0;
    va_start(arglist, format);

    COLORLOG("FATAL");

    ColorLog(FC_RED, FC_BLACK);
    va_end(arglist);
}

void kColorLog::Log(const char* label, const char* format, ...)
{
    unsigned int nLen = 0;
    va_list arglist = 0;
    va_start(arglist, format);

    memset(mLogMsg, 0, LOG_MESSAGE_LEN);

    if(mLogTime)
    {
        t = time(NULL);
#ifdef WIN32
        localtime_s(&local, &t);
        sprintf_s(mLogMsg, LOG_MESSAGE_LEN, "[%s][%04d-%02d-%02d %02d:%02d:%02d]: ", label?label:"",
            local.tm_year+1900,local.tm_mon+1, local.tm_mday,
            local.tm_hour,local.tm_min, local.tm_sec);
#else
        local = localtime(&t);
        sprintf(mLogMsg, "[%s][%04d-%02d-%02d %02d:%02d:%02d]: ", label?label:"",
            local->tm_year+1900,local->tm_mon+1, local->tm_mday,
            local->tm_hour,local->tm_min, local->tm_sec);
#endif
        nLen = (unsigned int)strlen(mLogMsg);
    }

#ifdef WIN32
    vsprintf_s(mLogMsg+nLen, LOG_MESSAGE_LEN, format, arglist);
#else
    vsprintf(mLogMsg+nLen, format, arglist);
#endif

    if(mFile)
    {
        nLen = (unsigned int)strlen(mLogMsg);
        fwrite(mLogMsg, sizeof(char), nLen, mFile);
        fflush(mFile);
    }

    printf("%s", mLogMsg);

    va_end(arglist);
}

void kColorLog::ColorLog(unsigned int fc, unsigned int bc)
{
#ifdef WIN32
    if(!m_hConsole) { return; }
    WORD color = fc | bc;
    GetConsoleScreenBufferInfo(m_hConsole, (PCONSOLE_SCREEN_BUFFER_INFO)&m_ScreenInfo);
    SetConsoleTextAttribute(m_hConsole, color);
    printf(mLogMsg);
    SetConsoleTextAttribute(m_hConsole, m_ScreenInfo.wAttributes);
#else
    printf("\e[%d;%dm", fc, bc);
    printf("%s", mLogMsg);
    printf("\e[%d;%dm", FC_WHITE, BC_BLACK); // 假设 Linux 默认为 前景白,背景黑
#endif
}

void kColorLog::ColorLog(const char* label, unsigned int fc, unsigned int bc, const char* format, ...)
{
    unsigned int nLen = 0;
    va_list arglist = 0;
#ifdef WIN32
    if(!m_hConsole) { return; }
#endif

    va_start(arglist, format);
    memset(mLogMsg, 0, LOG_MESSAGE_LEN);

    if(mLogTime)
    {
        t = time(NULL);
#ifdef WIN32
        localtime_s(&local, &t);
        sprintf_s(mLogMsg, LOG_MESSAGE_LEN, "[%s][%04d-%02d-%02d %02d:%02d:%02d]: ", label?label:"",
            local.tm_year+1900,local.tm_mon+1, local.tm_mday,
            local.tm_hour,local.tm_min, local.tm_sec);
#else
        local = localtime(&t);
        sprintf(mLogMsg, "[%s][%04d-%02d-%02d %02d:%02d:%02d]: ", label?label:"",
            local->tm_year+1900,local->tm_mon+1, local->tm_mday,
            local->tm_hour,local->tm_min, local->tm_sec);
#endif
        nLen = (unsigned int)strlen(mLogMsg);
    }

#ifdef WIN32
    vsprintf_s(mLogMsg+nLen, LOG_MESSAGE_LEN, format, arglist);
#else
    vsprintf(mLogMsg+nLen, format, arglist);
#endif


    if(mFile)
    {
        nLen = (unsigned int)strlen(mLogMsg);
        fwrite(mLogMsg, sizeof(char), nLen, mFile);
        fflush(mFile);
    }

#ifdef WIN32
    WORD color = fc | bc;
    GetConsoleScreenBufferInfo(m_hConsole, (PCONSOLE_SCREEN_BUFFER_INFO)&m_ScreenInfo);
    SetConsoleTextAttribute(m_hConsole, color);
    printf(mLogMsg);
    SetConsoleTextAttribute(m_hConsole, m_ScreenInfo.wAttributes);
#else
    printf("\e[%d;%dm", fc, bc);
    printf("%s", mLogMsg);
    printf("\e[%d;%dm", FC_WHITE, BC_BLACK); // 假设 Linux 默认为 前景白,背景黑
#endif

    va_end(arglist);
}

void kColorLog::Sample()
{
#ifdef WIN32
    if(!m_hConsole) { return; }
#endif

    Log("Tom", "##########\n");

    Debug("##########\n");
    Info("##########\n");
    Warn("##########\n");
    Error("##########\n");
    Fatal("##########\n");

    ColorLog("", FC_BLACK, BC_WHITE, "%-20s ##########\n", "FC_BLACK");
    ColorLog("", FC_DARK_BLUE, BC_DARK_GRAY, "%-20s ##########\n", "FC_DARK_BLUE");
    ColorLog("", FC_DARK_GREEN, BC_BLACK, "%-20s ##########\n", "FC_DARK_GREEN");
    ColorLog("", FC_DARK_CRAY, BC_BLACK, "%-20s ##########\n", "FC_DARK_CRAY");

    ColorLog("", FC_DARK_RED, BC_BLACK, "%-20s ##########\n", "FC_DARK_RED");
    ColorLog("", FC_DARK_MAGENTA, BC_BLACK, "%-20s ##########\n", "FC_DARK_MAGENTA");
    ColorLog("", FC_DARK_YELLOW, BC_BLACK, "%-20s ##########\n", "FC_DARK_YELLOW");
    ColorLog("", FC_GRAY, BC_BLACK, "%-20s ##########\n", "FC_GRAY");

    ColorLog("", FC_DARK_GRAY, BC_BLACK, "%-20s ##########\n", "FC_DARK_GRAY");
    ColorLog("", FC_BLUE, BC_BLACK, "%-20s ##########\n", "FC_BLUE");
    ColorLog("", FC_GREEN, BC_BLACK, "%-20s ##########\n", "FC_GREEN");
    ColorLog("", FC_CRAY, BC_BLACK, "%-20s ##########\n", "FC_CRAY");

    ColorLog("", FC_RED, BC_BLACK, "%-20s ##########\n", "FC_RED");
    ColorLog("", FC_MAGENTA, BC_BLACK, "%-20s ##########\n", "FC_MAGENTA");
    ColorLog("", FC_YELLOW, BC_BLACK, "%-20s ##########\n", "FC_YELLOW");
    ColorLog("", FC_WHITE, BC_BLACK, "%-20s ##########\n", "FC_WHITE");

    // ================================================================================

    ColorLog("", FC_WHITE, BC_BLACK, "%-20s ##########\n", "BC_BLACK");
    ColorLog("", FC_WHITE, BC_DARK_BLUE, "%-20s ##########\n", "BC_DARK_BLUE");
    ColorLog("", FC_WHITE, BC_DARK_GREEN, "%-20s ##########\n", "BC_DARK_GREEN");
    ColorLog("", FC_WHITE, BC_DARK_CRAY, "%-20s ##########\n", "BC_DARK_CRAY");

    ColorLog("", FC_WHITE, BC_DARK_RED, "%-20s ##########\n", "BC_DARK_RED");
    ColorLog("", FC_WHITE, BC_DARK_MAGENTA, "%-20s ##########\n", "BC_DARK_MAGENTA");
    ColorLog("", FC_WHITE, BC_DARK_YELLOW, "%-20s ##########\n", "BC_DARK_YELLOW");
    ColorLog("", FC_WHITE, BC_GRAY, "%-20s ##########\n", "BC_GRAY");

    ColorLog("", FC_WHITE, BC_DARK_GRAY, "%-20s ##########\n", "BC_DARK_GRAY");
    ColorLog("", FC_WHITE, BC_BLUE, "%-20s ##########\n", "BC_BLUE");
    ColorLog("", FC_WHITE, BC_GREEN, "%-20s ##########\n", "BC_GREEN");
    ColorLog("", FC_WHITE, BC_CRAY, "%-20s ##########\n", "BC_CRAY");

    ColorLog("", FC_WHITE, BC_RED, "%-20s ##########\n", "BC_RED");
    ColorLog("", FC_WHITE, BC_MAGENTA, "%-20s ##########\n", "BC_MAGENTA");
    ColorLog("", FC_DARK_GRAY, BC_YELLOW, "%-20s ##########\n", "BC_YELLOW");
    ColorLog("", FC_BLACK, BC_WHITE, "%-20s ##########\n", "BC_WHITE");
}

bool kColorLog::IsFolderExist(const char* path)
{
    if(0 != ACCESS(path,0)) return false;
    return true;
}

bool kColorLog::CreateFolder(const char* path)
{
    if(0 != MKDIR(path)) return false;
    return true;
}

};

  

posted @ 2012-02-29 12:05  tomren  阅读(551)  评论(0编辑  收藏  举报