MEDIUM: sink: inherit from caller fmt in ring_write() when rings didn't set one

implicit rings were automatically forced to the parent logger format, but
this was done upon ring creation.

This is quite restrictive because we might want to choose the desired
format right before generating the log header (ie: when producing the
log message), depending on the logger (log directive) that is
responsible for the log message, and with current logic this is not
possible. (To this day, we still have dedicated implicit ring per log
directive, but this might change)

In ring_write(), we check if the sink->fmt is specified:
 - defined: we use it since it is the most precise format
   (ie: for named rings)
 - undefined: then we fallback to the format from the logger

With this change, implicit rings' format is now set to UNSPEC upon
creation. This is safe because the log header building function
automatically enforces the "raw" format when UNSPEC is set. And since
logger->format also defaults to "raw", no change of default behavior
should be expected.
This commit is contained in:
Aurelien DARRAGON 2023-08-25 09:50:27 +02:00 committed by Christopher Faulet
parent 6dad0549a5
commit 5c0d1c1a74
2 changed files with 18 additions and 9 deletions

View File

@ -38,7 +38,8 @@ int sink_announce_dropped(struct sink *sink, struct log_header hdr);
/* tries to send <nmsg> message parts from message array <msg> to sink <sink>.
* Formatting according to the sink's preferences is done here.
* Formatting according to the sink's preference is done here, unless sink->fmt
* is unspecified, in which case the caller formatting will be used instead.
*
* It will stop writing at <maxlen> instead of sink->maxlen if <maxlen> is
* positive and inferior to sink->maxlen.

View File

@ -165,9 +165,11 @@ struct sink *sink_new_buf(const char *name, const char *desc, enum log_fmt fmt,
}
/* tries to send <nmsg> message parts from message array <msg> to sink <sink>.
* Formatting according to the sink's preference is done here. Lost messages
* are NOT accounted for. It is preferable to call sink_write() instead which
* will also try to emit the number of dropped messages when there are any.
* Formatting according to the sink's preference is done here, unless sink->fmt
* is unspecified, in which case the caller formatting will be used instead.
* Lost messages are NOT accounted for. It is preferable to call sink_write()
* instead which will also try to emit the number of dropped messages when there
* are any.
*
* It will stop writing at <maxlen> instead of sink->maxlen if <maxlen> is
* positive and inferior to sink->maxlen.
@ -183,6 +185,7 @@ struct sink *sink_new_buf(const char *name, const char *desc, enum log_fmt fmt,
if (sink->fmt == LOG_FORMAT_RAW)
goto send;
if (sink->fmt != LOG_FORMAT_UNSPEC)
hdr.format = sink->fmt; /* sink format prevails over log one */
pfx = build_log_header(hdr, &npfx);
@ -1191,11 +1194,16 @@ struct sink *sink_new_from_logger(struct logger *logger)
goto error;
}
/* disable sink->maxlen, we already have logger->maxlen */
sink->maxlen = 0;
/* ring format normally defaults to RAW, but here we set ring format
* to UNSPEC to inherit from caller format in sink_write() since we
* cannot customize implicit ring settings
*/
sink->fmt = LOG_FORMAT_UNSPEC;
/* set ring format from logger format */
sink->fmt = logger->format;
/* for the same reason, we disable sink->maxlen to inherit from caller
* maxlen in sink_write()
*/
sink->maxlen = 0;
/* Set default connect and server timeout for sink forward proxy */
sink->forward_px->timeout.connect = MS_TO_TICKS(1000);