From 24ca07188c0ee2dd818429e6edb67635b65647fb Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Sun, 3 Apr 2022 12:56:11 -0600 Subject: [PATCH 1/2] log: use ANSI colors for strings containing "warning" or "error" --- common/cmdlib.cc | 14 +++++++++++++- common/log.cc | 17 +++++++++++++++-- include/common/cmdlib.hh | 5 +++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/common/cmdlib.cc b/common/cmdlib.cc index 11d37a4e..4618bc18 100644 --- a/common/cmdlib.cc +++ b/common/cmdlib.cc @@ -37,6 +37,7 @@ #include +#include #include /* set these before calling CheckParm */ @@ -405,4 +406,15 @@ bool natstrlt(const char *s1, const char *s2, bool case_sensitive) if (*p1 != 0 && *p2 == 0) return false; return false; -} \ No newline at end of file +} + +std::string_view::const_iterator string_ifind(std::string_view haystack, std::string_view needle) +{ + return std::search(haystack.begin(), haystack.end(), needle.begin(), needle.end(), + [](char a, char b) { return tolower(a) == tolower(b); }); +} + +bool string_icontains(std::string_view haystack, std::string_view needle) +{ + return string_ifind(haystack, needle) != haystack.end(); +} diff --git a/common/log.cc b/common/log.cc index c9a53027..9c592ff1 100644 --- a/common/log.cc +++ b/common/log.cc @@ -68,6 +68,19 @@ void print(flag logflag, const char *str) return; } + static const char *escape_red = "\033[31m"; + static const char *escape_yellow = "\033[33m"; + static const char *escape_reset = "\033[0m"; + + std::string ansi_str; + if (string_icontains(str, "error")) { + ansi_str = fmt::format("{}{}{}", escape_red, str, escape_reset); + } else if (string_icontains(str, "warning")) { + ansi_str = fmt::format("{}{}{}", escape_yellow, str, escape_reset); + } else { + ansi_str = str; + } + print_mutex.lock(); if (logflag != flag::PERCENT) { @@ -79,12 +92,12 @@ void print(flag logflag, const char *str) #ifdef _WIN32 // print to windows console - OutputDebugStringA(str); + OutputDebugStringA(ansi_str.c_str()); #endif } // stdout - std::cout << str; + std::cout << ansi_str; print_mutex.unlock(); } diff --git a/include/common/cmdlib.hh b/include/common/cmdlib.hh index a1735a81..874ba7db 100644 --- a/include/common/cmdlib.hh +++ b/include/common/cmdlib.hh @@ -136,6 +136,11 @@ struct natural_case_insensitive_less bool operator()(const std::string &l, const std::string &r) const noexcept { return stlnatstrlt(l, r, false); } }; +#include + +std::string_view::const_iterator string_ifind(std::string_view haystack, std::string_view needle); +bool string_icontains(std::string_view haystack, std::string_view needle); + #include using qclock = std::chrono::high_resolution_clock; From 6102390986cab7f0c43172a326ec79850a5fe550 Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Sun, 3 Apr 2022 14:15:48 -0600 Subject: [PATCH 2/2] log: don't pass ANSI colors to OutputDebugStringA since they aren't rendered in VS's Output window --- common/log.cc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/common/log.cc b/common/log.cc index 9c592ff1..be46cba9 100644 --- a/common/log.cc +++ b/common/log.cc @@ -91,12 +91,13 @@ void print(flag logflag, const char *str) } #ifdef _WIN32 - // print to windows console - OutputDebugStringA(ansi_str.c_str()); + // print to windows console. + // if VS's Output window gets support for ANSI colors, we can change this to ansi_str.c_str() + OutputDebugStringA(str); #endif } - // stdout + // stdout (assume the termaial can render ANSI colors) std::cout << ansi_str; print_mutex.unlock();