From c67437d5bff3692314fa169a372a7925b3cf00c1 Mon Sep 17 00:00:00 2001 From: narzoul Date: Tue, 5 May 2020 23:59:21 +0200 Subject: [PATCH] Separate log files per process name and instance number --- DDrawCompat/Common/Hook.cpp | 8 +++--- DDrawCompat/Common/Log.cpp | 27 +++++++++++++++++-- DDrawCompat/Common/Log.h | 43 ++++++++++--------------------- DDrawCompat/D3dDdi/Resource.cpp | 2 ++ DDrawCompat/Dll/DllMain.cpp | 6 +++-- DDrawCompat/Gdi/Font.cpp | 2 +- DDrawCompat/Gdi/PaintHandlers.cpp | 4 +-- 7 files changed, 51 insertions(+), 41 deletions(-) diff --git a/DDrawCompat/Common/Hook.cpp b/DDrawCompat/Common/Hook.cpp index dfe899f..95c0ddf 100644 --- a/DDrawCompat/Common/Hook.cpp +++ b/DDrawCompat/Common/Hook.cpp @@ -144,14 +144,14 @@ namespace GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, static_cast(hookedFuncPtr), &module); - Compat::LogDebug() << "Hooking function: " << funcName << " (" << funcAddrToStr(hookedFuncPtr) << ')'; + LOG_DEBUG << "Hooking function: " << funcName << " (" << funcAddrToStr(hookedFuncPtr) << ')'; DetourTransactionBegin(); const bool attachSuccessful = NO_ERROR == DetourAttach(&origFuncPtr, newFuncPtr); const bool commitSuccessful = NO_ERROR == DetourTransactionCommit(); if (!attachSuccessful || !commitSuccessful) { - Compat::LogDebug() << "ERROR: Failed to hook a function: " << funcName; + LOG_DEBUG << "ERROR: Failed to hook a function: " << funcName; return; } @@ -270,7 +270,7 @@ namespace Compat FARPROC procAddr = getProcAddress(module, funcName); if (!procAddr) { - Compat::LogDebug() << "ERROR: Failed to load the address of a function: " << funcName; + LOG_DEBUG << "ERROR: Failed to load the address of a function: " << funcName; return; } @@ -294,7 +294,7 @@ namespace Compat FARPROC* func = findProcAddressInIat(module, importedModuleName, funcName); if (func) { - Compat::LogDebug() << "Hooking function via IAT: " << funcName << " (" << funcAddrToStr(*func) << ')'; + LOG_DEBUG << "Hooking function via IAT: " << funcName << " (" << funcAddrToStr(*func) << ')'; DWORD oldProtect = 0; VirtualProtect(func, sizeof(func), PAGE_READWRITE, &oldProtect); *func = static_cast(newFuncPtr); diff --git a/DDrawCompat/Common/Log.cpp b/DDrawCompat/Common/Log.cpp index 7cdb956..425d16f 100644 --- a/DDrawCompat/Common/Log.cpp +++ b/DDrawCompat/Common/Log.cpp @@ -1,3 +1,5 @@ +#include + #include namespace @@ -74,9 +76,30 @@ namespace Compat s_logFile << std::endl; } - void Log::initLogging() + void Log::initLogging(std::string processName) { - s_logFile.open("ddraw.log"); + if (processName.length() >= 4 && + 0 == _strcmpi(processName.substr(processName.length() - 4).c_str(), ".exe")) + { + processName.resize(processName.length() - 4); + } + + for (int i = 1; i < 100; ++i) + { + std::ostringstream logFileName; + logFileName << "DDrawCompat-" << processName; + if (i > 1) + { + logFileName << '[' << i << ']'; + } + logFileName << ".log"; + + s_logFile.open(logFileName.str(), std::ios_base::out, SH_DENYWR); + if (!s_logFile.fail()) + { + return; + } + } } thread_local DWORD Log::s_indent = 0; diff --git a/DDrawCompat/Common/Log.h b/DDrawCompat/Common/Log.h index 7a6cdd3..bb08e7e 100644 --- a/DDrawCompat/Common/Log.h +++ b/DDrawCompat/Common/Log.h @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -11,8 +12,15 @@ #include #include +#ifdef DEBUGLOGS +#define LOG_DEBUG Compat::Log() #define LOG_FUNC(...) Compat::LogFunc logFunc(__VA_ARGS__) #define LOG_RESULT(...) logFunc.setResult(__VA_ARGS__) +#else +#define LOG_DEBUG if (false) Compat::Log() +#define LOG_FUNC(...) +#define LOG_RESULT(...) __VA_ARGS__ +#endif #define LOG_ONCE(msg) \ { \ @@ -145,7 +153,7 @@ namespace Compat return *this; } - static void initLogging(); + static void initLogging(std::string processName); static bool isPointerDereferencingAllowed() { return s_isLeaveLog || 0 == s_outParamDepth; } protected: @@ -187,16 +195,6 @@ namespace Compat static std::ofstream s_logFile; }; - class LogStruct : public detail::LogFirstParam - { - public: - LogStruct(std::ostream& os) : detail::LogFirstParam(os) { m_os << '{'; } - ~LogStruct() { m_os << '}'; } - }; - -#ifdef DEBUGLOGS - typedef Log LogDebug; - class LogFunc { public: @@ -237,28 +235,13 @@ namespace Compat std::function m_printCall; std::function m_printResult; }; -#else - class LogDebug + + class LogStruct : public detail::LogFirstParam { public: - template LogDebug& operator<<(const T&) { return *this; } + LogStruct(std::ostream& os) : detail::LogFirstParam(os) { m_os << '{'; } + ~LogStruct() { m_os << '}'; } }; - - class LogFunc - { - public: - template - LogFunc(const char* /*funcName*/, Params...) - { - } - - template - T setResult(T result) - { - return result; - } - }; -#endif } template diff --git a/DDrawCompat/D3dDdi/Resource.cpp b/DDrawCompat/D3dDdi/Resource.cpp index e478f96..aba3a7a 100644 --- a/DDrawCompat/D3dDdi/Resource.cpp +++ b/DDrawCompat/D3dDdi/Resource.cpp @@ -464,7 +464,9 @@ namespace D3dDdi } } +#ifdef DEBUGLOGS LOG_RESULT(m_lockResource.get()); +#endif } void Resource::endGdiAccess(bool isReadOnly) diff --git a/DDrawCompat/Dll/DllMain.cpp b/DDrawCompat/Dll/DllMain.cpp index be61963..ebddade 100644 --- a/DDrawCompat/Dll/DllMain.cpp +++ b/DDrawCompat/Dll/DllMain.cpp @@ -214,8 +214,10 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) return TRUE; } - Compat::Log::initLogging(); - Compat::Log() << "Process path: " << getModulePath(nullptr); + auto processPath = getModulePath(nullptr); + Compat::Log::initLogging(getFileName(processPath)); + + Compat::Log() << "Process path: " << processPath; printEnvironmentVariable("__COMPAT_LAYER"); auto currentDllPath = getModulePath(hinstDLL); Compat::Log() << "Loading DDrawCompat " << (lpvReserved ? "statically" : "dynamically") << " from " << currentDllPath; diff --git a/DDrawCompat/Gdi/Font.cpp b/DDrawCompat/Gdi/Font.cpp index 15ea050..0f2f2a3 100644 --- a/DDrawCompat/Gdi/Font.cpp +++ b/DDrawCompat/Gdi/Font.cpp @@ -7,7 +7,7 @@ namespace BOOL g_isFontSmoothingEnabled = FALSE; BOOL WINAPI systemParametersInfo(UINT uiAction, UINT uiParam, PVOID pvParam, UINT fWinIni, - decltype(&SystemParametersInfoA) origSystemParametersInfo, const char* origFuncName) + decltype(&SystemParametersInfoA) origSystemParametersInfo, [[maybe_unused]] const char* origFuncName) { LOG_FUNC(origFuncName, Compat::hex(uiAction), uiParam, pvParam, fWinIni); switch (uiAction) diff --git a/DDrawCompat/Gdi/PaintHandlers.cpp b/DDrawCompat/Gdi/PaintHandlers.cpp index 3a7ae7a..73175a5 100644 --- a/DDrawCompat/Gdi/PaintHandlers.cpp +++ b/DDrawCompat/Gdi/PaintHandlers.cpp @@ -160,7 +160,7 @@ namespace } LRESULT defPaintProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, WNDPROC origWndProc, - const char* origWndProcName) + [[maybe_unused]] const char* origWndProcName) { LOG_FUNC(origWndProcName, hwnd, Compat::logWm(msg), Compat::hex(wParam), Compat::hex(lParam)); return LOG_RESULT(defPaintProc(hwnd, msg, wParam, lParam, origWndProc)); @@ -431,7 +431,7 @@ namespace } LRESULT CALLBACK user32WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, - const std::string& procName, WndProcHook wndProcHook, WNDPROC oldWndProcTrampoline) + [[maybe_unused]] const std::string& procName, WndProcHook wndProcHook, WNDPROC oldWndProcTrampoline) { LOG_FUNC(procName.c_str(), hwnd, Compat::logWm(uMsg), Compat::hex(wParam), Compat::hex(lParam)); return LOG_RESULT(wndProcHook(hwnd, uMsg, wParam, lParam, oldWndProcTrampoline));