]> git.proxmox.com Git - mirror_frr.git/blobdiff - lib/zlog_targets.c
Merge pull request #13649 from donaldsharp/unlock_the_node_or_else
[mirror_frr.git] / lib / zlog_targets.c
index 7799fbfda75015d81a3309cf69525dcb52fd102a..b0f757149256a8cb95516d4a2b4d86fe5b228e25 100644 (file)
@@ -1,17 +1,6 @@
+// SPDX-License-Identifier: ISC
 /*
  * Copyright (c) 2015-19  David Lamparter, for NetDEF, Inc.
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
 #include "zebra.h"
@@ -31,7 +20,6 @@
  * absolute end.
  */
 
-DECLARE_MGROUP(LOG);
 DEFINE_MGROUP_ACTIVEATEXIT(LOG, "logging subsystem");
 
 DEFINE_MTYPE_STATIC(LOG, LOG_FD,        "log file target");
@@ -79,13 +67,17 @@ 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) {
+                       struct fbuf fbuf = {
+                               .buf = ts_buf,
+                               .pos = ts_pos,
+                               .len = sizeof(ts_buf),
+                       };
+
                        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);
+                       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;
@@ -107,7 +99,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++;
                }
@@ -120,11 +112,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;
@@ -244,14 +231,14 @@ static bool zlog_file_cycle(struct zlog_cfg_file *zcf)
 
 void zlog_file_set_other(struct zlog_cfg_file *zcf)
 {
-       frr_with_mutex(&zcf->cfg_mtx) {
+       frr_with_mutex (&zcf->cfg_mtx) {
                zlog_file_cycle(zcf);
        }
 }
 
 bool zlog_file_set_filename(struct zlog_cfg_file *zcf, const char *filename)
 {
-       frr_with_mutex(&zcf->cfg_mtx) {
+       frr_with_mutex (&zcf->cfg_mtx) {
                XFREE(MTYPE_LOG_FD_NAME, zcf->filename);
                zcf->filename = XSTRDUP(MTYPE_LOG_FD_NAME, filename);
                zcf->fd = -1;
@@ -259,11 +246,12 @@ bool zlog_file_set_filename(struct zlog_cfg_file *zcf, const char *filename)
                return zlog_file_cycle(zcf);
        }
        assert(0);
+       return false;
 }
 
 bool zlog_file_set_fd(struct zlog_cfg_file *zcf, int fd)
 {
-       frr_with_mutex(&zcf->cfg_mtx) {
+       frr_with_mutex (&zcf->cfg_mtx) {
                if (zcf->fd == fd)
                        return true;
 
@@ -273,6 +261,7 @@ bool zlog_file_set_fd(struct zlog_cfg_file *zcf, int fd)
                return zlog_file_cycle(zcf);
        }
        assert(0);
+       return false;
 }
 
 struct rcu_close_rotate {
@@ -285,7 +274,7 @@ bool zlog_file_rotate(struct zlog_cfg_file *zcf)
        struct rcu_close_rotate *rcr;
        int fd;
 
-       frr_with_mutex(&zcf->cfg_mtx) {
+       frr_with_mutex (&zcf->cfg_mtx) {
                if (!zcf->active || !zcf->filename)
                        return true;
 
@@ -439,13 +428,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);
        }
 }
 
@@ -516,7 +508,7 @@ void zlog_syslog_set_facility(int facility)
        struct zlog_target *newztc;
        struct zlt_syslog *newzt;
 
-       frr_with_mutex(&syslog_cfg_mutex) {
+       frr_with_mutex (&syslog_cfg_mutex) {
                if (facility == syslog_facility)
                        return;
                syslog_facility = facility;
@@ -539,10 +531,11 @@ void zlog_syslog_set_facility(int facility)
 
 int zlog_syslog_get_facility(void)
 {
-       frr_with_mutex(&syslog_cfg_mutex) {
+       frr_with_mutex (&syslog_cfg_mutex) {
                return syslog_facility;
        }
        assert(0);
+       return 0;
 }
 
 void zlog_syslog_set_prio_min(int prio_min)
@@ -550,7 +543,7 @@ void zlog_syslog_set_prio_min(int prio_min)
        struct zlog_target *newztc;
        struct zlt_syslog *newzt = NULL;
 
-       frr_with_mutex(&syslog_cfg_mutex) {
+       frr_with_mutex (&syslog_cfg_mutex) {
                if (prio_min == syslog_prio_min)
                        return;
                syslog_prio_min = prio_min;
@@ -576,8 +569,9 @@ void zlog_syslog_set_prio_min(int prio_min)
 
 int zlog_syslog_get_prio_min(void)
 {
-       frr_with_mutex(&syslog_cfg_mutex) {
+       frr_with_mutex (&syslog_cfg_mutex) {
                return syslog_prio_min;
        }
        assert(0);
+       return 0;
 }