mirror of
https://github.com/systemd/systemd-stable.git
synced 2024-10-28 03:25:27 +03:00
sd-journal: when formatting log messages, implicitly strip trailing whitespace
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
This commit is contained in:
parent
bb557f90f8
commit
c24f1f9df1
@ -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. */
|
||||
|
Loading…
Reference in New Issue
Block a user