| 71 | } |
| 72 | |
| 73 | __ri void Log::WriteToConsole(LOGLEVEL level, ConsoleColors color, std::string_view message) |
| 74 | { |
| 75 | static constexpr std::string_view s_ansi_color_codes[ConsoleColors_Count] = { |
| 76 | "\033[0m"sv, // default |
| 77 | "\033[30m\033[1m"sv, // black |
| 78 | "\033[32m"sv, // green |
| 79 | "\033[31m"sv, // red |
| 80 | "\033[34m"sv, // blue |
| 81 | "\033[35m"sv, // magenta |
| 82 | "\033[35m"sv, // orange (FIXME) |
| 83 | "\033[37m"sv, // gray |
| 84 | "\033[36m"sv, // cyan |
| 85 | "\033[33m"sv, // yellow |
| 86 | "\033[37m"sv, // white |
| 87 | "\033[30m\033[1m"sv, // strong black |
| 88 | "\033[31m\033[1m"sv, // strong red |
| 89 | "\033[32m\033[1m"sv, // strong green |
| 90 | "\033[34m\033[1m"sv, // strong blue |
| 91 | "\033[35m\033[1m"sv, // strong magenta |
| 92 | "\033[35m\033[1m"sv, // strong orange (FIXME) |
| 93 | "\033[37m\033[1m"sv, // strong gray |
| 94 | "\033[36m\033[1m"sv, // strong cyan |
| 95 | "\033[33m\033[1m"sv, // strong yellow |
| 96 | "\033[37m\033[1m"sv, // strong white |
| 97 | }; |
| 98 | |
| 99 | static constexpr size_t BUFFER_SIZE = 512; |
| 100 | |
| 101 | #ifdef _WIN32 |
| 102 | constexpr bool supports_color_codes = true; |
| 103 | #else |
| 104 | const int fd = (level <= LOGLEVEL_WARNING) ? STDERR_FILENO : STDOUT_FILENO; |
| 105 | const bool supports_color_codes = static_cast<bool>(isatty(fd)); |
| 106 | #endif |
| 107 | |
| 108 | SmallStackString<BUFFER_SIZE> buffer; |
| 109 | buffer.reserve(static_cast<u32>(32 + message.length())); |
| 110 | if (supports_color_codes) |
| 111 | buffer.append(s_ansi_color_codes[color]); |
| 112 | |
| 113 | if (s_log_timestamps) |
| 114 | buffer.append_format(TIMESTAMP_FORMAT_STRING, Log::GetCurrentMessageTime()); |
| 115 | |
| 116 | buffer.append(message); |
| 117 | |
| 118 | if (supports_color_codes) |
| 119 | buffer.append(s_ansi_color_codes[Color_Default]); |
| 120 | buffer.append('\n'); |
| 121 | |
| 122 | #ifdef _WIN32 |
| 123 | const HANDLE hOutput = (level <= LOGLEVEL_WARNING) ? s_hConsoleStdErr : s_hConsoleStdOut; |
| 124 | |
| 125 | // Convert to UTF-16 first so Unicode characters display correctly. NT is going to do it anyway... |
| 126 | wchar_t wbuf[BUFFER_SIZE]; |
| 127 | wchar_t* wmessage_buf = wbuf; |
| 128 | int wmessage_buflen = static_cast<int>(std::size(wbuf) - 1); |
| 129 | if (buffer.length() >= std::size(wbuf)) |
| 130 | { |