util/cmake: enable ScopeTracing through cmake variable option
authorMarcus Fritzsch <marcus_fritzsch@mentor.com>
Tue, 12 Sep 2017 09:29:30 +0000 (11:29 +0200)
committerMarcus Fritzsch <marcus_fritzsch@mentor.com>
Tue, 12 Sep 2017 09:29:30 +0000 (11:29 +0200)
Signed-off-by: Marcus Fritzsch <marcus_fritzsch@mentor.com>
CMakeLists.txt
src/util.cpp
src/util.hpp

index a7382f0..e0e8f92 100644 (file)
@@ -50,6 +50,14 @@ else()
    remove_definitions(-DDEBUG_OUTPUT)
 endif()
 
+# Should modernize the following somehow...
+set(ENABLE_SCOPE_TRACING OFF CACHE BOOL "Enable scope enter/leave messages for certain parts of the code.")
+if(ENABLE_SCOPE_TRACING)
+   add_definitions(-DSCOPE_TRACING)
+else()
+   remove_definitions(-DSCOPE_TRACING)
+endif()
+
 set(SANITIZER_MODE "none" CACHE STRING "Build using a specific sanitizer (e.g. 'address', 'thread', 'leak', 'undefined'), depends on compiler; default none")
 
 set(LINK_LIBCXX OFF CACHE BOOL "Link against LLVMs libc++")
index db61bc2..44c377c 100644 (file)
 
 #include <unistd.h>
 
+#ifdef SCOPE_TRACING
 thread_local int ScopeTrace::indent = 0;
+explicit ScopeTrace::ScopeTrace(char const *func) : f(func) {
+   fprintf(stderr, "%lu %*s%s -->\n", pthread_self(), 2 * indent++, "", this->f);
+}
+ScopeTrace::~ScopeTrace() { fprintf(stderr, "%lu %*s%s <--\n", pthread_self(), 2 * --indent, "", this->f); }
+#endif
 
 unique_fd::~unique_fd() {
    if (this->fd != -1) {
index ff173c8..5d602ed 100644 (file)
@@ -27,6 +27,9 @@ extern "C" {
 #include <afb/afb-binding.h>
 };
 
+#define CONCAT_(X, Y) X##Y
+#define CONCAT(X, Y) CONCAT_(X, Y)
+
 #ifdef __GNUC__
 #define ATTR_FORMAT(stringindex, firsttocheck) \
    __attribute__((format(printf, stringindex, firsttocheck)))
@@ -50,13 +53,10 @@ extern "C" {
 #define logdebug(...)
 #endif
 
-#ifdef NDEBUG
+#ifndef SCOPE_TRACING
 #define ST()
 #define STN(N)
 #else
-#define CONCAT_(X, Y) X##Y
-#define CONCAT(X, Y) CONCAT_(X, Y)
-
 #define ST() \
     ScopeTrace __attribute__((unused)) CONCAT(trace_scope_, __LINE__)(__func__)
 #define STN(N) \
@@ -65,10 +65,8 @@ extern "C" {
 struct ScopeTrace {
    thread_local static int indent;
    char const *f{};
-   explicit ScopeTrace(char const *func) : f(func) {
-      fprintf(stderr, "%lu %*s%s -->\n", pthread_self(), 2 * indent++, "", this->f);
-   }
-   ~ScopeTrace() { fprintf(stderr, "%lu %*s%s <--\n", pthread_self(), 2 * --indent, "", this->f); }
+   explicit ScopeTrace(char const *func);
+   ~ScopeTrace();
 };
 #endif