Error handling system.

This commit is contained in:
James Turk 2003-01-13 06:31:08 +00:00
parent a06badf545
commit 9ac250e29c
2 changed files with 149 additions and 16 deletions

View File

@ -13,7 +13,7 @@
File: ZE_ZEngine.h <br> File: ZE_ZEngine.h <br>
Description: Header file for ZEngine class, the core of the ZEngine. <br> Description: Header file for ZEngine class, the core of the ZEngine. <br>
Author(s): James Turk <br> Author(s): James Turk <br>
$Id: ZE_ZEngine.h,v 1.10 2003/01/12 07:09:04 cozman Exp $<br> $Id: ZE_ZEngine.h,v 1.11 2003/01/13 06:31:08 cozman Exp $<br>
\file ZE_ZEngine.h \file ZE_ZEngine.h
\brief Definition file for core ZEngine class. \brief Definition file for core ZEngine class.
@ -24,10 +24,10 @@ $Id: ZE_ZEngine.h,v 1.10 2003/01/12 07:09:04 cozman Exp $<br>
#ifndef __ze_zengine_h__ #ifndef __ze_zengine_h__
#define __ze_zengine_h__ #define __ze_zengine_h__
#include "ZE_Error.h"
#include "ZE_Defines.h" #include "ZE_Defines.h"
#include "ZE_Macros.h" #include "ZE_Macros.h"
#include "ZE_Includes.h" #include "ZE_Includes.h"
#include "ZE_ZError.h"
/*! /*!
\brief ZEngine Namespace. \brief ZEngine Namespace.
@ -150,7 +150,7 @@ class ZEngine
/*! /*!
\brief Quit SDL and any Subsystems. \brief Quit SDL and any Subsystems.
Shut down SDL (and SDL_ttf,SDL_mixer if necessary). Shut down SDL (and SDL_ttf,SDL_mixer if necessary) You shouldn't ever have to call this, ReleaseInstance calls this for you.
**/ **/
void CloseDisplay(); void CloseDisplay();
@ -477,6 +477,79 @@ class ZEngine
#endif //USE_PHYSFS #endif //USE_PHYSFS
/////////////////
//Error Logging//
/////////////////
private:
//! Stack of Errors which have occured.
queue<ZError> mErrorQueue;
//! Current error.
ZError mCurError;
//! Option controlling how logfile is used.
bool mLogAllErrors;
//! C-style FILE* for error logging.
FILE *mErrlog;
/*!
\brief Writes an error to file.
Writes error to current error file.
\since 0.8.2
\param error ZError to write to file.
**/
void LogError(ZError error);
public:
/*!
\brief Modify Error Logging.
Change the way errors are logged and the file they are logged to, before calling this errors are logged to stderr.
(SDL may define stderr.txt on some platforms.)
\since 0.8.2
\param logAll If set to true every error will be written to file instead of stored in the logfile.
\param logFile Name of file to use as log, passing in stderr or stdio will set the log to the C streams.
Passing in nothing will not change the current error log file, which defaults to stderr.
**/
void SetErrorLog(bool logAll, string logFile="");
/*!
\brief Report an error.
Adds the error to the the error queue, and sets the current error to this error.
\since 0.8.2
\param code ZErrorCode of error.
\param desc Optional string describing error.
\param file Optional argument specifying the file the error occured in.
\param line Optional argument specifying the line the error occured on.
**/
void ReportError(ZErrorCode code, string desc="", string file="", unsigned int line=0);
/*!
\brief Get the last error.
Get the last error reported.
\since 0.8.2
\return ZErrorCode of last error reported.
**/
ZErrorCode GetLastError();
/*!
\brief Write to the log.
Write a string to the log, allowing special usage of the error log.
\since 0.8.2
\param str String to write to log file.
**/
void WriteLog(string str);
/*!
\brief Flush Stack of Errors to file.
Write the error stack to the error log.
\since 0.8.2
**/
void FlushErrors();
//////////////////////////// ////////////////////////////
//Data Loading + Unloading// //Data Loading + Unloading//

View File

