Wrap Scopetracker constructor/destructor in big try-catch
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Mon, 6 Aug 2018 10:43:33 +0000 (12:43 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Wed, 8 Aug 2018 09:29:42 +0000 (11:29 +0200)
src/tracefunc.cpp
src/tracefunc.hpp

index 10351f0..bad392d 100644 (file)
@@ -90,6 +90,7 @@ static PerThreadContainer *TLS_get_container() throw ()
  * @param level the desired indent level.
  */
 static void ensure_indent_level(PerThreadContainer *per_thread, unsigned int level)
+                       throw(std::length_error, std::bad_alloc)
 {
     // First initialization?
     if (per_thread->indents.empty())
@@ -137,9 +138,12 @@ static void ensure_indent_level(PerThreadContainer *per_thread, unsigned int lev
  * @param loc the source location.
  *
  * the message is indented according to the current depth of nested ScopeTracker instances.
+ *
+ * All exceptions happening in here (from vector::reserve, vector::push_back, acquiring lock for logger)
+ * are caught. Will try to log only that problem occurred
  */
 ScopeTracker::ScopeTracker(
-    const SourceLocation& loc)
+    const SourceLocation& loc) throw ()
     : Location(loc)
     , Depth(0)
     , FuncDepth(0)
@@ -150,29 +154,38 @@ ScopeTracker::ScopeTracker(
         try_logging_error("Failed to get thread local storage");
         return;
     }
-    if (!PerThread->scope_tracker_list.empty())
-   {
-      ScopeTracker* last_tracker= PerThread->scope_tracker_list.back();
-      if (Location)
-      {
-         if (last_tracker->Location
-             && last_tracker->Location.File == Location.File
-             && last_tracker->Location.FunctionName == Location.FunctionName)
-         {
-            FuncDepth= last_tracker->FuncDepth+1;
-         }
-      }
-      Depth= last_tracker->Depth + 1;
-   }
-   ensure_indent_level(PerThread, Depth);
-   PerThread->scope_tracker_list.push_back(this);
 
-   // spit a message
-   if (Logger::has_log_level(Logger::LogLevel::Debug))
-   {
-      GlobalLogger.debug() << PerThread->indents[Depth] << "ENTER " << get_tag();
-   }
-} // eo ScopeTrcaker::ScopeTracker(const SourceLocation&)
+    try
+    {
+        if (!PerThread->scope_tracker_list.empty())
+        {
+            ScopeTracker* last_tracker= PerThread->scope_tracker_list.back();
+            if (Location)
+            {
+                if (last_tracker->Location
+                        && last_tracker->Location.File == Location.File
+                        && last_tracker->Location.FunctionName == Location.FunctionName)
+                {
+                    FuncDepth= last_tracker->FuncDepth+1;
+                }
+            }
+            Depth= last_tracker->Depth + 1;
+        }
+
+        ensure_indent_level(PerThread, Depth);
+        PerThread->scope_tracker_list.push_back(this);
+
+        // spit a message
+        if (Logger::has_log_level(Logger::LogLevel::Debug))
+        {
+            GlobalLogger.debug() << PerThread->indents[Depth] << "ENTER " << get_tag();
+        }
+    }
+    catch (...)
+    {
+        try_logging_error("Caught exception in constructor");
+    }
+} // eo ScopeTracker::ScopeTracker(const SourceLocation&)
 
 
 /**
@@ -208,34 +221,42 @@ std::string& ScopeTracker::get_tag()
 /**
  * @brief destructor. emits a LEAVE message.
  *
- *
+ * All exceptions are caught, will try to log only that some problem occurred
  */
-ScopeTracker::~ScopeTracker()
+ScopeTracker::~ScopeTracker() throw ()
 {
     if (PerThread == NULL)
     {
         try_logging_error("Failed to get thread local storage");
         return;
     }
-   // spit a message
-   if (Logger::has_log_level(Logger::LogLevel::Debug))
-   {
-      GlobalLogger.debug() << PerThread->indents[Depth] << "LEAVE " << get_tag();
-   }
-   if (PerThread->scope_tracker_list.empty())
-   {
-      return;
-   }
-   if (PerThread->scope_tracker_list.back() == this)
-   {
-      PerThread->scope_tracker_list.pop_back();
-   }
-   else
-   {
-      // oh hell, this should never be the case!
-      //TODO
-      GlobalLogger.error() << "inconsistency detected in scope tracker";
-   }
+
+    try
+    {
+        // spit a message
+        if (Logger::has_log_level(Logger::LogLevel::Debug))
+        {
+            GlobalLogger.debug() << PerThread->indents[Depth] << "LEAVE " << get_tag();
+        }
+        if (PerThread->scope_tracker_list.empty())
+        {
+            return;
+        }
+        if (PerThread->scope_tracker_list.back() == this)
+        {
+            PerThread->scope_tracker_list.pop_back();
+        }
+        else
+        {
+            // oh hell, this should never be the case!
+            //TODO
+            GlobalLogger.error() << "inconsistency detected in scope tracker";
+        }
+    }
+    catch (...)
+    {
+        try_logging_error("Caught exception in destructor");
+    }
 } // eo ScopeTracker::~ScopeTracker()
 
 
index a575bfe..36a6227 100644 (file)
@@ -44,12 +44,15 @@ class PerThreadContainer;
  *
  * basically: emits a ENTER message on construction and a LEAVE message on destruction.
  * And indent these messages acoording to the nesting depth.
+ *
+ * If something goes wrong nothing is thrown from constructor/destructor. Will try to
+ * log only that some problem occurred.
  */
 class ScopeTracker
 {
    public:
-     ScopeTracker(const SourceLocation& loc);
-     ~ScopeTracker();
+     ScopeTracker(const SourceLocation& loc) throw ();
+     ~ScopeTracker() throw ();
 
    private:
      ScopeTracker(const ScopeTracker& rhs);