Poco C++ 日志框架使用 Pcoco Logger

#include <iostream>
#include <stdio.h>

#include "Poco/ConsoleChannel.h"
#include "Poco/FileChannel.h"
#include "Poco/SplitterChannel.h"
#include "Poco/FormattingChannel.h"
#include "Poco/PatternFormatter.h"
#include "Poco/Logger.h"
#include "Poco/AutoPtr.h"

using Poco::Logger;
using Poco::ConsoleChannel;
using Poco::FileChannel;
using Poco::SplitterChannel;
using Poco::FormattingChannel;
using Poco::PatternFormatter;
using Poco::AutoPtr;



#ifdef WIN32
#define ENTER_F EnterTrace(__FUNCTION__);
#define EXIT_F ExitTrace(__FUNCTION__);
#else
#define ENTER_F EnterTrace(__PRETTY_FUNCTION__ );
#define EXIT_F ExitTrace(__PRETTY_FUNCTION__ );
#endif

//#define poco_Elog(x) ErrorTrace(#x)
//#define poco_Dlog(x) DebugTrace(#x)

class TestLogger
{
public:
	TestLogger();
	virtual ~TestLogger() = 0;
	static void SetLevel(int level);
	static int GetLevel();
protected:
	inline void EnterTrace(const std::string &fname) const 
	{
		std::string text("==>>  ");
		text += fname;
		poco_trace(logger_, text);
	}

	inline void ExitTrace(const std::string &fname) const
	{
		std::string text("<<==  ");
		text += fname;
		poco_trace(logger_, text);
	}
	Poco::Logger &logger_;
};



TestLogger::TestLogger() : logger_(Poco::Logger::get("MS"))
{
	/*配置日志相关参数*/

	//标准输出
	AutoPtr<ConsoleChannel> pCsC(new ConsoleChannel);

	//日志文件参数
	AutoPtr<FileChannel> pFLC(new FileChannel);
	pFLC->setProperty("path", "E:\\test.log");
	pFLC->setProperty("archive", "timestamp");
	pFLC->setProperty("times", "local");
	pFLC->setProperty("rotation", "1 M");
	pFLC->setProperty("purgeAge", "1 weeks");
	pFLC->setProperty("compress", "true");

	//日志格式
	AutoPtr<PatternFormatter> pPF(new PatternFormatter);
	pPF->setProperty("pattern", "%Y-%m-%d %H:%M:%S %s [%p] %U(%u): %t");
	pPF->setProperty("times", "local");

	AutoPtr<FormattingChannel> pFFC(new FormattingChannel(pPF, pFLC));
	AutoPtr<FormattingChannel> pFCC(new FormattingChannel(pPF, pCsC));

	AutoPtr<SplitterChannel> pSPC(new SplitterChannel);
	pSPC->addChannel(pFFC);
	pSPC->addChannel(pFCC);

	logger_.setChannel(pSPC);
	logger_.setLevel(8);
}

TestLogger::~TestLogger() {}

void TestLogger::SetLevel(int level)
{
	Poco::Logger::get("MS").setLevel(level);
}

int TestLogger::GetLevel()
{
	return Poco::Logger::get("MS").getLevel();
}

class test:public TestLogger
{
public:
	void logprint()
	{
		ENTER_F //进入函数标志
		poco_debug(logger_,"test");
		//-----poco_error,poco_error_f1,poco_error_f2 答应错误日志,f1  f2分别可以带2个参数和一个参数
		EXIT_F	//退出函数标志
	}
};

int main()
{
	test obj;
	obj.logprint();
	getchar();
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值