[[PageNavi(NavigationList)]]
==== サンプル7 testmain.cpp ====
いろいろなAppenderを使ったサンプルです。カテゴリも複数使用しています。
恐らく、最も最初に見ることとなるサンプルでしょう。
===== 関連ファイル =====
* testmain.cpp
===== ファイルの中身 =====
testmain.cpp[[BR]]
{{{ code cpp
#include <stdio.h>
#include "log4cpp/Portability.hh"
#ifdef LOG4CPP_HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <iostream>
#include "log4cpp/Category.hh"
#include "log4cpp/Appender.hh"
#include "log4cpp/FileAppender.hh"
#include "log4cpp/OstreamAppender.hh"
#ifdef LOG4CPP_HAVE_SYSLOG
#include "log4cpp/SyslogAppender.hh"
#endif
#include "log4cpp/Layout.hh"
#include "log4cpp/BasicLayout.hh"
#include "log4cpp/Priority.hh"
#include "log4cpp/NDC.hh"
int main(int argc, char** argv) {
log4cpp::Appender* appender;
#ifdef LOG4CPP_HAVE_SYSLOG
log4cpp::SyslogAppender* syslogAppender;
syslogAppender = new log4cpp::SyslogAppender("syslog", "log4cpp");
#else
log4cpp::Appender* syslogAppender;
syslogAppender = new log4cpp::OstreamAppender("syslogdummy", &std::cout);
#endif
if (argc < 2) {
appender = new log4cpp::OstreamAppender("default", &std::cout);
} else {
appender = new log4cpp::FileAppender("default", argv[1]);
}
syslogAppender->setLayout(new log4cpp::BasicLayout());
appender->setLayout(new log4cpp::BasicLayout());
log4cpp::Category& root = log4cpp::Category::getRoot();
root.addAppender(syslogAppender);
root.setPriority(log4cpp::Priority::ERROR);
log4cpp::Category& sub1 = log4cpp::Category::getInstance(std::string("sub1"));
sub1.addAppender(appender);
log4cpp::Category& sub2 = log4cpp::Category::getInstance(std::string("sub1.sub2"));
log4cpp::NDC::push(std::string("ndc1"));
std::cout << " root prio = " << root.getPriority() << std::endl;
std::cout << " sub1 prio = " << sub1.getPriority() << std::endl;
std::cout << " sub2 prio = " << sub2.getPriority() << std::endl;
root.error("root error");
root.warn("root warn");
sub1.error("sub1 error");
sub1.warn("sub1 warn");
sub2.error("sub2 error");
sub2.warn("sub2 warn");
sub1.setPriority(log4cpp::Priority::INFO);
std::cout << " root prio = " << root.getPriority() << std::endl;
std::cout << " sub1 prio = " << sub1.getPriority() << std::endl;
std::cout << " sub2 prio = " << sub2.getPriority() << std::endl;
std::cout << "priority info" << std::endl;
root.error("root error");
root.warn("root warn");
sub1.error("sub1 error");
sub1.warn("sub1 warn");
sub2.error("sub2 error");
sub2.warn("sub2 warn");
sub2.warnStream() << "streamed warn";
sub2 << log4cpp::Priority::WARN << "warn2" << " warn3"
<< log4cpp::eol << " warn4";
{
for(int i = 0; i < 10000; i++) {
char ndc2[20];
sprintf(ndc2, "i=%d", i);
log4cpp::NDC::push(ndc2);
sub1.info("%s%d", "i = ", i);
if ((i % 10) == 0) {
sub1.log(log4cpp::Priority::NOTICE, "reopen log");
if (log4cpp::Appender::reopenAll()) {
sub1.info("log reopened");
} else {
sub1.warn("could not reopen log");
}
}
#ifndef WIN32
sleep(1);
#endif
log4cpp::NDC::pop();
}
}
return 0;
}
}}}
===== 実行結果例 =====
testmain.cppをビルドし、実行した結果です。実行環境はLinux(Ubuntu 11.0)です。
{{{
root prio = 300
sub1 prio = 800
sub2 prio = 800
1336867395 ERROR sub1 ndc1: sub1 error
1336867395 ERROR sub1.sub2 ndc1: sub2 error
root prio = 300
sub1 prio = 600
sub2 prio = 800
priority info
1336867395 ERROR sub1 ndc1: sub1 error
1336867395 WARN sub1 ndc1: sub1 warn
1336867395 ERROR sub1.sub2 ndc1: sub2 error
1336867395 WARN sub1.sub2 ndc1: sub2 warn
1336867395 WARN sub1.sub2 ndc1: streamed warn
1336867395 WARN sub1.sub2 ndc1: warn2 warn3
1336867395 WARN sub1.sub2 ndc1: warn4
1336867395 INFO sub1 ndc1 i=0: i = 0
1336867395 NOTICE sub1 ndc1 i=0: reopen log
1336867395 INFO sub1 ndc1 i=0: log reopened
1336867396 INFO sub1 ndc1 i=1: i = 1
1336867397 INFO sub1 ndc1 i=2: i = 2
1336867398 INFO sub1 ndc1 i=3: i = 3
1336867399 INFO sub1 ndc1 i=4: i = 4
1336867400 INFO sub1 ndc1 i=5: i = 5
1336867401 INFO sub1 ndc1 i=6: i = 6
1336867402 INFO sub1 ndc1 i=7: i = 7
1336867403 INFO sub1 ndc1 i=8: i = 8
1336867404 INFO sub1 ndc1 i=9: i = 9
1336867405 INFO sub1 ndc1 i=10: i = 10
1336867405 NOTICE sub1 ndc1 i=10: reopen log
1336867405 INFO sub1 ndc1 i=10: log reopened
1336867406 INFO sub1 ndc1 i=11: i = 11
1336867407 INFO sub1 ndc1 i=12: i = 12
1336867408 INFO sub1 ndc1 i=13: i = 13
1336867409 INFO sub1 ndc1 i=14: i = 14
1336867410 INFO sub1 ndc1 i=15: i = 15
1336867411 INFO sub1 ndc1 i=16: i = 16
1336867412 INFO sub1 ndc1 i=17: i = 17
1336867413 INFO sub1 ndc1 i=18: i = 18
1336867414 INFO sub1 ndc1 i=19: i = 19
1336867415 INFO sub1 ndc1 i=20: i = 20
1336867415 NOTICE sub1 ndc1 i=20: reopen log
1336867415 INFO sub1 ndc1 i=20: log reopened
1336867416 INFO sub1 ndc1 i=21: i = 21
1336867417 INFO sub1 ndc1 i=22: i = 22
1336867418 INFO sub1 ndc1 i=23: i = 23
1336867419 INFO sub1 ndc1 i=24: i = 24
1336867420 INFO sub1 ndc1 i=25: i = 25
1336867421 INFO sub1 ndc1 i=26: i = 26
1336867422 INFO sub1 ndc1 i=27: i = 27
1336867423 INFO sub1 ndc1 i=28: i = 28
1336867424 INFO sub1 ndc1 i=29: i = 29
1336867425 INFO sub1 ndc1 i=30: i = 30
1336867425 NOTICE sub1 ndc1 i=30: reopen log
1336867425 INFO sub1 ndc1 i=30: log reopened
1336867426 INFO sub1 ndc1 i=31: i = 31
1336867427 INFO sub1 ndc1 i=32: i = 32
1336867428 INFO sub1 ndc1 i=33: i = 33
1336867429 INFO sub1 ndc1 i=34: i = 34
※以下省略
}}}
[[PageNavi(NavigationList)]]