Use dynamic buffer size in CLoggerWindowsDebugger

Make sure the Windows debugger logger does not fail when log messages are too long.

The first call to `MultiByteToWideChar` gets the required size of the wide-char buffer, which should be at least one for the null termination. The second call is expected to produce exactly the same number of characters.
This commit is contained in:
Robert Müller 2022-10-28 00:21:26 +02:00
parent f74797ae0c
commit 72991fbe9a

View file

@ -423,9 +423,12 @@ class CLoggerWindowsDebugger : public ILogger
public: public:
void Log(const CLogMessage *pMessage) override void Log(const CLogMessage *pMessage) override
{ {
WCHAR aWBuffer[4096]; int WLen = MultiByteToWideChar(CP_UTF8, 0, pMessage->m_aLine, -1, NULL, 0);
MultiByteToWideChar(CP_UTF8, 0, pMessage->m_aLine, -1, aWBuffer, sizeof(aWBuffer) / sizeof(WCHAR)); dbg_assert(WLen > 0, "MultiByteToWideChar failure");
OutputDebugStringW(aWBuffer); WCHAR *pWide = (WCHAR *)malloc(WLen * sizeof(*pWide));
dbg_assert(MultiByteToWideChar(CP_UTF8, 0, pMessage->m_aLine, -1, pWide, WLen) == WLen, "MultiByteToWideChar failure");
OutputDebugStringW(pWide);
free(pWide);
} }
}; };
std::unique_ptr<ILogger> log_logger_windows_debugger() std::unique_ptr<ILogger> log_logger_windows_debugger()