Return NULL thread local storage instead of raising exception
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Mon, 6 Aug 2018 10:43:07 +0000 (12:43 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Wed, 8 Aug 2018 09:29:42 +0000 (11:29 +0200)
src/tracefunc.cpp

index ff03b50..10351f0 100644 (file)
@@ -63,13 +63,22 @@ boost::thread_specific_ptr<PerThreadContainer> 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 <anonymous>
 
 
@@ -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))
    {