Skip to content

Commit 61c2f92

Browse files
authored
Make sure that the logger static lock mutex is not destroyed before any global client instance (#977) (#992)
* Changed the logger lock mutex so that we can control its destruction order properly. We do not want it destroyed before the client is destructed. We need to control the order. static initialization and destruction order is not controllable if it is not a member of the logger class. It can be destroyed before the any global client istance is destroyed since global variables are also in static duration (https://www.learncpp.com/cpp-tutorial/introduction-to-global-variables).
1 parent e38d157 commit 61c2f92

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

hazelcast/include/hazelcast/logger.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <string>
44
#include <functional>
55
#include <limits>
6+
#include <mutex>
67

78
#include "hazelcast/util/export.h"
89

@@ -52,6 +53,7 @@ class HAZELCAST_API logger {
5253
const std::string cluster_name_;
5354
const level level_;
5455
const handler_type handler_;
56+
static std::mutex cout_lock_;
5557
};
5658

5759
enum class logger::level : int {

hazelcast/src/hazelcast/logger.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include <iomanip>
22
#include <iostream>
3-
#include <mutex>
43
#include <sstream>
54
#include <chrono>
65
#include <ctime>
@@ -42,7 +41,7 @@ logger::logger(std::string instance_name, std::string cluster_name, level level,
4241
: instance_name_{ std::move(instance_name) }
4342
, cluster_name_{ std::move(cluster_name) }
4443
, level_{ level }
45-
, handler_{ std::move(handler) }
44+
, handler_{ std::move(handler) }
4645
{}
4746

4847
bool logger::enabled(level lvl) noexcept {
@@ -69,18 +68,22 @@ std::tm time_t_to_localtime(const std::time_t &t) {
6968

7069
}
7170

72-
void logger::default_handler(const std::string &instance_name,
73-
const std::string &cluster_name,
74-
level lvl,
75-
const std::string &msg) noexcept {
71+
std::mutex logger::cout_lock_;
72+
73+
void
74+
logger::default_handler(const std::string& instance_name,
75+
const std::string& cluster_name,
76+
level lvl,
77+
const std::string& msg) noexcept
78+
{
7679

7780
auto tp = std::chrono::system_clock::now();
7881
auto t = std::chrono::system_clock::to_time_t(tp);
7982
auto local_t = time_t_to_localtime(t);
8083

8184
auto dur = tp.time_since_epoch();
8285
auto sec = std::chrono::duration_cast<std::chrono::seconds>(dur);
83-
86+
8487
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(dur - sec).count();
8588

8689
std::ostringstream sstrm;
@@ -96,11 +99,10 @@ void logger::default_handler(const std::string &instance_name,
9699
<< '\n';
97100

98101
{
99-
static std::mutex cout_lock;
100-
std::lock_guard<std::mutex> g(cout_lock);
102+
std::lock_guard<std::mutex> g(cout_lock_);
101103
std::cout << sstrm.str() << std::flush;
102104
}
103-
}
105+
}
104106

105107

106108
} // namespace hazelcast

0 commit comments

Comments
 (0)