Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

log: increase precision in log's timestamp to nanoseconds #9986

Draft
wants to merge 2 commits into
base: 3.2
Choose a base branch
from
Draft
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
35 changes: 31 additions & 4 deletions src/flb_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -561,20 +561,46 @@ 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)
{
int body_size;
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:
Expand Down Expand Up @@ -620,15 +646,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);
get_current_time(&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 */
Expand All @@ -639,6 +665,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 */
Expand Down
Loading