From 66acfa15caf852bc476e378d601a469c397be73a Mon Sep 17 00:00:00 2001 From: Rtch90 Date: Sun, 8 Apr 2012 00:51:03 +0100 Subject: [PATCH] [Add] Added Debug logging. --- Bin/Makefile | 9 ++-- src/Main/Game.cpp | 3 +- src/Main/LGLXWindow.cpp | 13 ++--- src/Main/main.cpp | 5 ++ src/System/Debug.cpp | 102 ++++++++++++++++++++++++++++++++++++++++ src/System/Debug.h | 23 +++++++++ src/System/Makefile | 20 ++++++++ 7 files changed, 165 insertions(+), 10 deletions(-) create mode 100644 src/System/Debug.cpp create mode 100644 src/System/Debug.h create mode 100644 src/System/Makefile diff --git a/Bin/Makefile b/Bin/Makefile index 271bb6b..f978e30 100644 --- a/Bin/Makefile +++ b/Bin/Makefile @@ -12,22 +12,25 @@ all: $(MAKE) -C ../src/Texture $(MAKE) -C ../src/Actor $(MAKE) -C ../src/Math + $(MAKE) -C ../src/System $(CC) $(CFLAGS) -o LibD ../src/Main/main.cpp ../src/Main/*.o ../src/Texture/*.o \ - ../src/Actor/*.o ../src/Math/*.o $(LDADD) + ../src/Actor/*.o ../src/Math/*.o ../src/System/*.o $(LDADD) static: @echo -e "\033[1;31mThis is an experimental build, if it does not work, don't complain...\033[0m" @sleep 1 $(MAKE) -C ../src/Main/ ../src/Main/*.o ../src/Texture/*.o \ - ../src/Actor/*.o ../src/Math/*.o + ../src/Actor/*.o ../src/Math/*.o ../src/System/*.o $(CC) $(CFLAGS) -o build/LibD-static ../src/Main/main.cpp ../src/Main/*.o \ - ../src/Texture/*.o ../src/Actor/*.o ../src/Math/*.o $(LDADDSTATIC) + ../src/Texture/*.o ../src/Actor/*.o ../src/Math/*.o \ + ../src/System/*.o$(LDADDSTATIC) clean: $(MAKE) -C ../src/Main/ clean $(MAKE) -C ../src/Texture/ clean $(MAKE) -C ../src/Actor/ clean $(MAKE) -C ../src/Math/ clean + $(MAKE) -C ../src/System/ clean rm -f LibD diff --git a/src/Main/Game.cpp b/src/Main/Game.cpp index 2a82cd7..8d0a1b5 100644 --- a/src/Main/Game.cpp +++ b/src/Main/Game.cpp @@ -5,6 +5,7 @@ #include #include +#include "../System/Debug.h" #include "Game.h" Game::Game(void) { @@ -48,7 +49,7 @@ void Game::Render(void) { } void Game::Shutdown(void) { - + } void Game::OnResize(int width, int height) { diff --git a/src/Main/LGLXWindow.cpp b/src/Main/LGLXWindow.cpp index 4037f86..b5b5246 100644 --- a/src/Main/LGLXWindow.cpp +++ b/src/Main/LGLXWindow.cpp @@ -5,6 +5,7 @@ #include #endif +#include "../System/Debug.h" #include "LGLXWindow.h" #include "Game.h" @@ -44,7 +45,7 @@ bool LGLXWindow::Create(int width, int height, int bpp, bool fullscreen) { // Open the default display. _display = XOpenDisplay(0); if(!_display) { - std::cerr << "Could not open the display." << std::endl; + Debug::logger->message("Could not open the display."); return false; } @@ -57,7 +58,7 @@ bool LGLXWindow::Create(int width, int height, int bpp, bool fullscreen) { XF86VidModeModeInfo **modes; if(!XF86VidModeGetAllModeLines(_display, _screenID, &modeNum, &modes)) { - std::cerr << "Could not query the video modes." << std::endl; + Debug::logger->message("Could not query the video modes."); return false; } @@ -71,7 +72,7 @@ bool LGLXWindow::Create(int width, int height, int bpp, bool fullscreen) { } if(bestMode == -1) { - std::cerr << "Could not find a suitable graphics mode." << std::endl; + Debug::logger->message("Could not find a suitable graphics mode."); return false; } @@ -88,13 +89,13 @@ bool LGLXWindow::Create(int width, int height, int bpp, bool fullscreen) { // Attempt to create a double buffered window. vi = glXChooseVisual(_display, _screenID, doubleBufferedAttribList); if(!vi) { - std::cerr << "Could not create a double buffere window.. Sux.." <message("Could not create a double buffere window.. Sux.."); } // Time to create a GL 2.1 context. GLXContext gl2Context = glXCreateContext(_display, vi, 0, GL_TRUE); if(!gl2Context) { - std::cerr << "Could Not create a GL 2.1 context, check your darn graphics drivers" << std::endl; + Debug::logger->message("Could Not create a GL 2.1 context, check your darn graphics drivers"); return false; } @@ -102,7 +103,7 @@ bool LGLXWindow::Create(int width, int height, int bpp, bool fullscreen) { PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribs = (PFNGLXCREATECONTEXTATTRIBSARBPROC) glXGetProcAddress((GLubyte*)"glXCreateContextAttribsARB"); if(glXCreateContextAttribs == NULL) { - std::cerr << "OpenGL 3.0 is not supported, falling back to 2.1" << std::endl; + Debug::logger->message("OpenGL 3.0 is not supported, falling back to 2.1"); _glContext = gl2Context; _GL3Supported = false; } else { diff --git a/src/Main/main.cpp b/src/Main/main.cpp index ea4d382..e5b67fa 100644 --- a/src/Main/main.cpp +++ b/src/Main/main.cpp @@ -11,12 +11,16 @@ #endif #include "Game.h" +#include "../System/Debug.h" #ifdef _WIN32 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR cmdLine, int cmdShow) { #else int main(int argc, char** argv) { #endif + // Start by opening a debug log. + Debug::openLog(true); + Debug::logger->message("\n ----- Engine Loading ------"); // Get our window settings. const int windowWidth = 800; const int windowHeight = 600; @@ -63,6 +67,7 @@ int main(int argc, char** argv) { programWindow.SwapBuffers(); } game.Shutdown(); // Free any resouces. + Debug::closeLog(); programWindow.Destroy(); // Destroy the program window. return 0; diff --git a/src/System/Debug.cpp b/src/System/Debug.cpp new file mode 100644 index 0000000..3cc6cfb --- /dev/null +++ b/src/System/Debug.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +#include +#include "Debug.h" +#include "string" + + +using namespace std; + +// =================================================================== +// The Debug log allows us to display ever piece of data that +// populates our class components, anything that is loaded, serialized, +// de-serialized etc will be printed out to a text file. +// (Running our program in a terminal, this debug log will print to it.) +// =================================================================== + +Debug *Debug::logger = NULL; + +Debug::Debug(bool logToFile) { + time_t timestamp; + if(logToFile) { + _logFile.open("../../Bin/Debug.log", ios::out); + if(!_logFile.is_open()) { + // We can not open our log. + cerr << "Warning: Can not open Debug.log to write, continueing without logging\n"; + + } else { + // Log File is open, let us give it a nice time stamp. + timestamp = time(NULL); + _logFile << "Log Started: " << ctime(×tamp) << endl; + } + } +} + +Debug::~Debug(void) { + time_t timestamp; + + // We only need to close the log if it is open. + if(_logFile.is_open()) { + // Give it a closing timestamp. + timestamp = time(NULL); + _logFile << endl << "Log Closed: " << ctime(×tamp) << endl; + + // Close the log file. + _logFile.close(); + } +} + +void Debug::message(std::string msg) { + if(_logFile.is_open()) { + _logFile << msg << endl; + } + cerr << msg << endl << endl; +} + +void Debug::message(const char *msg, ...) { + va_list vargList; // This is to handlle the variable arguments + + char outBuf[1024]; + unsigned short outLen; + + // This takes the arguments and puts them into the character array. + va_start(vargList, msg); + +#if defined WIN32 + outLen = _vsnprintf(outBuf, sizeof(outBuf), msg, vargList); +#else + outLen = vsnprintf(outBuf, sizeof(outBuf), msg, vargList); +#endif + + va_end(vargList); + + if(outLen >= sizeof(outBuf)) { + outLen = sizeof(outBuf); + } + + if(_logFile.is_open()) { + _logFile << outBuf << endl; + } + + cerr << outBuf << endl; +} + +bool Debug::openLog(bool logToFile) { + // Make sure the logger has not already been initialized. + if(logger != NULL) { + logger->message("Warning: Multiple calls to openLog()."); + return false; + } + logger = new Debug(logToFile); + return true; +} + +void Debug::closeLog(void) { + if(logger == NULL) { + cerr << "Warning: Call to closeLog() with NULL logger pointer." << endl; + return; + } + delete logger; + logger = NULL; +} diff --git a/src/System/Debug.h b/src/System/Debug.h new file mode 100644 index 0000000..e418cae --- /dev/null +++ b/src/System/Debug.h @@ -0,0 +1,23 @@ +#ifndef _DEBUG_H_ +#define _DEBUG_H_ +#include +#include "string" + +class Debug { +public: + Debug(bool logToFile); + ~Debug(void); + + // Log an error message. + void message(std::string msg); + void message(const char *msg, ...); + static bool openLog(bool logToFile); + static void closeLog(void); + + static Debug *logger; + +private: + std::ofstream _logFile; +}; + +#endif // _DEBUG_H_ diff --git a/src/System/Makefile b/src/System/Makefile new file mode 100644 index 0000000..0928bb6 --- /dev/null +++ b/src/System/Makefile @@ -0,0 +1,20 @@ +CC = g++ +CFLAGS = -ansi -Wall -g +LDADD = -lGL -lGLU -lSDL -lSDL_image + +objects = Debug.o \ + + +.PHONY: default all clean + +default: all + +%.cpp: %.h + +%.o: %.cpp + $(CC) $(CFLAGS) -c -o $@ $< + +all: $(objects) + +clean: + rm -f $(objects)