#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;
}
Poco C++ 日志框架使用 Pcoco Logger
最新推荐文章于 2023-12-15 23:47:40 发布