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_level.h>
11 #include <ifm3d/common/logging/log_writer_console.h>
13 # ifndef WIN32_LEAN_AND_MEAN
14 # define WIN32_LEAN_AND_MEAN
23 template <
class FORMATTER,
24 typename std::enable_if_t<std::is_same_v<
25 decltype(FORMATTER::Format(
27 std::string>>* =
nullptr>
36 _colored_output_available =
37 this->_is_a_tty && EnableVirtualTerminalProcessing();
40 _colored_output_available = this->_is_a_tty;
45 Write(
const LogEntry& entry)
override
47 if (this->_colored_output_available)
49 const auto str = FORMATTER::Format(entry);
50 const std::lock_guard<std::mutex> lock(this->_mutex);
51 this->set_color(entry.GetLogLevel());
54 this->_out << std::endl;
63 bool _colored_output_available =
false;
66 EnableVirtualTerminalProcessing()
68 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
69 if (hOut == INVALID_HANDLE_VALUE)
75 if (!GetConsoleMode(hOut, &dwMode))
80 dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
81 if (!SetConsoleMode(hOut, dwMode))
90 set_color(LogLevel log_level)
94 case LogLevel::Critical:
95 this->_out <<
"\x1B[97m\x1B[41m";
99 this->_out <<
"\x1B[91m";
102 case LogLevel::Warning:
103 this->_out <<
"\x1B[93m";
106 case LogLevel::Debug:
107 case LogLevel::Verbose:
108 this->_out <<
"\x1B[96m";
118 this->_out <<
"\x1B[0m\x1B[0K";
122 #endif // IFM3D_COMMON_LOGGING_LOG_WRITER_CONSOLE_COLORED_H