From 4d4368ca7e2dbb064aeef1d2a07394b7a761298e Mon Sep 17 00:00:00 2001
From: Sam Yates <halfflat@gmail.com>
Date: Tue, 5 Jul 2016 17:18:42 +0200
Subject: [PATCH] Avoid preprocessor conditions within debug.?pp

* Add `multithreaded()` constexpr function in threading
  namespace.
* Replace `WITH_TBB`-guarded code with multithreadedness-
  checking code.
---
 src/threading/serial.hpp |  4 +++-
 src/threading/tbb.hpp    |  2 ++
 src/util/debug.cpp       |  2 --
 src/util/debug.hpp       | 38 ++++++++++++++++++++++----------------
 4 files changed, 27 insertions(+), 19 deletions(-)

diff --git a/src/threading/serial.hpp b/src/threading/serial.hpp
index eef78394..fb66e6e2 100644
--- a/src/threading/serial.hpp
+++ b/src/threading/serial.hpp
@@ -1,7 +1,7 @@
 #pragma once
 
 #if !defined(WITH_SERIAL)
-    #error this header can only be loaded if WITH_SERIAL is set
+    #error "this header can only be loaded if WITH_SERIAL is set"
 #endif
 
 #include <array>
@@ -76,6 +76,8 @@ struct timer {
     }
 };
 
+constexpr bool multithreaded() { return false; }
+
 
 } // threading
 } // mc
diff --git a/src/threading/tbb.hpp b/src/threading/tbb.hpp
index ed82e26e..eae6b112 100644
--- a/src/threading/tbb.hpp
+++ b/src/threading/tbb.hpp
@@ -44,6 +44,8 @@ struct timer {
     }
 };
 
+constexpr bool multithreaded() { return true; }
+
 } // threading
 } // mc
 } // nest
diff --git a/src/util/debug.cpp b/src/util/debug.cpp
index 6dee975a..51c646bb 100644
--- a/src/util/debug.cpp
+++ b/src/util/debug.cpp
@@ -12,9 +12,7 @@ namespace nest {
 namespace mc {
 namespace util {
 
-#ifdef WITH_TBB
 std::mutex global_debug_cerr_mutex;
-#endif
 
 bool failed_assertion(const char* assertion, const char* file,
                       int line, const char* func)
diff --git a/src/util/debug.hpp b/src/util/debug.hpp
index 612b4331..f12f9ec4 100644
--- a/src/util/debug.hpp
+++ b/src/util/debug.hpp
@@ -1,9 +1,12 @@
 #pragma once
 
 #include <iostream>
+#include <memory>
 #include <sstream>
 #include <mutex>
 
+#include "threading/threading.hpp"
+
 namespace nest {
 namespace mc {
 namespace util {
@@ -24,27 +27,31 @@ void debug_emit(std::ostream& out, const Head& head, const Tail&... tail) {
     debug_emit(out, tail...);
 }
 
-#ifdef WITH_TBB
 extern std::mutex global_debug_cerr_mutex;
-#endif
 
 template <typename... Args>
 void debug_emit_trace(const char* file, int line, const char* varlist, const Args&... args) {
-#ifdef WITH_TBB
-    std::stringstream out;
-#else
-    auto &out = std::cerr;
-#endif
+    constexpr bool multithreaded = nest::mc::threading::multithreaded();
 
-    debug_emit_trace_leader(out, file, line, varlist);
-    debug_emit(out, args...);
+    std::unique_ptr<std::ostream> buffer;
+    std::ostream* out = &std::cerr;
 
-#ifdef WITH_TBB
-    std::lock_guard<std::mutex> guard(global_debug_cerr_mutex);
-    std::cerr << out.rdbuf();
-#else
-    out.flush();
-#endif
+    if (multithreaded) {
+        buffer.reset(new std::stringstream());
+        out = buffer.get();
+    }
+
+    debug_emit_trace_leader(*out, file, line, varlist);
+    debug_emit(*out, args...);
+
+    if (multithreaded) {
+        std::lock_guard<std::mutex> guard(global_debug_cerr_mutex);
+        std::cerr << out->rdbuf();
+        std::cerr.flush();
+    }
+    else {
+        out->flush();
+    }
 }
 
 } // namespace util
@@ -57,7 +64,6 @@ void debug_emit_trace(const char* file, int line, const char* varlist, const Arg
     #define TRACE(...)
 #endif
 
-
 #ifdef WITH_ASSERTIONS
     #ifdef __GNUC__
         #define DEBUG_FUNCTION_NAME __PRETTY_FUNCTION__
-- 
GitLab