Skip to content
Snippets Groups Projects
Commit 4d4368ca authored by Sam Yates's avatar Sam Yates
Browse files

Avoid preprocessor conditions within debug.?pp

* Add `multithreaded()` constexpr function in threading
  namespace.
* Replace `WITH_TBB`-guarded code with multithreadedness-
  checking code.
parent a1c7a312
No related branches found
No related tags found
No related merge requests found
#pragma once #pragma once
#if !defined(WITH_SERIAL) #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 #endif
#include <array> #include <array>
...@@ -76,6 +76,8 @@ struct timer { ...@@ -76,6 +76,8 @@ struct timer {
} }
}; };
constexpr bool multithreaded() { return false; }
} // threading } // threading
} // mc } // mc
......
...@@ -44,6 +44,8 @@ struct timer { ...@@ -44,6 +44,8 @@ struct timer {
} }
}; };
constexpr bool multithreaded() { return true; }
} // threading } // threading
} // mc } // mc
} // nest } // nest
......
...@@ -12,9 +12,7 @@ namespace nest { ...@@ -12,9 +12,7 @@ namespace nest {
namespace mc { namespace mc {
namespace util { namespace util {
#ifdef WITH_TBB
std::mutex global_debug_cerr_mutex; std::mutex global_debug_cerr_mutex;
#endif
bool failed_assertion(const char* assertion, const char* file, bool failed_assertion(const char* assertion, const char* file,
int line, const char* func) int line, const char* func)
......
#pragma once #pragma once
#include <iostream> #include <iostream>
#include <memory>
#include <sstream> #include <sstream>
#include <mutex> #include <mutex>
#include "threading/threading.hpp"
namespace nest { namespace nest {
namespace mc { namespace mc {
namespace util { namespace util {
...@@ -24,27 +27,31 @@ void debug_emit(std::ostream& out, const Head& head, const Tail&... tail) { ...@@ -24,27 +27,31 @@ void debug_emit(std::ostream& out, const Head& head, const Tail&... tail) {
debug_emit(out, tail...); debug_emit(out, tail...);
} }
#ifdef WITH_TBB
extern std::mutex global_debug_cerr_mutex; extern std::mutex global_debug_cerr_mutex;
#endif
template <typename... Args> template <typename... Args>
void debug_emit_trace(const char* file, int line, const char* varlist, const Args&... args) { void debug_emit_trace(const char* file, int line, const char* varlist, const Args&... args) {
#ifdef WITH_TBB constexpr bool multithreaded = nest::mc::threading::multithreaded();
std::stringstream out;
#else
auto &out = std::cerr;
#endif
debug_emit_trace_leader(out, file, line, varlist); std::unique_ptr<std::ostream> buffer;
debug_emit(out, args...); std::ostream* out = &std::cerr;
#ifdef WITH_TBB if (multithreaded) {
std::lock_guard<std::mutex> guard(global_debug_cerr_mutex); buffer.reset(new std::stringstream());
std::cerr << out.rdbuf(); out = buffer.get();
#else }
out.flush();
#endif 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 } // namespace util
...@@ -57,7 +64,6 @@ void debug_emit_trace(const char* file, int line, const char* varlist, const Arg ...@@ -57,7 +64,6 @@ void debug_emit_trace(const char* file, int line, const char* varlist, const Arg
#define TRACE(...) #define TRACE(...)
#endif #endif
#ifdef WITH_ASSERTIONS #ifdef WITH_ASSERTIONS
#ifdef __GNUC__ #ifdef __GNUC__
#define DEBUG_FUNCTION_NAME __PRETTY_FUNCTION__ #define DEBUG_FUNCTION_NAME __PRETTY_FUNCTION__
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment