testmain.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include "log4cpp/Portability.hh"
3 #ifdef LOG4CPP_HAVE_UNISTD_H
4 #include <unistd.h>
5 #endif
6 #include <iostream>
7 #include "log4cpp/Category.hh"
8 #include "log4cpp/Appender.hh"
11 #ifdef LOG4CPP_HAVE_SYSLOG
13 #endif
14 #include "log4cpp/Layout.hh"
15 #include "log4cpp/BasicLayout.hh"
16 #include "log4cpp/Priority.hh"
17 #include "log4cpp/NDC.hh"
18 
19 int main(int argc, char** argv) {
20  log4cpp::Appender* appender;
21 #ifdef LOG4CPP_HAVE_SYSLOG
22  log4cpp::SyslogAppender* syslogAppender;
23 
24  syslogAppender = new log4cpp::SyslogAppender("syslog", "log4cpp");
25 #else
26  log4cpp::Appender* syslogAppender;
27 
28  syslogAppender = new log4cpp::OstreamAppender("syslogdummy", &std::cout);
29 #endif
30 
31  if (argc < 2) {
32  appender = new log4cpp::OstreamAppender("default", &std::cout);
33  } else {
34  appender = new log4cpp::FileAppender("default", argv[1]);
35  }
36 
37  syslogAppender->setLayout(new log4cpp::BasicLayout());
38  appender->setLayout(new log4cpp::BasicLayout());
39 
41  root.addAppender(syslogAppender);
43 
44  log4cpp::Category& sub1 = log4cpp::Category::getInstance(std::string("sub1"));
45  sub1.addAppender(appender);
46 
47  log4cpp::Category& sub2 = log4cpp::Category::getInstance(std::string("sub1.sub2"));
48 
49  log4cpp::NDC::push(std::string("ndc1"));
50 
51  std::cout << " root prio = " << root.getPriority() << std::endl;
52  std::cout << " sub1 prio = " << sub1.getPriority() << std::endl;
53  std::cout << " sub2 prio = " << sub2.getPriority() << std::endl;
54 
55  root.error("root error");
56  root.warn("root warn");
57  sub1.error("sub1 error");
58  sub1.warn("sub1 warn");
59  sub2.error("sub2 error");
60  sub2.warn("sub2 warn");
61 
63  std::cout << " root prio = " << root.getPriority() << std::endl;
64  std::cout << " sub1 prio = " << sub1.getPriority() << std::endl;
65  std::cout << " sub2 prio = " << sub2.getPriority() << std::endl;
66 
67  std::cout << "priority info" << std::endl;
68  root.error("root error");
69  root.warn("root warn");
70  sub1.error("sub1 error");
71  sub1.warn("sub1 warn");
72  sub2.error("sub2 error");
73  sub2.warn("sub2 warn");
74 
75  sub2.warnStream() << "streamed warn";
76 
77  sub2 << log4cpp::Priority::WARN << "warn2" << " warn3"
78  << log4cpp::eol << " warn4";
79 
80  {
81  for(int i = 0; i < 10000; i++) {
82  char ndc2[20];
83  sprintf(ndc2, "i=%d", i);
84  log4cpp::NDC::push(ndc2);
85  sub1.info("%s%d", "i = ", i);
86  if ((i % 10) == 0) {
87  sub1.log(log4cpp::Priority::NOTICE, "reopen log");
89  sub1.info("log reopened");
90  } else {
91  sub1.warn("could not reopen log");
92  }
93  }
94 #ifndef WIN32
95  sleep(1);
96 #endif
98  }
99  }
100 
101  return 0;
102 }
CategoryStream warnStream()
Definition: Category.hh:420
void warn(const char *stringFormat,...)
Definition: Category.cpp:333
LOG4CPP_EXPORT CategoryStream & eol(CategoryStream &os)
virtual void log(Priority::Value priority, const char *stringFormat,...)
Definition: Category.cpp:267
static Category & getInstance(const std::string &name)
Definition: Category.cpp:35
static bool reopenAll()
Definition: Appender.cpp:46
static Category & getRoot()
Definition: Category.cpp:23
virtual Priority::Value getPriority() const
Definition: Category.cpp:67
virtual void setPriority(Priority::Value priority)
Definition: Category.cpp:71
static void push(const std::string &message)
Definition: NDC.cpp:61
int main(int argc, char **argv)
Definition: testmain.cpp:19
void info(const char *stringFormat,...)
Definition: Category.cpp:305
virtual void addAppender(Appender *appender)
Definition: Category.cpp:94
void error(const char *stringFormat,...)
Definition: Category.cpp:347
virtual void setLayout(Layout *layout)=0
static std::string pop()
Definition: NDC.cpp:57


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