Skip to content

Commit

Permalink
Fixed the time printing for Connection logs so that the msecs part …
Browse files Browse the repository at this point in the history
…is correctly written.

fixes hazelcast#892
  • Loading branch information
ihsandemir committed Aug 2, 2021
1 parent bc19241 commit 4d1e340
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
6 changes: 3 additions & 3 deletions hazelcast/src/hazelcast/client/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1015,10 +1015,10 @@ namespace hazelcast {
}

std::ostream &operator<<(std::ostream &os, const Connection &connection) {
os << "ClientConnection{"
os << "Connection{"
<< "alive=" << connection.is_alive()
<< ", connectionId=" << connection.get_connection_id()
<< ", remoteEndpoint=";
<< ", connection id=" << connection.get_connection_id()
<< ", remote endpoint=";
if (connection.get_remote_address()) {
os << *connection.get_remote_address();
} else {
Expand Down
11 changes: 9 additions & 2 deletions hazelcast/src/hazelcast/util/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,14 @@ namespace hazelcast {
}

auto systemDuration = duration_cast<system_clock::duration>(t - steady_clock::now());
auto brokenTime = system_clock::to_time_t(system_clock::now() + systemDuration);
auto msecs = duration_cast<milliseconds>(systemDuration).count() % 1000;
auto system_time = system_clock::now() + systemDuration;
if (msecs < 0) {
msecs = 1000 + msecs;
system_time -= std::chrono::seconds(1);
}

auto brokenTime = system_clock::to_time_t(system_time);
struct tm localBrokenTime;
int result = util::localtime(&brokenTime, &localBrokenTime);
assert(!result);
Expand All @@ -445,7 +452,7 @@ namespace hazelcast {
char time_buffer[80];
std::strftime(time_buffer, sizeof(time_buffer), "%Y-%m-%d %H:%M:%S", &localBrokenTime);
oss << time_buffer;
oss << '.' << std::setfill('0') << std::setw(3) << duration_cast<milliseconds>(systemDuration).count() % 1000;
oss << '.' << msecs;

return oss.str();
}
Expand Down
8 changes: 8 additions & 0 deletions hazelcast/test/src/HazelcastTests2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@ namespace hazelcast {
int64_t actualDuration = end - start;
ASSERT_GE(actualDuration, parkDurationNanos);
}

TEST_F (ClientUtilTest, print_older_stead_clock_time) {
auto now = std::chrono::steady_clock::now();
auto time_str = hazelcast::util::StringUtil::time_to_string(now);
ASSERT_EQ(std::string::npos, time_str.substr(time_str.find_last_of('.')).find('-'));
time_str = hazelcast::util::StringUtil::time_to_string(now - std::chrono::milliseconds(1720));
ASSERT_EQ(std::string::npos, time_str.substr(time_str.find_last_of('.')).find('-'));
}
}
}
}
Expand Down

0 comments on commit 4d1e340

Please sign in to comment.