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> 59 LogCreator(
const char* name, std::basic_streambuf<char>* streambuf)
69 for (
int i(0); i < 100; ++i)
71 double r(rand() / (
double)RAND_MAX * 100.0);
75 m_out <<
coil::sprintf(
"%s %03d %6.2f", m_name.c_str(), i, r) << std::endl;
82 double 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);
119 SILENT_LEVEL, PARANOID_LEVEL, SILENT_LEVEL)
124 virtual std::string&
prefix(std::string& prefix,
int level)
126 const char* level_str[] =
134 prefix = getFmtDate() + level_str[level];
140 m_dateFormat = std::string(format);
144 const char* level_str[] = {
151 *
this << getDate() + level_str[level] + m_name +
": ";
155 const int maxsize = 256;
162 date = localtime(&timer);
163 strftime(buf,
sizeof(buf), m_dateFormat.c_str(), date);
165 return std::string(buf);
174 const int maxsize = 256;
181 date = localtime(&timer);
182 strftime(buf, maxsize,
"%b %d %H:%M:%S ", date);
184 return std::string(buf);
198 SILENT_LEVEL, PARANOID_LEVEL, SILENT_LEVEL)
209 #define RTC_LOG(LV, fmt) \ 210 if (m_out.isValid(LV)) \ 213 m_out.level(LV) << ::coil::sprintf fmt << std::endl; \ 217 #define RTC_TRACE(fmt) RTC_LOG(TRACE_LEVEL, fmt) 229 m_out.setName(m_name.c_str());
230 m_out.setDateFormat(
"%b %d %H:%M:%S");
231 m_out.setLevel(PARANOID_LEVEL);
242 for (
int i(0); i < 200; ++i)
244 double r(rand() / (
double)RAND_MAX * 100.0);
253 double 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);
283 :
public CppUnit::TestFixture
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);
294 CPPUNIT_TEST_SUITE_END();
335 std::stringstream s0;
336 std::stringstream s1;
337 std::stringstream
s2;
339 f.open(
"log.log", std::ios::out);
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;
370 CPPUNIT_ASSERT(s0.str() == s1.str());
371 CPPUNIT_ASSERT(s1.str() == s2.str());
376 size_t len(s.size());
378 while (getline(s0, s))
380 CPPUNIT_ASSERT(len == s.size());
387 std::stringstream s0;
391 std::basic_ostream<char> out(&logger);
392 std::string str(
"::");
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());
405 std::stringstream s0;
431 std::ofstream f0(
"test_log_stream.log");
432 f0 << s0.str() << std::endl;
442 std::stringstream s0;
443 std::stringstream s1;
444 std::stringstream
s2;
446 f.open(
"log.log", std::ios::out);
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;
477 CPPUNIT_ASSERT(s0.str() == s1.str());
478 CPPUNIT_ASSERT(s1.str() == s2.str());
483 size_t len(s.size());
485 while (getline(s0, s))
487 CPPUNIT_ASSERT(len == s.size());
499 std::filebuf* of =
new std::filebuf();
500 of->open(
"test_log_stream3-1.log", std::ios::out |
std::ios::app);
504 f.open(
"test_log_stream3-2.log", std::ios::out |
std::ios::app);
515 std::vector< ::coil::LogStream::streambuf_type* > vt;
517 CPPUNIT_ASSERT_EQUAL(1, (
int)vt.size());
518 for (
int i(0), len(vt.size()); i < len; ++i)
522 CPPUNIT_ASSERT(of==vt[i]);
527 CPPUNIT_FAIL(
"Exception thrown." );
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";
544 std::stringstream s0;
553 std::ostringstream os,osm;
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());
572 int main(
int argc,
char* argv[])
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);
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.