From 86de1c9ac633d96049adbd04c6f89a934451d82b Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Fri, 28 Mar 2025 21:38:36 +0000 Subject: [PATCH] [idd] common: still print the message if FormatMsg failed --- idd/LGCommon/CDebug.cpp | 26 ++++++++++++++++---------- idd/LGCommon/CDebug.h | 1 + 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/idd/LGCommon/CDebug.cpp b/idd/LGCommon/CDebug.cpp index 34bc2f8c..ed036133 100644 --- a/idd/LGCommon/CDebug.cpp +++ b/idd/LGCommon/CDebug.cpp @@ -82,7 +82,7 @@ void CDebug::Init(const char * name) 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) 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 | "; const char* levelStr = m_levelStr[level]; - va_list args; - va_start(args, fmt); - int length = 0; length = _scprintf(fmtTemplate, levelStr, function, line); length += _vscprintf(fmt, args); @@ -102,10 +99,7 @@ void CDebug::Log(CDebug::Level level, const char * function, int line, const cha PCHAR buffer; buffer = (PCHAR)_malloca(length); if (!buffer) - { - va_end(args); return; - } /* Populate the buffer with the contents of the format string. */ StringCbPrintfA(buffer, length, fmtTemplate, levelStr, function, line); @@ -113,16 +107,23 @@ void CDebug::Log(CDebug::Level level, const char * function, int line, const cha size_t offset = 0; StringCbLengthA(buffer, length, &offset); StringCbVPrintfA(&buffer[offset], length - offset, fmt, args); - va_end(args); - buffer[length-2] = '\n'; - buffer[length-1] = '\0'; + buffer[length - 2] = '\n'; + buffer[length - 1] = '\0'; Write(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, ...) { 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()); + + va_list args; + va_start(args, fmt); + Log_va(level, function, line, fmt, args); + va_end(args); return; } diff --git a/idd/LGCommon/CDebug.h b/idd/LGCommon/CDebug.h index ade0e8bb..21a2bc40 100644 --- a/idd/LGCommon/CDebug.h +++ b/idd/LGCommon/CDebug.h @@ -45,6 +45,7 @@ class CDebug }; 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 LogHR(CDebug::Level level, HRESULT hr, const char* function, int line, const char* fmt, ...);