通用日志系统LogSystem

通用日志系统LogSystem

简介

  • 封装一个通用日志系统,能够通知支持标准输出和syslog日志输出方式
  • 当初始化日志系统时,判断环境变量LOGMODE,如果LOGMODE的值为syslog就选择syslog的日志打印方式,否则选择标准输出。
  • 在选择标准输出时,通过环境变量LOGLEVEL可以设置日志的打印级别

头文件logSystem.h定义

<br />#ifndef LOGSYSTEM_H
#define LOGSYSTEM_H

#include <stdint.h>
#include <sys/time.h>

/*-------------------color-------------------------------------*/
#define NONE         "\033[m"
#define RED          "\033[0;32;31m"
#define LIGHT_RED    "\033[1;31m"
#define GREEN        "\033[0;32;32m"
#define LIGHT_GREEN  "\033[1;32m"
#define BLUE         "\033[0;32;34m"
#define LIGHT_BLUE   "\033[1;34m"
#define DARY_GRAY    "\033[1;30m"
#define CYAN         "\033[0;36m"
#define LIGHT_CYAN   "\033[1;36m"
#define PURPLE       "\033[0;35m"
#define LIGHT_PURPLE "\033[1;35m"
#define BROWN        "\033[0;33m"
#define YELLOW       "\033[1;33m"
#define LIGHT_GRAY   "\033[0;37m"
#define WHITE        "\033[1;37m"
/*--------------------color------------------------------------*/


enum LogMode
{
    OUTPUT_TO_TTY = 0,
    OUTPUT_TO_SYSLOG,
};

enum SysLog
{
    LOG_EMERG = 0,  /* system is unusable */
    LOG_ALERT = 1,  /* action must be taken immediately */
    LOG_CRIT  = 2,  /* critical conditions */
    LOG_ERR   = 3,  /* error conditions */
    LOG_WARNING = 4,    /* warning conditions */
    LOG_NOTICE  = 5,    /* normal but significant condition */
    LOG_INFO  = 6,  /* informational */
    LOG_DEBUG = 7,  /* debug-level messages */
};

class LogSystem
{
public:
    LogSystem();
    virtual ~LogSystem();
    virtual void LogInfo(const char *stringFormat, ...) throw();
    virtual void LogDebug(const char *stringFormat, ...) throw();
    virtual void LogWarning(const char *stringFormat, ...) throw();
    virtual void LogError(const char *stringFormat, ...) throw();
    virtual void setLogPri(uint8_t pri);
    static LogSystem* getInstance();

};

extern LogSystem* logHandler;

class LogTotty:public LogSystem
{
public:
    LogTotty();
    virtual void LogInfo(const char *stringFormat, ...) throw();
    virtual void LogDebug(const char *stringFormat, ...) throw();
    virtual void LogWarning(const char *stringFormat, ...) throw();
    virtual void LogError(const char *stringFormat, ...) throw();
    virtual void setLogPri(uint8_t pri);

private:
    int priority;
    struct timespec ts;
    char logMsg[1024];
};

class LogTosyslog:public LogSystem
{
public:
    LogTosyslog();
    virtual ~LogTosyslog();
    virtual void LogInfo(const char *stringFormat, ...) throw();
    virtual void LogDebug(const char *stringFormat, ...) throw();
    virtual void LogWarning(const char *stringFormat, ...) throw();
    virtual void LogError(const char *stringFormat, ...) throw();
    virtual void setLogPri(uint8_t pri);

private:
    struct timespec ts;
    char logMsg[1024];
};


#endif

LogSystem为抽象类,LogTotty和LogTosyslog分别继承实现标准输出、syslog输出。基于的是多态的设计。

类的具体实现

<br />#include "logSystem.h"
#include <syslog.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#include <iostream>
#include <stdio.h>
#include <string.h>
LogSystem* logHandler = NULL;

LogSystem::LogSystem()
{
}

LogSystem::~LogSystem()
{
}

void LogSystem::LogInfo(const char *stringFormat, ...) throw()
{
}

void LogSystem::LogDebug(const char *stringFormat, ...) throw()
{
}

void LogSystem::LogWarning(const char *stringFormat, ...) throw()
{
}

void LogSystem::LogError(const char *stringFormat, ...) throw()
{
}

void LogSystem::setLogPri(uint8_t pri)
{
}

LogSystem* LogSystem::getInstance(){
    char *logMode;
    char sysMode[]="syslog";

    if(NULL==logHandler){
        logMode = getenv("LOGMODE");
        if(NULL==logMode||strcmp(sysMode,logMode)!=0){
            printf("log to tty\n");
            logHandler = new LogTotty();
        }else{
            printf("log to syslog\n");
            logHandler = new LogTosyslog();
        }
    }
    return logHandler;
}

