thread_test.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "ros/console.h"
31 
32 #include <gtest/gtest.h>
33 
34 #include <boost/thread.hpp>
35 
36 #include "log4cxx/appenderskeleton.h"
37 #include "log4cxx/spi/loggingevent.h"
38 #ifdef _MSC_VER
39  // Have to be able to encode wchar LogStrings on windows.
40  #include "log4cxx/helpers/transcoder.h"
41 #endif
42 
43 #include <vector>
44 
45 class TestAppender : public log4cxx::AppenderSkeleton
46 {
47 public:
48  struct Info
49  {
50  log4cxx::LevelPtr level_;
51  std::string message_;
52  std::string logger_name_;
53  };
54 
55  typedef std::vector<Info> V_Info;
56 
58 
59 protected:
60  virtual void append(const log4cxx::spi::LoggingEventPtr& event, log4cxx::helpers::Pool&)
61  {
62  Info info;
63  info.level_ = event->getLevel();
64 #ifdef _MSC_VER
65  LOG4CXX_ENCODE_CHAR(msgstr, event->getMessage()); // has to handle LogString with wchar types.
66  info.message_ = msgstr; // msgstr gets instantiated inside the LOG4CXX_ENCODE_CHAR macro
67 
68  LOG4CXX_ENCODE_CHAR(loggerstr, event->getLoggerName()); // has to handle LogString with wchar types.
69  info.logger_name_ = loggerstr; // loggerstr gets instantiated inside the LOG4CXX_ENCODE_CHAR macro
70 #else
71  info.message_ = event->getMessage();
72  info.logger_name_ = event->getLoggerName();
73 #endif
74 
75  info_.push_back( info );
76  }
77 
78  virtual void close()
79  {
80  }
81  virtual bool requiresLayout() const
82  {
83  return false;
84  }
85 };
86 
87 void threadFunc(boost::barrier* b)
88 {
89  b->wait();
90  ROS_INFO("Hello");
91 }
92 
93 // Ensure all threaded calls go out
94 TEST(Rosconsole, threadedCalls)
95 {
96  log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);
97 
98  TestAppender* appender = new TestAppender;
99  logger->addAppender( appender );
100 
101  boost::thread_group tg;
102  boost::barrier b(10);
103  for (uint32_t i = 0; i < 10; ++i)
104  {
105  tg.create_thread(boost::bind(threadFunc, &b));
106  }
107  tg.join_all();
108 
109  ASSERT_EQ(appender->info_.size(), 10ULL);
110 
111  logger->removeAppender(appender);
112 }
113 
114 int main(int argc, char **argv)
115 {
116  testing::InitGoogleTest(&argc, argv);
117  ros::Time::init();
118 
119  return RUN_ALL_TESTS();
120 }
TestAppender::V_Info
std::vector< Info > V_Info
Definition: thread_test.cpp:55
main
int main(int argc, char **argv)
Definition: thread_test.cpp:114
console.h
TestAppender::Info
Definition: thread_test.cpp:48
TestAppender::Info::message_
std::string message_
Definition: thread_test.cpp:51
TestAppender::append
virtual void append(const log4cxx::spi::LoggingEventPtr &event, log4cxx::helpers::Pool &)
Definition: thread_test.cpp:60
TestAppender::Info::logger_name_
std::string logger_name_
Definition: thread_test.cpp:52
TestAppender::requiresLayout
virtual bool requiresLayout() const
Definition: thread_test.cpp:81
TestAppender::info_
V_Info info_
Definition: thread_test.cpp:57
TEST
TEST(Rosconsole, threadedCalls)
Definition: thread_test.cpp:94
TestAppender::close
virtual void close()
Definition: thread_test.cpp:78
ros::Time::init
static void init()
TestAppender
Definition: thread_test.cpp:45
TestAppender::Info::level_
log4cxx::LevelPtr level_
Definition: thread_test.cpp:50
ROSCONSOLE_DEFAULT_NAME
#define ROSCONSOLE_DEFAULT_NAME
Definition: console.h:304
ROS_INFO
#define ROS_INFO(...)
Definition: macros_generated.h:110
threadFunc
void threadFunc(boost::barrier *b)
Definition: thread_test.cpp:87


rosconsole
Author(s): Josh Faust
autogenerated on Wed Mar 2 2022 00:53:52