diff options
author | Bernard Normier <bernard@zeroc.com> | 2014-10-29 20:38:19 +0000 |
---|---|---|
committer | Bernard Normier <bernard@zeroc.com> | 2014-10-29 20:38:19 +0000 |
commit | f29afb0ca26a0d4964a19d8fd5104cf5f0db81a3 (patch) | |
tree | e6a10ffa1f36d5d0726ff69bd6a91260e21d0259 /cpp | |
parent | Fixed (ICE-5800) - Slice build dependencies don't work properly in source tree (diff) | |
download | ice-f29afb0ca26a0d4964a19d8fd5104cf5f0db81a3.tar.bz2 ice-f29afb0ca26a0d4964a19d8fd5104cf5f0db81a3.tar.xz ice-f29afb0ca26a0d4964a19d8fd5104cf5f0db81a3.zip |
Fixed ICE-5796: LoggerUtil now deals properly with exceptions (print their stack traces when available)
Diffstat (limited to 'cpp')
-rw-r--r-- | cpp/include/Ice/LoggerUtil.h | 52 | ||||
-rw-r--r-- | cpp/src/Ice/LoggerUtil.cpp | 14 | ||||
-rw-r--r-- | cpp/test/Ice/exceptions/AllTests.cpp | 55 |
3 files changed, 100 insertions, 21 deletions
diff --git a/cpp/include/Ice/LoggerUtil.h b/cpp/include/Ice/LoggerUtil.h index c5576e1e0b2..1414bb37f03 100644 --- a/cpp/include/Ice/LoggerUtil.h +++ b/cpp/include/Ice/LoggerUtil.h @@ -13,6 +13,7 @@ #include <Ice/Logger.h> #include <Ice/CommunicatorF.h> #include <Ice/Plugin.h> +#include <Ice/Exception.h> namespace Ice { @@ -30,24 +31,61 @@ private: std::ostringstream _str; }; +ICE_API LoggerOutputBase& loggerInsert(LoggerOutputBase& out, const IceUtil::Exception& ex); + +template<typename T> +struct IsException +{ + static char test(IceUtil::Exception*); + static long test(...); + + static const bool value = sizeof(test(static_cast<T*>(0))) == sizeof(char); +}; + +template<typename T, bool = false> +struct LoggerOutputInserter +{ + static inline LoggerOutputBase& + insert(LoggerOutputBase& out, const T& val) + { + out.__str() << val; + return out; + } +}; + +// Partial specialization +template<typename T> +struct LoggerOutputInserter<T, true> +{ + static inline LoggerOutputBase& + insert(LoggerOutputBase& out, const T& ex) + { + return loggerInsert(out, ex); + } +}; + template<typename T> inline LoggerOutputBase& operator<<(LoggerOutputBase& out, const T& val) { - out.__str() << val; - return out; + return LoggerOutputInserter<T, IsException<T>::value>::insert(out, val); } -template<class Y> -LoggerOutputBase& -operator<<(LoggerOutputBase& os, ::IceInternal::ProxyHandle<Y> p) +template<typename T> +inline LoggerOutputBase& +operator<<(LoggerOutputBase& os, const ::IceInternal::ProxyHandle<T>& p) { return os << (p ? p->ice_toString() : ""); } -ICE_API LoggerOutputBase& operator<<(LoggerOutputBase&, std::ios_base& (*)(std::ios_base&)); -ICE_API LoggerOutputBase& operator<<(LoggerOutputBase&, const ::std::exception& ex); +inline LoggerOutputBase& +operator<<(LoggerOutputBase& out, const ::std::exception& ex) +{ + out.__str() << ex.what(); + return out; +} +ICE_API LoggerOutputBase& operator<<(LoggerOutputBase&, std::ios_base& (*)(std::ios_base&)); template<class L, class LPtr, void (L::*output)(const std::string&)> class LoggerOutput : public LoggerOutputBase diff --git a/cpp/src/Ice/LoggerUtil.cpp b/cpp/src/Ice/LoggerUtil.cpp index 9f1e85accb4..00b79a6845c 100644 --- a/cpp/src/Ice/LoggerUtil.cpp +++ b/cpp/src/Ice/LoggerUtil.cpp @@ -43,18 +43,16 @@ Ice::operator<<(Ice::LoggerOutputBase& out, ios_base& (*val)(ios_base&)) } Ice::LoggerOutputBase& -Ice::operator<<(Ice::LoggerOutputBase& out, const std::exception& ex) +Ice::loggerInsert(Ice::LoggerOutputBase& out, const IceUtil::Exception& ex) { if(IceUtilInternal::printStackTraces) { - const ::IceUtil::Exception* exception = dynamic_cast<const ::IceUtil::Exception*>(&ex); - if(exception) - { - out.__str() << exception->what() << '\n' << exception->ice_stackTrace(); - return out; - } + out.__str() << ex.what() << '\n' << ex.ice_stackTrace(); + } + else + { + out.__str() << ex.what(); } - out.__str() << ex.what(); return out; } diff --git a/cpp/test/Ice/exceptions/AllTests.cpp b/cpp/test/Ice/exceptions/AllTests.cpp index 5432ff35ee0..36a55f54c49 100644 --- a/cpp/test/Ice/exceptions/AllTests.cpp +++ b/cpp/test/Ice/exceptions/AllTests.cpp @@ -14,6 +14,12 @@ using namespace std; using namespace Test; +namespace +{ +const bool printException = false; +} + + class EmptyI : virtual public Empty { }; @@ -482,8 +488,13 @@ allTests(const Ice::CommunicatorPtr& communicator) first = communicator->createObjectAdapter("TestAdapter0"); test(false); } - catch(const Ice::InitializationException&) + catch(const Ice::InitializationException& ex) { + if(printException) + { + Ice::Print printer(communicator->getLogger()); + printer << ex; + } // Expected } @@ -494,8 +505,14 @@ allTests(const Ice::CommunicatorPtr& communicator) Ice::ObjectAdapterPtr second = communicator->createObjectAdapter("TestAdapter0"); test(false); } - catch(const Ice::AlreadyRegisteredException&) + catch(const Ice::AlreadyRegisteredException& ex) { + if(printException) + { + Ice::Print printer(communicator->getLogger()); + printer << ex; + } + // Expected } @@ -505,8 +522,14 @@ allTests(const Ice::CommunicatorPtr& communicator) communicator->createObjectAdapterWithEndpoints("TestAdapter0", "ssl -h foo -p 12011"); test(false); } - catch(const Ice::AlreadyRegisteredException&) + catch(const Ice::AlreadyRegisteredException& ex) { + if(printException) + { + Ice::Print printer(communicator->getLogger()); + printer << ex; + } + // Expected. } first->deactivate(); @@ -524,8 +547,13 @@ allTests(const Ice::CommunicatorPtr& communicator) adapter->add(obj, communicator->stringToIdentity("x")); test(false); } - catch(const Ice::AlreadyRegisteredException&) + catch(const Ice::AlreadyRegisteredException& ex) { + if(printException) + { + Ice::Print printer(communicator->getLogger()); + printer << ex; + } } try @@ -535,14 +563,24 @@ allTests(const Ice::CommunicatorPtr& communicator) catch(const Ice::IllegalIdentityException& ex) { test(ex.id.name == ""); + if(printException) + { + Ice::Print printer(communicator->getLogger()); + printer << ex; + } } try { adapter->add(0, communicator->stringToIdentity("x")); } - catch(const Ice::IllegalServantException&) + catch(const Ice::IllegalServantException& ex) { + if(printException) + { + Ice::Print printer(communicator->getLogger()); + printer << ex; + } } @@ -552,8 +590,13 @@ allTests(const Ice::CommunicatorPtr& communicator) adapter->remove(communicator->stringToIdentity("x")); test(false); } - catch(const Ice::NotRegisteredException&) + catch(const Ice::NotRegisteredException& ex) { + if(printException) + { + Ice::Print printer(communicator->getLogger()); + printer << ex; + } } adapter->deactivate(); |