From 7e27491a2bb0b72dacd166c48580f35623846c46 Mon Sep 17 00:00:00 2001 From: Martin Piatka Date: Tue, 27 Sep 2022 14:12:18 +0200 Subject: [PATCH] logging: log_vprintf: prune ansi sequences when not interactive Previously colored output using the inline TERM_* macros from color_out.h was done only using the color_printf() function. Since there are now places where they are used with log_msg(), we need to prune ansi codes here too. --- src/debug.cpp | 2 ++ src/utils/color_out.cpp | 7 +++++++ src/utils/color_out.h | 1 + 3 files changed, 10 insertions(+) diff --git a/src/debug.cpp b/src/debug.cpp index ebf63462b..36d42fa4c 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -124,6 +124,8 @@ int log_vprintf(int level, const char *format, va_list ap) } else { buf.append(TERM_RESET); } + } else { + prune_ansi_sequences_inplace(buf.get()); } int ret = buf.get().size(); buf.submit(); diff --git a/src/utils/color_out.cpp b/src/utils/color_out.cpp index f635b4457..522efe975 100644 --- a/src/utils/color_out.cpp +++ b/src/utils/color_out.cpp @@ -47,6 +47,7 @@ #include #include "debug.h" +#include "host.h" #include "utils/color_out.h" using std::cout; @@ -175,6 +176,12 @@ string prune_ansi_sequences_str(const char *in) { return out; } +void prune_ansi_sequences_inplace(std::string& str){ + auto c_ptr = str.data(); + auto len = prune_ansi_sequences(c_ptr, c_ptr); + str.resize(len); +} + int color_printf(const char *format, ...) { va_list ap; va_start(ap, format); diff --git a/src/utils/color_out.h b/src/utils/color_out.h index 9536c5401..adfa1e651 100644 --- a/src/utils/color_out.h +++ b/src/utils/color_out.h @@ -84,6 +84,7 @@ int color_printf(const char *format, ...) ATTRIBUTE(format (printf, 1, 2)); #include std::string prune_ansi_sequences_str(const char *in); +void prune_ansi_sequences_inplace(std::string& str); /** * Class wrapping color output to terminal. Sample usage: