testmain.cpp
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include "log4cpp/Portability.hh"
00003 #ifdef LOG4CPP_HAVE_UNISTD_H
00004 #include <unistd.h>
00005 #endif
00006 #include <iostream>
00007 #include "log4cpp/Category.hh"
00008 #include "log4cpp/Appender.hh"
00009 #include "log4cpp/FileAppender.hh"
00010 #include "log4cpp/OstreamAppender.hh"
00011 #ifdef LOG4CPP_HAVE_SYSLOG
00012 #include "log4cpp/SyslogAppender.hh"
00013 #endif
00014 #include "log4cpp/Layout.hh"
00015 #include "log4cpp/BasicLayout.hh"
00016 #include "log4cpp/Priority.hh"
00017 #include "log4cpp/NDC.hh"
00018 
00019 int main(int argc, char** argv) {
00020         log4cpp::Appender* appender;
00021 #ifdef LOG4CPP_HAVE_SYSLOG
00022         log4cpp::SyslogAppender* syslogAppender;
00023 
00024         syslogAppender = new log4cpp::SyslogAppender("syslog", "log4cpp");
00025 #else
00026         log4cpp::Appender* syslogAppender;
00027 
00028         syslogAppender = new log4cpp::OstreamAppender("syslogdummy", &std::cout);
00029 #endif
00030 
00031         if (argc < 2) {
00032                 appender = new log4cpp::OstreamAppender("default", &std::cout);
00033         } else {
00034                 appender = new log4cpp::FileAppender("default", argv[1]);
00035         }
00036 
00037         syslogAppender->setLayout(new log4cpp::BasicLayout());
00038         appender->setLayout(new log4cpp::BasicLayout());
00039 
00040         log4cpp::Category& root = log4cpp::Category::getRoot();
00041         root.addAppender(syslogAppender);
00042         root.setPriority(log4cpp::Priority::ERROR);
00043 
00044         log4cpp::Category& sub1 = log4cpp::Category::getInstance(std::string("sub1"));
00045         sub1.addAppender(appender);
00046 
00047         log4cpp::Category& sub2 = log4cpp::Category::getInstance(std::string("sub1.sub2"));
00048 
00049         log4cpp::NDC::push(std::string("ndc1"));
00050 
00051         std::cout << " root prio = " << root.getPriority() << std::endl;
00052         std::cout << " sub1 prio = " << sub1.getPriority() << std::endl;
00053         std::cout << " sub2 prio = " << sub2.getPriority() << std::endl;
00054 
00055         root.error("root error");
00056         root.warn("root warn");
00057         sub1.error("sub1 error");
00058         sub1.warn("sub1 warn");
00059         sub2.error("sub2 error");
00060         sub2.warn("sub2 warn");
00061 
00062         sub1.setPriority(log4cpp::Priority::INFO);
00063         std::cout << " root prio = " << root.getPriority() << std::endl;
00064         std::cout << " sub1 prio = " << sub1.getPriority() << std::endl;
00065         std::cout << " sub2 prio = " << sub2.getPriority() << std::endl;
00066 
00067         std::cout << "priority info" << std::endl;
00068         root.error("root error");
00069         root.warn("root warn");
00070         sub1.error("sub1 error");
00071         sub1.warn("sub1 warn");
00072         sub2.error("sub2 error");
00073         sub2.warn("sub2 warn");
00074 
00075         sub2.warnStream() << "streamed warn";
00076 
00077         sub2 << log4cpp::Priority::WARN << "warn2" << " warn3" 
00078              << log4cpp::eol << " warn4";
00079 
00080         {
00081                 for(int i = 0; i < 10000; i++) {
00082                         char ndc2[20];
00083                         sprintf(ndc2, "i=%d", i);
00084                         log4cpp::NDC::push(ndc2);
00085                         sub1.info("%s%d", "i = ", i);
00086                         if ((i % 10) == 0) {
00087                                 sub1.log(log4cpp::Priority::NOTICE, "reopen log");
00088                                 if (log4cpp::Appender::reopenAll()) {
00089                                         sub1.info("log reopened");
00090                                 } else {
00091                                         sub1.warn("could not reopen log");
00092                                 }
00093                         }
00094 #ifndef WIN32
00095                         sleep(1);
00096 #endif
00097                         log4cpp::NDC::pop();
00098                 }
00099         }
00100 
00101         return 0;
00102 }


log4cpp
Author(s): Stephen Roderick, Bastiaan Bakker, Cedric Le Goater, Steve Ostlind, Marcel Harkema, Walter Stroebel, Glenn Scott and Tony Cheung
autogenerated on Sun Aug 14 2016 03:18:26