Skip to content

Commit c34de5a

Browse files
committed
log: increase precision in log's timestamp to nanoseconds
Signed-off-by: lecaros <[email protected]>
1 parent a28a631 commit c34de5a

File tree

1 file changed

+6
-27
lines changed

1 file changed

+6
-27
lines changed

src/flb_log.c

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <fluent-bit/flb_config.h>
3333
#include <fluent-bit/flb_worker.h>
3434
#include <fluent-bit/flb_mem.h>
35+
#include <fluent-bit/flb_time.h>
3536
#include <cmetrics/cmetrics.h>
3637
#include <cmetrics/cmt_counter.h>
3738

@@ -673,13 +674,13 @@ int flb_log_construct(struct log_message *msg, int *ret_len,
673674
int ret;
674675
int len;
675676
int total;
676-
time_t now;
677677
const char *header_color = NULL;
678678
const char *header_title;
679679
const char *bold_color = ANSI_BOLD;
680680
const char *reset_color = ANSI_RESET;
681681
struct tm result;
682682
struct tm *current;
683+
struct flb_time now;
683684

684685
switch (type) {
685686
case FLB_LOG_HELP:
@@ -718,16 +719,16 @@ int flb_log_construct(struct log_message *msg, int *ret_len,
718719
}
719720
#endif // FLB_LOG_NO_CONTROL_CHARS
720721

721-
now = time(NULL);
722-
current = localtime_r(&now, &result);
722+
flb_time_get(&now);
723+
current = localtime_r(&now.tm.tv_sec, &result);
723724

724725
if (current == NULL) {
725726
return -1;
726727
}
727728

728729
header_title = flb_log_message_type_str(type);
729730
len = snprintf(msg->msg, sizeof(msg->msg) - 1,
730-
"%s[%s%i/%02i/%02i %02i:%02i:%02i%s]%s [%s%5s%s] ",
731+
"%s[%s%i/%02i/%02i %02i:%02i:%02i.%03ld%s]%s [%s%5s%s] ",
731732
/* time */ /* type */
732733

733734
/* time variables */
@@ -738,6 +739,7 @@ int flb_log_construct(struct log_message *msg, int *ret_len,
738739
current->tm_hour,
739740
current->tm_min,
740741
current->tm_sec,
742+
now.tm.tv_nsec,
741743
bold_color, reset_color,
742744

743745
/* type format */
@@ -800,11 +802,8 @@ void flb_log_print(int type, const char *file, int line, const char *fmt, ...)
800802
int ret;
801803
struct log_message msg = {0};
802804
va_list args;
803-
const char *msg_type_str;
804-
uint64_t ts;
805805

806806
struct flb_worker *w;
807-
struct flb_config *config;
808807

809808
va_start(args, fmt);
810809
ret = flb_log_construct(&msg, &len, type, file, line, fmt, &args);
@@ -816,25 +815,6 @@ void flb_log_print(int type, const char *file, int line, const char *fmt, ...)
816815

817816
w = flb_worker_get();
818817
if (w) {
819-
config = w->config;
820-
if (config != NULL && config->log != NULL) {
821-
msg_type_str = flb_log_message_type_str(type);
822-
if (msg_type_str == NULL) {
823-
msg_type_str = "unknown";
824-
}
825-
826-
ts = cfl_time_now();
827-
ret = cmt_counter_inc(config->log->metrics->logs_total_counter,
828-
ts,
829-
1, (char *[]) {msg_type_str});
830-
if (ret == -1) {
831-
/* Not using flb_log_debug to avoid recursing into this same function. */
832-
fprintf(stderr,
833-
"[log] failed to increment log total counter for message type '%s' (error=%d)\n",
834-
msg_type_str, ret);
835-
}
836-
}
837-
838818
n = flb_pipe_write_all(w->log[1], &msg, sizeof(msg));
839819

840820
if (n == -1) {
@@ -894,7 +874,6 @@ int flb_log_destroy(struct flb_log *log, struct flb_config *config)
894874
}
895875
flb_log_worker_destroy(log->worker);
896876
flb_free(log->worker);
897-
flb_log_metrics_destroy(log->metrics);
898877
flb_free(log);
899878

900879
return 0;

0 commit comments

Comments
 (0)