debug: Optimise construction of msg_no_nl
authorMartin Schwenke <martin@meltin.net>
Thu, 14 Oct 2021 00:08:38 +0000 (11:08 +1100)
committerVolker Lendecke <vl@samba.org>
Thu, 14 Oct 2021 11:10:40 +0000 (11:10 +0000)
If it isn't used then it isn't copied.

Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Volker Lendecke <vl@samba.org>
Autobuild-User(master): Volker Lendecke <vl@samba.org>
Autobuild-Date(master): Thu Oct 14 11:10:40 UTC 2021 on sn-devel-184

lib/util/debug.c

index c71264b3785317b75adbe22f932dc5ec646dc961..b0ac6ce40ea684f95769fd57f466cb0f22cc739e 100644 (file)
@@ -208,6 +208,8 @@ static int debug_level_to_priority(int level)
 /* -------------------------------------------------------------------------- **
  * Produce a version of the given buffer without any trailing newlines.
  */
+#if defined(HAVE_LIBSYSTEMD_JOURNAL) || defined(HAVE_LIBSYSTEMD) || \
+       defined(HAVE_LTTNG_TRACEF) || defined(HAVE_GPFS)
 static void copy_no_nl(char *out,
                       size_t out_size,
                       const char *in,
@@ -227,8 +229,6 @@ static void copy_no_nl(char *out,
        out[len] = '\0';
 }
 
-#if defined(HAVE_LIBSYSTEMD_JOURNAL) || defined(HAVE_LIBSYSTEMD) || \
-       defined(HAVE_LTTNG_TRACEF) || defined(HAVE_GPFS)
 static void ensure_copy_no_nl(char *out,
                              size_t out_size,
                              const char *in,
@@ -339,6 +339,9 @@ static void debug_systemd_log(int msg_level, const char *msg, size_t msg_len)
                                msg_level,
                                NULL);
        }
+       ensure_copy_no_nl(state.msg_no_nl,
+                         sizeof(state.msg_no_nl),
+                         msg, msg_len);
        sd_journal_send("MESSAGE=%s", state.msg_no_nl,
                        "PRIORITY=%d", debug_level_to_priority(msg_level),
                        "LEVEL=%d", msg_level,
@@ -357,6 +360,9 @@ static void debug_lttng_log(int msg_level, const char *msg, size_t msg_len)
                                  state.hs_len);
                tracef(state.header_str_no_nl);
        }
+       ensure_copy_no_nl(state.msg_no_nl,
+                         sizeof(state.msg_no_nl),
+                         msg, msg_len);
        tracef(state.msg_no_nl);
 }
 #endif /* WITH_LTTNG_TRACEF */
@@ -395,6 +401,9 @@ static void debug_gpfs_log(int msg_level, const char *msg, size_t msg_len)
                                  state.hs_len);
                gpfswrap_add_trace(msg_level, state.header_str_no_nl);
        }
+       ensure_copy_no_nl(state.msg_no_nl,
+                         sizeof(state.msg_no_nl),
+                         msg, msg_len);
        gpfswrap_add_trace(msg_level, state.msg_no_nl);
 }
 #endif /* HAVE_GPFS */
@@ -651,10 +660,11 @@ static void debug_backends_log(const char *msg, size_t msg_len, int msg_level)
        size_t i;
 
        /*
-        * Some backends already add an extra newline, so also provide
-        * a buffer without the newline character.
+        * Some backends already add an extra newline, so initialize a
+        * buffer without the newline character.  It will be filled by
+        * the first backend that needs it.
         */
-       copy_no_nl(state.msg_no_nl, FORMAT_BUFR_SIZE, msg, msg_len);
+       state.msg_no_nl[0] = '\0';
 
        for (i = 0; i < ARRAY_SIZE(debug_backends); i++) {
                if (msg_level <= debug_backends[i].log_level) {