diff --git a/hazelcast/include/hazelcast/logger.h b/hazelcast/include/hazelcast/logger.h index 75a2fb36d9..ffea4c8ea6 100644 --- a/hazelcast/include/hazelcast/logger.h +++ b/hazelcast/include/hazelcast/logger.h @@ -3,6 +3,7 @@ #include #include #include +#include #include "hazelcast/util/export.h" @@ -52,6 +53,7 @@ class HAZELCAST_API logger { const std::string cluster_name_; const level level_; const handler_type handler_; + static std::mutex cout_lock_; }; enum class logger::level : int { diff --git a/hazelcast/src/hazelcast/logger.cpp b/hazelcast/src/hazelcast/logger.cpp index 943ee687c9..7465743d53 100644 --- a/hazelcast/src/hazelcast/logger.cpp +++ b/hazelcast/src/hazelcast/logger.cpp @@ -1,6 +1,5 @@ #include #include -#include #include #include #include @@ -42,7 +41,7 @@ logger::logger(std::string instance_name, std::string cluster_name, level level, : instance_name_{ std::move(instance_name) } , cluster_name_{ std::move(cluster_name) } , level_{ level } - , handler_{ std::move(handler) } + , handler_{ std::move(handler) } {} bool logger::enabled(level lvl) noexcept { @@ -69,10 +68,14 @@ std::tm time_t_to_localtime(const std::time_t &t) { } -void logger::default_handler(const std::string &instance_name, - const std::string &cluster_name, - level lvl, - const std::string &msg) noexcept { +std::mutex logger::cout_lock_; + +void +logger::default_handler(const std::string& instance_name, + const std::string& cluster_name, + level lvl, + const std::string& msg) noexcept +{ auto tp = std::chrono::system_clock::now(); auto t = std::chrono::system_clock::to_time_t(tp); @@ -80,7 +83,7 @@ void logger::default_handler(const std::string &instance_name, auto dur = tp.time_since_epoch(); auto sec = std::chrono::duration_cast(dur); - + auto ms = std::chrono::duration_cast(dur - sec).count(); std::ostringstream sstrm; @@ -96,11 +99,10 @@ void logger::default_handler(const std::string &instance_name, << '\n'; { - static std::mutex cout_lock; - std::lock_guard g(cout_lock); + std::lock_guard g(cout_lock_); std::cout << sstrm.str() << std::flush; } -} +} } // namespace hazelcast