MCPcopy Create free account
hub / github.com/ddnet/ddnet / CWindowsConsoleLogger

Class CWindowsConsoleLogger

src/base/log.cpp:315–373  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

313}
314
315class CWindowsConsoleLogger : public ILogger
316{
317 HANDLE m_pConsole;
318 bool m_EnableColor;
319 int m_BackgroundColor;
320 int m_ForegroundColor;
321 CLock m_OutputLock;
322 bool m_Finished = false;
323
324public:
325 CWindowsConsoleLogger(HANDLE pConsole, bool EnableColor) :
326 m_pConsole(pConsole),
327 m_EnableColor(EnableColor)
328 {
329 CONSOLE_SCREEN_BUFFER_INFO ConsoleInfo;
330 if(GetConsoleScreenBufferInfo(pConsole, &ConsoleInfo))
331 {
332 m_BackgroundColor = ConsoleInfo.wAttributes & (BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY);
333 m_ForegroundColor = ConsoleInfo.wAttributes & (FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
334 }
335 else
336 {
337 m_BackgroundColor = 0;
338 m_ForegroundColor = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
339 }
340 }
341 void Log(const CLogMessage *pMessage) override REQUIRES(!m_OutputLock)
342 {
343 if(m_Filter.Filters(pMessage))
344 {
345 return;
346 }
347 const std::wstring WideMessage = windows_utf8_to_wide(pMessage->m_aLine);
348
349 int Color = m_BackgroundColor;
350 if(m_EnableColor && pMessage->m_HaveColor)
351 {
352 const ColorRGBA Rgba(pMessage->m_Color.r / 255.0f, pMessage->m_Color.g / 255.0f, pMessage->m_Color.b / 255.0f);
353 Color |= color_hsv_to_windows_console_color(color_cast<ColorHSVA>(Rgba));
354 }
355 else
356 Color |= FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
357
358 const CLockScope LockScope(m_OutputLock);
359 if(!m_Finished)
360 {
361 SetConsoleTextAttribute(m_pConsole, Color);
362 WriteConsoleW(m_pConsole, WideMessage.c_str(), WideMessage.length(), nullptr, nullptr);
363 WriteConsoleW(m_pConsole, L"\r\n", 2, nullptr, nullptr);
364 }
365 }
366 void GlobalFinish() override REQUIRES(!m_OutputLock)
367 {
368 // Restore original color
369 const CLockScope LockScope(m_OutputLock);
370 SetConsoleTextAttribute(m_pConsole, m_BackgroundColor | m_ForegroundColor);
371 m_Finished = true;
372 }

Callers

nothing calls this directly

Calls 3

windows_utf8_to_wideFunction · 0.85
FiltersMethod · 0.80

Tested by

no test coverage detected