From 377996909a42e73e188068e9412e8fd8b7ad7552 Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Mon, 6 Aug 2018 12:43:07 +0200 Subject: [PATCH] Return NULL thread local storage instead of raising exception --- src/tracefunc.cpp | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 40 insertions(+), 3 deletions(-) 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)) { -- 1.7.1