Run shorten_stl_types (and a bit more) only if needed in scope tracker
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Wed, 25 Jan 2017 08:39:48 +0000 (09:39 +0100)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Thu, 23 Mar 2017 09:21:35 +0000 (10:21 +0100)
src/tracefunc.cpp
src/tracefunc.hpp

index 4e6282a..ff03b50 100644 (file)
@@ -134,31 +134,46 @@ ScopeTracker::ScopeTracker(
    }
    ensure_indent_level(PerThread, Depth);
    PerThread->scope_tracker_list.push_back(this);
-   {
-      std::ostringstream ostr;
-      if (Location.FunctionName.empty())
-      {
-         ostr << "<unknown> (global scope?)";
-      }
-      else
-      {
-         ostr << shorten_stl_types(Location.FunctionName);
-         if (FuncDepth>0)
-         {
-            ostr << "#" << FuncDepth+1;
-         }
-      }
-      Tag= ostr.str();
-   }
+
    // spit a message
    if (Logger::has_log_level(Logger::LogLevel::Debug))
    {
-      GlobalLogger.debug() << PerThread->indents[Depth] << "ENTER " << Tag;
+      GlobalLogger.debug() << PerThread->indents[Depth] << "ENTER " << get_tag();
    }
 } // eo ScopeTrcaker::ScopeTracker(const SourceLocation&)
 
 
 /**
+ * @brief create Tag if empty; return reference to it
+ *
+ * Moved this from constructor into own function to avoid creating Tag if it is
+ * not required (i.e. log level is not DEBUG) but still create it only once and
+ * ensure it is available if log level changes inside tracked function
+ */
+std::string& ScopeTracker::get_tag()
+{
+    if (Tag.empty())
+    {
+        if (Location.FunctionName.empty())
+        {
+            Tag = "<unknown> (global scope?)";
+        }
+        else
+        {
+            std::ostringstream ostr;
+            ostr << shorten_stl_types(Location.FunctionName);
+            if (FuncDepth>0)
+            {
+                ostr << "#" << FuncDepth+1;
+            }
+            Tag= ostr.str();
+        }
+    }
+    return Tag;
+}
+
+
+/**
  * @brief destructor. emits a LEAVE message.
  *
  *
@@ -168,7 +183,7 @@ ScopeTracker::~ScopeTracker()
    // spit a message
    if (Logger::has_log_level(Logger::LogLevel::Debug))
    {
-      GlobalLogger.debug() << PerThread->indents[Depth] << "LEAVE " << Tag;
+      GlobalLogger.debug() << PerThread->indents[Depth] << "LEAVE " << get_tag();
    }
    if (PerThread->scope_tracker_list.empty())
    {
index 7f5d0df..a575bfe 100644 (file)
@@ -59,7 +59,8 @@ class ScopeTracker
      int Depth;
      int FuncDepth;
 
-     std::string Tag;
+     std::string Tag;    // might be empty
+     std::string &get_tag();   // returns Tag, creates it first if necessary
 
      PerThreadContainer *PerThread;
 }; // eo ScopeTracker