From 65ba70f7c9e9a9fb261a4e71878220adafac488a Mon Sep 17 00:00:00 2001 From: lecaros Date: Fri, 21 Feb 2025 13:38:03 -0300 Subject: [PATCH 1/3] log: increase precision in log's timestamp to nanoseconds Signed-off-by: lecaros --- src/flb_log.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/flb_log.c b/src/flb_log.c index c5c73d40ad0..2273fa52ec1 100644 --- a/src/flb_log.c +++ b/src/flb_log.c @@ -568,13 +568,13 @@ int flb_log_construct(struct log_message *msg, int *ret_len, int ret; int len; int total; - time_t now; const char *header_color = NULL; const char *header_title = NULL; const char *bold_color = ANSI_BOLD; const char *reset_color = ANSI_RESET; struct tm result; struct tm *current; + struct timespec ts; switch (type) { case FLB_LOG_HELP: @@ -620,15 +620,15 @@ int flb_log_construct(struct log_message *msg, int *ret_len, } #endif // FLB_LOG_NO_CONTROL_CHARS - now = time(NULL); - current = localtime_r(&now, &result); + clock_gettime(CLOCK_REALTIME, &ts); + current = localtime_r(&ts.tv_sec, &result); if (current == NULL) { return -1; } len = snprintf(msg->msg, sizeof(msg->msg) - 1, - "%s[%s%i/%02i/%02i %02i:%02i:%02i%s]%s [%s%5s%s] ", + "%s[%s%i/%02i/%02i %02i:%02i:%02i.%03ld%s]%s [%s%5s%s] ", /* time */ /* type */ /* time variables */ @@ -639,6 +639,7 @@ int flb_log_construct(struct log_message *msg, int *ret_len, current->tm_hour, current->tm_min, current->tm_sec, + ts.tv_nsec, bold_color, reset_color, /* type format */ From a23cfdde2990836db8cdc2ec086f83d9cf265f0b Mon Sep 17 00:00:00 2001 From: lecaros Date: Fri, 21 Feb 2025 14:52:35 -0300 Subject: [PATCH 2/3] log: create function get_current_time to include windows support Signed-off-by: lecaros --- src/flb_log.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/flb_log.c b/src/flb_log.c index 2273fa52ec1..7f258722eee 100644 --- a/src/flb_log.c +++ b/src/flb_log.c @@ -561,6 +561,32 @@ struct flb_log *flb_log_create(struct flb_config *config, int type, return log; } +#ifdef _WIN32 +#include "windows.h" + +#define WINDOWS_EPOCH_OFFSET 116444736000000000ULL + +void get_current_time(struct timespec *ts) +{ + FILETIME ft; + ULARGE_INTEGER li; + + GetSystemTimeAsFileTime(&ft); + li.LowPart = ft.dwLowDateTime; + li.HighPart = ft.dwHighDateTime; + + // Convert to Unix epoch + uint64_t time = (li.QuadPart - WINDOWS_EPOCH_OFFSET) / 10; + ts->tv_sec = time / 1000000; + ts->tv_nsec = (time % 1000000) * 1000; +} +#else +void get_current_time(struct timespec *ts) +{ + clock_gettime(CLOCK_REALTIME, ts); +} +#endif + int flb_log_construct(struct log_message *msg, int *ret_len, int type, const char *file, int line, const char *fmt, va_list *args) { @@ -620,7 +646,7 @@ int flb_log_construct(struct log_message *msg, int *ret_len, } #endif // FLB_LOG_NO_CONTROL_CHARS - clock_gettime(CLOCK_REALTIME, &ts); + get_current_time(&ts); current = localtime_r(&ts.tv_sec, &result); if (current == NULL) { From 4d0149b7a865d93206b7bfb00ece27a1f502b5cd Mon Sep 17 00:00:00 2001 From: lecaros Date: Sat, 22 Feb 2025 10:06:25 -0300 Subject: [PATCH 3/3] log: use cfl_time_now to get current time Signed-off-by: lecaros --- src/flb_log.c | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/src/flb_log.c b/src/flb_log.c index 7f258722eee..466089b7d34 100644 --- a/src/flb_log.c +++ b/src/flb_log.c @@ -32,6 +32,7 @@ #include #include #include +#include "cfl/cfl_time.h" #ifdef FLB_HAVE_AWS_ERROR_REPORTER #include @@ -41,6 +42,8 @@ extern struct flb_aws_error_reporter *error_reporter; FLB_TLS_DEFINE(struct flb_log, flb_log_ctx) +#define NANOSECONDS_IN_SECOND 1000000000 + /* Simple structure to dispatch messages to the log collector */ struct log_message { size_t size; @@ -561,31 +564,12 @@ struct flb_log *flb_log_create(struct flb_config *config, int type, return log; } -#ifdef _WIN32 -#include "windows.h" - -#define WINDOWS_EPOCH_OFFSET 116444736000000000ULL - void get_current_time(struct timespec *ts) { - FILETIME ft; - ULARGE_INTEGER li; - - GetSystemTimeAsFileTime(&ft); - li.LowPart = ft.dwLowDateTime; - li.HighPart = ft.dwHighDateTime; - - // Convert to Unix epoch - uint64_t time = (li.QuadPart - WINDOWS_EPOCH_OFFSET) / 10; - ts->tv_sec = time / 1000000; - ts->tv_nsec = (time % 1000000) * 1000; + uint64_t now = cfl_time_now(); + ts->tv_sec = now / NANOSECONDS_IN_SECOND; + ts->tv_nsec = now % NANOSECONDS_IN_SECOND; } -#else -void get_current_time(struct timespec *ts) -{ - clock_gettime(CLOCK_REALTIME, ts); -} -#endif int flb_log_construct(struct log_message *msg, int *ret_len, int type, const char *file, int line, const char *fmt, va_list *args)