From: Christian Herdtweck Date: Mon, 6 Aug 2018 10:43:07 +0000 (+0200) Subject: Return NULL thread local storage instead of raising exception X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=defc983271a38b2e07aa0590fdfaaab52d1c9f39;p=libi2ncommon Return NULL thread local storage instead of raising exception --- diff --git a/src/tracefunc.cpp b/src/tracefunc.cpp index ff03b50..10351f0 100644 --- a/src/tracefunc.cpp +++ b/src/tracefunc.cpp @@ -63,13 +63,22 @@ boost::thread_specific_ptr thread_container; /** * Helper function to retrieve TLS pointer. * Initializes the pointer if called for the first time. - * @return Pointer to the per thread container + * @return Pointer to the per thread container or NULL if error occurs */ -static PerThreadContainer *TLS_get_container() +static PerThreadContainer *TLS_get_container() throw () { // First call? Construct options container if (thread_container.get() == NULL) - thread_container.reset(new PerThreadContainer()); + { + try + { + thread_container.reset(new PerThreadContainer()); + } + catch (...) + { + return NULL; + } + } PerThreadContainer *per_thread = thread_container.get(); @@ -98,6 +107,24 @@ static void ensure_indent_level(PerThreadContainer *per_thread, unsigned int lev } } // eo ensure_indent_level(int) +/** + * @brief try logging that some problem occurred + * + * Keep in mind that problem could be with the logger itself, so wrap all this + * in a try-catch agin + */ + static void try_logging_error(const std::string &message) throw () +{ + try + { + GlobalLogger.debug() << "Problem occurred in ScopeTracker: " << message; + } + catch (...) + { // nothing more we can do + } +} + + } // eo namespace @@ -118,6 +145,11 @@ ScopeTracker::ScopeTracker( , FuncDepth(0) , PerThread(TLS_get_container()) { + if (PerThread == NULL) + { + try_logging_error("Failed to get thread local storage"); + return; + } if (!PerThread->scope_tracker_list.empty()) { ScopeTracker* last_tracker= PerThread->scope_tracker_list.back(); @@ -180,6 +212,11 @@ std::string& ScopeTracker::get_tag() */ ScopeTracker::~ScopeTracker() { + if (PerThread == NULL) + { + try_logging_error("Failed to get thread local storage"); + return; + } // spit a message if (Logger::has_log_level(Logger::LogLevel::Debug)) {