MINOR: log: add time second fraction field to rfc5424 log timestamp.

This patch adds the time second fraction in microseconds
as supported by the rfc.
This commit is contained in:
Emeric Brun 2020-07-02 16:16:59 +02:00 committed by Willy Tarreau
parent 4f58926352
commit 9f9b22c4f1
3 changed files with 11 additions and 5 deletions

View File

@ -137,7 +137,7 @@ char *lf_port(char *dst, const struct sockaddr *sockaddr, size_t size, const str
/* /*
* Function to handle log header building (exported for sinks) * Function to handle log header building (exported for sinks)
*/ */
char *update_log_hdr_rfc5424(const time_t time); char *update_log_hdr_rfc5424(const time_t time, suseconds_t frac);
char *update_log_hdr(const time_t time); char *update_log_hdr(const time_t time);
char * get_format_pid_sep1(int format, size_t *len); char * get_format_pid_sep1(int format, size_t *len);
char * get_format_pid_sep2(int format, size_t *len); char * get_format_pid_sep2(int format, size_t *len);

View File

@ -1483,10 +1483,11 @@ char *update_log_hdr(const time_t time)
* the beginning of logheader_rfc5424 once a second and return the pointer * the beginning of logheader_rfc5424 once a second and return the pointer
* to the first character after it. * to the first character after it.
*/ */
char *update_log_hdr_rfc5424(const time_t time) char *update_log_hdr_rfc5424(const time_t time, const suseconds_t frac)
{ {
static THREAD_LOCAL long tvsec; static THREAD_LOCAL long tvsec;
const char *gmt_offset; const char *gmt_offset;
char c;
if (unlikely(time != tvsec || logheader_rfc5424_end == NULL)) { if (unlikely(time != tvsec || logheader_rfc5424_end == NULL)) {
/* this string is rebuild only once a second */ /* this string is rebuild only once a second */
@ -1498,7 +1499,7 @@ char *update_log_hdr_rfc5424(const time_t time)
gmt_offset = get_gmt_offset(time, &tm); gmt_offset = get_gmt_offset(time, &tm);
hdr_len = snprintf(logheader_rfc5424, global.max_syslog_len, hdr_len = snprintf(logheader_rfc5424, global.max_syslog_len,
"<<<<>1 %4d-%02d-%02dT%02d:%02d:%02d%.3s:%.2s %s ", "<<<<>1 %4d-%02d-%02dT%02d:%02d:%02d.000000%.3s:%.2s %s ",
tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_hour, tm.tm_min, tm.tm_sec,
gmt_offset, gmt_offset+3, gmt_offset, gmt_offset+3,
@ -1513,6 +1514,11 @@ char *update_log_hdr_rfc5424(const time_t time)
logheader_rfc5424_end = logheader_rfc5424 + hdr_len; logheader_rfc5424_end = logheader_rfc5424 + hdr_len;
} }
/* utoa_pad add a trailing '\0' so we save the char to restore */
c = logheader_rfc5424[33];
utoa_pad(frac, logheader_rfc5424 + 27, 7);
logheader_rfc5424[33] = c;
logheader_rfc5424_end[0] = 0; // ensure we get rid of any previous attempt logheader_rfc5424_end[0] = 0; // ensure we get rid of any previous attempt
return logheader_rfc5424_end; return logheader_rfc5424_end;
@ -1629,7 +1635,7 @@ static inline void __do_send_log(struct logsrv *logsrv, int nblogger, char *pid_
case LOG_FORMAT_RFC5424: case LOG_FORMAT_RFC5424:
hdr = logheader_rfc5424; hdr = logheader_rfc5424;
hdr_ptr = update_log_hdr_rfc5424(time); hdr_ptr = update_log_hdr_rfc5424(time, date.tv_usec);
sd_max = sd_size; /* the SD part allowed only in RFC5424 */ sd_max = sd_size; /* the SD part allowed only in RFC5424 */
break; break;

View File

@ -182,7 +182,7 @@ ssize_t __sink_write(struct sink *sink, const struct ist msg[], size_t nmsg,
} }
else if (sink->fmt == SINK_FMT_RFC5424) { else if (sink->fmt == SINK_FMT_RFC5424) {
pfx[npfx].ptr = logheader_rfc5424; pfx[npfx].ptr = logheader_rfc5424;
pfx[npfx].len = update_log_hdr_rfc5424(date.tv_sec) - pfx[npfx].ptr; pfx[npfx].len = update_log_hdr_rfc5424(date.tv_sec, date.tv_usec) - pfx[npfx].ptr;
log_format = LOG_FORMAT_RFC5424; log_format = LOG_FORMAT_RFC5424;
} }
else { else {