diff --git a/DDrawCompat/Common/Log.cpp b/DDrawCompat/Common/Log.cpp index c60b7dc..e113b80 100644 --- a/DDrawCompat/Common/Log.cpp +++ b/DDrawCompat/Common/Log.cpp @@ -26,6 +26,11 @@ std::ostream& operator<<(std::ostream& os, const char* str) return os << "null"; } + if (!Compat::Log::isPointerDereferencingAllowed()) + { + return os << static_cast(str); + } + return os.write(str, strlen(str)); } @@ -41,6 +46,11 @@ std::ostream& operator<<(std::ostream& os, const WCHAR* wstr) return os << "null"; } + if (!Compat::Log::isPointerDereferencingAllowed()) + { + return os << static_cast(wstr); + } + CStringA str(wstr); return os << '"' << static_cast(str) << '"'; } @@ -167,4 +177,6 @@ namespace Compat } std::ofstream Log::s_logFile("ddraw.log"); + DWORD Log::s_outParamDepth = 0; + bool Log::s_isLeaveLog = false; } diff --git a/DDrawCompat/Common/Log.h b/DDrawCompat/Common/Log.h index ce97588..259831a 100644 --- a/DDrawCompat/Common/Log.h +++ b/DDrawCompat/Common/Log.h @@ -31,28 +31,10 @@ std::ostream& operator<<(std::ostream& os, const DDSURFACEDESC2& sd); std::ostream& operator<<(std::ostream& os, const CWPSTRUCT& cwrp); std::ostream& operator<<(std::ostream& os, const CWPRETSTRUCT& cwrp); -template -typename std::enable_if::value && !std::is_same::value, std::ostream&>::type -operator<<(std::ostream& os, const T& t) -{ - return os << static_cast(&t); -} - -template -typename std::enable_if::value, std::ostream&>::type -operator<<(std::ostream& os, T* t) -{ - return t ? (os << *t) : (os << "null"); -} - -template -std::ostream& operator<<(std::ostream& os, T** t) -{ - return t ? (os << reinterpret_cast(t) << '=' << *t) : (os << "null"); -} - namespace Compat { + using ::operator<<; + template struct Hex { @@ -76,6 +58,15 @@ namespace Compat return Array(elem, size); } + template + struct Out + { + explicit Out(const T& val) : val(val) {} + const T& val; + }; + + template Out out(const T& val) { return Out(val); } + class Log { public: @@ -89,6 +80,8 @@ namespace Compat return *this; } + static bool isPointerDereferencingAllowed() { return s_isLeaveLog || 0 == s_outParamDepth; } + protected: template Log(const char* prefix, const char* funcName, Params... params) : Log() @@ -99,6 +92,9 @@ namespace Compat } private: + friend class LogLeaveGuard; + template friend std::ostream& operator<<(std::ostream& os, Out out); + void toList() { } @@ -117,6 +113,8 @@ namespace Compat } static std::ofstream s_logFile; + static DWORD s_outParamDepth; + static bool s_isLeaveLog; }; class LogParams; @@ -162,7 +160,14 @@ namespace Compat } }; - class LogLeave : private Log + class LogLeaveGuard + { + public: + LogLeaveGuard() { Log::s_isLeaveLog = true; } + ~LogLeaveGuard() { Log::s_isLeaveLog = false; } + }; + + class LogLeave : private LogLeaveGuard, private Log { public: template @@ -193,26 +198,80 @@ namespace Compat typedef LogEnter LogLeave; #endif + + template + std::ostream& operator<<(std::ostream& os, Hex hex) + { + os << "0x" << std::hex << hex.val << std::dec; + return os; + } + + template + std::ostream& operator<<(std::ostream& os, Array array) + { + os << '['; + if (Log::isPointerDereferencingAllowed()) + { + if (0 != array.size) + { + os << array.elem[0]; + } + for (unsigned long i = 1; i < array.size; ++i) + { + os << ',' << array.elem[i]; + } + } + return os << ']'; + } + + template + std::ostream& operator<<(std::ostream& os, Out out) + { + ++Log::s_outParamDepth; + os << out.val; + --Log::s_outParamDepth; + return os; + } } -template -std::ostream& operator<<(std::ostream& os, Compat::Hex hex) +template +typename std::enable_if::value && !std::is_same::value, std::ostream&>::type +operator<<(std::ostream& os, const T& t) { - os << "0x" << std::hex << hex.val << std::dec; + return os << static_cast(&t); +} + +template +typename std::enable_if::value, std::ostream&>::type +operator<<(std::ostream& os, T* t) +{ + if (!t) + { + return os << "null"; + } + + if (!Compat::Log::isPointerDereferencingAllowed()) + { + return os << static_cast(t); + } + + return os << *t; +} + +template +std::ostream& operator<<(std::ostream& os, T** t) +{ + if (!t) + { + return os << "null"; + } + + os << static_cast(t); + + if (Compat::Log::isPointerDereferencingAllowed()) + { + os << '=' << *t; + } + return os; } - -template -std::ostream& operator<<(std::ostream& os, Compat::Array array) -{ - os << '['; - if (0 != array.size) - { - os << array.elem[0]; - } - for (unsigned long i = 1; i < array.size; ++i) - { - os << ',' << array.elem[i]; - } - return os << ']'; -} diff --git a/DDrawCompat/D3dDdi/Hooks.cpp b/DDrawCompat/D3dDdi/Hooks.cpp index bd46202..6166bb2 100644 --- a/DDrawCompat/D3dDdi/Hooks.cpp +++ b/DDrawCompat/D3dDdi/Hooks.cpp @@ -20,7 +20,7 @@ std::ostream& operator<<(std::ostream& os, const D3DDDIARG_OPENADAPTER& data) << data.Interface << data.Version << data.pAdapterCallbacks - << data.pAdapterFuncs + << Compat::out(data.pAdapterFuncs) << data.DriverVersion; } diff --git a/DDrawCompat/D3dDdi/Log/AdapterFuncsLog.cpp b/DDrawCompat/D3dDdi/Log/AdapterFuncsLog.cpp index eb99430..97c3419 100644 --- a/DDrawCompat/D3dDdi/Log/AdapterFuncsLog.cpp +++ b/DDrawCompat/D3dDdi/Log/AdapterFuncsLog.cpp @@ -28,11 +28,11 @@ std::ostream& operator<<(std::ostream& os, const D3DDDIARG_CREATEDEVICE& data) << data.pCallbacks << data.pCommandBuffer << data.CommandBufferSize - << data.pAllocationList + << Compat::out(Compat::array(data.pAllocationList, data.AllocationListSize)) << data.AllocationListSize - << data.pPatchLocationList + << Compat::out(Compat::array(data.pPatchLocationList, data.PatchLocationListSize)) << data.PatchLocationListSize - << data.pDeviceFuncs + << Compat::out(data.pDeviceFuncs) << Compat::hex(data.Flags.Value) << Compat::hex(data.CommandBuffer); } diff --git a/DDrawCompat/D3dDdi/Log/KernelModeThunksLog.cpp b/DDrawCompat/D3dDdi/Log/KernelModeThunksLog.cpp index c887004..c1cde2b 100644 --- a/DDrawCompat/D3dDdi/Log/KernelModeThunksLog.cpp +++ b/DDrawCompat/D3dDdi/Log/KernelModeThunksLog.cpp @@ -14,9 +14,9 @@ std::ostream& operator<<(std::ostream& os, const D3DKMT_CREATECONTEXT& data) << Compat::hex(data.hContext) << data.pCommandBuffer << data.CommandBufferSize - << Compat::array(data.pAllocationList, data.AllocationListSize) + << Compat::out(Compat::array(data.pAllocationList, data.AllocationListSize)) << data.AllocationListSize - << Compat::array(data.pPatchLocationList, data.PatchLocationListSize) + << Compat::out(Compat::array(data.pPatchLocationList, data.PatchLocationListSize)) << data.PatchLocationListSize << Compat::hex(data.CommandBuffer); } @@ -42,9 +42,9 @@ std::ostream& operator<<(std::ostream& os, const D3DKMT_CREATEDEVICE& data) << Compat::hex(data.hDevice) << data.pCommandBuffer << data.CommandBufferSize - << Compat::array(data.pAllocationList, data.AllocationListSize) + << Compat::out(Compat::array(data.pAllocationList, data.AllocationListSize)) << data.AllocationListSize - << Compat::array(data.pPatchLocationList, data.PatchLocationListSize) + << Compat::out(Compat::array(data.pPatchLocationList, data.PatchLocationListSize)) << data.PatchLocationListSize; }