]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: Check for TERM=dumb in show_status()
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Sun, 21 Apr 2024 09:21:14 +0000 (11:21 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 22 Apr 2024 11:27:27 +0000 (13:27 +0200)
We shouldn't try to use any ANSI escape sequences if TERM=dumb.
Also, the "\r\n" we output can get interpreted as a double newline
(for example by Github Actions), so let's output just "\n" when
TERM=dumb to clean up the CI logs.

src/basic/log.c
src/basic/terminal-util.c
src/basic/terminal-util.h
src/core/show-status.c

index 0d740c675ecf8e8d48961659dca71bdc90c11c15..411f2c36b169b0abd896ae836e9b478d64470e2b 100644 (file)
@@ -434,6 +434,8 @@ static int write_to_console(
                 const char *func,
                 const char *buffer) {
 
+        static int dumb = -1;
+
         char location[256],
              header_time[FORMAT_TIMESTAMP_MAX],
              prefix[1 + DECIMAL_STR_MAX(int) + 2],
@@ -445,6 +447,9 @@ static int write_to_console(
         if (console_fd < 0)
                 return 0;
 
+        if (dumb < 0)
+                dumb = getenv_terminal_is_dumb();
+
         if (LOG_PRI(level) > log_target_max_level[LOG_TARGET_CONSOLE])
                 return 0;
 
@@ -491,8 +496,9 @@ static int write_to_console(
         /* When writing to a TTY we output an extra '\r' (i.e. CR) first, to generate CRNL rather than just
          * NL. This is a robustness thing in case the TTY is currently in raw mode (specifically: has the
          * ONLCR flag off). We want that subsequent output definitely starts at the beginning of the line
-         * again, after all. If the TTY is not in raw mode the extra CR should not hurt. */
-        iovec[n++] = IOVEC_MAKE_STRING(check_console_fd_is_tty() ? "\r\n" : "\n");
+         * again, after all. If the TTY is not in raw mode the extra CR should not hurt. If we're writing to
+         * a dumb terminal, only write NL as CRNL might be interpreted as a double newline. */
+        iovec[n++] = IOVEC_MAKE_STRING(check_console_fd_is_tty() && !dumb ? "\r\n" : "\n");
 
         if (writev(console_fd, iovec, n) < 0) {
 
index 518412ec36d1d3df2600e04ecde046ea7ec1a360..9470c9fb1a5439535566cedcc41a8d7276993021 100644 (file)
@@ -1296,7 +1296,7 @@ static bool on_dev_null(void) {
         return cached_on_dev_null;
 }
 
-static bool getenv_terminal_is_dumb(void) {
+bool getenv_terminal_is_dumb(void) {
         const char *e;
 
         e = getenv("TERM");
index 0f3175ef43f1282b0fa4a610f813758037b762cd..2596752e562e86f684feee1555d9f6375b5e28bb 100644 (file)
@@ -172,6 +172,7 @@ void columns_lines_cache_reset(int _unused_ signum);
 void reset_terminal_feature_caches(void);
 
 bool on_tty(void);
+bool getenv_terminal_is_dumb(void);
 bool terminal_is_dumb(void);
 ColorMode get_color_mode(void);
 bool underline_enabled(void);
index 81b6b23fd7dd253d6f65a7d85905c5718a067814..57ad4db30b5adc433b0305d5227cb64075108026 100644 (file)
@@ -39,6 +39,7 @@ int parse_show_status(const char *v, ShowStatus *ret) {
 int status_vprintf(const char *status, ShowStatusFlags flags, const char *format, va_list ap) {
         static const char status_indent[] = "         "; /* "[" STATUS "] " */
         static bool prev_ephemeral = false;
+        static int dumb = -1;
 
         _cleanup_free_ char *s = NULL;
         _cleanup_close_ int fd = -EBADF;
@@ -47,6 +48,9 @@ int status_vprintf(const char *status, ShowStatusFlags flags, const char *format
 
         assert(format);
 
+        if (dumb < 0)
+                dumb = getenv_terminal_is_dumb();
+
         /* This is independent of logging, as status messages are
          * optional and go exclusively to the console. */
 
@@ -62,7 +66,7 @@ int status_vprintf(const char *status, ShowStatusFlags flags, const char *format
         if (fd < 0)
                 return fd;
 
-        if (FLAGS_SET(flags, SHOW_STATUS_ELLIPSIZE)) {
+        if (FLAGS_SET(flags, SHOW_STATUS_ELLIPSIZE) && !dumb) {
                 char *e;
                 size_t emax, sl;
                 int c;
@@ -82,7 +86,7 @@ int status_vprintf(const char *status, ShowStatusFlags flags, const char *format
                         free_and_replace(s, e);
         }
 
-        if (prev_ephemeral)
+        if (prev_ephemeral && !dumb)
                 iovec[n++] = IOVEC_MAKE_STRING(ANSI_REVERSE_LINEFEED "\r" ANSI_ERASE_TO_END_OF_LINE);
 
         if (status) {
@@ -95,9 +99,11 @@ int status_vprintf(const char *status, ShowStatusFlags flags, const char *format
         }
 
         iovec[n++] = IOVEC_MAKE_STRING(s);
-        iovec[n++] = IOVEC_MAKE_STRING("\r\n"); /* use CRNL instead of just NL, to be robust towards TTYs in raw mode */
+        /* use CRNL instead of just NL, to be robust towards TTYs in raw mode. If we're writing to a dumb
+         * terminal, use NL as CRNL might be interpreted as a double newline. */
+        iovec[n++] = IOVEC_MAKE_STRING(dumb ? "\n" : "\r\n");
 
-        if (prev_ephemeral && !FLAGS_SET(flags, SHOW_STATUS_EPHEMERAL))
+        if (prev_ephemeral && !FLAGS_SET(flags, SHOW_STATUS_EPHEMERAL) && !dumb)
                 iovec[n++] = IOVEC_MAKE_STRING(ANSI_ERASE_TO_END_OF_LINE);
         prev_ephemeral = FLAGS_SET(flags, SHOW_STATUS_EPHEMERAL);