20 #include <cppunit/ui/text/TestRunner.h> 21 #include <cppunit/TextOutputter.h> 22 #include <cppunit/extensions/TestFactoryRegistry.h> 23 #include <cppunit/extensions/HelperMacros.h> 24 #include <cppunit/TestAssert.h> 31 #include <coil/Logger.h> 32 #include <coil/Task.h> 33 #include <coil/TimeMeasure.h> 34 #include <coil/stringutil.h> 35 #include <coil/Time.h> 36 #include <coil/Properties.h> 38 #include <coil/Mutex.h> 39 #include <coil/Guard.h> 55 LogCreator(
const char* name, std::basic_streambuf<char>* streambuf)
65 for (
int i(0); i < 100; ++i)
67 double r(rand() / (
double)RAND_MAX * 100.0);
71 m_out <<
coil::sprintf(
"%s %03d %6.2f", m_name.c_str(), i, r) << std::endl;
78 double max, min, mean, stddev;
80 std::cout << m_name << std::endl;
81 printf(
"max : %04.2f [us]\n", max * 1000000);
82 printf(
"min : %04.2f [us]\n", min * 1000000);
83 printf(
"mean : %04.2f [us]\n", mean * 1000000);
84 printf(
"stddev: %04.2f [us]\n", stddev * 1000000);
115 SILENT_LEVEL, PARANOID_LEVEL, SILENT_LEVEL)
120 virtual std::string&
prefix(std::string& prefix,
int level)
122 const char* level_str[] =
130 prefix = getFmtDate() + level_str[level];
136 m_dateFormat = std::string(format);
140 const char* level_str[] = {
147 *
this << getDate() + level_str[level] + m_name +
": ";
151 const int maxsize = 256;
158 date = localtime(&timer);
159 strftime(buf,
sizeof(buf), m_dateFormat.c_str(), date);
161 return std::string(buf);
170 const int maxsize = 256;
177 date = localtime(&timer);
178 strftime(buf, maxsize,
"%b %d %H:%M:%S ", date);
180 return std::string(buf);
194 SILENT_LEVEL, PARANOID_LEVEL, SILENT_LEVEL)
205 #define RTC_LOG(LV, fmt) \ 206 if (m_out.isValid(LV)) \ 209 m_out.level(LV) << ::coil::sprintf fmt << std::endl; \ 213 #define RTC_TRACE(fmt) RTC_LOG(TRACE_LEVEL, fmt) 225 m_out.setName(m_name.c_str());
226 m_out.setDateFormat(
"%b %d %H:%M:%S");
227 m_out.setLevel(PARANOID_LEVEL);
238 for (
int i(0); i < 200; ++i)
240 double r(rand() / (
double)RAND_MAX * 100.0);
249 double max, min, mean, stddev;
251 std::cout << m_name << std::endl;
252 printf(
"max : %04.2f [us]\n", max * 1000000);
253 printf(
"min : %04.2f [us]\n", min * 1000000);
254 printf(
"mean : %04.2f [us]\n", mean * 1000000);
255 printf(
"stddev: %04.2f [us]\n", stddev * 1000000);
279 :
public CppUnit::TestFixture
283 CPPUNIT_TEST(test_log_streambuf);
284 CPPUNIT_TEST(test_log_streambuf2);
285 CPPUNIT_TEST(test_log_stream);
286 CPPUNIT_TEST(test_log_stream2);
287 CPPUNIT_TEST(test_log_stream_properties);
288 CPPUNIT_TEST(test_log_stream3);
290 CPPUNIT_TEST_SUITE_END();
331 std::stringstream s0;
332 std::stringstream s1;
333 std::stringstream
s2;
335 f.open(
"log.log", std::ios::out);
357 std::ofstream f0(
"log0.log");
358 std::ofstream f1(
"log1.log");
359 std::ofstream f2(
"log2.log");
360 f0 << s0.str() << std::endl;
361 f1 << s1.str() << std::endl;
362 f2 << s2.str() << std::endl;
366 CPPUNIT_ASSERT(s0.str() == s1.str());
367 CPPUNIT_ASSERT(s1.str() == s2.str());
372 size_t len(s.size());
374 while (getline(s0, s))
376 CPPUNIT_ASSERT(len == s.size());
383 std::stringstream s0;
387 std::basic_ostream<char> out(&logger);
388 std::string str(
"::");
390 out <<
"Logger"<<str<<
"test_log_streambuf"<<ic<<std::endl;
391 std::ostringstream os,osm;
392 os <<
"Logger"<<str<<
"test_log_streambuf"<<ic<<std::endl;
393 osm <<
"s0.str():"<<s0.str()<<
" os.str():"<<
"Logger"<<str<<
"test_log_streambuf"<<ic<<std::endl;
394 CPPUNIT_ASSERT_MESSAGE(osm.str(),s0.str() == os.str());
401 std::stringstream s0;
427 std::ofstream f0(
"test_log_stream.log");
428 f0 << s0.str() << std::endl;
438 std::stringstream s0;
439 std::stringstream s1;
440 std::stringstream
s2;
442 f.open(
"log.log", std::ios::out);
464 std::ofstream f0(
"log4.log");
465 std::ofstream f1(
"log5.log");
466 std::ofstream f2(
"log6.log");
467 f0 << s0.str() << std::endl;
468 f1 << s1.str() << std::endl;
469 f2 << s2.str() << std::endl;
473 CPPUNIT_ASSERT(s0.str() == s1.str());
474 CPPUNIT_ASSERT(s1.str() == s2.str());
479 size_t len(s.size());
481 while (getline(s0, s))
483 CPPUNIT_ASSERT(len == s.size());
495 std::filebuf* of =
new std::filebuf();
496 of->open(
"test_log_stream3-1.log", std::ios::out |
std::ios::app);
500 f.open(
"test_log_stream3-2.log", std::ios::out |
std::ios::app);
511 std::vector< ::coil::LogStream::streambuf_type* > vt;
513 CPPUNIT_ASSERT_EQUAL(1, (
int)vt.size());
514 for (
int i(0), len(vt.size()); i < len; ++i)
518 CPPUNIT_ASSERT(of==vt[i]);
523 CPPUNIT_FAIL(
"Exception thrown." );
531 std::map<std::string, std::string> defaults;
532 defaults[
"rtc.component.conf.path"] =
"C:\\Program\\ Files\\OpenRTM-aist";
533 defaults[
"rtc.manager.arch"] =
"i386";
534 defaults[
"rtc.manager.nameserver"] =
"zonu.a02.aist.go.jp";
535 defaults[
"rtc.manager.opening_message"] =
"Hello World";
540 std::stringstream s0;
549 std::ostringstream os,osm;
551 osm<<
"---s0.str---"<<std::endl;
552 osm<<s0.str()<<std::endl;
553 osm<<
"---os.str---"<<std::endl;
554 osm<<os.str()<<std::endl;
555 CPPUNIT_ASSERT_MESSAGE(osm.str(),s0.str() == os.str());
568 int main(
int argc,
char* argv[])
570 CppUnit::TextUi::TestRunner runner;
571 runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
572 CppUnit::Outputter* outputter =
573 new CppUnit::TextOutputter(&runner.result(), std::cout);
574 runner.setOutputter(outputter);
LoggerTests()
Constructor.
LogOut2(::coil::LogStream::streambuf_type *streambuf)
int main(int argc, char **argv)
virtual std::string & prefix(std::string &prefix, int level)
bool getStatistics(double &max_interval, double &min_interval, double &mean_interval, double &stddev)
Get total statistics.
LogOut(::coil::LogStream::streambuf_type *streambuf)
log_streambuf template class
virtual int svc()
Execute thread.
virtual void tearDown()
Test finalization.
virtual void setUp()
Test initialization.
log_stream< char > LogStream
std::string getDate(void)
LogOutCreator(const char *name, coil::LogStreamBuffer *streambuf)
std::vector< streambuf_type * > getBuffers()
Get stream buffer list.
~LoggerTests()
Destructor.
virtual int wait(void)
Waiting for the thread terminate.
void tack()
Finish time measurement for time statistics.
void addStream(streambuf_type *stream, bool cleanup=false)
Destructor.
void header(int level)
Message header appender function.
void setDateFormat(const char *format)
log_stream template class
void test_log_streambuf()
bool setLevel(int level)
Set the log level.
std::basic_streambuf< char_type, traits_type > streambuf_type
static coil::Mutex m_mutex
void test_log_stream_properties()
prop
Organization::get_organization_property ();.
ostream_type & level(int level)
Acquire log stream.
Class represents a set of properties.
std::string sprintf(char const *__restrict fmt,...)
Convert it into a format given with an argumen.
std::basic_ostream< char > m_out
LogCreator(const char *name, std::basic_streambuf< char > *streambuf)
::OutPortBase::Logger logger
void setName(const char *name)
CPPUNIT_TEST_SUITE_REGISTRATION(Logger::LoggerTests)
void test_log_streambuf2()
void tick()
Begin time measurement for time statistics.
bool removeStream(streambuf_type *stream)
Destructor.
virtual int svc()
Execute thread.
int usleep(useconds_t usec)
Stop a processing at specified micro second time.
virtual void activate()
Create a thread.
Common Object Interface Layer.