testCategory.cpp
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <iostream>
00003 #include <log4cpp/Category.hh>
00004 #include <log4cpp/Appender.hh>
00005 #include <log4cpp/OstreamAppender.hh>
00006 #include <log4cpp/Layout.hh>
00007 #include <log4cpp/BasicLayout.hh>
00008 #include <log4cpp/Priority.hh>
00009 #include <log4cpp/NDC.hh>
00010 
00011 
00012 void testLogva(log4cpp::Category& category,
00013                log4cpp::Priority::Value priority, 
00014                const char* stringFormat,
00015                ...)
00016 {
00017     va_list va;
00018     va_start(va, stringFormat);
00019     category.logva(priority, stringFormat, va);
00020     va_end(va);
00021 } /* end testLogva */
00022 
00023 void testGetAppender(log4cpp::Category& category,
00024                      log4cpp::Appender* appender,
00025                      log4cpp::Appender* appender2,
00026                      log4cpp::Appender& appender3)
00027 {
00028     // test getAppender() - should return one of the three appenders
00029     log4cpp::Appender *tmpAppender = category.getAppender();
00030     if ((tmpAppender == appender) ||
00031         (tmpAppender == appender2) ||
00032         (tmpAppender == &appender3))
00033     {
00034         std::cout << "tmpAppender == appender or appender2 or appender3" << std::endl;
00035     }
00036     else
00037     {
00038         std::cout << "tmpAppender != appender or appender2 or appender3" << std::endl;
00039     } /* end if-else */
00040 
00041     // test getAppender(const std::string& name) const
00042     tmpAppender = category.getAppender("appender2");
00043     if (tmpAppender == appender2)
00044     {
00045         std::cout << "tmpAppender == appender2" << std::endl;
00046     }
00047     else
00048     {
00049         std::cout << "tmpAppender != appender2" << std::endl;
00050     } /* end if-else */
00051 
00052     tmpAppender = category.getAppender("appender3");
00053     if (tmpAppender == &appender3)
00054     {
00055         std::cout << "tmpAppender == appender3" << std::endl;
00056     }
00057     else
00058     {
00059         std::cout << "tmpAppender != appender3" << std::endl;
00060     } /* end if-else */
00061 
00062 } /* end testGetAppender() */
00063 
00064 void testMultiAppenders()
00065 {
00066     log4cpp::Appender* appender = 
00067         new log4cpp::OstreamAppender("appender", &std::cout);
00068     
00069     log4cpp::Appender* appender2 = 
00070         new log4cpp::OstreamAppender("appender2", &std::cout);
00071 
00072     log4cpp::OstreamAppender appender3("appender3", &std::cout);
00073 
00074     log4cpp::Layout* layout = new log4cpp::BasicLayout();
00075     log4cpp::Layout* layout2 = new log4cpp::BasicLayout();
00076     log4cpp::Layout* layout3 = new log4cpp::BasicLayout();
00077 
00078     appender->setLayout(layout);
00079     appender2->setLayout(layout2);
00080     appender3.setLayout(layout3);
00081 
00082     // add three appenders to root category
00083     log4cpp::Category& root = log4cpp::Category::getRoot();
00084     root.setPriority(log4cpp::Priority::ERROR);
00085 
00086     // clear root's initial appender
00087     root.removeAllAppenders();
00088 
00089     root.addAppender(appender);
00090     root.addAppender(appender2);
00091     root.addAppender(appender3);
00092 
00093     // dump a message - should see three on the screen
00094     std::cout << "You should see three lines of \"root error #1\"" << std::endl;
00095     root.error("root error #1");
00096     std::cout << "Did you?" << std::endl;
00097 
00098     // get getAppender() changes on category with appenders
00099     std::cout << "You should see messages that tmpAppender == other appenders" << std::endl;
00100     testGetAppender(root, appender, appender2, appender3);
00101     std::cout << "Did you?" << std::endl;
00102 
00103     // add appender by reference to sub1 category
00104     log4cpp::Category& sub1 = 
00105         log4cpp::Category::getInstance(std::string("sub1"));
00106     sub1.addAppender(appender3);
00107 
00108     // clear all appenders
00109     root.removeAllAppenders();
00110     sub1.removeAllAppenders();
00111 
00112     // dump a message - should not see it on the screen
00113     std::cout << "You should not see any lines of  \"root error #2\"" << std::endl;
00114     root.error("root error #2");
00115     std::cout << "Did you?" << std::endl;
00116 
00117     // get getAppender() changes on category with no appenders
00118     std::cout << "You should see messages that tmpAppender != other appenders" << std::endl;
00119     testGetAppender(root, appender, appender2, appender3);
00120     std::cout << "Did you?" << std::endl;
00121 
00122 
00123     // add three appenders to root category
00124     appender = new log4cpp::OstreamAppender("appender", &std::cout);
00125     appender2 = new log4cpp::OstreamAppender("appender2", &std::cout);
00126     root.addAppender(appender);
00127     root.addAppender(appender2);
00128     root.addAppender(appender3);
00129 
00130     // test removing valid and invalid
00131     root.removeAppender(appender);
00132     root.removeAppender(appender2);
00133     root.removeAppender(&appender3);
00134 
00135 } /* end testMultiAppenders() */
00136 
00137 
00138 int main(int argc, char** argv) {    
00139 
00140     testMultiAppenders();
00141 
00142     log4cpp::Appender* appender = 
00143         new log4cpp::OstreamAppender("default", &std::cout);
00144 
00145     log4cpp::Appender* appender2 = 
00146         new log4cpp::OstreamAppender("default2", &std::cout);
00147 
00148     log4cpp::Layout* layout = new log4cpp::BasicLayout();
00149     log4cpp::Layout* layout2 = new log4cpp::BasicLayout();
00150     
00151     appender->setLayout(layout);
00152     appender2->setLayout(layout2);
00153 
00154     log4cpp::Category& root = log4cpp::Category::getRoot();
00155     root.addAppender(appender);
00156     root.setPriority(log4cpp::Priority::ERROR);
00157     
00158     log4cpp::Category& sub1 = 
00159         log4cpp::Category::getInstance(std::string("sub1"));
00160         sub1.addAppender(appender2);
00161         sub1.setAdditivity(false);
00162 
00163     log4cpp::Category& sub2 = 
00164         log4cpp::Category::getInstance(std::string("sub1.sub2"));
00165 
00166     std::cout << " root priority = " << root.getPriority() << std::endl;
00167     std::cout << " sub1 priority = " << sub1.getPriority() << std::endl;
00168     std::cout << " sub2 priority = " << sub2.getPriority() << std::endl;
00169     
00170     root.error("root error");
00171     root.warn("root warn");
00172     sub1.error("sub1 error");
00173     sub1.warn("sub1 warn");
00174     sub2.error("sub2 error");
00175     sub2.warn("sub2 warn");
00176     
00177     testLogva(root, log4cpp::Priority::EMERG, "This contains %d %s", 2, "variable arguments");
00178     testLogva(root, log4cpp::Priority::ALERT, "This contains %d %s", 2, "variable arguments");
00179     testLogva(root, log4cpp::Priority::CRIT, "This contains %d %s", 2, "variable arguments");
00180     testLogva(root, log4cpp::Priority::ERROR, "This contains %d %s", 2, "variable arguments");
00181     testLogva(root, log4cpp::Priority::WARN, "This contains %d %s", 2, "variable arguments");
00182     testLogva(root, log4cpp::Priority::INFO, "This contains %d %s", 2, "variable arguments");
00183     testLogva(root, log4cpp::Priority::NOTICE, "This contains %d %s", 2, "variable arguments");
00184     testLogva(root, log4cpp::Priority::DEBUG, "This contains %d %s", 2, "variable arguments");
00185 
00186     sub1.setPriority(log4cpp::Priority::INFO);
00187     std::cout << " root priority = " << root.getPriority() << std::endl;
00188     std::cout << " sub1 priority = " << sub1.getPriority() << std::endl;
00189     std::cout << " sub2 priority = " << sub2.getPriority() << std::endl;
00190    
00191     std::cout << "priority info" << std::endl;
00192     root.error("root error");
00193     root.warn("root warn");
00194     sub1.error("sub1 error");
00195     sub1.warn("sub1 warn");
00196     sub2.error("sub2 error");
00197     sub2.warn("sub2 warn");
00198     sub2.error("%s %s %d", "test", "vform", 123);
00199     sub2.warnStream() << "streamed warn";
00200 
00201     sub2 << log4cpp::Priority::WARN << "warn2.." << "..warn3..value=" << 0 
00202          << log4cpp::eol << "..warn4";
00203 
00204     log4cpp::Category::shutdown();
00205 
00206     return 0;
00207 }


log4cpp
Author(s): Stephen Roderick
autogenerated on Mon Oct 6 2014 03:13:14