testCategory.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <iostream>
3 #include <log4cpp/Category.hh>
4 #include <log4cpp/Appender.hh>
6 #include <log4cpp/Layout.hh>
7 #include <log4cpp/BasicLayout.hh>
8 #include <log4cpp/Priority.hh>
9 #include <log4cpp/NDC.hh>
10 
11 
13  log4cpp::Priority::Value priority,
14  const char* stringFormat,
15  ...)
16 {
17  va_list va;
18  va_start(va, stringFormat);
19  category.logva(priority, stringFormat, va);
20  va_end(va);
21 } /* end testLogva */
22 
24  log4cpp::Appender* appender,
25  log4cpp::Appender* appender2,
26  log4cpp::Appender& appender3)
27 {
28  // test getAppender() - should return one of the three appenders
29  log4cpp::Appender *tmpAppender = category.getAppender();
30  if ((tmpAppender == appender) ||
31  (tmpAppender == appender2) ||
32  (tmpAppender == &appender3))
33  {
34  std::cout << "tmpAppender == appender or appender2 or appender3" << std::endl;
35  }
36  else
37  {
38  std::cout << "tmpAppender != appender or appender2 or appender3" << std::endl;
39  } /* end if-else */
40 
41  // test getAppender(const std::string& name) const
42  tmpAppender = category.getAppender("appender2");
43  if (tmpAppender == appender2)
44  {
45  std::cout << "tmpAppender == appender2" << std::endl;
46  }
47  else
48  {
49  std::cout << "tmpAppender != appender2" << std::endl;
50  } /* end if-else */
51 
52  tmpAppender = category.getAppender("appender3");
53  if (tmpAppender == &appender3)
54  {
55  std::cout << "tmpAppender == appender3" << std::endl;
56  }
57  else
58  {
59  std::cout << "tmpAppender != appender3" << std::endl;
60  } /* end if-else */
61 
62 } /* end testGetAppender() */
63 
65 {
66  log4cpp::Appender* appender =
67  new log4cpp::OstreamAppender("appender", &std::cout);
68 
69  log4cpp::Appender* appender2 =
70  new log4cpp::OstreamAppender("appender2", &std::cout);
71 
72  log4cpp::OstreamAppender appender3("appender3", &std::cout);
73 
74  log4cpp::Layout* layout = new log4cpp::BasicLayout();
75  log4cpp::Layout* layout2 = new log4cpp::BasicLayout();
76  log4cpp::Layout* layout3 = new log4cpp::BasicLayout();
77 
78  appender->setLayout(layout);
79  appender2->setLayout(layout2);
80  appender3.setLayout(layout3);
81 
82  // add three appenders to root category
85 
86  // clear root's initial appender
87  root.removeAllAppenders();
88 
89  root.addAppender(appender);
90  root.addAppender(appender2);
91  root.addAppender(appender3);
92 
93  // dump a message - should see three on the screen
94  std::cout << "You should see three lines of \"root error #1\"" << std::endl;
95  root.error("root error #1");
96  std::cout << "Did you?" << std::endl;
97 
98  // get getAppender() changes on category with appenders
99  std::cout << "You should see messages that tmpAppender == other appenders" << std::endl;
100  testGetAppender(root, appender, appender2, appender3);
101  std::cout << "Did you?" << std::endl;
102 
103  // add appender by reference to sub1 category
104  log4cpp::Category& sub1 =
105  log4cpp::Category::getInstance(std::string("sub1"));
106  sub1.addAppender(appender3);
107 
108  // clear all appenders
109  root.removeAllAppenders();
110  sub1.removeAllAppenders();
111 
112  // dump a message - should not see it on the screen
113  std::cout << "You should not see any lines of \"root error #2\"" << std::endl;
114  root.error("root error #2");
115  std::cout << "Did you?" << std::endl;
116 
117  // get getAppender() changes on category with no appenders
118  std::cout << "You should see messages that tmpAppender != other appenders" << std::endl;
119  testGetAppender(root, appender, appender2, appender3);
120  std::cout << "Did you?" << std::endl;
121 
122 
123  // add three appenders to root category
124  appender = new log4cpp::OstreamAppender("appender", &std::cout);
125  appender2 = new log4cpp::OstreamAppender("appender2", &std::cout);
126  root.addAppender(appender);
127  root.addAppender(appender2);
128  root.addAppender(appender3);
129 
130  // test removing valid and invalid
131  root.removeAppender(appender);
132  root.removeAppender(appender2);
133  root.removeAppender(&appender3);
134 
135 } /* end testMultiAppenders() */
136 
137 
138 int main(int argc, char** argv) {
139 
141 
142  log4cpp::Appender* appender =
143  new log4cpp::OstreamAppender("default", &std::cout);
144 
145  log4cpp::Appender* appender2 =
146  new log4cpp::OstreamAppender("default2", &std::cout);
147 
148  log4cpp::Layout* layout = new log4cpp::BasicLayout();
149  log4cpp::Layout* layout2 = new log4cpp::BasicLayout();
150 
151  appender->setLayout(layout);
152  appender2->setLayout(layout2);
153 
155  root.addAppender(appender);
157 
158  log4cpp::Category& sub1 =
159  log4cpp::Category::getInstance(std::string("sub1"));
160  sub1.addAppender(appender2);
161  sub1.setAdditivity(false);
162 
163  log4cpp::Category& sub2 =
164  log4cpp::Category::getInstance(std::string("sub1.sub2"));
165 
166  std::cout << " root priority = " << root.getPriority() << std::endl;
167  std::cout << " sub1 priority = " << sub1.getPriority() << std::endl;
168  std::cout << " sub2 priority = " << sub2.getPriority() << std::endl;
169 
170  root.error("root error");
171  root.warn("root warn");
172  sub1.error("sub1 error");
173  sub1.warn("sub1 warn");
174  sub2.error("sub2 error");
175  sub2.warn("sub2 warn");
176 
177  testLogva(root, log4cpp::Priority::EMERG, "This contains %d %s", 2, "variable arguments");
178  testLogva(root, log4cpp::Priority::ALERT, "This contains %d %s", 2, "variable arguments");
179  testLogva(root, log4cpp::Priority::CRIT, "This contains %d %s", 2, "variable arguments");
180  testLogva(root, log4cpp::Priority::ERROR, "This contains %d %s", 2, "variable arguments");
181  testLogva(root, log4cpp::Priority::WARN, "This contains %d %s", 2, "variable arguments");
182  testLogva(root, log4cpp::Priority::INFO, "This contains %d %s", 2, "variable arguments");
183  testLogva(root, log4cpp::Priority::NOTICE, "This contains %d %s", 2, "variable arguments");
184  testLogva(root, log4cpp::Priority::DEBUG, "This contains %d %s", 2, "variable arguments");
185 
186  char lengthy1[] = "Test for variable-arguments lists overflow. Test for variable-arguments lists overflow. Test for variable-arguments lists overflow. Test for variable-arguments lists overflow. Test for variable-arguments lists overflow. Test for variable-arguments lists overflow. Test for variable-arguments lists overflow. Test for variable-arguments lists overflow. Test for variable-arguments lists overflow. Test for variable-arguments lists overflow. Test for variable-arguments lists overflow. Test for variable-arguments lists overflow. Test for variable-arguments lists overflow. Test for variable-arguments lists overflow. Test for variable-arguments lists overflow. Test for variable-arguments lists overflow. Test for variable-arguments lists overflow. Test for variable-arguments lists overflow. Test for variable-arguments lists overflow. Test for variable-arguments lists overflow. Test for variable-arguments lists overflow. Test for variable-arguments lists overflow. Test for variable-arguments lists overflow. Test for variable-arguments lists overflow. Test for variable-arguments lists overflow. Test for variable-arguments lists overflow. ";
187  testLogva(root, log4cpp::Priority::ERROR, "This contains really lengthy strings which should be logged well (%d bytes): %s", sizeof(lengthy1), lengthy1);
188 
190  std::cout << " root priority = " << root.getPriority() << std::endl;
191  std::cout << " sub1 priority = " << sub1.getPriority() << std::endl;
192  std::cout << " sub2 priority = " << sub2.getPriority() << std::endl;
193 
194  std::cout << "priority info" << std::endl;
195  root.error("root error");
196  root.warn("root warn");
197  sub1.error("sub1 error");
198  sub1.warn("sub1 warn");
199  sub2.error("sub2 error");
200  sub2.warn("sub2 warn");
201  sub2.error("%s %s %d", "test", "vform", 123);
202  sub2.warnStream() << "streamed warn";
203 
204  sub2 << log4cpp::Priority::WARN << "warn2.." << "..warn3..value=" << 0
205  << log4cpp::eol << "..warn4";
206 
208 
209  return 0;
210 }
CategoryStream warnStream()
Definition: Category.hh:420
void warn(const char *stringFormat,...)
Definition: Category.cpp:333
LOG4CPP_EXPORT CategoryStream & eol(CategoryStream &os)
virtual Appender * getAppender() const
Definition: Category.cpp:122
virtual void setAdditivity(bool additivity)
Definition: Category.cpp:235
void testLogva(log4cpp::Category &category, log4cpp::Priority::Value priority, const char *stringFormat,...)
static Category & getInstance(const std::string &name)
Definition: Category.cpp:35
static void shutdown()
Definition: Category.cpp:48
void testGetAppender(log4cpp::Category &category, log4cpp::Appender *appender, log4cpp::Appender *appender2, log4cpp::Appender &appender3)
virtual void logva(Priority::Value priority, const char *stringFormat, va_list va)
Definition: Category.cpp:283
int main(int argc, char **argv)
virtual void removeAllAppenders()
Definition: Category.cpp:151
static Category & getRoot()
Definition: Category.cpp:23
virtual void removeAppender(Appender *appender)
Definition: Category.cpp:168
virtual Priority::Value getPriority() const
Definition: Category.cpp:67
virtual void setPriority(Priority::Value priority)
Definition: Category.cpp:71
virtual void setLayout(Layout *layout=NULL)
virtual void addAppender(Appender *appender)
Definition: Category.cpp:94
void error(const char *stringFormat,...)
Definition: Category.cpp:347
void testMultiAppenders()
virtual void setLayout(Layout *layout)=0


log4cpp
Author(s): Stephen Roderick, Bastiaan Bakker, Cedric Le Goater, Steve Ostlind, Marcel Harkema, Walter Stroebel, Glenn Scott and Tony Cheung
autogenerated on Sun Jun 23 2019 19:14:17