对log4cpp单例封装

在网上简单搜了下,没有找到合适自己的代码,所以花点时间自己封装下

1.log4cpphelper.h代码

/****************************************************************************
**
** Copyright (C) 2017 [email protected]
** All rights reserved.
**
****************************************************************************/

#ifndef log4cpphelper_h
#define log4cpphelper_h

#include 
#include 

using std::string;

#include "singleton.h"

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define LOG_INFO_ARGS(formatStr, ...) {\
        singleton::Singleton::Instance().LogInfo(log4cpp::Priority::INFO, formatStr, __VA_ARGS__);  \
    }

#define LOG_NOTICE_ARGS(formatStr, ...) {\
        singleton::Singleton::Instance().LogInfo(log4cpp::Priority::NOTICE, formatStr, __VA_ARGS__);  \
    }

#define LOG_WARN_ARGS(formatStr, ...) {\
        singleton::Singleton::Instance().LogInfo(log4cpp::Priority::WARN, formatStr, __VA_ARGS__);  \
    }

#define LOG_ERROR_ARGS(formatStr, ...) {\
        singleton::Singleton::Instance().LogInfo(log4cpp::Priority::ERROR, formatStr, __VA_ARGS__);  \
    }

#define LOG_CRIT_ARGS(formatStr, ...) {\
        singleton::Singleton::Instance().LogInfo(log4cpp::Priority::CRIT, formatStr, __VA_ARGS__);  \
    }

#define LOG_ALTER_ARGS(formatStr, ...) {\
        singleton::Singleton::Instance().LogInfo(log4cpp::Priority::ALERT, formatStr, __VA_ARGS__);  \
    }

#define LOG_INFO(msg) \
    singleton::Singleton::Instance().GetLogCategory() << \
    log4cpp::Priority::INFO << __FILE__ << " [" << __LINE__  << "] " << msg;

#define LOG_NOTICE(msg) \
    singleton::Singleton::Instance().GetLogCategory() << \
    log4cpp::Priority::NOTICE << __FILE__ << " [" << __LINE__  << "] " << msg;

#define LOG_WARN(msg) \
    singleton::Singleton::Instance().GetLogCategory() << \
    log4cpp::Priority::WARN << __FILE__ << " [" << __LINE__  << "] " << msg;

#define LOG_ERROR(msg) \
    singleton::Singleton::Instance().GetLogCategory() << \
    log4cpp::Priority::ERROR << __FILE__ << " [" << __LINE__  << "] " << \
    msg;

#define LOG_CRIT(msg) \
    singleton::Singleton::Instance().GetLogCategory() << \
    log4cpp::Priority::CRIT << __FILE__ << " [" << __LINE__  << "] " << msg;

#define LOG_ALERT(msg) \
    singleton::Singleton::Instance().GetLogCategory() << \
    log4cpp::Priority::ALERT << __FILE__ << " [" << __LINE__  << "] " << \
    msg;

class Log4cppHelper : public singleton::Singleton
{
public:
    static Log4cppHelper & Instance();

public:
    ~Log4cppHelper();

    log4cpp::Category& GetLogCategory();

    void LogInfo(log4cpp::Priority::Value priority,
                 const char* stringFormat,
                 ...);

private:
    Log4cppHelper();
    Log4cppHelper(const Log4cppHelper &);
    Log4cppHelper& operator=(const Log4cppHelper &);

private:
    friend singleton::Singleton;
};

#endif // log4cpphelper_h

 2.log4cpphelper.cpp代码

#include "log4cpphelper.h"
#include "timstamp.h"

#define LOG_FILE_NAME "defualt_name"

Log4cppHelper::Log4cppHelper(const Log4cppHelper &)
{}

Log4cppHelper& Log4cppHelper::operator=(const Log4cppHelper &)
{ return *this; }

Log4cppHelper::Log4cppHelper()
{
    log4cpp::Appender *appender = new log4cpp::RollingFileAppender("default", 
                                  string(LOG_FILE_NAME)+
                                  Timestamp::now().toFormattedString(
                                  Timestamp::STRING_STYLE) + string(".log")
                                  );
    log4cpp::PatternLayout *patternlayout = new log4cpp::PatternLayout();
    patternlayout->setConversionPattern("%d [%p] %m%n");
    appender->setLayout(patternlayout);

    log4cpp::Category& log = log4cpp::Category::getInstance(
                                                std::string(LOG_FILE_NAME));
    log.addAppender(appender);
}

Log4cppHelper::~Log4cppHelper()
{
    log4cpp::Category::shutdown();
}

void Log4cppHelper::LogInfo(log4cpp::Priority::Value priority,
                            const char* stringFormat,
                            ...)
{
    va_list va;

    va_start(va, stringFormat);
    log4cpp::Category::getInstance(std::string(LOG_FILE_NAME)).logva(
                                   priority, 
                                   stringFormat, 
                                   va);
    
    va_end(va);
}

log4cpp::Category& Log4cppHelper::GetLogCategory()
{
    return log4cpp::Category::getInstance(std::string(LOG_FILE_NAME));
}

3.main.cpp用法

#include 

#include "timstamp.h"
#include "singleton.h"
#include "log4cpphelper.h"

int main(int argc, char ** argv[])
{
    int i = 0;

    // LOG_INFO("test info"); 
    LOG_INFO_ARGS("%s [%d] test %d %s", __FILE__, __LINE__ , 5, "linux");
    LOG_NOTICE("test NOTICE"); 
    LOG_WARN("test warn"); 
    LOG_ERROR("test error"); 
    LOG_CRIT("test crit"); 
    LOG_ALERT("test alter");

    getchar();
	return 0;
}

 

 4.项目的完整下载地址

  https://files.cnblogs.com/files/L-Lune/test.7z

  注意编译的时候,vs编译选项选择debug x86模式

 

转载于:https://www.cnblogs.com/L-Lune/p/8968910.html

你可能感兴趣的:(对log4cpp单例封装)