aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManawyrm <git@tbspace.de>2023-11-07 11:56:02 +0100
committerOliver Smith <osmith@sysmocom.de>2023-11-28 11:11:32 +0100
commit7d41444d5df894577e3eab304a7978f54fcd1a47 (patch)
treec9cbf4288b09e4bcbfd141ba1577ce1fb2c144dd
parentaca2c724ae94f3b4b6dba8cb16a6ddc57e405c65 (diff)
logging: ensure ANSI color escape is sent in same line/before newline
This fixes multi-line color clobbering in logging daemons like systemd-journald, which work with single-lines only. Change-Id: Ia7de9d88aa5ac48ec0d5c1a931a89d21c02c5433 Closes: OS#6249 (cherry picked from commit 11a416827dd9f2da6b7c1db0e1e83adb1e6e5cc8)
-rw-r--r--src/core/logging.c10
-rw-r--r--tests/loggingrb/logging_test.err5
2 files changed, 11 insertions, 4 deletions
diff --git a/src/core/logging.c b/src/core/logging.c
index c6774f57..dc19cf30 100644
--- a/src/core/logging.c
+++ b/src/core/logging.c
@@ -616,7 +616,15 @@ static int _output_buf(char *buf, int buf_len, struct log_target *target, unsign
}
if (target->use_color && c_subsys) {
- ret = snprintf(buf + offset, rem, OSMO_LOGCOLOR_END);
+ /* Ensure the last color escape is sent before the newline
+ * (to not clobber journald, which works on single-lines only) */
+ if (offset > 0 && buf[offset - 1] == '\n') {
+ offset--; rem++;
+ ret = snprintf(buf + offset, rem, OSMO_LOGCOLOR_END "\n");
+ } else {
+ ret = snprintf(buf + offset, rem, OSMO_LOGCOLOR_END);
+ }
+
if (ret < 0)
goto err;
OSMO_SNPRINTF_RET(ret, rem, offset, len);
diff --git a/tests/loggingrb/logging_test.err b/tests/loggingrb/logging_test.err
index b59d2e83..e070561f 100644
--- a/tests/loggingrb/logging_test.err
+++ b/tests/loggingrb/logging_test.err
@@ -1,3 +1,2 @@
-You should see this
-You should see this
- \ No newline at end of file
+You should see this
+You should see this