From b8a674ffdd1295fe98b21f207016e6379a329793 Mon Sep 17 00:00:00 2001 From: narzoul Date: Tue, 9 Aug 2016 15:38:31 +0200 Subject: [PATCH] Refactored logging of structs --- DDrawCompat/DDrawLog.cpp | 87 +++++++++++++++++++++++----------------- DDrawCompat/DDrawLog.h | 57 ++++++++++++++++++++++++-- 2 files changed, 103 insertions(+), 41 deletions(-) diff --git a/DDrawCompat/DDrawLog.cpp b/DDrawCompat/DDrawLog.cpp index 3a92a29..f69c0df 100644 --- a/DDrawCompat/DDrawLog.cpp +++ b/DDrawCompat/DDrawLog.cpp @@ -7,19 +7,15 @@ namespace { - template - bool isEmptyStruct(const T& t) - { - static T empty = {}; - empty.dwSize = t.dwSize; - return 0 == memcmp(&t, &empty, sizeof(t)); - } - template std::ostream& streamDevMode(std::ostream& os, const DevMode& dm) { - return os << "DM(" << dm.dmPelsWidth << ',' << dm.dmPelsHeight << ',' << dm.dmBitsPerPel << - ',' << dm.dmDisplayFrequency << ',' << dm.dmDisplayFlags << ')'; + return Compat::LogStruct(os) + << dm.dmPelsWidth + << dm.dmPelsHeight + << dm.dmBitsPerPel + << dm.dmDisplayFrequency + << dm.dmDisplayFlags; } } @@ -61,7 +57,11 @@ std::ostream& operator<<(std::ostream& os, const DEVMODEW& dm) std::ostream& operator<<(std::ostream& os, const RECT& rect) { - return os << "R(" << rect.left << ',' << rect.top << ',' << rect.right << ',' << rect.bottom << ')'; + return Compat::LogStruct(os) + << rect.left + << rect.top + << rect.right + << rect.bottom; } std::ostream& operator<<(std::ostream& os, HDC__& dc) @@ -80,59 +80,72 @@ std::ostream& operator<<(std::ostream& os, HWND__& hwnd) std::ostream& operator<<(std::ostream& os, const DDSCAPS& caps) { - return os << "C(" << std::hex << caps.dwCaps << std::dec << ')'; + return Compat::LogStruct(os) + << Compat::hex(caps.dwCaps); } std::ostream& operator<<(std::ostream& os, const DDSCAPS2& caps) { - return os << "C(" << std::hex << - caps.dwCaps << ',' << caps.dwCaps2 << ',' << caps.dwCaps3 << ',' << caps.dwCaps4 << std::dec << ')'; + return Compat::LogStruct(os) + << Compat::hex(caps.dwCaps) + << Compat::hex(caps.dwCaps2) + << Compat::hex(caps.dwCaps3) + << Compat::hex(caps.dwCaps4); } std::ostream& operator<<(std::ostream& os, const DDPIXELFORMAT& pf) { - if (isEmptyStruct(pf)) - { - return os << "PF()"; - } - - return os << "PF(" << std::hex << pf.dwFlags << "," << pf.dwFourCC << "," << - std::dec << pf.dwRGBBitCount << "," << - std::hex << pf.dwRBitMask << "," << pf.dwGBitMask << "," << pf.dwBBitMask << "," << - pf.dwRGBAlphaBitMask << std::dec << ')'; + return Compat::LogStruct(os) + << Compat::hex(pf.dwFlags) + << Compat::hex(pf.dwFourCC) + << pf.dwRGBBitCount + << Compat::hex(pf.dwRBitMask) + << Compat::hex(pf.dwGBitMask) + << Compat::hex(pf.dwBBitMask) + << Compat::hex(pf.dwRGBAlphaBitMask); } std::ostream& operator<<(std::ostream& os, const DDSURFACEDESC& sd) { DDSURFACEDESC2 sd2 = {}; memcpy(&sd2, &sd, sizeof(sd)); - sd2.dwSize = sizeof(sd2); return os << sd2; } std::ostream& operator<<(std::ostream& os, const DDSURFACEDESC2& sd) { - if (isEmptyStruct(sd)) - { - return os << "SD()"; - } - - return os << "SD(" << std::hex << sd.dwFlags << std::dec << "," << - sd.dwHeight << "," << sd.dwWidth << "," << sd.lPitch << "," << sd.dwBackBufferCount << "," << - sd.dwMipMapCount << "," << sd.dwAlphaBitDepth << "," << sd.dwReserved << "," << - sd.lpSurface << "," << sd.ddpfPixelFormat << "," << sd.ddsCaps << "," << sd.dwTextureStage << ')'; + return Compat::LogStruct(os) + << Compat::hex(sd.dwFlags) + << sd.dwHeight + << sd.dwWidth + << sd.lPitch + << sd.dwBackBufferCount + << sd.dwMipMapCount + << sd.dwAlphaBitDepth + << sd.dwReserved + << sd.lpSurface + << sd.ddpfPixelFormat + << sd.ddsCaps + << sd.dwTextureStage; } std::ostream& operator<<(std::ostream& os, const CWPSTRUCT& cwrp) { - return os << "CWP(" << std::hex << cwrp.message << "," << std::dec << cwrp.hwnd << "," << - std::hex << cwrp.wParam << "," << cwrp.lParam << std::dec << ")"; + return Compat::LogStruct(os) + << Compat::hex(cwrp.message) + << cwrp.hwnd + << Compat::hex(cwrp.wParam) + << Compat::hex(cwrp.lParam); } std::ostream& operator<<(std::ostream& os, const CWPRETSTRUCT& cwrp) { - return os << "CWRP(" << std::hex << cwrp.message << "," << std::dec << cwrp.hwnd << "," << - std::hex << cwrp.wParam << "," << cwrp.lParam << "," << cwrp.lResult << std::dec << ")"; + return Compat::LogStruct(os) + << Compat::hex(cwrp.message) + << cwrp.hwnd + << Compat::hex(cwrp.wParam) + << Compat::hex(cwrp.lParam) + << Compat::hex(cwrp.lResult); } namespace Compat diff --git a/DDrawCompat/DDrawLog.h b/DDrawCompat/DDrawLog.h index 076c714..925c28a 100644 --- a/DDrawCompat/DDrawLog.h +++ b/DDrawCompat/DDrawLog.h @@ -4,6 +4,7 @@ #include #include +#include #include #define LOG_ONCE(msg) \ @@ -52,6 +53,15 @@ std::ostream& operator<<(std::ostream& os, T** t) namespace Compat { + template + struct Hex + { + explicit Hex(Num val) : val(val) {} + Num val; + }; + + template Hex hex(Num val) { return Hex(val); } + class Log { public: @@ -95,6 +105,37 @@ namespace Compat static std::ofstream s_logFile; }; + class LogParams; + + class LogFirstParam + { + public: + LogFirstParam(std::ostream& os) : m_os(os) {} + template LogParams operator<<(const T& val) { m_os << val; return LogParams(m_os); } + + protected: + std::ostream& m_os; + }; + + class LogParams + { + public: + LogParams(std::ostream& os) : m_os(os) {} + template LogParams& operator<<(const T& val) { m_os << ',' << val; return *this; } + + operator std::ostream&() { return m_os; } + + private: + std::ostream& m_os; + }; + + class LogStruct : public LogFirstParam + { + public: + LogStruct(std::ostream& os) : LogFirstParam(os) { m_os << '{'; } + ~LogStruct() { m_os << '}'; } + }; + #ifdef _DEBUG typedef Log LogDebug; @@ -122,19 +163,27 @@ namespace Compat } }; #else - class LogDebug + class LogNull { public: - template LogDebug& operator<<(const T&) { return *this; } + template LogNull& operator<<(const T&) { return *this; } }; - class LogEnter + typedef LogNull LogDebug; + + class LogEnter : public LogNull { public: template LogEnter(const char*, Params...) {} - template void operator<<(const Result&) {} }; typedef LogEnter LogLeave; #endif } + +template +std::ostream& operator<<(std::ostream& os, Compat::Hex hex) +{ + os << "0x" << std::hex << hex.val << std::dec; + return os; +}