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

来源:互联网 发布:js new一个女朋友 编辑:程序博客网 时间:2024/05/22 03:51
#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;}

原创粉丝点击