Main Page
Related Pages
Namespaces
Classes
Files
File List
File Members
tests
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
"
9
#include "
log4cpp/FileAppender.hh
"
10
#include "
log4cpp/OstreamAppender.hh
"
11
#ifdef LOG4CPP_HAVE_SYSLOG
12
#include "
log4cpp/SyslogAppender.hh
"
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
40
log4cpp::Category
& root =
log4cpp::Category::getRoot
();
41
root.
addAppender
(syslogAppender);
42
root.
setPriority
(
log4cpp::Priority::ERROR
);
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
62
sub1.
setPriority
(
log4cpp::Priority::INFO
);
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"
);
88
if
(
log4cpp::Appender::reopenAll
()) {
89
sub1.
info
(
"log reopened"
);
90
}
else
{
91
sub1.
warn
(
"could not reopen log"
);
92
}
93
}
94
#ifndef WIN32
95
sleep(1);
96
#endif
97
log4cpp::NDC::pop
();
98
}
99
}
100
101
return
0;
102
}
log4cpp::Category::warnStream
CategoryStream warnStream()
Definition:
Category.hh:420
log4cpp::Priority::INFO
Definition:
Priority.hh:77
log4cpp::Category
Definition:
Category.hh:33
Layout.hh
log4cpp::FileAppender
Definition:
FileAppender.hh:20
log4cpp::Category::warn
void warn(const char *stringFormat,...)
Definition:
Category.cpp:333
OstreamAppender.hh
log4cpp::eol
LOG4CPP_EXPORT CategoryStream & eol(CategoryStream &os)
Definition:
CategoryStream.cpp:65
log4cpp::Category::log
virtual void log(Priority::Value priority, const char *stringFormat,...)
Definition:
Category.cpp:267
log4cpp::Category::getInstance
static Category & getInstance(const std::string &name)
Definition:
Category.cpp:35
log4cpp::Appender::reopenAll
static bool reopenAll()
Definition:
Appender.cpp:46
Appender.hh
Portability.hh
SyslogAppender.hh
NDC.hh
log4cpp::BasicLayout
Definition:
BasicLayout.hh:21
log4cpp::SyslogAppender
Definition:
SyslogAppender.hh:25
log4cpp::Category::getRoot
static Category & getRoot()
Definition:
Category.cpp:23
log4cpp::Category::getPriority
virtual Priority::Value getPriority() const
Definition:
Category.cpp:67
log4cpp::Category::setPriority
virtual void setPriority(Priority::Value priority)
Definition:
Category.cpp:71
log4cpp::NDC::push
static void push(const std::string &message)
Definition:
NDC.cpp:61
Category.hh
BasicLayout.hh
log4cpp::Appender
Definition:
Appender.hh:33
main
int main(int argc, char **argv)
Definition:
testmain.cpp:19
log4cpp::Category::info
void info(const char *stringFormat,...)
Definition:
Category.cpp:305
log4cpp::Category::addAppender
virtual void addAppender(Appender *appender)
Definition:
Category.cpp:94
log4cpp::Category::error
void error(const char *stringFormat,...)
Definition:
Category.cpp:347
log4cpp::Priority::ERROR
Definition:
Priority.hh:74
FileAppender.hh
log4cpp::Priority::WARN
Definition:
Priority.hh:75
log4cpp::Appender::setLayout
virtual void setLayout(Layout *layout)=0
Priority.hh
log4cpp::Priority::NOTICE
Definition:
Priority.hh:76
log4cpp::NDC::pop
static std::string pop()
Definition:
NDC.cpp:57
log4cpp::OstreamAppender
Definition:
OstreamAppender.hh:23
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