Added a basic Logging system. Currently calls SDL_ShowSimpleMessageBox(...).

master
kts 2015-02-11 01:58:43 -08:00
parent 30d4b87298
commit f7acf3c219
5 changed files with 99 additions and 3 deletions

View File

@ -75,6 +75,7 @@ xcopy /d /y "..\..\..\..\Dev\SDL2-2.0.3\lib\x86\SDL2.dll" "$(OutDir)"</Command>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\..\src\Log.cpp" />
<ClCompile Include="..\..\src\main.cpp" />
<ClCompile Include="..\..\src\Mat4.cpp" />
<ClCompile Include="..\..\src\RenderCamera.cpp" />
@ -96,6 +97,7 @@ xcopy /d /y "..\..\..\..\Dev\SDL2-2.0.3\lib\x86\SDL2.dll" "$(OutDir)"</Command>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\common.hpp" />
<ClInclude Include="..\..\src\Log.hpp" />
<ClInclude Include="..\..\src\Mat4.hpp" />
<ClInclude Include="..\..\src\RenderCamera.hpp" />
<ClInclude Include="..\..\src\RenderObject.hpp" />

View File

@ -45,6 +45,9 @@
<ClCompile Include="..\..\src\Mat4.cpp">
<Filter>Classes</Filter>
</ClCompile>
<ClCompile Include="..\..\src\Log.cpp">
<Filter>Classes</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="..\..\..\..\Dev\SDL2-2.0.3\lib\x86\SDL2.dll">
@ -79,5 +82,8 @@
<ClInclude Include="..\..\src\Mat4.hpp">
<Filter>Classes</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Log.hpp">
<Filter>Classes</Filter>
</ClInclude>
</ItemGroup>
</Project>

42
src/Log.cpp 100644
View File

@ -0,0 +1,42 @@
/* ================================================================
Log
----------------
This file defines the class for Log.
================================================================ */
#include "Log.hpp"
Log::Log() {}
std::ostringstream& Log::Get(LogLevel level) {
l = level;
time_t current_time = time(0);
struct tm *ltm;
#ifdef _MSC_VER
struct tm lltm;
localtime_s(&lltm, &current_time);
ltm = &lltm;
#else
ltm = localtime(&current_time);
#endif
os << 1900+ltm->tm_year << "/" << 1+ltm->tm_mon << "/" << ltm->tm_mday << " " << ltm->tm_hour << ":" << ltm->tm_min << ':' << ltm->tm_sec;
os << "\n";
return os;
}
Log::~Log() {
os << std::endl;
fprintf(stderr, "%s\n", os.str().c_str());
fflush(stderr);
char *title = "Undefined";
switch(l) {
case LOG_ERROR:
title = "Error";
break;
case LOG_WARNING:
title = "Warning";
break;
case LOG_DEBUG:
title = "Debug";
break;
}
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, title, os.str().c_str(), NULL);
}

42
src/Log.hpp 100644
View File

@ -0,0 +1,42 @@
/* ================================================================
Log
----------------
This header file describes the Log class
================================================================ */
#ifndef LOG_HPP
#define LOG_HPP
#include <SDL.h>
#include <iostream>
#include <sstream>
#include <ctime>
#include <time.h>
#include <string>
#ifdef _MSC_VER // Visual Studio
#define FUNC_NAME __FUNCSIG__
#else // GCC/clang
#define FUNC_NAME __PRETTY_FUNCTION__
#endif
enum LogLevel { LOG_ERROR, LOG_WARNING, LOG_DEBUG };
class Log {
public:
Log();
virtual ~Log();
std::ostringstream& Get(LogLevel level = LOG_DEBUG);
protected:
std::ostringstream os;
private:
int l;
Log(const Log&);
Log& operator =(const Log&);
};
#ifndef MAX_LOG_LEVEL
#define MAX_LOG_LEVEL LOG_ERROR
#endif
#define LOG(level) if (level > MAX_LOG_LEVEL) ; else Log().Get(level)
#endif

View File

@ -4,11 +4,14 @@ RenderScene
This header file defines the RenderScene class.
================================================================ */
#include "RenderScene.hpp"
#include "Log.hpp"
#include <stdexcept> // for std::out_of_range
#include <iostream> // for std::cerr
#include <algorithm> // for std::find
/* ======== Constructors and Destructors ======== */
RenderScene::RenderScene() {
}
RenderScene::~RenderScene() {
}
@ -18,7 +21,7 @@ int RenderScene::addCamera(RenderCamera* camera) {
try {
cameras.push_back(camera);
} catch(...) {
std::cerr << __PRETTY_FUNCTION__ << ": Could not add RenderCamera" << '\n';
LOG(LOG_ERROR) << FUNC_NAME << ": Could not add RenderCamera";
}
// return size regardless of success, likely The Wrong Thing
return cameras.size();
@ -29,6 +32,7 @@ int RenderScene::remCamera(int id) {
cameras.erase(cameras.begin()+(id-1));
return 0;
}
LOG(LOG_WARNING) << FUNC_NAME << ": Could not remove RenderCamera " << id;
return 1;
}
int RenderScene::remCamera(RenderCamera *camera) {
@ -37,6 +41,7 @@ int RenderScene::remCamera(RenderCamera *camera) {
cameras.erase(it);
return 0;
}
LOG(LOG_WARNING) << FUNC_NAME << ": Could not remove RenderCamera";
return 1;
}
@ -44,8 +49,7 @@ RenderCamera* RenderScene::getCamera(int id) {
try {
return cameras.at(id-1);
} catch (const std::out_of_range& oor) {
std::cerr << __PRETTY_FUNCTION__ << ": Camera index out of range: "
<< oor.what() << '\n';
LOG(LOG_ERROR) << FUNC_NAME << ": RenderCamera index out of range: " << oor.what();
}
return NULL;
}