From 1dddf8c03e8b7781f549636be6d23e6ca245512b Mon Sep 17 00:00:00 2001 From: Marcus Fritzsch Date: Tue, 12 Sep 2017 11:29:30 +0200 Subject: util/cmake: enable ScopeTracing through cmake variable option Signed-off-by: Marcus Fritzsch --- CMakeLists.txt | 8 ++++++++ src/util.cpp | 6 ++++++ src/util.hpp | 14 ++++++-------- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a7382f0..e0e8f92 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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++") diff --git a/src/util.cpp b/src/util.cpp index db61bc2..44c377c 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -24,7 +24,13 @@ #include +#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) { diff --git a/src/util.hpp b/src/util.hpp index ff173c8..5d602ed 100644 --- a/src/util.hpp +++ b/src/util.hpp @@ -27,6 +27,9 @@ extern "C" { #include }; +#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 -- cgit 1.2.3-korg