From 653d3f9a8f3c58e6f4ff66da863c47cd0e2f6ff3 Mon Sep 17 00:00:00 2001 From: Yunus Emre Inci Date: Fri, 13 Nov 2020 14:32:22 +0300 Subject: [PATCH 1/4] Use version API in logger impl, Implement tests for logger configuration --- hazelcast/src/hazelcast/logger.cpp | 3 +- hazelcast/test/src/HazelcastTests2.cpp | 44 ++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/hazelcast/src/hazelcast/logger.cpp b/hazelcast/src/hazelcast/logger.cpp index 5eee34d7d6..b398b73fde 100644 --- a/hazelcast/src/hazelcast/logger.cpp +++ b/hazelcast/src/hazelcast/logger.cpp @@ -7,6 +7,7 @@ #include #include "hazelcast/logger.h" +#include "hazelcast/client/hazelcast_client.h" namespace hazelcast { @@ -74,7 +75,7 @@ void logger::default_handler(const std::string &instance_name, << std::setfill('0') << std::setw(3) << ms << ' ' << lvl << ": [" << std::this_thread::get_id() << "] " << instance_name << '[' << cluster_name << "] [" - << HAZELCAST_VERSION << "] [" // TODO once we have an API for the library version, use that instead + << client::version() << "] [" << file_name << ':' << line << "] " << msg << '\n'; diff --git a/hazelcast/test/src/HazelcastTests2.cpp b/hazelcast/test/src/HazelcastTests2.cpp index 9b306c6b52..dfeddff016 100644 --- a/hazelcast/test/src/HazelcastTests2.cpp +++ b/hazelcast/test/src/HazelcastTests2.cpp @@ -508,6 +508,50 @@ namespace hazelcast { ASSERT_EQ(name, user_pass_credential->name()); ASSERT_EQ(password, user_pass_credential->password()); } + + TEST_F(ClientConfigTest, testSetNullLogHandlerThrows) { + client_config config; + auto logger_config = config.get_logger_config(); + + ASSERT_THROW(config.get_logger_config().handler(nullptr), exception::illegal_argument); + } + + TEST_F(ClientConfigTest, testSetGetLoadHandler) { + client_config config; + auto logger_config = config.get_logger_config(); + + bool called = false; + + auto h = [&called](const std::string &, + const std::string &, + const char*, + int, + logger::level, + const std::string &) { + called = true; + }; + + logger_config.handler(h); + logger_config.handler()("", "", "", 0, logger::level::fine, ""); + + ASSERT_TRUE(called); + } + + TEST_F(ClientConfigTest, testDefaultLogLevel) { + client_config config; + auto logger_config = config.get_logger_config(); + + ASSERT_EQ(logger::level::info, logger_config.level()); + } + + TEST_F(ClientConfigTest, testSetGetLogLevel) { + client_config config; + auto logger_config = config.get_logger_config(); + + logger_config.level(logger::level::fine); + + ASSERT_EQ(logger::level::fine, logger_config.level()); + } } } } From 344c657323cbea8afac40d89ec746e0a549353b3 Mon Sep 17 00:00:00 2001 From: Yunus Emre Inci Date: Fri, 13 Nov 2020 14:41:07 +0300 Subject: [PATCH 2/4] Suppress W4251 in logger.h --- hazelcast/include/hazelcast/logger.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/hazelcast/include/hazelcast/logger.h b/hazelcast/include/hazelcast/logger.h index 8cde442abb..70c3fd622d 100644 --- a/hazelcast/include/hazelcast/logger.h +++ b/hazelcast/include/hazelcast/logger.h @@ -6,6 +6,11 @@ #include +#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) +#pragma warning(push) +#pragma warning(disable: 4251) //for dll export +#endif + #ifndef HZ_LOGGING_DISABLED #define HZ_LOG(lg, lvl, msg) \ if ((lg).enabled( logger::level::lvl )) { \ @@ -67,5 +72,8 @@ enum class logger::level : int { HAZELCAST_API std::ostream& operator<<(std::ostream&, logger::level level); +} // namespace hazelcast -} // namespace hazelcast \ No newline at end of file +#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) +#pragma warning(pop) +#endif \ No newline at end of file From 4e379f13b6510003f61f65fe68dc6c84043a834e Mon Sep 17 00:00:00 2001 From: Yunus Emre Inci Date: Fri, 13 Nov 2020 15:09:25 +0300 Subject: [PATCH 3/4] Use localtime_s or localtime_r instead of unsafe localtime --- hazelcast/src/hazelcast/logger.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/hazelcast/src/hazelcast/logger.cpp b/hazelcast/src/hazelcast/logger.cpp index b398b73fde..c5673334cb 100644 --- a/hazelcast/src/hazelcast/logger.cpp +++ b/hazelcast/src/hazelcast/logger.cpp @@ -54,6 +54,22 @@ void logger::log(const char *file_name, int line, handler_(instance_name_, cluster_name_, file_name, line, lvl, msg); } +namespace { + +std::tm time_t_to_localtime(const std::time_t &t) { + std::tm lt; + +#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) + ::localtime_s(<, &t); +#else + ::localtime_r(&t, <); +#endif + + return lt; +} + +} + void logger::default_handler(const std::string &instance_name, const std::string &cluster_name, const char* file_name, @@ -63,7 +79,8 @@ void logger::default_handler(const std::string &instance_name, 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(dur); @@ -71,7 +88,7 @@ void logger::default_handler(const std::string &instance_name, std::ostringstream sstrm; - sstrm << std::put_time(std::localtime(&t), "%d/%m/%Y %H:%M:%S.") + sstrm << std::put_time(&local_t, "%d/%m/%Y %H:%M:%S.") << std::setfill('0') << std::setw(3) << ms << ' ' << lvl << ": [" << std::this_thread::get_id() << "] " << instance_name << '[' << cluster_name << "] [" From 0ea4cae27d9b5c8e6e5efbc15469f0b2f0d31274 Mon Sep 17 00:00:00 2001 From: Yunus Emre Inci Date: Fri, 13 Nov 2020 17:47:53 +0300 Subject: [PATCH 4/4] Fix format --- hazelcast/include/hazelcast/logger.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hazelcast/include/hazelcast/logger.h b/hazelcast/include/hazelcast/logger.h index 70c3fd622d..133659e07c 100644 --- a/hazelcast/include/hazelcast/logger.h +++ b/hazelcast/include/hazelcast/logger.h @@ -76,4 +76,4 @@ HAZELCAST_API std::ostream& operator<<(std::ostream&, logger::level level); #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) #pragma warning(pop) -#endif \ No newline at end of file +#endif