chiark / gitweb /
share/log: unify two code paths
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 26 Nov 2012 15:39:46 +0000 (16:39 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 18 Jan 2013 06:15:54 +0000 (01:15 -0500)
Explicit zeroing is replaced with initialization to {0}.

No functional change.

src/shared/log.c

index 8d3458e7311dbb0f293725985b32f3a49c85df98..b39b5acb5bd469e4ae8cabc681031c91f87da267 100644 (file)
@@ -441,31 +441,18 @@ static int write_to_kmsg(
         return 1;
 }
 
-static int write_to_journal(
-        int level,
-        const char*file,
-        int line,
-        const char *func,
-        const char *object_name,
-        const char *object,
-        const char *buffer) {
-
-        char header[LINE_MAX];
-        struct iovec iovec[3];
-        struct msghdr mh;
-
-        if (journal_fd < 0)
-                return 0;
-
-        snprintf(header, sizeof(header),
+static int log_do_header(char *header, size_t size,
+                         int level,
+                         const char *file, int line, const char *func,
+                         const char *object_name, const char *object) {
+        snprintf(header, size,
                  "PRIORITY=%i\n"
                  "SYSLOG_FACILITY=%i\n"
                  "CODE_FILE=%s\n"
                  "CODE_LINE=%i\n"
                  "CODE_FUNCTION=%s\n"
                  "%s%.*s%s"
-                 "SYSLOG_IDENTIFIER=%s\n"
-                 "MESSAGE=",
+                 "SYSLOG_IDENTIFIER=%s\n",
                  LOG_PRI(level),
                  LOG_FAC(level),
                  file,
@@ -475,15 +462,34 @@ static int write_to_journal(
                  object ? LINE_MAX : 0, object, /* %.0s means no output */
                  object ? "\n" : "",
                  program_invocation_short_name);
+        header[size - 1] = '\0';
+        return 0;
+}
 
-        char_array_0(header);
+static int write_to_journal(
+        int level,
+        const char*file,
+        int line,
+        const char *func,
+        const char *object_name,
+        const char *object,
+        const char *buffer) {
+
+        char header[LINE_MAX];
+        struct iovec iovec[4] = {{0}};
+        struct msghdr mh = {0};
+
+        if (journal_fd < 0)
+                return 0;
+
+        log_do_header(header, sizeof(header), level,
+                      file, line, func, object_name, object);
 
-        zero(iovec);
         IOVEC_SET_STRING(iovec[0], header);
-        IOVEC_SET_STRING(iovec[1], buffer);
-        IOVEC_SET_STRING(iovec[2], "\n");
+        IOVEC_SET_STRING(iovec[1], "MESSAGE=");
+        IOVEC_SET_STRING(iovec[2], buffer);
+        IOVEC_SET_STRING(iovec[3], "\n");
 
-        zero(mh);
         mh.msg_iov = iovec;
         mh.msg_iovlen = ELEMENTSOF(iovec);
 
@@ -744,29 +750,14 @@ int log_struct_internal(
             journal_fd >= 0) {
 
                 char header[LINE_MAX];
-                struct iovec iovec[17];
+                struct iovec iovec[17] = {{0}};
                 unsigned n = 0, i;
                 struct msghdr mh;
-                const char nl = '\n';
+                static const char nl = '\n';
 
                 /* If the journal is available do structured logging */
-
-                snprintf(header, sizeof(header),
-                        "PRIORITY=%i\n"
-                        "SYSLOG_FACILITY=%i\n"
-                        "CODE_FILE=%s\n"
-                        "CODE_LINE=%i\n"
-                        "CODE_FUNCTION=%s\n"
-                        "SYSLOG_IDENTIFIER=%s\n",
-                        LOG_PRI(level),
-                        LOG_FAC(level),
-                        file,
-                        line,
-                        func,
-                        program_invocation_short_name);
-                char_array_0(header);
-
-                zero(iovec);
+                log_do_header(header, sizeof(header), level,
+                              file, line, func, NULL, NULL);
                 IOVEC_SET_STRING(iovec[n++], header);
 
                 va_start(ap, format);