[idd] CDebug: implement new debug print class and write to a file

This commit is contained in:
Geoffrey McRae 2025-03-16 16:11:21 +00:00
parent 4f2eb984d3
commit 0db9d3a27b
17 changed files with 260 additions and 131 deletions

View File

@ -19,7 +19,7 @@
*/
#include "CD3D11Device.h"
#include "Debug.h"
#include "CDebug.h"
HRESULT CD3D11Device::Init()
{

View File

@ -1,5 +1,5 @@
#include "CD3D12CommandQueue.h"
#include "Debug.h"
#include "CDebug.h"
bool CD3D12CommandQueue::Init(ID3D12Device3 * device, D3D12_COMMAND_LIST_TYPE type, const WCHAR* name)
{

View File

@ -1,5 +1,5 @@
#include "CD3D12Device.h"
#include "Debug.h"
#include "CDebug.h"
CD3D12Device::CD3D12Device(LUID adapterLuid) :
m_adapterLuid(adapterLuid),

199
idd/LGIdd/CDebug.cpp Normal file
View File

@ -0,0 +1,199 @@
/**
* 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 <Windows.h>
#include <string>
#include <malloc.h>
#include <strsafe.h>
#include "CDebug.h"
CDebug g_debug;
CDebug::CDebug()
{
// don't redirect the debug output if running under a debugger
if (IsDebuggerPresent())
return;
// get the system temp directory
char tempPath[MAX_PATH];
DWORD pathLen = GetTempPathA(sizeof(tempPath), tempPath);
if (pathLen == 0)
{
DBGPRINT_HR(GetLastError(), "Failed to get the temp path");
return;
}
std::string folder = tempPath;
std::string baseName = "looking-glass-idd";
std::string ext = ".txt";
std::string logFile = folder + baseName + ext;
//rotate out old logs
DeleteFileA((folder + baseName + ".5" + ext).c_str());
for (int i = 4; i >= 0; --i)
{
std::string oldPath;
std::string newPath;
if (i == 0)
{
oldPath = logFile;
newPath = folder + baseName + ".1" + ext;
}
else
{
oldPath = folder + baseName + "." + std::to_string(i) + ext;
newPath = folder + baseName + "." + std::to_string(i + 1) + ext;
}
MoveFileA(oldPath.c_str(), newPath.c_str());
}
/// open the new log file
std::ofstream stream(logFile, std::ios::out | std::ios::trunc);
if (!stream.is_open())
{
DBGPRINT_HR(GetLastError(), "Failed to open the log file %s", logFile.c_str());
return;
}
else
DBGPRINT("Logging to: %s", logFile.c_str());
m_stream = std::move(stream);
}
void CDebug::Log(CDebug::Level level, const char * function, int line, const char * fmt, ...)
{
if (level < 0 || level >= LEVEL_MAX)
level = LEVEL_NONE;
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);
length += 2;
/* Depending on the size of the format string, allocate space on the stack or the heap. */
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);
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';
Write(buffer);
_freea(buffer);
}
void CDebug::LogHR(CDebug::Level level, HRESULT hr, const char * function, int line, const char * fmt, ...)
{
if (level < 0 || level >= LEVEL_MAX)
level = LEVEL_NONE;
char * hrBuffer;
if (!FormatMessageA(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
hr,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(char*)&hrBuffer,
1024,
NULL
))
{
DBGPRINT("FormatMessage failed with code 0x%08x", GetLastError());
return;
}
// Remove trailing CRLF in hrBuffer
size_t len = strlen(hrBuffer);
while (len && (hrBuffer[len - 1] == '\n' || hrBuffer[len - 1] == '\r'))
hrBuffer[--len] = '\0';
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);
length += 2 + 4 + (int)strlen(hrBuffer) + 1;
/* Depending on the size of the format string, allocate space on the stack or the heap. */
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);
size_t offset = 0;
StringCbLengthA(buffer, length, &offset);
StringCbVPrintfA(&buffer[offset], length - offset, fmt, args);
va_end(args);
/* append the formatted error */
StringCbLengthA(buffer, length, &offset);
StringCbPrintfA(&buffer[offset], length - offset, " (%s)\n", hrBuffer);
Write(buffer);
_freea(buffer);
LocalFree(hrBuffer);
}
void CDebug::Write(const char * line)
{
if (m_stream.is_open())
{
m_stream << line;
m_stream.flush();
}
else
OutputDebugStringA(line);
}

View File

