| Index: src/engine/Debug.cpp |
| =================================================================== |
| new file mode 100644 |
| --- /dev/null |
| +++ b/src/engine/Debug.cpp |
| @@ -0,0 +1,77 @@ |
| +#include "stdafx.h" |
| + |
| +#include <fstream> |
| +#include <stdio.h> |
| + |
| +#include "Debug.h" |
| +#include "Utils.h" |
| + |
| +#ifdef _DEBUG |
|
Felix Dahlke
2013/06/04 06:57:58
I assume this is something defined in the plugin?
Wladimir Palant
2013/06/04 09:33:59
No, this is defined by the compiler.
Felix Dahlke
2013/06/04 09:39:09
Hm, didn't know that one, seems to be MSVS only. B
|
| + |
| +namespace |
| +{ |
| + class CriticalSection |
| + { |
| + public: |
| + CriticalSection() |
| + { |
| + InitializeCriticalSection(§ion); |
| + } |
| + |
| + ~CriticalSection() |
|
Felix Dahlke
2013/06/04 06:57:58
Rule of Three again :P I suggest making copy const
|
| + { |
| + DeleteCriticalSection(§ion); |
| + } |
| + |
| + class Lock |
| + { |
| + public: |
| + Lock(CriticalSection& cs) |
| + : section(&cs.section) |
| + { |
| + EnterCriticalSection(section); |
| + } |
| + |
| + ~Lock() |
|
Felix Dahlke
2013/06/04 06:57:58
Same as above, should have a private copy construc
|
| + { |
| + LeaveCriticalSection(section); |
| + } |
| + private: |
| + LPCRITICAL_SECTION section; |
| + }; |
| + private: |
| + CRITICAL_SECTION section; |
| + }; |
| + |
| + static CriticalSection debugLock; |
|
Felix Dahlke
2013/06/04 06:57:58
static in namespaces is deprecated in C++. Don't t
|
| +} |
| + |
| +void Debug(const std::string& text) |
| +{ |
| + SYSTEMTIME st; |
| + ::GetSystemTime(&st); |
| + |
| + char timeBuf[14]; |
| + _snprintf_s(timeBuf, _TRUNCATE, "%02i:%02i:%02i.%03i", st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); |
| + |
| + std::wstring filePath = GetAppDataPath() + L"\\debug_engine.txt"; |
| + |
| + CriticalSection::Lock lock(debugLock); |
| + std::ofstream out(filePath, std::ios::app); |
| + out << timeBuf << " - " << text << std::endl; |
| + out.flush(); |
| +} |
| + |
| +void DebugLastError(const std::string& message) |
| +{ |
| + std::stringstream stream; |
| + stream << message << " (Error code: " << GetLastError() << ")"; |
| + Debug(stream.str()); |
| +} |
| + |
| +void DebugException(const std::exception& exception) |
| +{ |
| + Debug(std::string("An exception occurred: ") + exception.what()); |
| +} |
| + |
| +#endif // _DEBUG |