File: testmain.cpp

package info (click to toggle)
log4shib 1.0.9-3~bpo7%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports-sloppy
  • size: 4,244 kB
  • sloc: cpp: 4,767; sh: 4,210; ansic: 818; makefile: 268
file content (102 lines) | stat: -rw-r--r-- 2,834 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <stdio.h>
#include "log4shib/Portability.hh"
#ifdef LOG4SHIB_HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <iostream>
#include "log4shib/Category.hh"
#include "log4shib/Appender.hh"
#include "log4shib/FileAppender.hh"
#include "log4shib/OstreamAppender.hh"
#ifdef LOG4SHIB_HAVE_SYSLOG
#include "log4shib/SyslogAppender.hh"
#endif
#include "log4shib/Layout.hh"
#include "log4shib/BasicLayout.hh"
#include "log4shib/Priority.hh"
#include "log4shib/NDC.hh"

int main(int argc, char** argv) {
	log4shib::Appender* appender;
#ifdef LOG4SHIB_HAVE_SYSLOG
	log4shib::SyslogAppender* syslogAppender;

	syslogAppender = new log4shib::SyslogAppender("syslog", "log4shib");
#else
	log4shib::Appender* syslogAppender;

	syslogAppender = new log4shib::OstreamAppender("syslogdummy", &std::cout);
#endif

	if (argc < 2) {
		appender = new log4shib::OstreamAppender("default", &std::cout);
	} else {
		appender = new log4shib::FileAppender("default", argv[1]);
	}

	syslogAppender->setLayout(new log4shib::BasicLayout());
	appender->setLayout(new log4shib::BasicLayout());

	log4shib::Category& root = log4shib::Category::getRoot();
	root.addAppender(syslogAppender);
	root.setPriority(log4shib::Priority::ERROR);

	log4shib::Category& sub1 = log4shib::Category::getInstance(std::string("sub1"));
	sub1.addAppender(appender);

	log4shib::Category& sub2 = log4shib::Category::getInstance(std::string("sub1.sub2"));

	log4shib::NDC::push(std::string("ndc1"));

	std::cout << " root prio = " << root.getPriority() << std::endl;
	std::cout << " sub1 prio = " << sub1.getPriority() << std::endl;
	std::cout << " sub2 prio = " << sub2.getPriority() << std::endl;

	root.error("root error");
	root.warn("root warn");
	sub1.error("sub1 error");
	sub1.warn("sub1 warn");
	sub2.error("sub2 error");
	sub2.warn("sub2 warn");

	sub1.setPriority(log4shib::Priority::INFO);
	std::cout << " root prio = " << root.getPriority() << std::endl;
	std::cout << " sub1 prio = " << sub1.getPriority() << std::endl;
	std::cout << " sub2 prio = " << sub2.getPriority() << std::endl;

	std::cout << "priority info" << std::endl;
	root.error("root error");
	root.warn("root warn");
	sub1.error("sub1 error");
	sub1.warn("sub1 warn");
	sub2.error("sub2 error");
	sub2.warn("sub2 warn");

	sub2.warnStream() << "streamed warn";

	sub2 << log4shib::Priority::WARN << "warn2" << " warn3" <<
		log4shib::eol << " warn4";

	{
		for(int i = 0; i < 10000; i++) {
			char ndc2[20];
			sprintf(ndc2, "i=%d", i);
			log4shib::NDC::push(ndc2);
			sub1.info("%s%d", "i = ", i);
			if ((i % 10) == 0) {
				sub1.log(log4shib::Priority::NOTICE, "reopen log");
				if (log4shib::Appender::reopenAll()) {
					sub1.info("log reopened");
				} else {
					sub1.warn("could not reopen log");
				}
			}
#ifndef WIN32
			sleep(1);
#endif
			log4shib::NDC::pop();
		}
	}

	return 0;
}