util: add scope tracer utility (hack, but more or less useful)
authorMarcus Fritzsch <marcus_fritzsch@mentor.com>
Tue, 12 Sep 2017 09:29:26 +0000 (11:29 +0200)
committerMarcus Fritzsch <marcus_fritzsch@mentor.com>
Tue, 12 Sep 2017 09:29:26 +0000 (11:29 +0200)
Signed-off-by: Marcus Fritzsch <marcus_fritzsch@mentor.com>
src/util.cpp
src/util.hpp

index 1f2bc05..db61bc2 100644 (file)
@@ -24,6 +24,8 @@
 
 #include <unistd.h>
 
+thread_local int ScopeTrace::indent = 0;
+
 unique_fd::~unique_fd() {
    if (this->fd != -1) {
       close(this->fd);
index ab9019b..ff173c8 100644 (file)
 #define WM_UTIL_HPP
 
 #include <functional>
-#include <sys/poll.h>
-
+#include <thread>
 #include <vector>
 
+#include <sys/poll.h>
+
 extern "C" {
 #include <afb/afb-binding.h>
 };
@@ -49,6 +50,28 @@ extern "C" {
 #define logdebug(...)
 #endif
 
+#ifdef NDEBUG
+#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) \
+    ScopeTrace __attribute__((unused)) CONCAT(named_trace_scope_, __LINE__)(#N)
+
+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); }
+};
+#endif
+
 //      _                   _                 _                       __     _
 //  ___| |_ _ __ _   _  ___| |_   _   _ _ __ (_) __ _ _   _  ___     / _| __| |
 // / __| __| '__| | | |/ __| __| | | | | '_ \| |/ _` | | | |/ _ \   | |_ / _` |