Skip to content

Commit

Permalink
Make sure that the logger static lock mutex is not destroyed before a…
Browse files Browse the repository at this point in the history
…ny global client instance (#977) (#991)

* 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).
  • Loading branch information
ihsandemir authored Jun 20, 2022
1 parent 6aac44d commit 5978dfa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
2 changes: 2 additions & 0 deletions hazelcast/include/hazelcast/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <string>
#include <functional>
#include <limits>
#include <mutex>

#include "hazelcast/util/export.h"

Expand Down Expand Up @@ -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 {
Expand Down
22 changes: 12 additions & 10 deletions hazelcast/src/hazelcast/logger.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <iomanip>
#include <iostream>
#include <mutex>
#include <sstream>
#include <chrono>
#include <ctime>
Expand Down Expand Up @@ -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 {
Expand All @@ -69,18 +68,22 @@ 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);
auto local_t = time_t_to_localtime(t);

auto dur = tp.time_since_epoch();
auto sec = std::chrono::duration_cast<std::chrono::seconds>(dur);

auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(dur - sec).count();

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

{
static std::mutex cout_lock;
std::lock_guard<std::mutex> g(cout_lock);
std::lock_guard<std::mutex> g(cout_lock_);
std::cout << sstrm.str() << std::flush;
}
}
}


} // namespace hazelcast

0 comments on commit 5978dfa

Please sign in to comment.