wibble
1.1
wibble
log.test.h
Go to the documentation of this file.
1
/* -*- C++ -*- (c) 2007 Petr Rockai <me@mornfall.net>
2
(c) 2007 Enrico Zini <enrico@enricozini.org> */
3
4
#include <
wibble/test.h
>
5
#include <
wibble/log/stream.h
>
6
#include <
wibble/log/null.h
>
7
#include <
wibble/log/file.h
>
8
#include <
wibble/log/ostream.h
>
9
#include <vector>
10
#include <iostream>
11
#include <fstream>
12
17
namespace
{
18
19
using namespace
std;
20
using namespace
wibble
;
21
using namespace
wibble::log
;
22
23
struct
TestLog {
24
25
// Test sender for log::Streambuf
26
struct
Sender1 :
public
log::Sender
27
{
28
// Here go the log messages
29
std::vector< std::pair<Level, std::string> > log;
30
31
virtual
~Sender1() {}
32
33
// Interface for the streambuf to send messages
34
virtual
void
send(
Level
level,
const
std::string& msg)
35
{
36
log.push_back(
make_pair
(level, msg));
37
}
38
39
// Dump all the logged messages to cerr
40
void
dump()
41
{
42
for
(
size_t
i = 0; i < log.size(); ++i)
43
std::cerr << log[i].
first
<<
" -> "
<< log[i].
second
<<
" <-"
<< std::endl;
44
}
45
};
46
47
Test
streambuf() {
48
// Instantiate a Streambuf and write something in it
49
50
Sender1 s;
51
{
52
log::Streambuf
ls
(&s);
53
ostream
o
(&
ls
);
54
55
// Send a normal log message
56
o
<<
"test"
<<
endl
;
57
assert_eq
(s.log.size(), 1u);
58
assert_eq
(s.log[0].first,
log::INFO
);
59
assert_eq
(s.log[0].second,
"test"
);
60
61
// Send a log message with a different priority
62
//o << log::lev(log::WARN) << "test" << endl;
63
o
<<
log::WARN
<<
"test"
<<
endl
;
64
assert_eq
(s.log.size(), 2u);
65
assert_eq
(s.log[1].first,
log::WARN
);
66
assert_eq
(s.log[1].second,
"test"
);
67
68
// Ensure that log messages are only sent after a newline
69
o
<<
"should eventually appear"
;
70
assert_eq
(s.log.size(), 2u);
71
}
72
// Or at streambuf destruction
73
assert_eq
(s.log.size(), 3u);
74
assert_eq
(s.log[2].first,
log::INFO
);
75
assert_eq
(s.log[2].second,
"should eventually appear"
);
76
77
//s.dump();
78
}
79
80
// Test the NullSender
81
Test
nullSender
() {
82
// Null does nothing, so we cannot test the results.
83
84
log::NullSender
ns
;
85
ns
.send(
log::INFO
,
"test"
);
86
87
log::Streambuf
null
(&
ns
);
88
ostream
o
(&
null
);
89
90
// Send a normal log message
91
o
<<
"test"
<<
endl
;
92
93
// Send a log message with a different priority
94
//o << log::lev(log::WARN) << "test" << endl;
95
o
<<
log::WARN
<<
"test"
<<
endl
;
96
97
// Ensure that log messages are only sent after a newline
98
o
<<
"should eventually appear"
;
99
}
100
101
// Test the FileSender
102
Test
fileSender
() {
103
#ifdef POSIX
// there's no /dev/null on win32
104
// We send to /dev/null, so we cannot test the results.
105
106
log::FileSender
ns
(
"/dev/null"
);
107
ns
.send(
log::INFO
,
"test"
);
108
109
log::Streambuf
file(&
ns
);
110
ostream
o
(&file);
111
112
// Send a normal log message
113
o
<<
"test"
<<
endl
;
114
115
// Send a log message with a different priority
116
//o << log::lev(log::WARN) << "test" << endl;
117
o
<<
log::WARN
<<
"test"
<<
endl
;
118
119
// Ensure that log messages are only sent after a newline
120
o
<<
"should eventually appear"
;
121
#endif
122
}
123
124
// Test the OstreamSender
125
Test
ostreamSender
() {
126
// We send to /dev/null, so we cannot test the results.
127
128
#ifdef POSIX
// there's no /dev/null on win32
129
std::ofstream
null
(
"/dev/null"
, std::ios::out);
130
assert
(!
null
.fail());
131
132
log::OstreamSender
sender(
null
);
133
sender.send(
log::INFO
,
"test"
);
134
135
log::Streambuf
log(&sender);
136
ostream
o
(&log);
137
138
// Send a normal log message
139
o
<<
"test"
<<
endl
;
140
141
// Send a log message with a different priority
142
//o << log::lev(log::WARN) << "test" << endl;
143
o
<<
log::WARN
<<
"test"
<<
endl
;
144
145
// Ensure that log messages are only sent after a newline
146
o
<<
"should eventually appear"
;
147
#endif
148
}
149
150
};
151
152
}
153
154
// vim:set ts=4 sw=4:
wibble::log::Streambuf
Streambuf class for logging.
Definition
stream.h:35
file.h
wibble::log
Definition
file.cpp:12
wibble::log::Level
Level
Urgency of a log message.
Definition
stream.h:12
wibble::log::WARN
@ WARN
Definition
stream.h:16
wibble::log::INFO
@ INFO
Definition
stream.h:14
wibble
Definition
amorph.h:17
null.h
ostream.h
stream.h
wibble::SanitizeReturn
Definition
amorph.h:30
wibble::log::FileSender
Discard all messages.
Definition
file.h:11
wibble::log::NullSender
Discard all messages.
Definition
null.h:11
wibble::log::OstreamSender
Discard all messages.
Definition
ostream.h:12
wibble::log::Sender
Handle sending a log message.
Definition
stream.h:23
test.h
assert_eq
#define assert_eq(x, y)
Definition
test.h:33
assert
#define assert(x)
Definition
test.h:30
Generated by
1.9.8