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 #ifdef __QNX__
42 using std::rand;
43 #endif
44 
45 namespace coil
46 {
47 };
48 
53 namespace Logger
54 {
56  : public coil::Task
57 {
58 public:
59  LogCreator(const char* name, std::basic_streambuf<char>* streambuf)
60  : m_name(name),
61  m_out(streambuf)
62  // logger(name)
63  {
64  }
65 
66  virtual int svc()
67  {
69  for (int i(0); i < 100; ++i)
70  {
71  double r(rand() / (double)RAND_MAX * 100.0);
72  tm.tick();
73  {
74  coil::Guard<coil::Mutex> guard(m_mutex);
75  m_out << coil::sprintf("%s %03d %6.2f", m_name.c_str(), i, r) << std::endl;
76  }
77  tm.tack();
78  coil::usleep((int)r);
79  }
80 
81 #if 0
82  double max, min, mean, stddev;
83  tm.getStatistics(max, min, mean, stddev);
84  std::cout << m_name << std::endl;
85  printf("max : %04.2f [us]\n", max * 1000000);
86  printf("min : %04.2f [us]\n", min * 1000000);
87  printf("mean : %04.2f [us]\n", mean * 1000000);
88  printf("stddev: %04.2f [us]\n", stddev * 1000000);
89 #endif
90  return 0;
91  }
92 
93 private:
94  std::string m_name;
95  std::basic_ostream<char> m_out;
96 // coil::Mutex m_mutex;
97 public:
99 };
100 coil::Mutex LogCreator::m_mutex;
101 
103  {
108  PARANOID_LEVEL
109  };
110 
111 class LogOut
112  : public coil::LogStream
113 {
114 
115 
116 public:
118  : ::coil::LogStream(streambuf, /* "test", */
119  SILENT_LEVEL, PARANOID_LEVEL, SILENT_LEVEL)
120  {
121  }
122  virtual ~LogOut(){}
123 
124  virtual std::string& prefix(std::string& prefix, int level)
125  {
126  const char* level_str[] =
127  {
128  "SILENT ",
129  "INFO ",
130  "ERROR ",
131  "TRACE ",
132  "PARANOID "
133  };
134  prefix = getFmtDate() + level_str[level];
135  return prefix;
136  }
137 
138  void setDateFormat(const char* format)
139  {
140  m_dateFormat = std::string(format);
141  }
142  void header(int level)
143  {
144  const char* level_str[] = {
145  " SILENT :",
146  " INFO :",
147  " ERROR :",
148  " TRACE :",
149  " PARANOID:"
150  };
151  *this << getDate() + level_str[level] + m_name + ": ";
152  }
153  std::string getDate(void)
154  {
155  const int maxsize = 256;
156  char buf[maxsize];
157 
158  time_t timer;
159  struct tm* date;
160 
161  timer = time(NULL);
162  date = localtime(&timer);
163  strftime(buf, sizeof(buf), m_dateFormat.c_str(), date);
164 
165  return std::string(buf);
166  }
167  void setName(const char* name)
168  {
169  m_name = name;
170  }
171 protected:
172  std::string getFmtDate()
173  {
174  const int maxsize = 256;
175  char buf[maxsize];
176 
177  time_t timer;
178  struct tm* date;
179 
180  timer = time(NULL);
181  date = localtime(&timer);
182  strftime(buf, maxsize, "%b %d %H:%M:%S ", date);
183 
184  return std::string(buf);
185  }
186  std::string m_name;
187  std::string m_dateFormat;
188 };
189 
190 class LogOut2
191  : public coil::LogStream
192 {
193 
194 
195 public:
197  : ::coil::LogStream(streambuf, /* "test", */
198  SILENT_LEVEL, PARANOID_LEVEL, SILENT_LEVEL)
199  {
200  }
201  virtual ~LogOut2(){}
202 
203 
204 protected:
205 };
206 
207 
208 
209 #define RTC_LOG(LV, fmt) \
210  if (m_out.isValid(LV)) \
211  { \
212  m_out.lock(); \
213  m_out.level(LV) << ::coil::sprintf fmt << std::endl; \
214  m_out.unlock(); \
215  }
216 
217 #define RTC_TRACE(fmt) RTC_LOG(TRACE_LEVEL, fmt)
218 
219 
221  : public coil::Task
222 {
223 public:
224  LogOutCreator(const char* name, coil::LogStreamBuffer *streambuf)
225  : m_name(name),
226  m_out(streambuf)
227  // logger(name)
228  {
229  m_out.setName(m_name.c_str());
230  m_out.setDateFormat("%b %d %H:%M:%S");
231  m_out.setLevel(PARANOID_LEVEL);
232  m_out.enableLock();
233  }
235  {
236  m_out.disableLock();
237  }
238 
239  virtual int svc()
240  {
242  for (int i(0); i < 200; ++i)
243  {
244  double r(rand() / (double)RAND_MAX * 100.0);
245  tm.tick();
246  std::string str = coil::sprintf("svc() %03d %6.2f", i, r);
247  RTC_TRACE((str.c_str()));
248  tm.tack();
249  coil::usleep((int)r);
250  }
251 
252 #if 0
253  double max, min, mean, stddev;
254  tm.getStatistics(max, min, mean, stddev);
255  std::cout << m_name << std::endl;
256  printf("max : %04.2f [us]\n", max * 1000000);
257  printf("min : %04.2f [us]\n", min * 1000000);
258  printf("mean : %04.2f [us]\n", mean * 1000000);
259  printf("stddev: %04.2f [us]\n", stddev * 1000000);
260 #endif
261  return 0;
262  }
263 
264 private:
265  std::string m_name;
267  int dummy0(int ic)
268  {
269  RTC_TRACE(("IN dummy0"));
270  RTC_TRACE((" ic=%d",ic));
271  RTC_TRACE(("OUT dummy0"));
272  return ic;
273  }
274 
275 };
276 
277 
283  : public CppUnit::TestFixture
284  {
285  CPPUNIT_TEST_SUITE(LoggerTests);
286 
287  CPPUNIT_TEST(test_log_streambuf);
288  CPPUNIT_TEST(test_log_streambuf2);
289  CPPUNIT_TEST(test_log_stream);
290  CPPUNIT_TEST(test_log_stream2);
291  CPPUNIT_TEST(test_log_stream_properties);
292  CPPUNIT_TEST(test_log_stream3);
293 
294  CPPUNIT_TEST_SUITE_END();
295 
296  private:
297 
298  public:
299 
304  {
305  }
306 
311  {
312  }
313 
317  virtual void setUp()
318  {
319  }
320 
324  virtual void tearDown()
325  {
326 
327 
328  }
329 
330  /* test case */
332  {
334 
335  std::stringstream s0;
336  std::stringstream s1;
337  std::stringstream s2;
338  std::filebuf f;
339  f.open("log.log", std::ios::out);
340 
341  logger.addStream(s0.rdbuf());
342  logger.addStream(s1.rdbuf());
343  logger.addStream(s2.rdbuf());
344  logger.addStream(&f);
345 
346  LogCreator l0("log0", &logger);
347  LogCreator l1("log1", &logger);
348  LogCreator l2("log2", &logger);
349  LogCreator l3("log3", &logger);
350 
351  l0.activate();
352  l1.activate();
353  l2.activate();
354  l3.activate();
355 
356  l0.wait();
357  l1.wait();
358  l2.wait();
359  l3.wait();
360 
361  std::ofstream f0("log0.log");
362  std::ofstream f1("log1.log");
363  std::ofstream f2("log2.log");
364  f0 << s0.str() << std::endl;
365  f1 << s1.str() << std::endl;
366  f2 << s2.str() << std::endl;
367  f0.close();
368  f1.close();
369  f2.close();
370  CPPUNIT_ASSERT(s0.str() == s1.str());
371  CPPUNIT_ASSERT(s1.str() == s2.str());
372 
373 
374  std::string s;
375  getline(s0, s);
376  size_t len(s.size());
377 
378  while (getline(s0, s))
379  {
380  CPPUNIT_ASSERT(len == s.size());
381  }
382  }
383 
385  {
387  std::stringstream s0;
388 // logger.addStream(std::cout.rdbuf());
389  logger.addStream(s0.rdbuf());
390 
391  std::basic_ostream<char> out(&logger);
392  std::string str("::");
393  int ic(2);
394  out <<"Logger"<<str<<"test_log_streambuf"<<ic<<std::endl;
395  std::ostringstream os,osm;
396  os <<"Logger"<<str<<"test_log_streambuf"<<ic<<std::endl;
397  osm <<"s0.str():"<<s0.str()<<" os.str():"<<"Logger"<<str<<"test_log_streambuf"<<ic<<std::endl;
398  CPPUNIT_ASSERT_MESSAGE(osm.str(),s0.str() == os.str());
399 
400  }
401 
403  {
404  coil::LogStreamBuffer logbuf;
405  std::stringstream s0;
406  logbuf.addStream(s0.rdbuf());
407 // logbuf.addStream(std::cout.rdbuf());
408 
409  LogOut log(&logbuf);
410  log.setLevel(PARANOID_LEVEL);
411 // std::cout << std::endl;
412  log.level(SILENT_LEVEL) << coil::sprintf("This is silent message.") << std::endl;
413  log.level(INFO_LEVEL) << coil::sprintf("This is info message.") << std::endl;
414  log.level(ERROR_LEVEL) << coil::sprintf("This is error message.") << std::endl;
415  log.level(PARANOID_LEVEL) << coil::sprintf("This is paranoid message.") << std::endl;
416 
417 // std::cout << std::endl;
418  log.setLevel(INFO_LEVEL);
419  log.level(SILENT_LEVEL) << coil::sprintf("This is silent message.") << std::endl;
420  log.level(INFO_LEVEL) << coil::sprintf("This is info message.") << std::endl;
421  log.level(ERROR_LEVEL) << coil::sprintf("This is error message.") << std::endl;
422  log.level(PARANOID_LEVEL) << coil::sprintf("This is paranoid message.") << std::endl;
423 
424 // std::cout << std::endl;
425  log.setLevel(SILENT_LEVEL);
426  log.level(SILENT_LEVEL) << coil::sprintf("This is silent message.") << std::endl;
427  log.level(INFO_LEVEL) << coil::sprintf("This is info message.") << std::endl;
428  log.level(ERROR_LEVEL) << coil::sprintf("This is error message.") << std::endl;
429  log.level(PARANOID_LEVEL) << coil::sprintf("This is paranoid message.") << std::endl;
430 
431  std::ofstream f0("test_log_stream.log");
432  f0 << s0.str() << std::endl;
433  f0.close();
434 
435 
436  }
437 
439  {
441 
442  std::stringstream s0;
443  std::stringstream s1;
444  std::stringstream s2;
445  std::filebuf f;
446  f.open("log.log", std::ios::out);
447 
448  logger.addStream(s0.rdbuf());
449  logger.addStream(s1.rdbuf());
450  logger.addStream(s2.rdbuf());
451  logger.addStream(&f);
452 
453  LogOutCreator l0("test_log_stream_log0", &logger);
454  LogOutCreator l1("test_log_stream_log1", &logger);
455  LogOutCreator l2("test_log_stream_log2", &logger);
456  LogOutCreator l3("test_log_stream_log3", &logger);
457 
458  l0.activate();
459  l1.activate();
460  l2.activate();
461  l3.activate();
462 
463  l0.wait();
464  l1.wait();
465  l2.wait();
466  l3.wait();
467 
468  std::ofstream f0("log4.log");
469  std::ofstream f1("log5.log");
470  std::ofstream f2("log6.log");
471  f0 << s0.str() << std::endl;
472  f1 << s1.str() << std::endl;
473  f2 << s2.str() << std::endl;
474  f0.close();
475  f1.close();
476  f2.close();
477  CPPUNIT_ASSERT(s0.str() == s1.str());
478  CPPUNIT_ASSERT(s1.str() == s2.str());
479 
480 
481  std::string s;
482  getline(s0, s);
483  size_t len(s.size());
484 
485  while (getline(s0, s))
486  {
487  CPPUNIT_ASSERT(len == s.size());
488  }
489  }
490 
492  {
493  //
494  //
495  //
496  coil::LogStreamBuffer logbuf;
497 
498  LogOut log(&logbuf);
499  std::filebuf* of = new std::filebuf();
500  of->open("test_log_stream3-1.log", std::ios::out | std::ios::app);
501  logbuf.addStream(of);
502 
503  std::filebuf f;
504  f.open("test_log_stream3-2.log", std::ios::out | std::ios::app);
505  logbuf.addStream(&f);
506 
507 
508  log.setLevel(INFO_LEVEL);
509  log.level(SILENT_LEVEL) << coil::sprintf("This is silent message.") << std::endl;
510  log.level(INFO_LEVEL) << coil::sprintf("This is info message.") << std::endl;
511  log.level(ERROR_LEVEL) << coil::sprintf("This is error message.") << std::endl;
512  log.level(PARANOID_LEVEL) << coil::sprintf("This is paranoid message.") << std::endl;
513 
514  CPPUNIT_ASSERT_EQUAL(true, logbuf.removeStream(&f));
515  std::vector< ::coil::LogStream::streambuf_type* > vt;
516  vt = logbuf.getBuffers();
517  CPPUNIT_ASSERT_EQUAL(1, (int)vt.size());
518  for (int i(0), len(vt.size()); i < len; ++i)
519  {
520  try
521  {
522  CPPUNIT_ASSERT(of==vt[i]);
523  delete vt[i];
524  }
525  catch(...)
526  {
527  CPPUNIT_FAIL( "Exception thrown." );
528  }
529 
530  }
531 
532  }
534  {
535  std::map<std::string, std::string> defaults;
536  defaults["rtc.component.conf.path"] = "C:\\Program\\ Files\\OpenRTM-aist";
537  defaults["rtc.manager.arch"] = "i386";
538  defaults["rtc.manager.nameserver"] = "zonu.a02.aist.go.jp";
539  defaults["rtc.manager.opening_message"] = "Hello World";
540 
541  coil::Properties prop(defaults);
542 
543  coil::LogStreamBuffer logbuf;
544  std::stringstream s0;
545  logbuf.addStream(s0.rdbuf());
546 // logbuf.addStream(std::cout.rdbuf());
547 
548  LogOut2 log(&logbuf);
549  log.setLevel(PARANOID_LEVEL);
550 
551  log.level(PARANOID_LEVEL) << prop;
552 
553  std::ostringstream os,osm;
554  os << prop;
555  osm<<"---s0.str---"<<std::endl;
556  osm<<s0.str()<<std::endl;
557  osm<<"---os.str---"<<std::endl;
558  osm<<os.str()<<std::endl;
559  CPPUNIT_ASSERT_MESSAGE(osm.str(),s0.str() == os.str());
560 
561  }
562 
563  };
564 }; // namespace Logger
565 
566 /*
567  * Register test suite
568  */
570 
571 #ifdef LOCAL_MAIN
572 int main(int argc, char* argv[])
573 {
574  CppUnit::TextUi::TestRunner runner;
575  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
576  CppUnit::Outputter* outputter =
577  new CppUnit::TextOutputter(&runner.result(), std::cout);
578  runner.setOutputter(outputter);
579  bool retcode = runner.run();
580  return !retcode;
581 }
582 #endif // MAIN
583 #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:94
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:98
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:598
Task class.
std::basic_ostream< char > m_out
Definition: LoggerTests.cpp:95
LogCreator(const char *name, std::basic_streambuf< char > *streambuf)
Definition: LoggerTests.cpp:59
::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:66
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 Mon Feb 28 2022 23:00:43