7 #ifndef IFM3D_COMMON_LOGGING_LOG_WRITER_CONSOLE_COLORED_H
8 #define IFM3D_COMMON_LOGGING_LOG_WRITER_CONSOLE_COLORED_H
10 #include <ifm3d/common/logging/log_writer_console.h>
12 # ifndef WIN32_LEAN_AND_MEAN
13 # define WIN32_LEAN_AND_MEAN
22 template <
class Formatter,
23 typename std::enable_if_t<std::is_same_v<
24 decltype(Formatter::format(
26 std::string>>* =
nullptr>
34 colored_output_available_ =
35 this->is_a_tty_ && EnableVirtualTerminalProcessing();
37 colored_output_available_ = this->is_a_tty_;
42 Write(
const LogEntry& entry)
override
44 if (this->colored_output_available_)
46 const auto str = Formatter::format(entry);
47 const std::lock_guard<std::mutex> lock(this->mutex_);
48 this->SetColor(entry.GetLogLevel());
51 this->out_ << std::endl;
60 bool colored_output_available_ =
false;
63 EnableVirtualTerminalProcessing()
65 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
66 if (hOut == INVALID_HANDLE_VALUE)
72 if (!GetConsoleMode(hOut, &dwMode))
77 dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
78 if (!SetConsoleMode(hOut, dwMode))
87 SetColor(LogLevel log_level)
91 case LogLevel::Critical:
92 this->out_ <<
"\x1B[97m\x1B[41m";
96 this->out_ <<
"\x1B[91m";
99 case LogLevel::Warning:
100 this->out_ <<
"\x1B[93m";
103 case LogLevel::Debug:
104 case LogLevel::Verbose:
105 this->out_ <<
"\x1B[96m";
115 this->out_ <<
"\x1B[0m\x1B[0K";
119 #endif // IFM3D_COMMON_LOGGING_LOG_WRITER_CONSOLE_COLORED_H