53#define MOD_NAME "[color_out] "
61static bool setWinTermAnsiColors(DWORD stream) {
62 HANDLE h = GetStdHandle(stream);
63 if (h == INVALID_HANDLE_VALUE) {
67 if (!GetConsoleMode(h, &dwMode)) {
70 dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
71 if (!SetConsoleMode(h, dwMode)) {
78static bool isMsysPty(
int fd) {
80 const auto ptrGetFileInformationByHandleEx
81 =
reinterpret_cast<decltype(&GetFileInformationByHandleEx)
>(
reinterpret_cast<void *
>(
82 GetProcAddress(GetModuleHandle(TEXT(
"kernel32.dll")),
83 "GetFileInformationByHandleEx")));
84 if (!ptrGetFileInformationByHandleEx) {
88 HANDLE h =
reinterpret_cast<HANDLE
>(_get_osfhandle(fd));
89 if (h == INVALID_HANDLE_VALUE) {
94 if (GetFileType(h) != FILE_TYPE_PIPE) {
100 struct MY_FILE_NAME_INFO {
101 DWORD FileNameLength;
102 WCHAR FileName[MAX_PATH];
105 auto pNameInfo = std::unique_ptr<MY_FILE_NAME_INFO>(
106 new (std::nothrow) MY_FILE_NAME_INFO());
113 if (!ptrGetFileInformationByHandleEx(h, FileNameInfo, pNameInfo.get(),
114 sizeof(MY_FILE_NAME_INFO))) {
117 std::wstring
name(pNameInfo->FileName, pNameInfo->FileNameLength /
sizeof(WCHAR));
118 if ((
name.find(L
"msys-") == std::wstring::npos
119 &&
name.find(L
"cygwin-") == std::wstring::npos)
120 ||
name.find(L
"-pty") == std::wstring::npos) {
132 " Force enable/disable ANSI text formatting.\n");
134 " Force disable ANSI text formatting.\n");
144 if (param_val !=
nullptr) {
145 return strcmp(param_val,
"no") != 0;
149 MOD_NAME "The param log-nocolor is deprecated, use "
150 "'log-color=no' instead\n");
154 const char *env_val = getenv(
"ULTRAGRID_COLOR_OUT");
155 if (env_val !=
nullptr && strlen(env_val) > 0) {
156 return strcmp(env_val,
"0") != 0;
159 return isMsysPty(fileno(stdout)) ||
160 (_isatty(fileno(stdout)) &&
161 setWinTermAnsiColors(STD_OUTPUT_HANDLE));
163 return isatty(fileno(stdout));
174template<
class OutputIt>
177 bool in_control =
false;
209 auto c_ptr = str.data();
222 va_start(ap, format);
223 int size =
vsnprintf(NULL, 0, format, ap);
228 buf.
append(size + 1,
'\0');
229 va_start(ap, format);
230 vsnprintf(buf.data(), size + 1, format, ap);
235 buf.get().resize(
len);
void append(std::string_view sv)
char * prune_ansi_sequences_inplace_cstr(char *cstr)
void prune_ansi_sequences_inplace(std::string &str)
static int prune_ansi_sequences(const char *in, OutputIt out)
bool color_output_init()
sets internal variable color_stdout and retunrs its contents
string prune_ansi_sequences_str(const char *in)
static bool is_output_color()
int color_printf(const char *format,...)
Log_output & get_log_output()
const char * get_commandline_param(const char *key)
#define ADD_TO_PARAM(param, doc)
int vsnprintf(char *s, size_t buf_size, const char *format, va_list ap)