]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: log: automate string array construction in sess_build_logline()
authorAurelien DARRAGON <adarragon@haproxy.com>
Wed, 31 Jan 2024 13:37:07 +0000 (14:37 +0100)
committerAurelien DARRAGON <adarragon@haproxy.com>
Tue, 20 Feb 2024 14:49:55 +0000 (15:49 +0100)
make it so string array construction is performed by dedicated macro
helpers instead of manual char insertion between string members.

The goal is to easily be able to support multiple forms of array
construction depending on the data encoding format (raw, json..).

Only %hrl and %hsl logformats are concerned.

src/log.c

index c211fe52005ba20bb1de04763ed15e60f1e5314b..b0c112a1ad04a00606323c9583bb8a2970c2bc46 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -2544,6 +2544,19 @@ const char sess_set_cookie[8] = "NPDIRU67";      /* No set-cookie, Set-cookie found a
                        LOGCHAR(chr);                                  \
                } while (0)
 
+/* indicate the start of a string array */
+#define LOG_STRARRAY_START() do {                                      \
+               } while (0)
+
+/* indicate that a new element is added to the string array */
+#define LOG_STRARRAY_NEXT() do {                                       \
+                       LOGCHAR(' ');                                  \
+               } while (0)
+
+/* indicate the end of a string array */
+#define LOG_STRARRAY_END() do {                                        \
+               } while (0)
+
 /* Initializes some log data at boot */
 static void init_log()
 {
@@ -3335,9 +3348,10 @@ int sess_build_logline(struct session *sess, struct stream *s, char *dst, size_t
                        case LOG_FMT_HDRREQUESTLIST: // %hrl
                                /* request header list */
                                if (fe->nb_req_cap && s && s->req_cap) {
+                                       LOG_STRARRAY_START();
                                        for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
                                                if (hdr > 0)
-                                                       LOGCHAR(' ');
+                                                       LOG_STRARRAY_NEXT();
                                                LOGQUOTE_START();
                                                if (s->req_cap[hdr] != NULL) {
                                                        ret = lf_encode_string(tmplog, dst + maxsize,
@@ -3352,6 +3366,7 @@ int sess_build_logline(struct session *sess, struct stream *s, char *dst, size_t
                                                 */
                                                LOGQUOTE_END();
                                        }
+                                       LOG_STRARRAY_END();
                                }
                                break;
 
@@ -3379,9 +3394,10 @@ int sess_build_logline(struct session *sess, struct stream *s, char *dst, size_t
                        case LOG_FMT_HDRRESPONSLIST: // %hsl
                                /* response header list */
                                if (fe->nb_rsp_cap && s && s->res_cap) {
+                                       LOG_STRARRAY_START();
                                        for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
                                                if (hdr > 0)
-                                                       LOGCHAR(' ');
+                                                       LOG_STRARRAY_NEXT();
                                                LOGQUOTE_START();
                                                if (s->res_cap[hdr] != NULL) {
                                                        ret = lf_encode_string(tmplog, dst + maxsize,
@@ -3396,6 +3412,7 @@ int sess_build_logline(struct session *sess, struct stream *s, char *dst, size_t
                                                 */
                                                LOGQUOTE_END();
                                        }
+                                       LOG_STRARRAY_END();
                                }
                                break;