diff --git a/hazelcast/include/hazelcast/logger.h b/hazelcast/include/hazelcast/logger.h index 8cde442abb..133659e07c 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 diff --git a/hazelcast/src/hazelcast/logger.cpp b/hazelcast/src/hazelcast/logger.cpp index 5eee34d7d6..c5673334cb 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 { @@ -53,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, @@ -62,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); @@ -70,11 +88,11 @@ 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 << "] [" - << 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()); + } } } }