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