From 812bcbfc784babdad6487f70e4c91e143113dcaf Mon Sep 17 00:00:00 2001 From: Martin Pulec Date: Tue, 9 Aug 2022 09:42:40 +0200 Subject: [PATCH] logger: remove ANSI seqences if not terminal This allows use of raw ANSI sequences instead of the rang stuff. --- src/debug.h | 4 ++++ src/utils/color_out.cpp | 10 +++++++++- src/utils/color_out.h | 4 ++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/debug.h b/src/debug.h index cd289e984..61e0be4ff 100644 --- a/src/debug.h +++ b/src/debug.h @@ -276,6 +276,10 @@ public: std::string msg = oss.str(); + if (!get_log_output().is_interactive()) { + msg = prune_ansi_sequences_str(msg.c_str()); + } + auto buf = get_log_output().get_buffer(); buf.append(msg); buf.submit(); diff --git a/src/utils/color_out.cpp b/src/utils/color_out.cpp index 1ee68e5b0..d8fca146f 100644 --- a/src/utils/color_out.cpp +++ b/src/utils/color_out.cpp @@ -48,6 +48,7 @@ #include "utils/color_out.h" using std::cout; +using std::string; static bool color_stdout; @@ -180,7 +181,8 @@ bool color_output_init() { return color_stdout; } -static int prune_ansi_sequences(const char *in, char *out) { +template +static int prune_ansi_sequences(const char *in, OutputIt out) { char c = *in; bool in_control = false; int written = 0; @@ -207,6 +209,12 @@ static int prune_ansi_sequences(const char *in, char *out) { return written; } +string prune_ansi_sequences_str(const char *in) { + string out; + prune_ansi_sequences(in, std::back_inserter(out)); + return out; +} + 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 8d246d94d..2e7babca0 100644 --- a/src/utils/color_out.h +++ b/src/utils/color_out.h @@ -114,6 +114,10 @@ bool isMsysPty(int fd); #ifdef __cplusplus #include +#include + +std::string prune_ansi_sequences_str(const char *in); + /** * Class wrapping color output to terminal. Sample usage: *