core: Make logger thread-safe
Change-Id: Ic45dc0912da9c2e3f074d3f74921bfe33741bf63
Refs: #2489
diff --git a/core/logger.hpp b/core/logger.hpp
index 6d01fa0..54af7cb 100644
--- a/core/logger.hpp
+++ b/core/logger.hpp
@@ -32,6 +32,8 @@
#include "custom-logger.hpp"
#else
+#include <mutex>
+
namespace nfd {
/** \brief indicates a log level
@@ -128,12 +130,15 @@
template<> \
nfd::Logger& cls<s1, s2>::g_logger = nfd::LoggerFactory::create(name)
+extern std::mutex g_logMutex;
#define NFD_LOG(level, msg, expression) \
do { \
- if (g_logger.isEnabled(::nfd::LOG_##level)) \
+ if (g_logger.isEnabled(::nfd::LOG_##level)) { \
+ std::lock_guard<std::mutex> lock(::nfd::g_logMutex); \
std::clog << ::nfd::Logger::now() << " "#msg": " \
<< "[" << g_logger << "] " << expression << "\n"; \
+ } \
} while (false)
#define NFD_LOG_TRACE(expression) NFD_LOG(TRACE, TRACE, expression)