From e366f215898f45a33a1670723394d4a9fad6e9f0 Mon Sep 17 00:00:00 2001
From: Florian Fischer <florian.fl.fischer@fau.de>
Date: Tue, 16 Mar 2021 12:42:55 +0100
Subject: [PATCH] [Debug] use a lambda to build up the log message

This hides the used std::stringstream from the context using the LOG macros.
So we don't have to see the a huge messy std::stringstream object in gdb
for example.

The use of a lambda is a ISO C++ alternative to GCC's statement expression.
---
 emper/Debug.hpp | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/emper/Debug.hpp b/emper/Debug.hpp
index 57d1dfac..2ef222ca 100644
--- a/emper/Debug.hpp
+++ b/emper/Debug.hpp
@@ -14,8 +14,14 @@
 
 // clang-format off
 
+// Hide the stringstream inside a lamba from the context where our LOG macros are used.
+// The lamda should be fairly cheap/free because it will be inlined.
+// The use of a lambda here is the ISO C++ equivalent to GCC statement expressions.
 // NOLINTNEXTLINE(bugprone-macro-parentheses)
-#define LOG(level, x, log_func, new_line) do {if constexpr (level > EMPER_LOG_LEVEL) { break; } std::stringstream sst; sst << x; if constexpr (new_line) { sst << std::endl; }; log_func(sst.str()); } while (false)
+#define LOG_STR_LAMBDA(x, new_line) [&](){std::stringstream sst; sst << x; if constexpr (new_line) { sst << std::endl; }; return sst.str(); }
+
+// NOLINTNEXTLINE(bugprone-macro-parentheses)
+#define LOG(level, x, log_func, new_line) do {if constexpr (level > EMPER_LOG_LEVEL) { break; } log_func(LOG_STR_LAMBDA(x, new_line)()); } while (false)
 
 // NOLINTNEXTLINE(bugprone-macro-parentheses)
 #define DBG(x) LOG(Debug, x, emper_log_no_prefix, true);
-- 
GitLab