LogTotty::LogTotty()
{
    priority = atoi( NULL == getenv( "LOGLEVEL" ) ? "3" : getenv( "LOGLEVEL" ) );
    logMsg[1024]={0};
}

void LogTotty::LogInfo(const char *stringFormat, ...) throw()
{

    if (priority >= LOG_INFO)
    {
        va_list va;
        va_start(va,stringFormat);
        clock_gettime(CLOCK_MONOTONIC, &ts);
        vsprintf(logMsg, stringFormat, va);
        std::cout <<GREEN<< "[IPC_Serial][INFO]:" <<'['<<ts.tv_sec <<'.'<<ts.tv_nsec<<']'<< logMsg<< std::endl;
        va_end(va);
    }
}

void LogTotty::LogDebug(const char *stringFormat, ...) throw()
{
    if (priority >= LOG_DEBUG)
    {
        va_list va;
        va_start(va,stringFormat);
        clock_gettime(CLOCK_MONOTONIC, &ts);
        vsprintf(logMsg, stringFormat, va);
        std::cout <<GREEN << "[IPC_Serial][DEBUG]:" <<'['<<ts.tv_sec <<'.'<<ts.tv_nsec<<']'<<logMsg << std::endl;
        va_end(va);
    }
}

void LogTotty::LogWarning(const char *stringFormat, ...) throw()
{
    if (priority >= LOG_WARNING)
    {
        va_list va;
        va_start(va,stringFormat);
        clock_gettime(CLOCK_MONOTONIC, &ts);
        vsprintf(logMsg, stringFormat, va);
        std::cout <<YELLOW<< "[IPC_Serial][WARN]:" <<'['<<ts.tv_sec <<'.'<<ts.tv_nsec<<']'<<logMsg << std::endl;
        va_end(va);
    }
}

void LogTotty::LogError(const char *stringFormat, ...) throw()
{
    if (priority >= LOG_ERR)
    {
        va_list va;
        va_start(va,stringFormat);
        clock_gettime(CLOCK_MONOTONIC, &ts);
        vsprintf(logMsg, stringFormat, va);
        std::cout <<RED<< "[IPC_Serial][ERROR]:" <<'['<<ts.tv_sec <<'.'<<ts.tv_nsec<<']'<<logMsg << std::endl;
        va_end(va);
    }
}

void LogTotty::setLogPri(uint8_t pri)
{
    std::cout <<"current log priority is "<<priority<<", will to"<<pri<<std::endl;
    priority = pri;

}

LogTosyslog::LogTosyslog()
{
    openlog("[IPC_Serial]", LOG_PID|LOG_CONS, LOG_LOCAL0);
    setlogmask(LOG_UPTO(LOG_DEBUG));
}

LogTosyslog::~LogTosyslog()
{
    closelog();
}

void LogTosyslog::LogInfo(const char *stringFormat, ...) throw()
{
    va_list va;
    va_start(va,stringFormat);
    clock_gettime(CLOCK_MONOTONIC, &ts);
    vsprintf(logMsg, stringFormat, va);
    syslog(LOG_INFO, "[%ld.%ld]%s", ts.tv_sec, ts.tv_nsec,logMsg);
    va_end(va);
}

void LogTosyslog::LogDebug(const char *stringFormat, ...) throw()
{

    va_list va;
    va_start(va,stringFormat);
    clock_gettime(CLOCK_MONOTONIC, &ts);
    vsprintf(logMsg, stringFormat, va);
    syslog(LOG_DEBUG, "[%ld.%ld]%s", ts.tv_sec, ts.tv_nsec,logMsg);
    va_end(va);
}

void LogTosyslog::LogWarning(const char *stringFormat, ...) throw()
{
    va_list va;
    va_start(va,stringFormat);
    clock_gettime(CLOCK_MONOTONIC, &ts);
    vsprintf(logMsg, stringFormat, va);
    syslog(LOG_WARNING, "[%ld.%ld]%s", ts.tv_sec, ts.tv_nsec,logMsg);
    va_end(va);
}

void LogTosyslog::LogError(const char *stringFormat, ...) throw()
{

    va_list va;
    va_start(va,stringFormat);
    clock_gettime(CLOCK_MONOTONIC, &ts);
    vsprintf(logMsg, stringFormat, va);
    syslog(LOG_ERR, "[%ld.%ld]%s", ts.tv_sec, ts.tv_nsec,logMsg);
    va_end(va);
}

void LogTosyslog::setLogPri(uint8_t pri)
{
    LogInfo("current log priority will upto %d", pri);
    setlogmask(LOG_UPTO(pri));
}

源代码

GitHub查看源代码

发表评论

电子邮件地址不会被公开。 必填项已用*标注

This site uses Akismet to reduce spam. Learn how your comment data is processed.