From 28028fac0ab808b7e7d165b9c2ef6e0f37a71515 Mon Sep 17 00:00:00 2001 From: Florian Fischer <florian.fl.fischer@fau.de> Date: Tue, 23 Mar 2021 18:28:02 +0100 Subject: [PATCH] [Debug] s/WDBG/DBG and always terminate a log messager with a new line Rename the macro used to build up the debug message, to better reflect its generality. It can be used anywhere to build a string with C++ stream formatting. std::string s = EMPER_BUILD_STR("foo = " << 1 " , bar = " << bar(42)); Since WDBG was the only log macro without a terminating newline, which was in fact broken according to flow, we can remove it and the new line handling code in the LOG macro. --- apps/Main.cpp | 6 +++--- emper/Debug.cpp | 2 +- emper/Debug.hpp | 24 +++++++++++------------- tests/RuntimeDestroyTest.cpp | 6 +++--- tests/SimpleActorTest.cpp | 6 +++--- tests/SimplestFibTest.cpp | 6 +++--- 6 files changed, 24 insertions(+), 26 deletions(-) diff --git a/apps/Main.cpp b/apps/Main.cpp index 0ab7d437..b2df11bd 100644 --- a/apps/Main.cpp +++ b/apps/Main.cpp @@ -5,7 +5,7 @@ #include "BinaryPrivateSemaphore.hpp" // for BPS #include "CountingPrivateSemaphore.hpp" // for CPS -#include "Debug.hpp" // for WDBG +#include "Debug.hpp" // for DBG #include "Fiber.hpp" // for Fiber #include "PrivateSemaphore.hpp" // for PS #include "Runtime.hpp" // for Runtime @@ -46,13 +46,13 @@ static void fib(void* voidParams) { runtime->schedule(*f1); runtime->schedule(*f2); - WDBG("fib: Calling wait for n=" << n); + DBG("fib: Calling wait for n=" << n); newSem.wait(); *result = a + b; } - WDBG("fib: Calling signalAndExit for n=" << n); + DBG("fib: Calling signalAndExit for n=" << n); sem->signalAndExit(); } diff --git a/emper/Debug.cpp b/emper/Debug.cpp index c8cfd09b..08f13c59 100644 --- a/emper/Debug.cpp +++ b/emper/Debug.cpp @@ -52,7 +52,7 @@ void emper_log(const std::string& prefix, const std::string& message) { logMessage << " "; } - logMessage << message; + logMessage << message << std::endl; std::unique_lock<std::mutex> lock(emper_log_mutex); std::cerr << logMessage.str(); diff --git a/emper/Debug.hpp b/emper/Debug.hpp index 768a07e0..06cdd502 100644 --- a/emper/Debug.hpp +++ b/emper/Debug.hpp @@ -18,38 +18,36 @@ // 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_STR_LAMBDA(x, new_line) [&]() -> std::string {std::stringstream sst; sst << x; if constexpr (new_line) { sst << std::endl; }; return sst.str(); } +#define EMPER_BUILD_STR(x) [&]() -> std::string {std::stringstream sst; sst << x; 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) +#define LOG(level, x, log_func) do {if constexpr (level > EMPER_LOG_LEVEL) { break; } log_func(EMPER_BUILD_STR(x)()); } while (false) // NOLINTNEXTLINE(bugprone-macro-parentheses) -#define DBG(x) LOG(Debug, x, emper_log_no_prefix, true); -// NOLINTNEXTLINE(bugprone-macro-parentheses) -#define WDBG(x) LOG(Debug, x, emper_log_no_prefix, false); +#define DBG(x) LOG(Debug, x, emper_log_no_prefix); // To avoid "error: there are no arguments to ‘logD’ that depend on a // template parameter, so a declaration of ‘logD’ must be available" // we use "this->logD()" instead of simply "logD()" below. // NOLINTNEXTLINE(bugprone-macro-parentheses) -#define LOGD(x) LOG(Debug, x, this->logD, true); +#define LOGD(x) LOG(Debug, x, this->logD); // NOLINTNEXTLINE(bugprone-macro-parentheses) -#define LOGDD(x) LOG(FineDebug, x, this->logDD, true); +#define LOGDD(x) LOG(FineDebug, x, this->logDD); // NOLINTNEXTLINE(bugprone-macro-parentheses) -#define LOGI(x) LOG(Info, "Info: " << x, emper_log_no_prefix, true); +#define LOGI(x) LOG(Info, "Info: " << x, emper_log_no_prefix); // NOLINTNEXTLINE(bugprone-macro-parentheses) -#define LOGGER_LOGI(x) LOG(Info, "Info: " << x, this->logI, true); +#define LOGGER_LOGI(x) LOG(Info, "Info: " << x, this->logI); // NOLINTNEXTLINE(bugprone-macro-parentheses) -#define LOGW(x) LOG(Warning, "Warning: " << x, emper_log_no_prefix, true); +#define LOGW(x) LOG(Warning, "Warning: " << x, emper_log_no_prefix); // NOLINTNEXTLINE(bugprone-macro-parentheses) -#define LOGGER_LOGW(x) LOG(Warning, "Warning: " << x, this->logW, true); +#define LOGGER_LOGW(x) LOG(Warning, "Warning: " << x, this->logW); // NOLINTNEXTLINE(bugprone-macro-parentheses) -#define LOGE(x) LOG(Error, "Error: " << x, emper_log_no_prefix, true); +#define LOGE(x) LOG(Error, "Error: " << x, emper_log_no_prefix); // NOLINTNEXTLINE(bugprone-macro-parentheses) -#define LOGGER_LOGE(x) LOG(Error, "Error: " << x, emper_log_no_prefix, true); +#define LOGGER_LOGE(x) LOG(Error, "Error: " << x, emper_log_no_prefix); // NOLINTNEXTLINE(bugprone-macro-parentheses) #define ABORT(x) { LOGE(x); abort(); } diff --git a/tests/RuntimeDestroyTest.cpp b/tests/RuntimeDestroyTest.cpp index 6b1d6308..1617b6e9 100644 --- a/tests/RuntimeDestroyTest.cpp +++ b/tests/RuntimeDestroyTest.cpp @@ -5,7 +5,7 @@ #include "BinaryPrivateSemaphore.hpp" // for BPS #include "CountingPrivateSemaphore.hpp" // for CPS -#include "Debug.hpp" // for WDBG +#include "Debug.hpp" // for DBG #include "Fiber.hpp" // for Fiber #include "PrivateSemaphore.hpp" // for PS #include "Runtime.hpp" // for Runtime @@ -53,13 +53,13 @@ static void fib(void* voidParams) { runtime->schedule(*f1); runtime->schedule(*f2); - WDBG("fib: Calling wait for n=" << n); + DBG("fib: Calling wait for n=" << n); newSem.wait(); *result = a + b; } - WDBG("fib: Calling signalAndExit for n=" << n); + DBG("fib: Calling signalAndExit for n=" << n); sem->signalAndExit(); } diff --git a/tests/SimpleActorTest.cpp b/tests/SimpleActorTest.cpp index e02da94c..c5b284ad 100644 --- a/tests/SimpleActorTest.cpp +++ b/tests/SimpleActorTest.cpp @@ -7,7 +7,7 @@ #include "Actor.hpp" // for Actor #include "CountingPrivateSemaphore.hpp" // for CPS -#include "Debug.hpp" // for WDBG +#include "Debug.hpp" // for DBG #include "Dispatcher.hpp" // for Dispatcher #include "Fiber.hpp" // for Fiber #include "Runtime.hpp" // for Runtime @@ -47,8 +47,8 @@ static void mainFiber(void* runtime_ptr) { for (unsigned int fiberNum = 0; fiberNum < FIBER_COUNT; ++fiberNum) { spawn( [&sumActor, fiberNum] { - WDBG(Dispatcher::getCurrentFiber() - << " (" << fiberNum << ") starts to count to " << FIBERS_COUNT_TO); + DBG(Dispatcher::getCurrentFiber() + << " (" << fiberNum << ") starts to count to " << FIBERS_COUNT_TO); for (uint64_t i = 1; i <= FIBERS_COUNT_TO; ++i) { sumActor.tell(i); } diff --git a/tests/SimplestFibTest.cpp b/tests/SimplestFibTest.cpp index bd0700a3..b0ef2db7 100644 --- a/tests/SimplestFibTest.cpp +++ b/tests/SimplestFibTest.cpp @@ -5,7 +5,7 @@ #include "BinaryPrivateSemaphore.hpp" // for BPS #include "CountingPrivateSemaphore.hpp" // for CPS -#include "Debug.hpp" // for WDBG +#include "Debug.hpp" // for DBG #include "Fiber.hpp" // for Fiber #include "PrivateSemaphore.hpp" // for PS #include "Runtime.hpp" // for Runtime @@ -52,13 +52,13 @@ static void fib(void* voidParams) { runtime->schedule(*f1); runtime->schedule(*f2); - WDBG("fib: Calling wait for n=" << n); + DBG("fib: Calling wait for n=" << n); newSem.wait(); *result = a + b; } - WDBG("fib: Calling signalAndExit for n=" << n); + DBG("fib: Calling signalAndExit for n=" << n); sem->signalAndExit(); } -- GitLab