/** * Looking Glass * Copyright © 2017-2025 The Looking Glass Authors * https://looking-glass.io * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., 59 * Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #include "Debug.h" /* credit: https://stackoverflow.com/questions/29049686/is-there-a-better-way-to-pass-formatted-output-to-outputdebugstring */ VOID _DBGPRINT(PCSTR kwszFunction, INT iLineNumber, LPCSTR kszDebugFormatString, ...) { INT cbFormatString = 0; va_list args; PCHAR szDebugString = NULL; size_t stOffset = 0; va_start(args, kszDebugFormatString); cbFormatString = _scprintf("[%s:%d] ", kwszFunction, iLineNumber); cbFormatString += _vscprintf(kszDebugFormatString, args); cbFormatString += 2; /* Depending on the size of the format string, allocate space on the stack or the heap. */ szDebugString = (PCHAR)_malloca(cbFormatString); if (!szDebugString) return; /* Populate the buffer with the contents of the format string. */ StringCbPrintfA(szDebugString, cbFormatString, "[%s:%d] ", kwszFunction, iLineNumber); StringCbLengthA(szDebugString, cbFormatString, &stOffset); StringCbVPrintfA(&szDebugString[stOffset], cbFormatString - stOffset, kszDebugFormatString, args); OutputDebugStringA(szDebugString); _freea(szDebugString); va_end(args); } VOID _DBGPRINT_HR(PCSTR kwszFunction, INT iLineNumber, LPCSTR kszDebugFormatString, HRESULT status, ...) { char * buffer; if (!FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, status, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (char*)&buffer, 1024, NULL )) { DBGPRINT("FormatMessage failed with code 0x%08x", GetLastError()); return; } INT cbFormatString = 0; va_list args; PCHAR szDebugString = NULL; size_t stOffset = 0; va_start(args, kszDebugFormatString); cbFormatString = _scprintf("[%s:%d] ", kwszFunction, iLineNumber); cbFormatString += _vscprintf(kszDebugFormatString, args); cbFormatString += 2 + 4 + (int)strlen(buffer); /* Depending on the size of the format string, allocate space on the stack or the heap. */ szDebugString = (PCHAR)_malloca(cbFormatString); if (!szDebugString) { va_end(args); return; } /* Populate the buffer with the contents of the format string. */ StringCbPrintfA(szDebugString, cbFormatString, "[%s:%d] ", kwszFunction, iLineNumber); StringCbLengthA(szDebugString, cbFormatString, &stOffset); StringCbVPrintfA(&szDebugString[stOffset], cbFormatString - stOffset, kszDebugFormatString, args); /* append the formatted error */ StringCbLengthA(szDebugString, cbFormatString, &stOffset); StringCbPrintfA(&szDebugString[stOffset], cbFormatString - stOffset, " (%s)", buffer); OutputDebugStringA(szDebugString); _freea(szDebugString); va_end(args); LocalFree(buffer); }