wibble
0.1.28
|
00001 /* -*- C++ -*- (c) 2007 Petr Rockai <me@mornfall.net> 00002 (c) 2007 Enrico Zini <enrico@enricozini.org> */ 00003 00004 #include <wibble/test.h> 00005 #include <wibble/log/stream.h> 00006 #include <wibble/log/null.h> 00007 #include <wibble/log/file.h> 00008 #include <wibble/log/ostream.h> 00009 #include <vector> 00010 #include <iostream> 00011 #include <fstream> 00012 00017 namespace { 00018 00019 using namespace std; 00020 using namespace wibble; 00021 using namespace wibble::log; 00022 00023 struct TestLog { 00024 00025 // Test sender for log::Streambuf 00026 struct Sender1 : public log::Sender 00027 { 00028 // Here go the log messages 00029 std::vector< std::pair<Level, std::string> > log; 00030 00031 virtual ~Sender1() {} 00032 00033 // Interface for the streambuf to send messages 00034 virtual void send(Level level, const std::string& msg) 00035 { 00036 log.push_back(make_pair(level, msg)); 00037 } 00038 00039 // Dump all the logged messages to cerr 00040 void dump() 00041 { 00042 for (size_t i = 0; i < log.size(); ++i) 00043 std::cerr << log[i].first << " -> " << log[i].second << " <-" << std::endl; 00044 } 00045 }; 00046 00047 Test streambuf() { 00048 // Instantiate a Streambuf and write something in it 00049 00050 Sender1 s; 00051 { 00052 log::Streambuf ls(&s); 00053 ostream o(&ls); 00054 00055 // Send a normal log message 00056 o << "test" << endl; 00057 assert_eq(s.log.size(), 1u); 00058 assert_eq(s.log[0].first, log::INFO); 00059 assert_eq(s.log[0].second, "test"); 00060 00061 // Send a log message with a different priority 00062 //o << log::lev(log::WARN) << "test" << endl; 00063 o << log::WARN << "test" << endl; 00064 assert_eq(s.log.size(), 2u); 00065 assert_eq(s.log[1].first, log::WARN); 00066 assert_eq(s.log[1].second, "test"); 00067 00068 // Ensure that log messages are only sent after a newline 00069 o << "should eventually appear"; 00070 assert_eq(s.log.size(), 2u); 00071 } 00072 // Or at streambuf destruction 00073 assert_eq(s.log.size(), 3u); 00074 assert_eq(s.log[2].first, log::INFO); 00075 assert_eq(s.log[2].second, "should eventually appear"); 00076 00077 //s.dump(); 00078 } 00079 00080 // Test the NullSender 00081 Test nullSender() { 00082 // Null does nothing, so we cannot test the results. 00083 00084 log::NullSender ns; 00085 ns.send(log::INFO, "test"); 00086 00087 log::Streambuf null(&ns); 00088 ostream o(&null); 00089 00090 // Send a normal log message 00091 o << "test" << endl; 00092 00093 // Send a log message with a different priority 00094 //o << log::lev(log::WARN) << "test" << endl; 00095 o << log::WARN << "test" << endl; 00096 00097 // Ensure that log messages are only sent after a newline 00098 o << "should eventually appear"; 00099 } 00100 00101 // Test the FileSender 00102 Test fileSender() { 00103 00104 // We send to /dev/null, so we cannot test the results. 00105 00106 log::FileSender ns("/dev/null"); 00107 ns.send(log::INFO, "test"); 00108 00109 log::Streambuf file(&ns); 00110 ostream o(&file); 00111 00112 // Send a normal log message 00113 o << "test" << endl; 00114 00115 // Send a log message with a different priority 00116 //o << log::lev(log::WARN) << "test" << endl; 00117 o << log::WARN << "test" << endl; 00118 00119 // Ensure that log messages are only sent after a newline 00120 o << "should eventually appear"; 00121 } 00122 00123 // Test the OstreamSender 00124 Test ostreamSender() { 00125 // We send to /dev/null, so we cannot test the results. 00126 00127 std::ofstream null("/dev/null", std::ios::out); 00128 assert(!null.fail()); 00129 00130 log::OstreamSender sender(null); 00131 sender.send(log::INFO, "test"); 00132 00133 log::Streambuf log(&sender); 00134 ostream o(&log); 00135 00136 // Send a normal log message 00137 o << "test" << endl; 00138 00139 // Send a log message with a different priority 00140 //o << log::lev(log::WARN) << "test" << endl; 00141 o << log::WARN << "test" << endl; 00142 00143 // Ensure that log messages are only sent after a newline 00144 o << "should eventually appear"; 00145 } 00146 00147 }; 00148 00149 } 00150 00151 // vim:set ts=4 sw=4: