logger: remove ANSI seqences if not terminal

This allows use of raw ANSI sequences instead of the rang stuff.
This commit is contained in:
Martin Pulec
2022-08-09 09:42:40 +02:00
parent 7719e8aa47
commit 812bcbfc78
3 changed files with 17 additions and 1 deletions

View File

@@ -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();

View File

@@ -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<class OutputIt>
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);

View File

@@ -114,6 +114,10 @@ bool isMsysPty(int fd);
#ifdef __cplusplus
#include <iostream>
#include <string>
std::string prune_ansi_sequences_str(const char *in);
/**
* Class wrapping color output to terminal. Sample usage:
*