LoggerTests.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
12 /*
13  * $Log$
14  *
15  */
16 
17 #ifndef Logger_cpp
18 #define Logger_cpp
19 
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>
25 
26 #include <string>
27 #include <map>
28 #include <iostream>
29 #include <cstdlib>
30 
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>
37 
38 #include <coil/Mutex.h>
39 #include <coil/Guard.h>
40 
41 namespace coil
42 {
43 };
44 
49 namespace Logger
50 {
52  : public coil::Task
53 {
54 public:
55  LogCreator(const char* name, std::basic_streambuf<char>* streambuf)
56  : m_name(name),
57  m_out(streambuf)
58  // logger(name)
59  {
60  }
61 
62  virtual int svc()
63  {
65  for (int i(0); i < 100; ++i)
66  {
67  double r(rand() / (double)RAND_MAX * 100.0);
68  tm.tick();
69  {
70  coil::Guard<coil::Mutex> guard(m_mutex);
71  m_out << coil::sprintf("%s %03d %6.2f", m_name.c_str(), i, r) << std::endl;
72  }
73  tm.tack();
74  coil::usleep((int)r);
75  }
76 
77 #if 0
78  double max, min, mean, stddev;
79  tm.getStatistics(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);
85 #endif
86  return 0;
87  }
88 
89 private:
90  std::string m_name;
91  std::basic_ostream<char> m_out;
92 // coil::Mutex m_mutex;
93 public:
95 };
96 coil::Mutex LogCreator::m_mutex;
97 
99  {
104  PARANOID_LEVEL
105  };
106 
107 class LogOut
108  : public coil::LogStream
109 {
110 
111 
112 public:
114  : ::coil::LogStream(streambuf, /* "test", */
115  SILENT_LEVEL, PARANOID_LEVEL, SILENT_LEVEL)
116  {
117  }
118  virtual ~LogOut(){}
119 
120  virtual std::string& prefix(std::string& prefix, int level)
121  {
122  const char* level_str[] =
123  {
124  "SILENT ",
125  "INFO ",
126  "ERROR ",
127  "TRACE ",
128  "PARANOID "
129  };
130  prefix = getFmtDate() + level_str[level];
131  return prefix;
132  }
133 
134  void setDateFormat(const char* format)
135  {
136  m_dateFormat = std::string(format);
137  }
138  void header(int level)
139  {
140  const char* level_str[] = {
141  " SILENT :",
142  " INFO :",
143  " ERROR :",
144  " TRACE :",
145  " PARANOID:"
146  };
147  *this << getDate() + level_str[level] + m_name + ": ";
148  }
149  std::string getDate(void)
150  {
151  const int maxsize = 256;
152  char buf[maxsize];
153 
154  time_t timer;
155  struct tm* date;
156 
157  timer = time(NULL);
158  date = localtime(&timer);
159  strftime(buf, sizeof(buf), m_dateFormat.c_str(), date);
160 
161  return std::string(buf);
162  }
163  void setName(const char* name)
164  {
165  m_name = name;
166  }
167 protected:
168  std::string getFmtDate()
169  {
170  const int maxsize = 256;
171  char buf[maxsize];
172 
173  time_t timer;
174  struct tm* date;
175 
176  timer = time(NULL);
177  date = localtime(&timer);
178  strftime(buf, maxsize, "%b %d %H:%M:%S ", date);
179 
180  return std::string(buf);
181  }
182  std::string m_name;
183  std::string m_dateFormat;
184 };
185 
186 class LogOut2
187  : public coil::LogStream
188 {
189 
190 
191 public:
193  : ::coil::LogStream(streambuf, /* "test", */
194  SILENT_LEVEL, PARANOID_LEVEL, SILENT_LEVEL)
195  {
196  }
197  virtual ~LogOut2(){}
198 
199 
200 protected:
201 };
202 
203 
204 
205 #define RTC_LOG(LV, fmt) \
206  if (m_out.isValid(LV)) \
207  { \
208  m_out.lock(); \
209  m_out.level(LV) << ::coil::sprintf fmt << std::endl; \
210  m_out.unlock(); \
211  }
212 
213 #define RTC_TRACE(fmt) RTC_LOG(TRACE_LEVEL, fmt)
214 
215 
217  : public coil::Task
218 {
219 public:
220  LogOutCreator(const char* name, coil::LogStreamBuffer *streambuf)
221  : m_name(name),
222  m_out(streambuf)
223  // logger(name)
224  {
225  m_out.setName(m_name.c_str());
226  m_out.setDateFormat("%b %d %H:%M:%S");
227  m_out.setLevel(PARANOID_LEVEL);
228  m_out.enableLock();
229  }
231  {
232  m_out.disableLock();
233  }
234 
235  virtual int svc()
236  {
238  for (int i(0); i < 200; ++i)
239  {
240  double r(rand() / (double)RAND_MAX * 100.0);
241  tm.tick();
242  std::string str = coil::sprintf("svc() %03d %6.2f", i, r);
243  RTC_TRACE((str.c_str()));
244  tm.tack();
245  coil::usleep((int)r);
246  }
247 
248 #if 0
249  double max, min, mean, stddev;
250  tm.getStatistics(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);
256 #endif
257  return 0;
258  }
259 
260 private:
261  std::string m_name;
263  int dummy0(int ic)
264  {
265  RTC_TRACE(("IN dummy0"));
266  RTC_TRACE((" ic=%d",ic));
267  RTC_TRACE(("OUT dummy0"));
268  return ic;
269  }
270 
271 };
272 
273 
279  : public CppUnit::TestFixture
280  {
281  CPPUNIT_TEST_SUITE(LoggerTests);
282 
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);
289 
290  CPPUNIT_TEST_SUITE_END();
291 
292  private:
293 
294  public:
295 
300  {
301  }
302 
307  {
308  }
309 
313  virtual void setUp()
314  {
315  }
316 
320  virtual void tearDown()
321  {
322 
323 
324  }
325 
326  /* test case */
328  {
330 
331  std::stringstream s0;
332  std::stringstream s1;
333  std::stringstream s2;
334  std::filebuf f;
335  f.open("log.log", std::ios::out);
336 
337  logger.addStream(s0.rdbuf());
338  logger.addStream(s1.rdbuf());
339  logger.addStream(s2.rdbuf());
340  logger.addStream(&f);
341 
342  LogCreator l0("log0", &logger);
343  LogCreator l1("log1", &logger);
344  LogCreator l2("log2", &logger);
345  LogCreator l3("log3", &logger);
346 
347  l0.activate();
348  l1.activate();
349  l2.activate();
350  l3.activate();
351 
352  l0.wait();
353  l1.wait();
354  l2.wait();
355  l3.wait();
356 
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;
363  f0.close();
364  f1.close();
365  f2.close();
366  CPPUNIT_ASSERT(s0.str() == s1.str());
367  CPPUNIT_ASSERT(s1.str() == s2.str());
368 
369 
370  std::string s;
371  getline(s0, s);
372  size_t len(s.size());
373 
374  while (getline(s0, s))
375  {
376  CPPUNIT_ASSERT(len == s.size());
377  }
378  }
379 
381  {
383  std::stringstream s0;
384 // logger.addStream(std::cout.rdbuf());
385  logger.addStream(s0.rdbuf());
386 
387  std::basic_ostream<char> out(&logger);
388  std::string str("::");
389  int ic(2);
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());
395 
396  }
397 
399  {
400  coil::LogStreamBuffer logbuf;
401  std::stringstream s0;
402  logbuf.addStream(s0.rdbuf());
403 // logbuf.addStream(std::cout.rdbuf());
404 
405  LogOut log(&logbuf);
406  log.setLevel(PARANOID_LEVEL);
407 // std::cout << std::endl;
408  log.level(SILENT_LEVEL) << coil::sprintf("This is silent message.") << std::endl;
409  log.level(INFO_LEVEL) << coil::sprintf("This is info message.") << std::endl;
410  log.level(ERROR_LEVEL) << coil::sprintf("This is error message.") << std::endl;
411  log.level(PARANOID_LEVEL) << coil::sprintf("This is paranoid message.") << std::endl;
412 
413 // std::cout << std::endl;
414  log.setLevel(INFO_LEVEL);
415  log.level(SILENT_LEVEL) << coil::sprintf("This is silent message.") << std::endl;
416  log.level(INFO_LEVEL) << coil::sprintf("This is info message.") << std::endl;
417  log.level(ERROR_LEVEL) << coil::sprintf("This is error message.") << std::endl;
418  log.level(PARANOID_LEVEL) << coil::sprintf("This is paranoid message.") << std::endl;
419 
420 // std::cout << std::endl;
421  log.setLevel(SILENT_LEVEL);
422  log.level(SILENT_LEVEL) << coil::sprintf("This is silent message.") << std::endl;
423  log.level(INFO_LEVEL) << coil::sprintf("This is info message.") << std::endl;
424  log.level(ERROR_LEVEL) << coil::sprintf("This is error message.") << std::endl;
425  log.level(PARANOID_LEVEL) << coil::sprintf("This is paranoid message.") << std::endl;
426 
427  std::ofstream f0("test_log_stream.log");
428  f0 << s0.str() << std::endl;
429  f0.close();
430 
431 
432  }
433 
435  {
437 
438  std::stringstream s0;
439  std::stringstream s1;
440  std::stringstream s2;
441  std::filebuf f;
442  f.open("log.log", std::ios::out);
443 
444  logger.addStream(s0.rdbuf());
445  logger.addStream(s1.rdbuf());
446  logger.addStream(s2.rdbuf());
447  logger.addStream(&f);
448 
449  LogOutCreator l0("test_log_stream_log0", &logger);
450  LogOutCreator l1("test_log_stream_log1", &logger);
451  LogOutCreator l2("test_log_stream_log2", &logger);
452  LogOutCreator l3("test_log_stream_log3", &logger);
453 
454  l0.activate();
455  l1.activate();
456  l2.activate();
457  l3.activate();
458 
459  l0.wait();
460  l1.wait();
461  l2.wait();
462  l3.wait();
463 
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;
470  f0.close();
471  f1.close();
472  f2.close();
473  CPPUNIT_ASSERT(s0.str() == s1.str());
474  CPPUNIT_ASSERT(s1.str() == s2.str());
475 
476 
477  std::string s;
478  getline(s0, s);
479  size_t len(s.size());
480 
481  while (getline(s0, s))
482  {
483  CPPUNIT_ASSERT(len == s.size());
484  }
485  }
486 
488  {
489  //
490  //
491  //
492  coil::LogStreamBuffer logbuf;
493 
494  LogOut log(&logbuf);
495  std::filebuf* of = new std::filebuf();
496  of->open("test_log_stream3-1.log", std::ios::out | std::ios::app);
497  logbuf.addStream(of);
498 
499  std::filebuf f;
500  f.open("test_log_stream3-2.log", std::ios::out | std::ios::app);
501  logbuf.addStream(&f);
502 
503 
504  log.setLevel(INFO_LEVEL);
505  log.level(SILENT_LEVEL) << coil::sprintf("This is silent message.") << std::endl;
506  log.level(INFO_LEVEL) << coil::sprintf("This is info message.") << std::endl;
507  log.level(ERROR_LEVEL) << coil::sprintf("This is error message.") << std::endl;
508  log.level(PARANOID_LEVEL) << coil::sprintf("This is paranoid message.") << std::endl;
509 
510  CPPUNIT_ASSERT_EQUAL(true, logbuf.removeStream(&f));
511  std::vector< ::coil::LogStream::streambuf_type* > vt;
512  vt = logbuf.getBuffers();
513  CPPUNIT_ASSERT_EQUAL(1, (int)vt.size());
514  for (int i(0), len(vt.size()); i < len; ++i)
515  {
516  try
517  {
518  CPPUNIT_ASSERT(of==vt[i]);
519  delete vt[i];
520  }
521  catch(...)
522  {
523  CPPUNIT_FAIL( "Exception thrown." );
524  }
525 
526  }
527 
528  }
530  {
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";
536 
537  coil::Properties prop(defaults);
538 
539  coil::LogStreamBuffer logbuf;
540  std::stringstream s0;
541  logbuf.addStream(s0.rdbuf());
542 // logbuf.addStream(std::cout.rdbuf());
543 
544  LogOut2 log(&logbuf);
545  log.setLevel(PARANOID_LEVEL);
546 
547  log.level(PARANOID_LEVEL) << prop;
548 
549  std::ostringstream os,osm;
550  os << prop;
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());
556 
557  }
558 
559  };
560 }; // namespace Logger
561 
562 /*
563  * Register test suite
564  */
566 
567 #ifdef LOCAL_MAIN
568 int main(int argc, char* argv[])
569 {
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);
575  bool retcode = runner.run();
576  return !retcode;
577 }
578 #endif // MAIN
579 #endif // Logger_cpp
LoggerTests()
Constructor.
virtual ~LogOut2()
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.
Mutex class.
virtual void tearDown()
Test finalization.
virtual void setUp()
Test initialization.
log_stream< char > LogStream
std::string getFmtDate()
std::string getDate(void)
std::string m_name
Definition: LoggerTests.cpp:90
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.
Definition: TimeMeasure.cpp:72
void addStream(streambuf_type *stream, bool cleanup=false)
Destructor.
void header(int level)
Message header appender function.
void setDateFormat(const char *format)
#define RTC_TRACE(fmt)
std::string m_dateFormat
log_stream template class
virtual ~LogOut()
bool setLevel(int level)
Set the log level.
std::basic_streambuf< char_type, traits_type > streambuf_type
static coil::Mutex m_mutex
Definition: LoggerTests.cpp:94
void test_log_stream_properties()
prop
Organization::get_organization_property ();.
ostream_type & level(int level)
Acquire log stream.
Class represents a set of properties.
Definition: Properties.h:101
std::string sprintf(char const *__restrict fmt,...)
Convert it into a format given with an argumen.
Definition: stringutil.cpp:593
Task class.
std::basic_ostream< char > m_out
Definition: LoggerTests.cpp:91
LogCreator(const char *name, std::basic_streambuf< char > *streambuf)
Definition: LoggerTests.cpp:55
::OutPortBase::Logger logger
TimeMeasure class.
Definition: TimeMeasure.h:49
void setName(const char *name)
CPPUNIT_TEST_SUITE_REGISTRATION(Logger::LoggerTests)
void tick()
Begin time measurement for time statistics.
Definition: TimeMeasure.cpp:60
std::string m_name
bool removeStream(streambuf_type *stream)
Destructor.
virtual int svc()
Execute thread.
Definition: LoggerTests.cpp:62
int usleep(useconds_t usec)
Stop a processing at specified micro second time.
Definition: ace/coil/Time.h:51
virtual void activate()
Create a thread.
Common Object Interface Layer.


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Jun 6 2019 19:25:58