@ -13,12 +13,12 @@
File: ZE_ZEngine.cpp <br> File: ZE_ZEngine.cpp <br>
Description: Implementation source file for ZEngine library main singleton class. <br> Description: Implementation source file for ZEngine library main singleton class. <br>
Author(s): James Turk <br> Author(s): James Turk <br>
$Id: ZE_ZEngine.cpp,v 1.14 2003/01/12 20:55:46 cozman Exp $<br> $Id: ZE_ZEngine.cpp,v 1.15 2003/01/13 06:31:09 cozman Exp $<br>
\file ZE_ZEngine.cpp \file ZE_ZEngine.cpp
\brief Central source file for ZEngine. \brief Central source file for ZEngine.
Actual implementation of ZEngine singleton class at heart of ZEngine. Actual implementation of ZEngine singleton class, the core of ZEngine.
**/ **/
#include "ZE_ZEngine.h" #include "ZE_ZEngine.h"
@ -55,6 +55,9 @@ ZEngine::ZEngine()
mDesiredFramerate = 0; mDesiredFramerate = 0;
mNextUpdate = mLastPause = mPausedTime = mLastTime = 0; mNextUpdate = mLastPause = mPausedTime = mLastTime = 0;
mSecPerFrame = 0.0; mSecPerFrame = 0.0;
mLogAllErrors = true;
mErrlog = stderr;
} }
ZEngine* ZEngine::GetInstance() ZEngine* ZEngine::GetInstance()
@ -106,14 +109,14 @@ bool ZEngine::CreateDisplay(string title, string icon)
if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO) < 0) if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO) < 0)
{ {
LogError(FormatStr("SDL could not be initialized: %s", SDL_GetError())); ReportError(ZERR_SDL_INIT,SDL_GetError());
return false; return false;
} }
#ifdef USE_SDL_MIXER #ifdef USE_SDL_MIXER
if(Mix_OpenAudio(mRate, AUDIO_S16SYS, mStereo?2:1, 4096) < 0) //Open Audio (Stereo?2:1 is conditional for number of channels) if(Mix_OpenAudio(mRate, AUDIO_S16SYS, mStereo?2:1, 4096) < 0) //Open Audio (Stereo?2:1 is conditional for number of channels)
{ {
LogError(FormatStr("SDL_mixer could not be initialized: %s", Mix_GetError())); ReportError(ZERR_MIX_INIT,SDL_GetError());
return false; return false;
} }
#endif //USE_SDL_MIXER #endif //USE_SDL_MIXER
@ -172,6 +175,7 @@ bool ZEngine::CreateDisplay(string title, string icon)
if(!mScreen) //try 0 for BPP if supplied bpp failed if(!mScreen) //try 0 for BPP if supplied bpp failed
{ {
ReportError(ZERR_VIDMODE,SDL_GetError());
mScreen = SDL_SetVideoMode(mWidth, mHeight, 0, flags); mScreen = SDL_SetVideoMode(mWidth, mHeight, 0, flags);
if(!mScreen) //if safe screen setup fails if(!mScreen) //if safe screen setup fails
@ -180,11 +184,10 @@ bool ZEngine::CreateDisplay(string title, string icon)
Mix_CloseAudio(); Mix_CloseAudio();
#endif #endif
SDL_Quit(); SDL_Quit();
LogError(FormatStr("Unable to set video mode %dx%d (%dBpp): %s. Desktop Depth failed, fatal error.",mWidth,mHeight,mBPP,SDL_GetError()));
ReportError(ZERR_VIDMODE,FormatStr("Desktop Depth failed: %s",SDL_GetError()));
return false; return false;
} }
else //let them know what they wanted failed
LogError(FormatStr("Unable to set video mode %dx%d (%dBpp): %s. Desktop Depth worked.",mWidth,mHeight,mBPP,SDL_GetError()));
} }
mWidth = mScreen->w; mWidth = mScreen->w;
@ -200,7 +203,7 @@ bool ZEngine::CreateDisplay(string title, string icon)
#ifdef USE_SDL_TTF #ifdef USE_SDL_TTF
if(TTF_Init() < 0) if(TTF_Init() < 0)
{ {
LogError(FormatStr("SDL_ttf could not be initialized: %s", TTF_GetError())); ReportError(ZERR_TTF_INIT,TTF_GetError());
return false; return false;
} }
#endif //USE_SDL_TTF #endif //USE_SDL_TTF
@ -226,6 +229,9 @@ void ZEngine::CloseDisplay()
#endif #endif
SDL_Quit(); SDL_Quit();
if(mErrlog != stderr && mErrlog != stdin)
fclose(mErrlog);
} }
SDL_Surface *ZEngine::Display() SDL_Surface *ZEngine::Display()
@ -489,6 +495,57 @@ void ZEngine::AddPhysFSDir(string dir)
#endif //USE_PHYSFS #endif //USE_PHYSFS
void ZEngine::SetErrorLog(bool logAll, string logFile)
{
mLogAllErrors = logAll;
if(logFile.length())
{
if(logFile == "stderr")
mErrlog = stderr;
else if(logFile == "stdout")
mErrlog = stdout;
else
mErrlog = fopen(logFile.c_str(),"w");
}
}
void ZEngine::ReportError(ZErrorCode code, string desc, string file, unsigned int line)
{
mCurError.Create(code,desc,file,line);
if(mLogAllErrors)
{
LogError(mCurError);
fflush(mErrlog);
}
else
mErrorQueue.push(mCurError);
}
ZErrorCode ZEngine::GetLastError()
{
return mCurError.Code();
}
void ZEngine::WriteLog(string str)
{
fprintf(mErrlog,str.c_str());
}
void ZEngine::LogError(ZError error)
{
fprintf(mErrlog,error.LogString().c_str());
}
void ZEngine::FlushErrors()
{
while(!mErrorQueue.empty())
{
LogError(mErrorQueue.front());
mErrorQueue.pop();
}
}
SDL_Surface* ZEngine::LoadImage(string filename) SDL_Surface* ZEngine::LoadImage(string filename)
{ {
SDL_Surface *image; SDL_Surface *image;
@ -497,7 +554,10 @@ SDL_Surface* ZEngine::LoadImage(string filename)
SDL_RWops *rw; SDL_RWops *rw;
rw = PHYSFSRWOPS_openRead(filename.c_str()); rw = PHYSFSRWOPS_openRead(filename.c_str());
if(!rw) if(!rw)
LogError(FormatStr("PhysFS RWops failed: %s",SDL_GetError())); {
ReportError(ZERR_LOAD_IMAGE,FormatStr("%s [PhysFS RWops failed: %s]",filename.c_str(),SDL_GetError()));
return NULL;
}
#ifdef USE_SDL_IMAGE #ifdef USE_SDL_IMAGE
image = IMG_Load_RW(rw,0); image = IMG_Load_RW(rw,0);
#else #else
@ -517,7 +577,7 @@ SDL_Surface* ZEngine::LoadImage(string filename)
if(!image) if(!image)
{ {
LogError(FormatStr("LoadImage could not load %s.",filename.c_str())); ReportError(ZERR_LOAD_IMAGE,filename);
return NULL; return NULL;
} }
else else
@ -541,7 +601,7 @@ Mix_Chunk* ZEngine::LoadSound(string filename)
if(!sound) if(!sound)
{ {
LogError(FormatStr("LoadImage could not load %s.",filename.c_str())); ReportError(ZERR_LOAD_SOUND,filename);
return NULL; return NULL;
} }
else else
@ -564,7 +624,7 @@ Mix_Music* ZEngine::LoadMusic(string filename)
if(!music) if(!music)
{ {
LogError(FormatStr("LoadMusic could not load %s.",filename.c_str())); ReportError(ZERR_LOAD_MUSIC,filename);
return NULL; return NULL;
} }
else else
@ -591,7 +651,7 @@ TTF_Font* ZEngine::LoadFont(string filename, int size)
if(!font) if(!font)
{ {
LogError(FormatStr("LoadFont could not load %s.",filename.c_str())); ReportError(ZERR_LOAD_FONT,filename);
return NULL; return NULL;
} }
else else