首页 > 其他 > 详细

log4cpp - 使用

时间:2014-05-21 10:03:04      阅读:408      评论:0      收藏:0      [点我收藏+]
#pragma once
#include "ThreadLock.h"
#include <string>
#include <cstdio>
#include <log4cpp/Category.hh>
#include <log4cpp/Appender.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/Priority.hh>
#include <log4cpp/PatternLayout.hh>
#include <log4cpp/RollingFileAppender.hh>


class QuoteLog
{
public:
static QuoteLog* getInstance();
void loginfo(const std::string& message);
void logdebug(const std::string& message);
void logwarn(const std::string& message);
void logerror(const std::string& message);
private:
QuoteLog();
~QuoteLog();
log4cpp::Category& m_root;
static bool initialized;
static CThreadLock creationLock;

};


---------------------------------------------------------------------------------------------------------------
#include "QuoteLog.h"


#include <iostream>


bool QuoteLog::initialized=false;
CThreadLock QuoteLog::creationLock;




QuoteLog* QuoteLog::getInstance() {
bool needUnlock=false;
if (!initialized) {
creationLock.Lock();
needUnlock=true;
}


static QuoteLog instance;

if (needUnlock) {
initialized=true;
creationLock.UnLock();
}


return &instance;
}


QuoteLog::QuoteLog() : m_root(log4cpp::Category::getRoot().getInstance("QuoteLog"))
{
    log4cpp::PatternLayout* pLayout2 = new log4cpp::PatternLayout();
    pLayout2->setConversionPattern("%d: %p : %m%n");
    log4cpp::RollingFileAppender* rollfileAppender = new log4cpp::RollingFileAppender( "rollfileAppender","quote.log",5*1024,1);
    rollfileAppender->setLayout(pLayout2);
 
    
    m_root.addAppender(rollfileAppender);
    m_root.setPriority(log4cpp::Priority::DEBUG);


}
void QuoteLog::loginfo(const std::string& message)
{
m_root.info(message);
}
void QuoteLog::logdebug(const std::string& message)
{
m_root.debug(message);


}
void QuoteLog::logwarn(const std::string& message)
{
m_root.warn(message);
}
void QuoteLog::logerror(const std::string& message)
{
m_root.error(message);
}


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

log4cpp - 使用,布布扣,bubuko.com

log4cpp - 使用

原文:http://blog.csdn.net/ctxbloger/article/details/26385977

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!