[Add] Added Debug logging.

This commit is contained in:
Rtch90 2012-04-08 00:51:03 +01:00
parent 09ac982feb
commit 66acfa15ca
7 changed files with 165 additions and 10 deletions

View File

@ -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

View File

@ -5,6 +5,7 @@
#include <GL/gl.h>
#include <GL/glu.h>
#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) {

View File

@ -5,6 +5,7 @@
#include <sys/time.h>
#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.." <<std::endl;
Debug::logger->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 {

View File

@ -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;

102
src/System/Debug.cpp Normal file
View File

@ -0,0 +1,102 @@
#include <iostream>
#include <fstream>
#include <cstdarg>
#include <ctime>
#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(&timestamp) << 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(&timestamp) << 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;
}

23
src/System/Debug.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef _DEBUG_H_
#define _DEBUG_H_
#include <fstream>
#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_

20
src/System/Makefile Normal file
View File

@ -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)