对log4cpp单例封装

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

1.log4cpphelper.h代码

/****************************************************************************
**
** Copyright (C) 2017 liushixiongcpp@163.com
** 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代码

#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 <iostream>

#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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值