]> git.puffer.fish Git - mirror/frr.git/commitdiff
lib: include `\n` in zlog_msg_text()
authorDavid Lamparter <equinox@diac24.net>
Sat, 10 Apr 2021 19:51:48 +0000 (21:51 +0200)
committerDavid Lamparter <equinox@opensourcerouting.org>
Fri, 18 Jun 2021 19:05:21 +0000 (21:05 +0200)
Since the file targets append one anyway, save them some extra work.
syslog can use `%.*s` since it's "forced" printf by API anyway.

Signed-off-by: David Lamparter <equinox@diac24.net>
lib/log_filter.c
lib/zlog.c
lib/zlog.h
lib/zlog_targets.c

index 721e57a62804cdd1678f2b30b623ded72bd43867..f01497dead4042b62b9d45a50477369b888f33dd 100644 (file)
@@ -111,13 +111,14 @@ int zlog_filter_dump(char *buf, size_t max_size)
        return len;
 }
 
-static int search_buf(const char *buf)
+static int search_buf(const char *buf, size_t len)
 {
        char *found = NULL;
 
        frr_with_mutex(&logfilterlock) {
                for (int i = 0; i < zlog_filter_count; i++) {
-                       found = strstr(buf, zlog_filters[i]);
+                       found = memmem(buf, len, zlog_filters[i],
+                                      strlen(zlog_filters[i]));
                        if (found != NULL)
                                return 0;
                }
@@ -131,12 +132,15 @@ static void zlog_filterfile_fd(struct zlog_target *zt, struct zlog_msg *msgs[],
 {
        struct zlog_msg *msgfilt[nmsgs];
        size_t i, o = 0;
+       const char *text;
+       size_t text_len;
 
        for (i = 0; i < nmsgs; i++) {
-               if (zlog_msg_prio(msgs[i]) >= LOG_DEBUG
-                   && search_buf(zlog_msg_text(msgs[i], NULL)) < 0)
-                       continue;
-
+               if (zlog_msg_prio(msgs[i]) >= LOG_DEBUG) {
+                       text = zlog_msg_text(msgs[i], &text_len);
+                       if (search_buf(text, text_len) < 0)
+                               continue;
+               }
                msgfilt[o++] = msgs[i];
        }
 
index d88337cb834be3f99955b6cf8a93072e086eef41..91cb7151eb4ce6357e50449bcdf51ef01de0cae5 100644 (file)
@@ -620,7 +620,7 @@ const char *zlog_msg_text(struct zlog_msg *msg, size_t *textlen)
                va_end(args);
 
                msg->textlen = need;
-               need += bputch(&fb, '\0');
+               need += bputch(&fb, '\n');
 
                if (need <= msg->stackbufsz)
                        msg->text = msg->stackbuf;
@@ -638,7 +638,7 @@ const char *zlog_msg_text(struct zlog_msg *msg, size_t *textlen)
                        vbprintfrr(&fb, msg->fmt, args);
                        va_end(args);
 
-                       bputch(&fb, '\0');
+                       bputch(&fb, '\n');
                }
 
                msg->n_argpos = fb.outpos_i;
index f71d28c99a46835ff794d06bd217242f6069af43..1b03e3fd1285edc02aab811af3aff012dfac9aea 100644 (file)
@@ -144,7 +144,13 @@ struct zlog_msg;
 extern int zlog_msg_prio(struct zlog_msg *msg);
 extern const struct xref_logmsg *zlog_msg_xref(struct zlog_msg *msg);
 
-/* pass NULL as textlen if you don't need it. */
+/* text is NOT \0 terminated; instead there is a \n after textlen since the
+ * logging targets would jump extra hoops otherwise for a single byte.  (the
+ * \n is not included in textlen)
+ *
+ * calling this with NULL textlen is likely wrong.
+ * use  "%.*s", (int)textlen, text  when passing to printf-like functions
+ */
 extern const char *zlog_msg_text(struct zlog_msg *msg, size_t *textlen);
 
 extern void zlog_msg_args(struct zlog_msg *msg, size_t *hdrlen,
index 80cb246b7a0c4746987b700cda13372670016986..127de1224043674653c7f55599524155a77d55ac 100644 (file)
@@ -79,15 +79,13 @@ void zlog_fd(struct zlog_target *zt, struct zlog_msg *msgs[], size_t nmsgs)
                int prio = zlog_msg_prio(msg);
 
                if (prio <= zt->prio_min) {
-                       iov[iovpos].iov_base = ts_pos;
-                       if (iovpos > 0)
-                               *ts_pos++ = '\n';
-
                        struct fbuf fbuf = {
                                .buf = ts_buf,
                                .pos = ts_pos,
                                .len = sizeof(ts_buf),
                        };
+
+                       iov[iovpos].iov_base = ts_pos;
                        zlog_msg_ts(msg, &fbuf,
                                    ZLOG_TS_LEGACY | zte->ts_subsec);
                        ts_pos = fbuf.pos;
@@ -113,7 +111,7 @@ void zlog_fd(struct zlog_target *zt, struct zlog_msg *msgs[], size_t nmsgs)
 
                        iov[iovpos].iov_base =
                                (char *)zlog_msg_text(msg, &textlen);
-                       iov[iovpos].iov_len = textlen;
+                       iov[iovpos].iov_len = textlen + 1;
 
                        iovpos++;
                }
@@ -126,11 +124,6 @@ void zlog_fd(struct zlog_target *zt, struct zlog_msg *msgs[], size_t nmsgs)
                if (iovpos > 0 && (ts_buf + sizeof(ts_buf) - ts_pos < TS_LEN
                                   || i + 1 == nmsgs
                                   || array_size(iov) - iovpos < 5)) {
-                       iov[iovpos].iov_base = (char *)"\n";
-                       iov[iovpos].iov_len = 1;
-
-                       iovpos++;
-
                        writev(fd, iov, iovpos);
 
                        iovpos = 0;
@@ -445,13 +438,16 @@ static void zlog_syslog(struct zlog_target *zt, struct zlog_msg *msgs[],
 {
        size_t i;
        struct zlt_syslog *zte = container_of(zt, struct zlt_syslog, zt);
+       const char *text;
+       size_t text_len;
 
        for (i = 0; i < nmsgs; i++) {
                if (zlog_msg_prio(msgs[i]) > zt->prio_min)
                        continue;
 
-               syslog(zlog_msg_prio(msgs[i]) | zte->syslog_facility, "%s",
-                      zlog_msg_text(msgs[i], NULL));
+               text = zlog_msg_text(msgs[i], &text_len);
+               syslog(zlog_msg_prio(msgs[i]) | zte->syslog_facility, "%.*s",
+                      (int)text_len, text);
        }
 }