wibble  1.1
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: