]> git.puffer.fish Git - matthieu/frr.git/commitdiff
lib: use fbuf for zlog_msg_ts()
authorDavid Lamparter <equinox@diac24.net>
Sat, 10 Apr 2021 19:36:50 +0000 (21:36 +0200)
committerDavid Lamparter <equinox@opensourcerouting.org>
Fri, 18 Jun 2021 19:05:21 +0000 (21:05 +0200)
Signed-off-by: David Lamparter <equinox@diac24.net>
lib/zlog.c
lib/zlog.h
lib/zlog_targets.c

index c96e2fc08ee7e469d443a789e46aa1107f926a79..e843c1e475e2945978e24be7136d0d48aa795040 100644 (file)
@@ -659,9 +659,9 @@ void zlog_msg_args(struct zlog_msg *msg, size_t *hdrlen, size_t *n_argpos,
 #define ZLOG_TS_FORMAT         (ZLOG_TS_ISO8601 | ZLOG_TS_LEGACY)
 #define ZLOG_TS_FLAGS          ~ZLOG_TS_PREC
 
-size_t zlog_msg_ts(struct zlog_msg *msg, char *out, size_t outsz,
-                  uint32_t flags)
+size_t zlog_msg_ts(struct zlog_msg *msg, struct fbuf *out, uint32_t flags)
 {
+       size_t outsz = out ? (out->buf + out->len - out->pos) : 0;
        size_t len1;
 
        if (!(flags & ZLOG_TS_FORMAT))
@@ -703,32 +703,45 @@ size_t zlog_msg_ts(struct zlog_msg *msg, char *out, size_t outsz,
                len1 = strlen(msg->ts_str);
 
        if (flags & ZLOG_TS_LEGACY) {
-               if (len1 + 1 > outsz)
-                       return 0;
+               if (!out)
+                       return len1;
+
+               if (len1 > outsz) {
+                       memset(out->pos, 0, outsz);
+                       out->pos += outsz;
+                       return len1;
+               }
 
                /* just swap out the formatting, faster than redoing it */
                for (char *p = msg->ts_str; p < msg->ts_str + len1; p++) {
                        switch (*p) {
                        case '-':
-                               *out++ = '/';
+                               *out->pos++ = '/';
                                break;
                        case 'T':
-                               *out++ = ' ';
+                               *out->pos++ = ' ';
                                break;
                        default:
-                               *out++ = *p;
+                               *out->pos++ = *p;
                        }
                }
-               *out = '\0';
                return len1;
        } else {
                size_t len2 = strlen(msg->ts_zonetail);
 
-               if (len1 + len2 + 1 > outsz)
-                       return 0;
-               memcpy(out, msg->ts_str, len1);
-               memcpy(out + len1, msg->ts_zonetail, len2);
-               out[len1 + len2] = '\0';
+               if (!out)
+                       return len1 + len2;
+
+               if (len1 + len2 > outsz) {
+                       memset(out->pos, 0, outsz);
+                       out->pos += outsz;
+                       return len1 + len2;
+               }
+
+               memcpy(out->pos, msg->ts_str, len1);
+               out->pos += len1;
+               memcpy(out->pos, msg->ts_zonetail, len2);
+               out->pos += len2;
                return len1 + len2;
        }
 }
index b463606d974e4cc81a96aa6198860167c1601cb5..452dbcbd0f15e3dfa8507d44f2bdd2818bd7ad36 100644 (file)
@@ -165,7 +165,7 @@ extern void zlog_msg_args(struct zlog_msg *msg, size_t *hdrlen,
 /* default is local time zone */
 #define ZLOG_TS_UTC            (1 << 10)
 
-extern size_t zlog_msg_ts(struct zlog_msg *msg, char *out, size_t outsz,
+extern size_t zlog_msg_ts(struct zlog_msg *msg, struct fbuf *out,
                          uint32_t flags);
 
 /* This list & struct implements the actual logging targets.  It is accessed
index 7799fbfda75015d81a3309cf69525dcb52fd102a..80cb246b7a0c4746987b700cda13372670016986 100644 (file)
@@ -82,10 +82,16 @@ void zlog_fd(struct zlog_target *zt, struct zlog_msg *msgs[], size_t nmsgs)
                        iov[iovpos].iov_base = ts_pos;
                        if (iovpos > 0)
                                *ts_pos++ = '\n';
-                       ts_pos += zlog_msg_ts(msg, ts_pos,
-                                             sizeof(ts_buf) - 1
-                                                     - (ts_pos - ts_buf),
-                                             ZLOG_TS_LEGACY | zte->ts_subsec);
+
+                       struct fbuf fbuf = {
+                               .buf = ts_buf,
+                               .pos = ts_pos,
+                               .len = sizeof(ts_buf),
+                       };
+                       zlog_msg_ts(msg, &fbuf,
+                                   ZLOG_TS_LEGACY | zte->ts_subsec);
+                       ts_pos = fbuf.pos;
+
                        *ts_pos++ = ' ';
                        iov[iovpos].iov_len =
                                ts_pos - (char *)iov[iovpos].iov_base;