1
0
mirror of https://github.com/narzoul/DDrawCompat synced 2024-12-30 08:55:36 +01:00

Refactored logging of structs

This commit is contained in:
narzoul 2016-08-09 15:38:31 +02:00
parent 12ad482d29
commit b8a674ffdd
2 changed files with 103 additions and 41 deletions

View File

@ -7,19 +7,15 @@
namespace namespace
{ {
template <typename T>
bool isEmptyStruct(const T& t)
{
static T empty = {};
empty.dwSize = t.dwSize;
return 0 == memcmp(&t, &empty, sizeof(t));
}
template <typename DevMode> template <typename DevMode>
std::ostream& streamDevMode(std::ostream& os, const DevMode& dm) std::ostream& streamDevMode(std::ostream& os, const DevMode& dm)
{ {
return os << "DM(" << dm.dmPelsWidth << ',' << dm.dmPelsHeight << ',' << dm.dmBitsPerPel << return Compat::LogStruct(os)
',' << dm.dmDisplayFrequency << ',' << dm.dmDisplayFlags << ')'; << 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) 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) 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) 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) std::ostream& operator<<(std::ostream& os, const DDSCAPS2& caps)
{ {
return os << "C(" << std::hex << return Compat::LogStruct(os)
caps.dwCaps << ',' << caps.dwCaps2 << ',' << caps.dwCaps3 << ',' << caps.dwCaps4 << std::dec << ')'; << 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) std::ostream& operator<<(std::ostream& os, const DDPIXELFORMAT& pf)
{ {
if (isEmptyStruct(pf)) return Compat::LogStruct(os)
{ << Compat::hex(pf.dwFlags)
return os << "PF()"; << Compat::hex(pf.dwFourCC)
} << pf.dwRGBBitCount
<< Compat::hex(pf.dwRBitMask)
return os << "PF(" << std::hex << pf.dwFlags << "," << pf.dwFourCC << "," << << Compat::hex(pf.dwGBitMask)
std::dec << pf.dwRGBBitCount << "," << << Compat::hex(pf.dwBBitMask)
std::hex << pf.dwRBitMask << "," << pf.dwGBitMask << "," << pf.dwBBitMask << "," << << Compat::hex(pf.dwRGBAlphaBitMask);
pf.dwRGBAlphaBitMask << std::dec << ')';
} }
std::ostream& operator<<(std::ostream& os, const DDSURFACEDESC& sd) std::ostream& operator<<(std::ostream& os, const DDSURFACEDESC& sd)
{ {
DDSURFACEDESC2 sd2 = {}; DDSURFACEDESC2 sd2 = {};
memcpy(&sd2, &sd, sizeof(sd)); memcpy(&sd2, &sd, sizeof(sd));
sd2.dwSize = sizeof(sd2);
return os << sd2; return os << sd2;
} }
std::ostream& operator<<(std::ostream& os, const DDSURFACEDESC2& sd) std::ostream& operator<<(std::ostream& os, const DDSURFACEDESC2& sd)
{ {
if (isEmptyStruct(sd)) return Compat::LogStruct(os)
{ << Compat::hex(sd.dwFlags)
return os << "SD()"; << sd.dwHeight
} << sd.dwWidth
<< sd.lPitch
return os << "SD(" << std::hex << sd.dwFlags << std::dec << "," << << sd.dwBackBufferCount
sd.dwHeight << "," << sd.dwWidth << "," << sd.lPitch << "," << sd.dwBackBufferCount << "," << << sd.dwMipMapCount
sd.dwMipMapCount << "," << sd.dwAlphaBitDepth << "," << sd.dwReserved << "," << << sd.dwAlphaBitDepth
sd.lpSurface << "," << sd.ddpfPixelFormat << "," << sd.ddsCaps << "," << sd.dwTextureStage << ')'; << sd.dwReserved
<< sd.lpSurface
<< sd.ddpfPixelFormat
<< sd.ddsCaps
<< sd.dwTextureStage;
} }
std::ostream& operator<<(std::ostream& os, const CWPSTRUCT& cwrp) std::ostream& operator<<(std::ostream& os, const CWPSTRUCT& cwrp)
{ {
return os << "CWP(" << std::hex << cwrp.message << "," << std::dec << cwrp.hwnd << "," << return Compat::LogStruct(os)
std::hex << cwrp.wParam << "," << cwrp.lParam << std::dec << ")"; << Compat::hex(cwrp.message)
<< cwrp.hwnd
<< Compat::hex(cwrp.wParam)
<< Compat::hex(cwrp.lParam);
} }
std::ostream& operator<<(std::ostream& os, const CWPRETSTRUCT& cwrp) std::ostream& operator<<(std::ostream& os, const CWPRETSTRUCT& cwrp)
{ {
return os << "CWRP(" << std::hex << cwrp.message << "," << std::dec << cwrp.hwnd << "," << return Compat::LogStruct(os)
std::hex << cwrp.wParam << "," << cwrp.lParam << "," << cwrp.lResult << std::dec << ")"; << Compat::hex(cwrp.message)
<< cwrp.hwnd
<< Compat::hex(cwrp.wParam)
<< Compat::hex(cwrp.lParam)
<< Compat::hex(cwrp.lResult);
} }
namespace Compat namespace Compat

View File

@ -4,6 +4,7 @@
#include <ddraw.h> #include <ddraw.h>
#include <fstream> #include <fstream>
#include <ostream>
#include <type_traits> #include <type_traits>
#define LOG_ONCE(msg) \ #define LOG_ONCE(msg) \
@ -52,6 +53,15 @@ std::ostream& operator<<(std::ostream& os, T** t)
namespace Compat namespace Compat
{ {
template <typename Num>
struct Hex
{
explicit Hex(Num val) : val(val) {}
Num val;
};
template <typename Num> Hex<Num> hex(Num val) { return Hex<Num>(val); }
class Log class Log
{ {
public: public:
@ -95,6 +105,37 @@ namespace Compat
static std::ofstream s_logFile; static std::ofstream s_logFile;
}; };
class LogParams;
class LogFirstParam
{
public:
LogFirstParam(std::ostream& os) : m_os(os) {}
template <typename T> 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 <typename T> 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 #ifdef _DEBUG
typedef Log LogDebug; typedef Log LogDebug;
@ -122,19 +163,27 @@ namespace Compat
} }
}; };
#else #else
class LogDebug class LogNull
{ {
public: public:
template <typename T> LogDebug& operator<<(const T&) { return *this; } template <typename T> LogNull& operator<<(const T&) { return *this; }
}; };
class LogEnter typedef LogNull LogDebug;
class LogEnter : public LogNull
{ {
public: public:
template <typename... Params> LogEnter(const char*, Params...) {} template <typename... Params> LogEnter(const char*, Params...) {}
template <typename Result> void operator<<(const Result&) {}
}; };
typedef LogEnter LogLeave; typedef LogEnter LogLeave;
#endif #endif
} }
template <typename Num>
std::ostream& operator<<(std::ostream& os, Compat::Hex<Num> hex)
{
os << "0x" << std::hex << hex.val << std::dec;
return os;
}