Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: src/engine/Debug.cpp

Issue 10790071: Send debug output to a file (Closed)
Patch Set: Created May 31, 2013, 9:54 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/engine/Debug.h ('k') | src/engine/Utils.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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(&section);
+ }
+
+ ~CriticalSection()
Felix Dahlke 2013/06/04 06:57:58 Rule of Three again :P I suggest making copy const
+ {
+ DeleteCriticalSection(&section);
+ }
+
+ 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
« no previous file with comments | « src/engine/Debug.h ('k') | src/engine/Utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld