Encapsulate log4cpp singleton

I did a simple search on the Internet, but I didn't find a suitable code for myself, so I took some time to package it myself.

1.log4cpphelper.h code

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

#ifndef log4cpphelper_h
#define log4cpphelper_h

#include <string>
#include <stdarg.h>

using std::string;

#include "singleton.h"

#include <log4cpp/Appender.hh>
#include <log4cpp/Category.hh>
#include <log4cpp/Appender.hh>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/Layout.hh>
#include <log4cpp/BasicLayout.hh>
#include <log4cpp/Priority.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/RollingFileAppender.hh>
#include <log4cpp/PatternLayout.hh>
#include <log4cpp/NDC.hh>

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

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

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

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

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

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

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

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

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

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

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

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

class Log4cppHelper : public singleton::Singleton<Log4cppHelper>
{
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<Log4cppHelper>;
};

#endif // log4cpphelper_h

 2.log4cpphelper.cpp code

#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);
    
    go_end(go);
}

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

 3. The complete download address of the project

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325023029&siteId=291194637