changeset 48:4663f93aefc4

Tracing
author koryspansel
date Fri, 23 Sep 2011 11:50:59 -0700
parents 81d413160824
children 48705dc6e568
files .hgignore LightClone/LightClone.vcproj LightClone/Source/Core.h LightClone/Source/GraphicsDevice.cpp LightClone/Source/Mediator.cpp LightClone/Source/Trace.cpp LightClone/Source/Trace.h LightClone/Source/VirtualMachine.cpp LightClone/Source/Window.cpp
diffstat 9 files changed, 281 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Thu Sep 22 14:38:07 2011 -0700
+++ b/.hgignore	Fri Sep 23 11:50:59 2011 -0700
@@ -4,10 +4,12 @@
 *.pyc
 *.ncb
 *.suo
+*.txt
 
 Assets\UI\*.png
 Assets\UI\*.jpg
 Assets\UI\*.gif
 Bin\*
 Intermediate\*
-LightTools\Build\*
\ No newline at end of file
+LightTools\Build\*
+Release\*.rar
\ No newline at end of file
--- a/LightClone/LightClone.vcproj	Thu Sep 22 14:38:07 2011 -0700
+++ b/LightClone/LightClone.vcproj	Fri Sep 23 11:50:59 2011 -0700
@@ -42,7 +42,7 @@
 				Name="VCCLCompilerTool"
 				Optimization="0"
 				AdditionalIncludeDirectories=""
-				PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;WINVER=0x0600;_WIN32_WINNT=0x0600;_WIN32_WINDOWS=0x0410;_WIN32_IE=0x0700;WIN32_LEAN_AND_MEAN;_CRT_SECURE_NO_WARNINGS"
+				PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;WINVER=0x0600;_WIN32_WINNT=0x0600;_WIN32_WINDOWS=0x0410;_WIN32_IE=0x0700;WIN32_LEAN_AND_MEAN;_CRT_SECURE_NO_WARNINGS;TRACE_ENABLE"
 				MinimalRebuild="true"
 				BasicRuntimeChecks="3"
 				RuntimeLibrary="3"
@@ -118,7 +118,7 @@
 				Optimization="2"
 				EnableIntrinsicFunctions="true"
 				AdditionalIncludeDirectories=""
-				PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;WINVER=0x0600;_WIN32_WINNT=0x0600;_WIN32_WINDOWS=0x0410;_WIN32_IE=0x0700;WIN32_LEAN_AND_MEAN;_CRT_SECURE_NO_WARNINGS"
+				PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;WINVER=0x0600;_WIN32_WINNT=0x0600;_WIN32_WINDOWS=0x0410;_WIN32_IE=0x0700;WIN32_LEAN_AND_MEAN;_CRT_SECURE_NO_WARNINGS;TRACE_ENABLE"
 				RuntimeLibrary="2"
 				EnableFunctionLevelLinking="true"
 				UsePrecompiledHeader="0"
@@ -292,6 +292,10 @@
 					>
 				</File>
 				<File
+					RelativePath=".\Source\Trace.cpp"
+					>
+				</File>
+				<File
 					RelativePath=".\Source\Util.cpp"
 					>
 				</File>
@@ -446,6 +450,10 @@
 					>
 				</File>
 				<File
+					RelativePath=".\Source\Trace.h"
+					>
+				</File>
+				<File
 					RelativePath=".\Source\Types.h"
 					>
 				</File>
--- a/LightClone/Source/Core.h	Thu Sep 22 14:38:07 2011 -0700
+++ b/LightClone/Source/Core.h	Fri Sep 23 11:50:59 2011 -0700
@@ -6,16 +6,10 @@
 #define __CORE_H__
 
 #include "Types.h"
+#include "Trace.h"
 #include <d3d9.h>
 #include <d3dx9.h>
 
-#ifdef _DEBUG
-#include <stdio.h>
-#define DEBUG_PRINT printf
-#else
-#define DEBUG_PRINT(...)
-#endif
-
 /*
  * GameState
  */
