// log4cplus.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <log4cpp/Category.hh>
#include <log4cpp/Appender.hh>
#include "log4cpp/FileAppender.hh"
#include <log4cpp/OstreamAppender.hh>
#include "log4cpp/NTEventLogAppender.hh"
#include <log4cpp/StringQueueAppender.hh>
#include <log4cpp/RollingFileAppender.hh>
//#include "log4cpp/SyslogAppender.hh"
#include <log4cpp/Layout.hh>
#include <log4cpp/BasicLayout.hh>
#include <log4cpp/PatternLayout.hh>
#include <log4cpp/BasicConfigurator.hh>
#include <log4cpp/PropertyConfigurator.hh>
#include <log4cpp/Priority.hh>
#include <log4cpp/NDC.hh>
#include <string>
#include <queue>
using namespace std;
using namespace log4cpp;
#pragma comment(lib,".\\Result\\log4cpp.lib")
void TestFileAppender();
void TestOstreamAppender();
void TestSyslogAppender();
void Ndc();
void TestNTEventAppender();
void TestPatternLayout();
void TestPropConfig();
void TestStringQueue();
void TestRollingFile();
int _tmain(int argc, _TCHAR* argv[])
{
TestFileAppender();
TestOstreamAppender();
//TestSyslogAppender(); //#include <syslog.h> 不存在
Ndc();
TestNTEventAppender();
TestPatternLayout();
TestPropConfig();
TestStringQueue();
TestRollingFile();
system("puase");
return 0;
}
void testLogva(log4cpp::Category& category,log4cpp::Priority::Value priority, const char* stringFormat,...)
{
va_list va;
va_start(va, stringFormat);
category.logva(priority, stringFormat, va);
va_end(va);
}
void TestFileAppender()
{
log4cpp::Appender* appender = new log4cpp::FileAppender("default", "fileappender.log");
log4cpp::Layout* layout = new log4cpp::BasicLayout();
appender->setLayout(layout);
log4cpp::Category& root = log4cpp::Category::getRoot();
root.addAppender(appender);
root.setPriority(log4cpp::Priority::INFO);
root.log(log4cpp::Priority::INFO, "reopen log");
if (appender->reopen()) //reopenAll()
{
root.info("log reopened");
}
else
{
root.warn("could not reopen log");
}
root.log(log4cpp::Priority::ERROR, "reopen log");
if (appender->reopen()) //reopenAll()
{
root.info("log reopened");
}
else
{
root.warn("could not reopen log");
}
log4cpp::Category::shutdown();
}
void TestOstreamAppender()
{
log4cpp::Appender* appender = new log4cpp::OstreamAppender("appender", &std::cout);
log4cpp::Layout* layout = new log4cpp::BasicLayout();
appender->setLayout(layout);
log4cpp::Category& root = log4cpp::Category::getRoot();
root.setPriority(log4cpp::Priority::ERROR);
root.removeAllAppenders();
root.addAppender(appender);
root.error("root error #1");
root.warn("root error #2"); //不能被输出
log4cpp::Appender* subappender = new log4cpp::OstreamAppender("appender", &std::cout);
log4cpp::Category& sub = log4cpp::Category::getInstance(std::string("sub1"));
sub.addAppender(subappender);
sub.error("root error #3"); //输出两次
std::cout<<(root.getAppender() == appender)<<std::endl;
std::cout<<(root.getAppender() == subappender)<<std::endl;
root.removeAllAppenders();
sub.removeAllAppenders();
root.error("root error #4"); //不能被输出
sub.error("root error #5"); //不能被输出
std::cout<<(root.getAppender() == subappender)<<std::endl;
//root.addAppender(appender); //直接中断错误
root.error("root error #6"); //不能被显示
root.removeAllAppenders();
appender = new log4cpp::OstreamAppender("appender", &std::cout);
root.addAppender(appender);
root.error("root error #6");
subappender = new log4cpp::OstreamAppender("appender", &std::cout);
sub.addAppender(subappender);
sub.setAdditivity(false); //false覆盖,true添加又一端口
sub.error("root error #7"); //只显示一次
std::cout << " root priority = " << root.getPriority() << std::endl;
std::cout << " sub priority = " << sub.getPriority() << std::endl;
sub.setPriority(log4cpp::Priority::INFO);
std::cout << " root priority = " << root.getPriority() << std::endl;
std::cout << " sub priority = " << sub.getPriority() << std::endl;
testLogva(root, log4cpp::Priority::EMERG, "This contains %d %s", 2, "variable arguments");
testLogva(root, log4cpp::Priority::ALERT, "This contains %d %s", 2, "variable arguments");
testLogva(root, log4cpp::Priority::CRIT, "This contains %d %s", 2, "variable arguments");
testLogva(root, log4cpp::Priority::ERROR, "This contains %d %s", 2, "variable arguments");
testLogva(root, log4cpp::Priority::WARN, "This contains %d %s", 2, "variable arguments");
testLogva(root, log4cpp::Priority::INFO, "This contains %d %s", 2, "variable arguments");
testLogva(root, log4cpp::Priority::NOTICE, "This contains %d %s", 2, "variable arguments");
testLogva(root, log4cpp::Priority::DEBUG, "This contains %d %s", 2, "variable arguments");
sub.warn("sub2 warn");
sub.error("%s %s %d", "test", "vform", 123);
sub.warnStream() << "streamed warn";
sub << log4cpp::Priority::WARN << "warn2.." << "..warn3..value=" << 0
<< log4cpp::eol << "..warn4";
/*root.removeAllAppenders();
sub.removeAllAppenders();*/
std::vector<log4cpp::Category*>* allCats;
std::vector<log4cpp::Category*>::const_iterator iter;
allCats = log4cpp::Category::getCurrentCategories();
// remove all appenders from all categories
for (iter = allCats->begin(); iter != allCats->end(); iter++) {
(*iter)->removeAllAppenders();
}
log4cpp::Category::shutdown();
}
void TestSyslogAppender()
{
/*log4cpp::SyslogAppender* syslogAppender;
syslogAppender = new log4cpp::SyslogAppender("syslog", "log4cpp");*/
log4cpp::Appender*syslogAppender = new log4cpp::FileAppender("default", "fileappender");
syslogAppender->setLayout(new log4cpp::BasicLayout());
log4cpp::Category& root = log4cpp::Category::getRoot();
root.addAppender(syslogAppender);
root.setPriority(log4cpp::Priority::ERROR);
root.log(log4cpp::Priority::NOTICE, "reopen log");
if (log4cpp::Appender::reopenAll())
{
root.info("log reopened");
}
else
{
root.warn("could not reopen log");
}
log4cpp::Category::shutdown();
}
void Ndc()
{
std::cout << "1. empty NDC: " << NDC::get() << std::endl;
NDC::push("context1");
std::cout << "2. push context1: " << NDC::get() << std::endl;
NDC::push("context2");
std::cout << "3. push context2: " << NDC::get() << std::endl;
NDC::push("context3");
std::cout << "4. push context3: " << NDC::get() << std::endl;
std::cout << "5. get depth: " << NDC::getDepth() << std::endl;
std::cout << "6. pop: " << NDC::pop() << std::endl;
NDC::clear();
std::cout << "7. clear: " << NDC::get() << std::endl;
for(int i = 0; i < 100; i++)
{
char ndc2[20];
sprintf(ndc2, "i=%d", i);
log4cpp::NDC::push(ndc2);
std::cout << i<<" : " << NDC::get() << std::endl;
log4cpp::NDC::pop();
}
log4cpp::Category::shutdown();
}
void TestNTEventAppender()
{
log4cpp::Appender* appender = new log4cpp::NTEventLogAppender("default", "testNTEventAppender.Log");
log4cpp::Category& sub1 = log4cpp::Category::getInstance(std::string("sub1"));
sub1.addAppender(appender);
sub1.setPriority(log4cpp::Priority::DEBUG);
sub1.emerg("sub1 emerg");
sub1.fatal("sub1 fatal");
sub1.alert("sub1 alert");
sub1.crit("sub1 crit");
sub1.error("sub1 error");
sub1.warn("sub1 warn");
sub1.notice("sub1 notice");
sub1.info("sub1 info");
sub1.debug("sub1 debug");
sub1.log(log4cpp::Priority::NOTSET, "sub1 notset");
sub1.log(log4cpp::Priority::ERROR, "sub1 error");
log4cpp::Category::shutdown();
}
void TestPatternLayout()
{
/*log4cpp::Appender* appender = new log4cpp::OstreamAppender("appender", &std::cout);
log4cpp::PatternLayout* layout = new log4cpp::PatternLayout();
appender->setLayout(layout);
log4cpp::Category& root = log4cpp::Category::getRoot();
root.addAppender(appender);*/
log4cpp::BasicConfigurator::configure(); //初始化
log4cpp::PatternLayout* layout = new log4cpp::PatternLayout();
layout->setConversionPattern("%d: %p %c %x: %m%n");
log4cpp::Category::getRoot().getAppender()->setLayout(layout);
log4cpp::Category& root = log4cpp::Category::getRoot();
log4cpp::NDC::push("ndc1");
root.error("message");
std::string format = "%% %r %c:%d (%R / %r) [%p] %x %m %% (%u) %n";
layout->setConversionPattern(format);
root.error("message");
// test date format
format = "%d{%d %b %Y %H:%M:%S.%l} %m %n";
layout->setConversionPattern(format);
root.error("message");
format = "%d{%d %b %Y %H:%M:%S.%l";
layout->setConversionPattern(format);
root.error("message");
format = "%d%n";
layout->setConversionPattern(format);
root.error("message");
format = "%.32m %d%n";
layout->setConversionPattern(format);
int i;
for (i = 0; i < 10; i++)
{
root.error("%d message", i);
}
format = "%32m %d{%H:%M:%S.%l %d %b %Y}%n";
layout->setConversionPattern(format);
for (i = 0; i < 10; i++)
{
root.error("%d message", i);
}
}
void TestPropConfig()
{
std::string initFileName;
initFileName = ".\\include\\tests\\log4cpp.nt.property";
log4cpp::PropertyConfigurator::configure(initFileName);
log4cpp::Category& root = log4cpp::Category::getRoot();
log4cpp::Category& sub1 =
log4cpp::Category::getInstance(std::string("sub1"));
log4cpp::Category& sub2 =
log4cpp::Category::getInstance(std::string("sub1.sub2"));
root.error("root error");
root.warn("root warn");
sub1.error("sub1 error");
sub1.warn("sub1 warn");
// the following 2 should only be in A1, not A2
sub1.info("sub1 info");
sub1.debug("sub1 debug");
// do a few so that the log rolls over
sub2.warn("sub2 warn 1");
sub2.warn("sub2 warn 2");
sub2.warn("sub2 warn 3");
sub2.warn("sub2 warn 4");
sub2.warn("sub2 warn 5");
sub2.warn("sub2 warn 6");
sub2.warn("sub2 warn 7");
#if defined(WIN32)
log4cpp::Category& nt = log4cpp::Category::getInstance(std::string("subNT"));
nt.error("subNT error");
nt.warn("subNT warn");
nt.debug("subNT debug");
#endif
log4cpp::Category::shutdown();
}
void TestStringQueue()
{
log4cpp::StringQueueAppender* strQAppender = new log4cpp::StringQueueAppender("strQAppender");
strQAppender->setLayout(new log4cpp::BasicLayout());
log4cpp::Category& root = log4cpp::Category::getRoot();
root.addAppender(strQAppender);
root.setPriority(log4cpp::Priority::DEBUG);
root.error("Hello log4cpp in a Error Message!");
root.warn("Hello log4cpp in a Warning Message!");
cout<<"Get message from Memory Queue!"<<endl;
cout<<"-------------------------------------------"<<endl;
queue<string>& myStrQ = strQAppender->getQueue();
while(!myStrQ.empty())
{
cout<<myStrQ.front();
myStrQ.pop();
}
}
void TestRollingFile()
{
log4cpp::PatternLayout* pLayout1 = new log4cpp::PatternLayout();
pLayout1->setConversionPattern("%d: %p %c %x: %m%n");
log4cpp::PatternLayout* pLayout2 = new log4cpp::PatternLayout();
pLayout2->setConversionPattern("%d: %p %c %x: %m%n");
log4cpp::Appender* fileAppender = new log4cpp::FileAppender("fileAppender","wxb.log");
fileAppender->setLayout(pLayout1);
log4cpp::RollingFileAppender* rollfileAppender = new log4cpp::RollingFileAppender( "rollfileAppender","rollwxb.log",5*1024,1);
rollfileAppender->setLayout(pLayout2);
log4cpp::Category& root = log4cpp::Category::getRoot().getInstance("RootName");
root.addAppender(fileAppender);
root.addAppender(rollfileAppender);
root.setPriority(log4cpp::Priority::DEBUG);
for (int i = 0; i < 100; i++)
{
string strError;
ostringstream oss;
oss<<i<<":Root Error Message!";
strError = oss.str();
root.error(strError);
}
log4cpp::Category::shutdown();
}
/*
# property configurator test file
log4cpp.rootCategory=DEBUG, rootAppender
log4cpp.category.sub1=DEBUG, A1, A2
log4cpp.category.sub1.sub2=DEBUG, A3
log4cpp.appender.rootAppender=ConsoleAppender
log4cpp.appender.rootAppender.layout=BasicLayout
log4cpp.appender.A1=FileAppender
log4cpp.appender.A1.fileName=A1.log
log4cpp.appender.A1.layout=PatternLayout
log4cpp.appender.A1.layout.ConversionPattern=The message %m at time %d%n
log4cpp.appender.A2=FileAppender
log4cpp.appender.A2.threshold=WARN
log4cpp.appender.A2.fileName=A2.log
log4cpp.appender.A2.layout=PatternLayout
log4cpp.appender.A2.layout.ConversionPattern=The message %m at time %d%n
log4cpp.appender.A3=RollingFileAppender
log4cpp.appender.A3.fileName=A3.log
log4cpp.appender.A3.maxFileSize=80
log4cpp.appender.A3.maxBackupIndex=1
log4cpp.appender.A3.layout=PatternLayout
log4cpp.appender.A3.layout.ConversionPattern=The message %m at time %d{%H:%M}%n
log4cpp.category.subNT=INFO, subNT
log4cpp.appender.subNT=NTEventLogAppender
log4cpp.appender.subNT.source=log4cpp-test
*/
/*
log4cpp::FileAppender // 输出到文件
log4cpp::RollingFileAppender // 输出到回卷文件,即当文件到达某个大小后回卷
log4cpp::OstreamAppender // 输出到一个ostream类
log4cpp::StringQueueAppender // 内存队列
log4cpp::Win32DebugAppender // 发送到缺省系统调试器
log4cpp::NTEventLogAppender // 发送到win 事件日志
*/