Skip to content

Commit

Permalink
[irods#95] Use std::chrono for timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
SwooshyCueb committed Nov 7, 2022
1 parent a59af5d commit 9619976
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions libirods_rule_engine_plugin-audit_amqp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@
#include <fmt/compile.h>

// stl includes
#include <version>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <chrono>
#include <ctime>
#include <map>
#include <fstream>
#include <mutex>
Expand All @@ -60,6 +60,15 @@ namespace
std::mutex audit_plugin_mutex;
// NOLINTEND(cert-err58-cpp, cppcoreguidelines-avoid-non-const-global-variables)

#if __cpp_lib_chrono >= 201907
// we use millisecond precision in our timestamps, so we want to use a clock
// that does not implement leap seconds as repeated non-leap seconds, if we can.
using ts_clock = std::chrono::utc_clock;
#else
// fallback to system_clock
using ts_clock = std::chrono::system_clock;
#endif

// See qpid-cpp docs
// https://qpid.apache.org/releases/qpid-proton-0.36.0/proton/cpp/api/simple_send_8cpp-example.html
class send_handler : public proton::messaging_handler
Expand Down Expand Up @@ -210,10 +219,6 @@ namespace

nlohmann::json json_obj;

struct timeval tv;
gettimeofday(&tv, NULL);
unsigned long time_ms = tv.tv_sec * 1000 + tv.tv_usec / 1000;

char host_name[MAX_NAME_LEN];
gethostname(host_name, MAX_NAME_LEN);

Expand All @@ -223,6 +228,7 @@ namespace
std::string log_file;

try {
auto time_ms = ts_clock::now().time_since_epoch() / std::chrono::milliseconds(1);
json_obj["time_stamp"] = std::to_string(time_ms);
json_obj["hostname"] = host_name;
json_obj["pid"] = std::to_string(pid);
Expand Down Expand Up @@ -273,9 +279,7 @@ namespace
std::string log_file;

try {
struct timeval tv;
gettimeofday(&tv, NULL);
unsigned long time_ms = tv.tv_sec * 1000 + tv.tv_usec / 1000;
auto time_ms = ts_clock::now().time_since_epoch() / std::chrono::milliseconds(1);
json_obj["time_stamp"] = std::to_string(time_ms);

char host_name[MAX_NAME_LEN];
Expand Down Expand Up @@ -365,9 +369,7 @@ namespace
std::string log_file;

try {
struct timeval tv;
gettimeofday(&tv, NULL);
unsigned long time_ms = tv.tv_sec * 1000 + tv.tv_usec / 1000;
auto time_ms = ts_clock::now().time_since_epoch() / std::chrono::milliseconds(1);
json_obj["time_stamp"] = std::to_string(time_ms);

char host_name[MAX_NAME_LEN];
Expand Down

0 comments on commit 9619976

Please sign in to comment.