]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: logs: buffer targets now rely on new sink_write
authorEmeric Brun <ebrun@haproxy.com>
Wed, 6 May 2020 15:23:59 +0000 (17:23 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 19 May 2020 09:04:11 +0000 (11:04 +0200)
Before this path, they rely directly on ring_write bypassing
a part of the sink API.

Now the maxlen parameter of the log will apply only on the text
message part (and not the header, for this you woud prefer
to use the maxlen parameter on the sink/ring).

sink_write prototype was also reviewed to return the number of Bytes
written to be compliant with the other write functions.

include/proto/sink.h
include/types/log.h
src/log.c

index bab968140796543584ff55c51855f56043a68c3f..d30f30a742791ed8aca034ac69e0542282bf5f51 100644 (file)
@@ -39,8 +39,10 @@ int sink_announce_dropped(struct sink *sink, int facility, struct ist *pid);
  * array <msg> to sink <sink>. Formatting according to the sink's preference is
  * done here. Lost messages are accounted for in the sink's counter. If there
  * were lost messages, an attempt is first made to indicate it.
+ * The function returns the number of Bytes effectively sent or announced.
+ * or <= 0 in other cases.
  */
-static inline void sink_write(struct sink *sink, const struct ist msg[], size_t nmsg,
+static inline ssize_t sink_write(struct sink *sink, const struct ist msg[], size_t nmsg,
                               int level, int facility, struct ist * tag,
                               struct ist *pid, struct ist *sd)
 {
@@ -72,6 +74,8 @@ static inline void sink_write(struct sink *sink, const struct ist msg[], size_t
  fail:
        if (unlikely(sent <= 0))
                HA_ATOMIC_ADD(&sink->ctx.dropped, 1);
+
+       return sent;
 }
 
 #endif /* _PROTO_SINK_H */
index 868c08f01d294bcb0b82303ef9a1c024302ff43b..88c3a8c46275364092867d4dbc3811703eaaa985 100644 (file)
@@ -211,7 +211,7 @@ struct logsrv {
        struct list list;
        struct sockaddr_storage addr;
        struct smp_info lb;
-       struct ring *ring;
+       struct sink *sink;
        enum log_tgt type;
        int format;
        int facility;
index 482ce4dac5f8372f6ef54f240c8798e3d71e6597..0aa043f255b51e3b4602fe594b9cbf0dc6bf1efa 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -1048,7 +1048,7 @@ int parse_logsrv(char **args, struct list *logsrvs, int do_del, char **err)
 
                logsrv->addr.ss_family = AF_UNSPEC;
                logsrv->type = LOG_TARGET_BUFFER;
-               logsrv->ring = sink->ctx.ring;
+               logsrv->sink = sink;
                goto done;
        }
 
@@ -1563,7 +1563,7 @@ static inline void __do_send_log(struct logsrv *logsrv, int nblogger, char *pid_
        static THREAD_LOCAL int logfdinet = -1; /* syslog to AF_INET socket */
        static THREAD_LOCAL char *dataptr = NULL;
        time_t time = date.tv_sec;
-       char *hdr, *hdr_ptr;
+       char *hdr, *hdr_ptr = NULL;
        size_t hdr_size;
        int fac_level;
        int *plogfd;
@@ -1593,6 +1593,7 @@ static inline void __do_send_log(struct logsrv *logsrv, int nblogger, char *pid_
        }
        else if (logsrv->type == LOG_TARGET_BUFFER) {
                plogfd = NULL;
+               goto send;
        }
        else if (logsrv->addr.ss_family == AF_UNIX)
                plogfd = &logfdunix;
@@ -1739,18 +1740,23 @@ send:
                /* the target is a file descriptor or a ring buffer */
                struct ist msg[7];
 
-               msg[0].ptr = hdr_ptr;  msg[0].len = hdr_max;
-               msg[1].ptr = tag_str;  msg[1].len = tag_max;
-               msg[2].ptr = pid_sep1; msg[2].len = pid_sep1_max;
-               msg[3].ptr = pid_str;  msg[3].len = pid_max;
-               msg[4].ptr = pid_sep2; msg[4].len = pid_sep2_max;
-               msg[5].ptr = sd;       msg[5].len = sd_max;
-               msg[6].ptr = dataptr;  msg[6].len = max;
-
-               if (logsrv->type == LOG_TARGET_BUFFER)
-                       sent = ring_write(logsrv->ring, ~0, NULL, 0, msg, 7);
-               else /* LOG_TARGET_FD */
+               if (logsrv->type == LOG_TARGET_BUFFER) {
+                       msg[0] = ist2(message, MIN(size, logsrv->maxlen));
+                       msg[1] = ist2(tag_str, tag_size);
+                       msg[2] = ist2(pid_str, pid_size);
+                       msg[3] = ist2(sd, sd_size);
+                       sent = sink_write(logsrv->sink, msg, 1, level, logsrv->facility, &msg[1], &msg[2], &msg[3]);
+               }
+               else /* LOG_TARGET_FD */ {
+                       msg[0] = ist2(hdr_ptr, hdr_max);
+                       msg[1] = ist2(tag_str, tag_max);
+                       msg[2] = ist2(pid_sep1, pid_sep1_max);
+                       msg[3] = ist2(pid_str, pid_max);
+                       msg[4] = ist2(pid_sep2, pid_sep2_max);
+                       msg[5] = ist2(sd, sd_max);
+                       msg[6] = ist2(dataptr, max);
                        sent = fd_write_frag_line(*plogfd, ~0, NULL, 0, msg, 7, 1);
+               }
        }
        else {
                iovec[0].iov_base = hdr_ptr;