sd-journal: when formatting log messages, implicitly strip trailing whitespace
authorLennart Poettering <lennart@poettering.net>
Fri, 1 Jul 2016 00:37:21 +0000 (17:37 -0700)
committerLennart Poettering <lennart@poettering.net>
Tue, 19 Jul 2016 12:21:49 +0000 (14:21 +0200)
When converting log messages from human readable text into binary records to
send off to journald in sd_journal_print(), strip trailing whitespace in the
log message. This way, handling of logs made via syslog(), stdout/stderr and
sd_journal_print() are treated the same way: trailing (but not leading)
whitespace is automatically removed, in particular \n and \r. Note that in case
of syslog() and stdout/stderr based logging the stripping takes place
server-side though, while for the native protocol based transport this takes
place client-side. This is because in the former cases conversion from
free-form human-readable strings into structured, binary log records takes
place on the server-side while for journal-native logging it happens on the
client side, and after conversion into binary records we probably shouldn't
alter the data anymore.

See: #3416

src/journal/journal-send.c

index 5e8a3e3..1b92585 100644 (file)
@@ -107,6 +107,9 @@ _public_ int sd_journal_printv(int priority, const char *format, va_list ap) {
         memcpy(buffer, "MESSAGE=", 8);
         vsnprintf(buffer+8, sizeof(buffer) - 8, format, ap);
 
+        /* Strip trailing whitespace, keep prefix whitespace. */
+        (void) strstrip(buffer);
+
         zero(iov);
         IOVEC_SET_STRING(iov[0], buffer);
         IOVEC_SET_STRING(iov[1], p);
@@ -158,6 +161,8 @@ _printf_(1, 0) static int fill_iovec_sprintf(const char *format, va_list ap, int
 
                 VA_FORMAT_ADVANCE(format, ap);
 
+                (void) strstrip(buffer); /* strip trailing whitespace, keep prefixing whitespace */
+
                 IOVEC_SET_STRING(iov[i++], buffer);
 
                 format = va_arg(ap, char *);
@@ -471,6 +476,8 @@ _public_ int sd_journal_printv_with_location(int priority, const char *file, con
         memcpy(buffer, "MESSAGE=", 8);
         vsnprintf(buffer+8, sizeof(buffer) - 8, format, ap);
 
+        (void) strstrip(buffer); /* strip trailing whitespace, keep prefixing whitespace */
+
         /* func is initialized from __func__ which is not a macro, but
          * a static const char[], hence cannot easily be prefixed with
          * CODE_FUNC=, hence let's do it manually here. */