@ -21,12 +21,51 @@
#pragma once
#include <Windows.h>
#include <tchar.h>
#include <wdf.h>
#include <fstream>
class CDebug
{
private:
std::ofstream m_stream;
void Write(const char * line);
public:
CDebug();
enum Level
{
LEVEL_NONE = 0,
LEVEL_INFO,
LEVEL_WARN,
LEVEL_ERROR,
LEVEL_TRACE,
LEVEL_FIXME,
LEVEL_FATAL,
LEVEL_MAX
};
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, ...);
private:
const char* m_levelStr[LEVEL_MAX] =
{
" ",
"I",
"W",
"E",
"T",
"!",
"F"
};
};
extern CDebug g_debug;
VOID _DBGPRINT(PCSTR kszFunction, INT iLineNumber, LPCSTR kszDebugFormatString, ...);
#define DBGPRINT(kszDebugFormatString, ...) \
_DBGPRINT(__FUNCTION__, __LINE__, kszDebugFormatString "\n", __VA_ARGS__)
g_debug.Log(CDebug::LEVEL_INFO, __FUNCTION__, __LINE__, kszDebugFormatString, __VA_ARGS__)
VOID _DBGPRINT_HR(PCSTR kwszFunction, INT iLineNumber, LPCSTR kszDebugFormatString, HRESULT status, ...);
#define DBGPRINT_HR(status, kszDebugFormatString, ...) \
_DBGPRINT_HR(__FUNCTION__, __LINE__, kszDebugFormatString "\n", status, __VA_ARGS__)
g_debug.LogHR(CDebug::LEVEL_INFO, status, __FUNCTION__, __LINE__, kszDebugFormatString, __VA_ARGS__)

View File

@ -1,6 +1,6 @@
#include "CFrameBufferResource.h"
#include "CSwapChainProcessor.h"
#include "Debug.h"
#include "CDebug.h"
bool CFrameBufferResource::Init(CSwapChainProcessor * swapChain, uint8_t * base, size_t size)
{

View File

@ -25,7 +25,7 @@
#include <algorithm>
#include <winioctl.h>
#include "Debug.h"
#include "CDebug.h"
#include "ivshmem/ivshmem.h"
CIVSHMEM::CIVSHMEM()

View File

@ -22,7 +22,7 @@
#include "CIndirectMonitorContext.h"
#include "CPlatformInfo.h"
#include "Debug.h"
#include "CDebug.h"
#include <sstream>

View File

@ -20,7 +20,7 @@
#include "CIndirectMonitorContext.h"
#include "CPlatformInfo.h"
#include "Debug.h"
#include "CDebug.h"
CIndirectMonitorContext::CIndirectMonitorContext(_In_ IDDCX_MONITOR monitor, CIndirectDeviceContext * device) :
m_monitor(monitor),

View File

@ -1,6 +1,5 @@
#include "CInteropResource.h"
#include "Debug.h"
#include "CDebug.h"
bool CInteropResource::Init(std::shared_ptr<CD3D11Device> dx11Device, std::shared_ptr<CD3D12Device> dx12Device, ComPtr<ID3D11Texture2D> srcTex)
{

View File

@ -1,5 +1,5 @@
#include "CInteropResourcePool.h"
#include "Debug.h"
#include "CDebug.h"
void CInteropResourcePool::Init(std::shared_ptr<CD3D11Device> dx11Device, std::shared_ptr<CD3D12Device> dx12Device)
{

View File

@ -20,7 +20,7 @@
#include "CPlatformInfo.h"
#include "Debug.h"
#include "CDebug.h"
#include <Windows.h>
size_t CPlatformInfo::m_pageSize = 0;

View File

@ -21,7 +21,7 @@
#include "CSwapChainProcessor.h"
#include <avrt.h>
#include "Debug.h"
#include "CDebug.h"
#define LOCK(lock) \
while (InterlockedCompareExchange((volatile LONG*)&(lock), 1, 0) != 0) {};
@ -111,7 +111,7 @@ void CSwapChainProcessor::SwapChainThreadCore()
hr = IddCxSwapChainSetDevice(m_hSwapChain, &setDevice);
if (FAILED(hr))
{
DBGPRINT("IddCxSwapChainSetDevice Failed (%08x)", hr);
DBGPRINT_HR(hr, "IddCxSwapChainSetDevice Failed");
return;
}

View File

@ -1,108 +0,0 @@
/**
* 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 <Windows.h>
#include <malloc.h>
#include <strsafe.h>
#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);
}

View File

@ -29,7 +29,7 @@
#include <avrt.h>
#include <wrl.h>
#include "Debug.h"
#include "CDebug.h"
#include "CIndirectDeviceContext.h"
#include "CIndirectMonitorContext.h"

View File

@ -49,7 +49,7 @@
<ClCompile Include="CPlatformInfo.cpp" />
<ClCompile Include="CSwapChainProcessor.cpp" />
<ClCompile Include="CD3D12Device.cpp" />
<ClCompile Include="Debug.cpp" />
<ClCompile Include="CDebug.cpp" />
<ClCompile Include="Device.cpp" />
<ClCompile Include="CD3D11Device.cpp" />
<ClCompile Include="Driver.cpp" />
@ -65,7 +65,7 @@
<ClInclude Include="CPlatformInfo.h" />
<ClInclude Include="CSwapChainProcessor.h" />
<ClInclude Include="CD3D12Device.h" />
<ClInclude Include="Debug.h" />
<ClInclude Include="CDebug.h" />
<ClInclude Include="Device.h" />
<ClInclude Include="CD3D11Device.h" />
<ClInclude Include="Driver.h" />

View File

@ -51,7 +51,7 @@
<ClInclude Include="CIVSHMEM.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Debug.h">
<ClInclude Include="CDebug.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="CPlatformInfo.h">
@ -98,7 +98,7 @@
<ClCompile Include="CIVSHMEM.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Debug.cpp">
<ClCompile Include="CDebug.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="CPlatformInfo.cpp">