--- a/LightClone/Source/GraphicsDevice.cpp	Thu Sep 22 14:38:07 2011 -0700
+++ b/LightClone/Source/GraphicsDevice.cpp	Fri Sep 23 11:50:59 2011 -0700
@@ -41,6 +41,8 @@
  */
 ErrorCode GraphicsDevice::Create(HWND kWindow, uint32 nWidth, uint32 nHeight, GraphicsDevice** pInstance)
 {
+	TRACE("GraphicsDevice::Create\n");
+
 	ErrorCode eCode = Error_Fail;
 
 	if(pInstance)
@@ -65,17 +67,24 @@
 			D3DPRESENT_INTERVAL_DEFAULT,
 		};
 
+		TRACE("  Creating DirectX context\n");
 		IDirect3D9* pContext = Direct3DCreate9(D3D_SDK_VERSION);
 		if(pContext)
 		{
+			TRACE("    Success!\n");
 			IDirect3DDevice9* pDevice = NULL;
 
+			TRACE("  Creating DirectX device\n");
 			HRESULT hResult = pContext->CreateDevice(nAdapter, D3DDEVTYPE_HAL, kWindow, nFlags, &kParameters, &pDevice);
 			if(SUCCEEDED(hResult))
 			{
+				TRACE("    Success!\n");
+				TRACE("  Initializing vertex types\n");
+
 				eCode = InitializeVertexTypes(pDevice);
 				if(eCode == Error_Success)
 				{
+					TRACE("    Success!\n");
 					*pInstance = new GraphicsDevice(pContext, pDevice, kParameters);
 				}
 
--- a/LightClone/Source/Mediator.cpp	Thu Sep 22 14:38:07 2011 -0700
+++ b/LightClone/Source/Mediator.cpp	Fri Sep 23 11:50:59 2011 -0700
@@ -81,6 +81,8 @@
  */
 ErrorCode Mediator::Initialize()
 {
+	InitializeTrace(TraceFlag_Debug | TraceFlag_File);
+
 	ErrorCode eCode = kWindow.Initialize();
 	if(eCode == Error_Success)
 	{
@@ -134,6 +136,8 @@
 	kContext.Terminate();
 
 	GraphicsDevice::Destroy(pGraphicsDevice);
+
+	TerminateTrace();
 }
 
 /*
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LightClone/Source/Trace.cpp	Fri Sep 23 11:50:59 2011 -0700
@@ -0,0 +1,150 @@
+/*
+ * Trace
+ */
+
+#include "Trace.h"
+
+#if defined(TRACE_ENABLE)
+
+#include "ArrayList.h"
+#include <stdarg.h>
+#include <stdio.h>
+
+/*
+ * MaximumTraceBufferSize
+ */
+static const uint32 MaximumTraceBufferSize = 1024;
+
+/*
+ * kHandlers
+ */
+ArrayList<TraceHandler*> kHandlers;
+
+/*
+ * ConsoleTraceHandler
+ */
+class ConsoleTraceHandler : public TraceHandler
+{
+public:
+
+	/*
+	 * Trace
+	 */
+	virtual void Trace(const char* pBuffer, uint32 nSize)
+	{
+		printf(pBuffer);
+	}
+};
+
+/*
+ * DebugTraceHandler
+ */
+class DebugTraceHandler : public TraceHandler
+{
+public:
+
+	/*
+	 * Trace
+	 */
+	virtual void Trace(const char* pBuffer, uint32 nSize)
+	{
+		OutputDebugStringA(pBuffer);
+	}
+};
+
+/*
+ * FileTraceHandler
+ */
+class FileTraceHandler : public TraceHandler
+{
+	/*
+	 * pFile
+	 */
+	FILE* pFile;
+
+public:
+
+	/*
+	 * FileTraceHandler
+	 */
+	FileTraceHandler(const char* pName) : pFile(NULL)
+	{
+		pFile = fopen(pName, "wt");
+		//ASSERT(pFile != NULL);
+	}
+
+	/*
+	 * ~FileTraceHandler
+	 */
+	~FileTraceHandler()
+	{
+		if(pFile)
+		{
+			fclose(pFile);
+		}
+	}
+
+	/*
+	 * Trace
+	 */
+	virtual void Trace(const char* pBuffer, uint32 nSize)
+	{
+		if(pFile)
+		{
+			fwrite(pBuffer, sizeof(char), nSize, pFile);
+		}
+	}
+};
+
+/*
+ * InitializeTrace
+ */
+ErrorCode InitializeTrace(uint32 nFlags)
+{
+	if(nFlags & TraceFlag_Console)
+		kHandlers.Add(new ConsoleTraceHandler());
+
+	if(nFlags & TraceFlag_Debug)
+		kHandlers.Add(new DebugTraceHandler());
+
+	if(nFlags & TraceFlag_File)
+		kHandlers.Add(new FileTraceHandler("Trace.txt"));
+
+	return Error_Success;
+}
+
+/*
+ * TerminateTrace
+ */
+void TerminateTrace()
+{
+	for(uint32 i = 0; i < kHandlers.Size(); ++i)
+	{
+		delete kHandlers[i];
+	}
+
+	kHandlers.Clear();
+}
+
+/*
+ * PerformTrace
+ */
+void PerformTrace(const char* pFormat, ...)
+{
+	va_list kArguments;
+	va_start(kArguments, pFormat);
+
+	char kBuffer[MaximumTraceBufferSize];
+	int32 nCount = vsnprintf(kBuffer, sizeof(kBuffer), pFormat, kArguments);
+	if(nCount > 0)
+	{
+		for(uint32 i = 0; i < kHandlers.Size(); ++i)
+		{
+			kHandlers[i]->Trace(kBuffer, nCount);
+		}
+	}
+
+	va_end(kArguments);
+}
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LightClone/Source/Trace.h	Fri Sep 23 11:50:59 2011 -0700
@@ -0,0 +1,86 @@
+/*
+ * Trace
+ */
+
+#ifndef __TRACE_H__
+#define __TRACE_H__
+
+#include "Types.h"
+
+#if defined(TRACE_ENABLE)
+
+/*
+ * TraceFlag
+ */
+enum
+{
+	TraceFlag_Debug		= 1 << 0,
+	TraceFlag_Console	= 1 << 1,
+	TraceFlag_File		= 1 << 2,
+};
+
+/*
+ * TRACE
+ */
+#define TRACE(pFormat, ...) PerformTrace(pFormat, __VA_ARGS__)
+
+/*
+ * InitializeTrace
+ */
+ErrorCode InitializeTrace(uint32 nFlags);
+
+/*
+ * TerminateTrace
+ */
+void TerminateTrace();
+
+/*
+ * PerformTrace
+ */
+void PerformTrace(const char* pFormat, ...);
+
+/*
+ * TraceHandler
+ */
+class TraceHandler
+{
+public:
+
+	/*
+	 * ~TraceHandler
+	 */
+	virtual ~TraceHandler()
+	{
+	}
+
+	/*
+	 * Trace
+	 */
+	virtual void Trace(const char* pBuffer, uint32 nSize) = 0;
+};
+
+#else
+
+/*
+ * TRACE
+ */
+#define TRACE(...)
+
+/*
+ * InitializeTrace
+ */
+ErrorCode InitializeTrace(uint32)
+{
+	return Error_Success;
+}
+
+/*
+ * TerminateTrace
+ */
+void TerminateTrace()
+{
+}
+
+#endif
+
+#endif //__TRACE_H__
--- a/LightClone/Source/VirtualMachine.cpp	Thu Sep 22 14:38:07 2011 -0700
+++ b/LightClone/Source/VirtualMachine.cpp	Fri Sep 23 11:50:59 2011 -0700
@@ -23,13 +23,13 @@
 
 	if(nSourceFunction < nFunctionCount && nSourceOffset <= kFunction[nSourceFunction].nSize)
 	{
-		DEBUG_PRINT("Reading address %d\n", nInstructionPointer);
+		TRACE("Reading address %d\n", nInstructionPointer);
 
 		// read the next instruction
 		const uint8 nInstruction	= Read();
 		const uint8 nParameter		= Read();
 
-		DEBUG_PRINT("Decoded I:%d P:%d\n", nInstruction, nParameter);
+		TRACE("Decoded I:%d P:%d\n", nInstruction, nParameter);
 
 		if(nInstruction == Instruction_None)
 		{
@@ -40,14 +40,14 @@
 		if(nInstruction == Instruction_Action)
 		{
 			// action instruction, the parameter is the action
-			DEBUG_PRINT("  -- Action Decoded: %d\n", nParameter);
+			TRACE("  -- Action Decoded: %d\n", nParameter);
 			return nParameter;
 		}
 		else
 	
 		if(nInstruction == Instruction_Call)
 		{
-			DEBUG_PRINT("  -- Call Decoded: %d\n", nParameter);
+			TRACE("  -- Call Decoded: %d\n", nParameter);
 
 			// set instruction pointer to destination
 			if(nParameter < nFunctionCount)
@@ -58,9 +58,9 @@
 				nMemory[nParameter * FunctionStride + nDestinationSize + 0] = Instruction_Jump;
 				nMemory[nParameter * FunctionStride + nDestinationSize + 1] = nInstructionPointer;
 
-				DEBUG_PRINT("       - Inserting continuation\n");
-				DEBUG_PRINT("         Instruction: %d\n", Instruction_Jump);
-				DEBUG_PRINT("         Destination: %d\n", nInstructionPointer);
+				TRACE("       - Inserting continuation\n");
+				TRACE("         Instruction: %d\n", Instruction_Jump);
+				TRACE("         Destination: %d\n", nInstructionPointer);
 
 				// update instruction pointer to destination address
 				nInstructionPointer = nParameter * FunctionStride;
@@ -73,14 +73,14 @@
 
 		if(nInstruction == Instruction_Jump)
 		{
-			DEBUG_PRINT("  -- Jump Decoded: %d\n", nParameter);
+			TRACE("  -- Jump Decoded: %d\n", nParameter);
 
 			// decode and validate destination function and offset
 			const uint8 nDestinationFunction	= DecodeAddressFunction(nParameter);
 			const uint8 nDestinationOffset		= DecodeAddressOffset(nParameter);
 
-			DEBUG_PRINT("       - Function: %d\n", nDestinationFunction);
-			DEBUG_PRINT("       - Offset  : %d\n", nDestinationOffset);
+			TRACE("       - Function: %d\n", nDestinationFunction);
+			TRACE("       - Offset  : %d\n", nDestinationOffset);
 
 			// set instruction pointer to destination
 			if(nDestinationFunction < nFunctionCount && nDestinationOffset <= kFunction[nDestinationFunction].nSize)
--- a/LightClone/Source/Window.cpp	Thu Sep 22 14:38:07 2011 -0700
+++ b/LightClone/Source/Window.cpp	Fri Sep 23 11:50:59 2011 -0700
@@ -34,6 +34,8 @@
  */
 ErrorCode Window::Initialize()
 {
+	TRACE("Window::Initialize\n");
+
 	HINSTANCE hInstance = GetModuleHandle(NULL);
 
 	WNDCLASSEX kClass;
@@ -50,9 +52,12 @@
 	kClass.lpszMenuName		= NULL;
 	kClass.style			= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
 
+	TRACE("  Registering window class\n");
 	ATOM kAtom = RegisterClassEx(&kClass);
 	if(kAtom)
 	{
+		TRACE("    Success!\n");
+
 		const uint32 nStyleEx	= WS_EX_OVERLAPPEDWINDOW;
 		const uint32 nStyle		= WS_OVERLAPPEDWINDOW | WS_VISIBLE;
 
@@ -72,15 +77,18 @@
 		uint32 nX			= ((kDesktopRectangle.right - kDesktopRectangle.left) - nW) / 2;
 		uint32 nY			= ((kDesktopRectangle.bottom - kDesktopRectangle.top) - nH) / 2;
 
+		TRACE("  Creating window\n");
 		kWindow = CreateWindowEx(nStyleEx, kClassName, kCaption, nStyle, nX, nY, nW, nH, NULL, NULL, hInstance, NULL);
 		if(kWindow)
 		{
+			TRACE("    Success!\n");
 			UpdateWindow(kWindow);
 
 			return Error_Success;
 		}
 	}
 
+	TRACE("  Failed to initialize window!\n");
 	return Error_Fail;
 }