testPattern.cpp
Go to the documentation of this file.
00001 // testLog4CPP.cpp : Derived from testCategory.cpp
00002 //
00003 #include <log4cpp/Category.hh>
00004 #include <log4cpp/Appender.hh>
00005 #include <log4cpp/Priority.hh>
00006 #include <log4cpp/NDC.hh>
00007 #include <log4cpp/PatternLayout.hh>
00008 #include <log4cpp/BasicConfigurator.hh>
00009 #include <iostream>
00010 
00011 void test(std::string pattern, log4cpp::PatternLayout* layout, log4cpp::Category& cat)
00012 {
00013     try {       
00014         layout->setConversionPattern(pattern);
00015         cat.error("message");
00016     } catch(log4cpp::ConfigureFailure& f) {
00017         std::cerr << "configure failure: " << f.what() << std::endl;
00018     }   
00019 }                                                                              
00020 
00021 int main(int argc, char* argv[])
00022 {
00023     log4cpp::BasicConfigurator::configure();
00024     log4cpp::PatternLayout* layout = new log4cpp::PatternLayout();
00025     log4cpp::Category::getRoot().getAppender()->setLayout(layout);
00026 
00027     log4cpp::Category& cat = log4cpp::Category::getInstance("cat1");
00028     
00029     log4cpp::NDC::push("ndc1");
00030 
00031     test("%% %r %c:%d (%R / %r) [%p] %x %m %% (%u) %n", layout, cat);
00032 
00033     // test format specifiers
00034     test(">%10m<%n", layout, cat);
00035     test(">%-10m<%n", layout, cat);
00036     test(">%3.5m<%n", layout, cat);
00037     test(">%.5m<%n", layout, cat);
00038 
00039     // category test
00040     test(">%c{2}<%n", layout, log4cpp::Category::getInstance("c1.c2.c3.c4"));
00041 
00042     // test date format
00043     test("%d{%d %b %Y %H:%M:%S.%l} %m %n", layout, cat);
00044     test("%d{%d %b %Y %H:%M:%S.%l", layout, cat);
00045     test("%d%n", layout, cat);
00046 
00047     test("%m %d%n", layout, cat);
00048     int i;
00049     for (i = 0; i < 10; i++) {
00050         cat.error("%d message", i);
00051     }
00052     test("%m %d{%H:%M:%S.%l %d %b %Y}%n", layout, cat);
00053     for (i = 0; i < 10; i++) {
00054         cat.error("%d message", i);
00055     }
00056 
00057     // test bug #688715
00058     test("%.10m", layout, cat);
00059 
00060     log4cpp::Category::shutdown();
00061 
00062     return 0;
00063 }
00064 


log4cpp
Author(s): Stephen Roderick, Bastiaan Bakker, Cedric Le Goater, Steve Ostlind, Marcel Harkema, Walter Stroebel, Glenn Scott and Tony Cheung.
autogenerated on Wed Sep 16 2015 10:27:14