Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion hazelcast/include/hazelcast/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

#include <hazelcast/util/hazelcast_dll.h>

#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 )) { \
Expand Down Expand Up @@ -67,5 +72,8 @@ enum class logger::level : int {

HAZELCAST_API std::ostream& operator<<(std::ostream&, logger::level level);

} // namespace hazelcast

} // namespace hazelcast
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
#pragma warning(pop)
#endif
24 changes: 21 additions & 3 deletions hazelcast/src/hazelcast/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <thread>

#include "hazelcast/logger.h"
#include "hazelcast/client/hazelcast_client.h"

namespace hazelcast {

Expand Down Expand Up @@ -53,6 +54,22 @@ void logger::log(const char *file_name, int line,
handler_(instance_name_, cluster_name_, file_name, line, lvl, msg);
}

namespace {

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we just utilize utilities from boost and not have an extra method? did you check boost? e.g. https://stackoverflow.com/questions/2492775/get-local-time-with-boost The boost::posix_time::second_clock::local_time() uses localtime_r

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We decided to do this later before 4.0 release. An issue will be opened for this.

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(&lt, &t);
#else
::localtime_r(&t, &lt);
#endif

return lt;
}

}

void logger::default_handler(const std::string &instance_name,
const std::string &cluster_name,
const char* file_name,
Expand All @@ -62,19 +79,20 @@ 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<std::chrono::seconds>(dur);

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

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';
Expand Down
44 changes: 44 additions & 0 deletions hazelcast/test/src/HazelcastTests2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
}
Expand Down