[idd] common: still print the message if FormatMsg failed

This commit is contained in:
Geoffrey McRae 2025-03-28 21:38:36 +00:00
parent d839a45d0b
commit 86de1c9ac6
2 changed files with 17 additions and 10 deletions

View File

@ -82,7 +82,7 @@ void CDebug::Init(const char * name)
m_stream = std::move(stream); m_stream = std::move(stream);
} }
void CDebug::Log(CDebug::Level level, const char * function, int line, const char * fmt, ...) void CDebug::Log_va(CDebug::Level level, const char* function, int line, const char* fmt, va_list args)
{ {
if (level < 0 || level >= LEVEL_MAX) if (level < 0 || level >= LEVEL_MAX)
level = LEVEL_NONE; level = LEVEL_NONE;
@ -90,9 +90,6 @@ void CDebug::Log(CDebug::Level level, const char * function, int line, const cha
static const char* fmtTemplate = "[%s] %40s:%-4d | "; static const char* fmtTemplate = "[%s] %40s:%-4d | ";
const char* levelStr = m_levelStr[level]; const char* levelStr = m_levelStr[level];
va_list args;
va_start(args, fmt);
int length = 0; int length = 0;
length = _scprintf(fmtTemplate, levelStr, function, line); length = _scprintf(fmtTemplate, levelStr, function, line);
length += _vscprintf(fmt, args); length += _vscprintf(fmt, args);
@ -102,10 +99,7 @@ void CDebug::Log(CDebug::Level level, const char * function, int line, const cha
PCHAR buffer; PCHAR buffer;
buffer = (PCHAR)_malloca(length); buffer = (PCHAR)_malloca(length);
if (!buffer) if (!buffer)
{
va_end(args);
return; return;
}
/* Populate the buffer with the contents of the format string. */ /* Populate the buffer with the contents of the format string. */
StringCbPrintfA(buffer, length, fmtTemplate, levelStr, function, line); StringCbPrintfA(buffer, length, fmtTemplate, levelStr, function, line);
@ -113,7 +107,6 @@ void CDebug::Log(CDebug::Level level, const char * function, int line, const cha
size_t offset = 0; size_t offset = 0;
StringCbLengthA(buffer, length, &offset); StringCbLengthA(buffer, length, &offset);
StringCbVPrintfA(&buffer[offset], length - offset, fmt, args); StringCbVPrintfA(&buffer[offset], length - offset, fmt, args);
va_end(args);
buffer[length - 2] = '\n'; buffer[length - 2] = '\n';
buffer[length - 1] = '\0'; buffer[length - 1] = '\0';
@ -123,6 +116,14 @@ void CDebug::Log(CDebug::Level level, const char * function, int line, const cha
_freea(buffer); _freea(buffer);
} }
void CDebug::Log(CDebug::Level level, const char * function, int line, const char * fmt, ...)
{
va_list args;
va_start(args, fmt);
Log_va(level, function, line, fmt, args);
va_end(args);
}
void CDebug::LogHR(CDebug::Level level, HRESULT hr, const char * function, int line, const char * fmt, ...) void CDebug::LogHR(CDebug::Level level, HRESULT hr, const char * function, int line, const char * fmt, ...)
{ {
if (level < 0 || level >= LEVEL_MAX) if (level < 0 || level >= LEVEL_MAX)
@ -140,6 +141,11 @@ void CDebug::LogHR(CDebug::Level level, HRESULT hr, const char * function, int l
)) ))
{ {
DEBUG_INFO("FormatMessage failed with code 0x%08x", GetLastError()); DEBUG_INFO("FormatMessage failed with code 0x%08x", GetLastError());
va_list args;
va_start(args, fmt);
Log_va(level, function, line, fmt, args);
va_end(args);
return; return;
} }

View File

@ -45,6 +45,7 @@ class CDebug
}; };
void Init(const char * name); void Init(const char * name);
void Log_va(CDebug::Level level, const char* function, int line, const char* fmt, va_list args);
void Log(CDebug::Level level, const char * function, int line, const char * fmt, ...); void Log(CDebug::Level level, const char * function, int line, const char * fmt, ...);
void LogHR(CDebug::Level level, HRESULT hr, const char* function, int line, const char* fmt, ...); void LogHR(CDebug::Level level, HRESULT hr, const char* function, int line, const char* fmt, ...);