diff options
author | Benoit Foucher <benoit@zeroc.com> | 2018-12-27 18:34:51 +0100 |
---|---|---|
committer | Benoit Foucher <benoit@zeroc.com> | 2018-12-27 18:34:51 +0100 |
commit | d57bd8450ed96196f11f7e7f024a75a182750632 (patch) | |
tree | d3f307542071c2ea81db67326150bab42d44ca43 /cpp | |
parent | CHANGELOG updates (diff) | |
download | ice-d57bd8450ed96196f11f7e7f024a75a182750632.tar.bz2 ice-d57bd8450ed96196f11f7e7f024a75a182750632.tar.xz ice-d57bd8450ed96196f11f7e7f024a75a182750632.zip |
Thread safe redirect for controller applications
Diffstat (limited to 'cpp')
-rw-r--r-- | cpp/test/Common/TestHelper.cpp | 88 | ||||
-rw-r--r-- | cpp/test/include/TestHelper.h | 69 | ||||
-rw-r--r-- | cpp/test/ios/controller/Bundle/ControllerI.mm | 14 | ||||
-rw-r--r-- | cpp/test/uwp/controller/ControllerView.xaml.cpp | 13 |
4 files changed, 120 insertions, 64 deletions
diff --git a/cpp/test/Common/TestHelper.cpp b/cpp/test/Common/TestHelper.cpp index 55849584e00..018f134d665 100644 --- a/cpp/test/Common/TestHelper.cpp +++ b/cpp/test/Common/TestHelper.cpp @@ -30,6 +30,94 @@ shutdownOnInterruptCallback(int) } +StreamHelper::StreamHelper() : _controllerHelper(0) +{ + setp(&data[0], &data[sizeof(data) - 1]); + + _previousCoutBuffer = std::cout.rdbuf(); + std::cout.rdbuf(this); + + _previousCerrBuffer = std::cerr.rdbuf(); + std::cerr.rdbuf(this); +} + +StreamHelper::~StreamHelper() +{ + std::cout.rdbuf(_previousCoutBuffer); + std::cerr.rdbuf(_previousCerrBuffer); +} + +void +StreamHelper::setControllerHelper(ControllerHelper* controllerHelper) +{ + IceUtil::Mutex::Lock sync(_mutex); + assert(_controllerHelper && !controllerHelper || !_controllerHelper && controllerHelper); + _controllerHelper = controllerHelper; + + if(_controllerHelper) + { + _previousLogger = Ice::getProcessLogger(); + Ice::setProcessLogger(Ice::getProcessLogger()->cloneWithPrefix(_controllerHelper->loggerPrefix())); + } + else + { + Ice::setProcessLogger(_previousLogger); + } +} + +void +StreamHelper::flush() +{ +} + +void +StreamHelper::newLine() +{ + IceUtil::Mutex::Lock sync(_mutex); + if(_controllerHelper) + { + _controllerHelper->print("\n"); + } +} + +int +StreamHelper::sync() +{ + assert(_controllerHelper); + std::streamsize n = pptr() - pbase(); + { + IceUtil::Mutex::Lock sync(_mutex); + if(_controllerHelper) + { + _controllerHelper->print(std::string(pbase(), static_cast<int>(n))); + } + } + pbump(-static_cast<int>(pptr() - pbase())); + return 0; +} + +int +StreamHelper::overflow(int ch) +{ + sync(); + if(ch != EOF) + { + assert(pptr() != epptr()); + sputc(static_cast<char>(ch)); + } + return 0; +} + +int +StreamHelper::sputc(char c) +{ + if(c == '\n') + { + pubsync(); + } + return std::streambuf::sputc(c); +} + Test::TestHelper::TestHelper(bool registerPlugins) : _controllerHelper(0) #if !defined(ICE_OS_UWP) && (!defined(__APPLE__) || TARGET_OS_IPHONE == 0) diff --git a/cpp/test/include/TestHelper.h b/cpp/test/include/TestHelper.h index bad48159649..56ce76e31c7 100644 --- a/cpp/test/include/TestHelper.h +++ b/cpp/test/include/TestHelper.h @@ -63,72 +63,21 @@ class StreamHelper : public std::streambuf { public: - StreamHelper(ControllerHelper* controllerHelper, bool redirect) : _controllerHelper(controllerHelper) - { - setp(&data[0], &data[sizeof(data) - 1]); - if(redirect) - { - _previousLogger = Ice::getProcessLogger(); - Ice::setProcessLogger(Ice::getProcessLogger()->cloneWithPrefix(_controllerHelper->loggerPrefix())); - - _previousCoutBuffer = std::cout.rdbuf(); - std::cout.rdbuf(this); - - _previousCerrBuffer = std::cerr.rdbuf(); - std::cerr.rdbuf(this); - } - } - - ~StreamHelper() - { - if(_previousLogger) - { - Ice::setProcessLogger(_previousLogger); - std::cout.rdbuf(_previousCoutBuffer); - std::cerr.rdbuf(_previousCerrBuffer); - } - } + StreamHelper(); + ~StreamHelper(); - virtual void flush() - { - } + void setControllerHelper(ControllerHelper*); - virtual void newLine() - { - _controllerHelper->print("\n"); - } + virtual void flush(); + virtual void newLine(); private: - int sync() - { - assert(_controllerHelper); - std::streamsize n = pptr() - pbase(); - _controllerHelper->print(std::string(pbase(), static_cast<int>(n))); - pbump(-static_cast<int>(pptr() - pbase())); - return 0; - } - - int overflow(int ch) - { - sync(); - if(ch != EOF) - { - assert(pptr() != epptr()); - sputc(static_cast<char>(ch)); - } - return 0; - } - - int sputc(char c) - { - if(c == '\n') - { - pubsync(); - } - return std::streambuf::sputc(c); - } + virtual int sync(); + virtual int overflow(int); + virtual int sputc(char); + IceUtil::Mutex _mutex; ControllerHelper* _controllerHelper; char data[1024]; Ice::LoggerPtr _previousLogger; diff --git a/cpp/test/ios/controller/Bundle/ControllerI.mm b/cpp/test/ios/controller/Bundle/ControllerI.mm index 8bcbbb84697..46cf7739c9f 100644 --- a/cpp/test/ios/controller/Bundle/ControllerI.mm +++ b/cpp/test/ios/controller/Bundle/ControllerI.mm @@ -109,6 +109,8 @@ namespace Ice::CommunicatorPtr _communicator; }; + + Test::StreamHelper streamRedirect; } ControllerHelperI::ControllerHelperI(id<ControllerView> controller, const string& dll, const StringSeq& args) : @@ -197,6 +199,11 @@ ControllerHelperI::run() return; } + if(_dll.find("client") != string::npos || _dll.find("collocated") != string::npos) + { + streamRedirect.setControllerHelper(this); + } + CREATE_HELPER_ENTRY_POINT createHelper = (CREATE_HELPER_ENTRY_POINT)sym; char** argv = new char*[_args.size() + 1]; for(unsigned int i = 0; i < _args.size(); ++i) @@ -206,8 +213,6 @@ ControllerHelperI::run() argv[_args.size()] = 0; try { - Test::StreamHelper streamHelper(this, - _dll.find("client") != string::npos || _dll.find("collocated") != string::npos); IceInternal::UniquePtr<Test::TestHelper> helper(createHelper()); helper->setControllerHelper(this); helper->run(static_cast<int>(_args.size()), argv); @@ -225,6 +230,11 @@ ControllerHelperI::run() } delete[] argv; + if(_dll.find("client") != string::npos || _dll.find("collocated") != string::npos) + { + streamRedirect.setControllerHelper(0); + } + CFBundleUnloadExecutable(handle); CFRelease(handle); } diff --git a/cpp/test/uwp/controller/ControllerView.xaml.cpp b/cpp/test/uwp/controller/ControllerView.xaml.cpp index 4d17ef2960b..2a118ee658b 100644 --- a/cpp/test/uwp/controller/ControllerView.xaml.cpp +++ b/cpp/test/uwp/controller/ControllerView.xaml.cpp @@ -131,6 +131,8 @@ private: shared_ptr<Test::Common::ProcessControllerRegistryPrx> _registry; }; +Test::StreamHelper streamRedirect; + } ControllerHelperI::ControllerHelperI(const string& dll, const StringSeq& args) : @@ -199,10 +201,13 @@ ControllerHelperI::run() argv[i] = const_cast<char*>(_args[i].c_str()); } argv[_args.size()] = 0; + + if(_dll.find("client") != string::npos || _dll.find("collocated") != string::npos) + { + streamRedirect.setControllerHelper(this); + } try { - StreamHelper streamHelper(this, - _dll.find("client") != string::npos || _dll.find("collocated") != string::npos); auto helper = unique_ptr<Test::TestHelper>(createHelper()); helper->setControllerHelper(this); helper->run(static_cast<int>(_args.size()), argv); @@ -218,6 +223,10 @@ ControllerHelperI::run() print("unexpected unknown exception while running `" + _args[0] + "'"); completed(1); } + if(_dll.find("client") != string::npos || _dll.find("collocated") != string::npos) + { + streamRedirect.setControllerHelper(0); + } delete[] argv; }); _thread = move(t); |