From a1426de5aaaef17f310fe3ccf9e062f594fef88a Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Tue, 27 Aug 2019 14:21:02 +0200 Subject: [PATCH] MINOR: sink: now call the generic fd write function Let's not mess up with fd-specific code, locking nor message formating here, and use the new generic function instead. This substantially simplifies the sink_write() code and makes it more agnostic to the output representation and storage. --- src/sink.c | 40 +++++++--------------------------------- 1 file changed, 7 insertions(+), 33 deletions(-) diff --git a/src/sink.c b/src/sink.c index 214b272ef..18b5c07b5 100644 --- a/src/sink.c +++ b/src/sink.c @@ -18,7 +18,6 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include #include #include #include @@ -108,48 +107,23 @@ struct sink *sink_new_fd(const char *name, const char *desc, enum sink_fmt fmt, */ void sink_write(struct sink *sink, const struct ist msg[], size_t nmsg) { - struct iovec iovec[10]; char short_hdr[4]; - size_t maxlen = sink->maxlen ? sink->maxlen : ~0; + struct ist pfx[4]; + size_t npfx = 0; size_t sent = 0; - int vec = 0; - - /* keep one char for a possible trailing '\n' in any case */ - maxlen--; if (sink->fmt == SINK_FMT_SHORT) { short_hdr[0] = '<'; short_hdr[1] = '0' + sink->syslog_minlvl; short_hdr[2] = '>'; - iovec[vec].iov_base = short_hdr; - iovec[vec].iov_len = MIN(maxlen, 3); - maxlen -= iovec[vec].iov_len; - vec++; - } - - /* copy the remaining entries from the original message. Skip empty fields and - * truncate the whole message to maxlen. - */ - while (nmsg && vec < (sizeof(iovec) / sizeof(iovec[0]) - 1)) { - iovec[vec].iov_base = msg->ptr; - iovec[vec].iov_len = MIN(maxlen, msg->len); - maxlen -= iovec[vec].iov_len; - if (iovec[vec].iov_len) - vec++; - msg++; nmsg--; - } + pfx[npfx].ptr = short_hdr; + pfx[npfx].len = 3; + npfx++; + } if (sink->type == SINK_TYPE_FD) { - /* For the FD we always emit the trailing \n. It was already provisioned above. */ - iovec[vec].iov_base = "\n"; - iovec[vec].iov_len = 1; - vec++; - - HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &sink->ctx.lock); - sent = writev(sink->ctx.fd, iovec, vec); - HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &sink->ctx.lock); - /* sent > 0 if the message was delivered */ + sent = fd_write_frag_line(sink->ctx.fd, sink->maxlen, pfx, npfx, msg, nmsg, 1); } /* account for errors now */