This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.

| Simple Error Handler Submitted by

This is a simple error handling class which I have created as an exercise of my knowledge of the Standard Template Library. It uses STL strings and iostreams and is made for/using Microsoft Visual C++ 6. I don't think I need to give any extra comments because the code is pretty self-explanatory. You can use the code for whatever you want.
I would like to hear your opinions about it, if you think something needs to be done otherwise I would like to hear it. -----
Bernardo Quiroga aka Drago
http://www.casema.net/~drago/index.html

Currently browsing [errorhandle.zip] (4,391 bytes) - [dll_interface.h] - (1,081 bytes)

    /******************************************************************************
    	filename:		dll_interface.h
    	date:			04/Nov/2000.
    	author:			Bernardo Quiroga.
    	contact at:		bernardo_quiroga@hotmail.com, ICQ UIN: 13639620.
    	website:		http://www.casema.net/~drago/projects/mango/index.html
    	---------------------------------------------------------------------------
    	This file is part of "Mango".

    	This header file takes care of dll exportation/importation.
    ******************************************************************************/
    #ifndef __DLL_INTERFACE_H__
    #define __DLL_INTERFACE_H__

    ///////////////////////////////////////////////////////////////////////////////
    // Included header files
    #ifdef _MANGO_GAME_ENGINE
    #define DLL_INTERFACE __declspec(dllexport)
    #else
    #define DLL_INTERFACE __declspec(dllimport)
    #endif

    #endif // __DLL_INTERFACE_H__

    ///////////////////////////////////////////////////////////////////////////////
    // dll_interface.h - End of file.
    ///////////////////////////////////////////////////////////////////////////////

Currently browsing [errorhandle.zip] (4,391 bytes) - [sys_cerrorhandler.cpp] - (7,305 bytes)

    /******************************************************************************
    	filename:		sys_cerrorhandler.cpp
    	date:			04/Nov/2000.
    	author:			Bernardo Quiroga.
    	contact at:		bernardo_quiroga@hotmail.com, ICQ UIN: 13639620.
    	website:		http://www.casema.net/~drago/projects/mango/index.html
    	---------------------------------------------------------------------------
    	This file is part of "Mango".

    	This class handles any error that might pop up.
    ******************************************************************************/

    ///////////////////////////////////////////////////////////////////////////////
    // Includes
    #include "sys_cerrorhandler.h"

    ///////////////////////////////////////////////////////////////////////////////
    // Static data declaration.
    string sys_cerrorhandler::m_strErrorMessage;

    ///////////////////////////////////////////////////////////////////////////////
    // sys_cerrorhandler::sys_cerrorhandler()
    /////////////////////////////////////////
    // Class constructor.
    sys_cerrorhandler::sys_cerrorhandler() :
    									m_bLogAndQuit(DEFAULT_MODE_OF_PROCESSING),
    									m_strLogFilename(DEFAULT_LOG_FILENAME),
    									m_bLogFailed(false)
    {
    }

    ///////////////////////////////////////////////////////////////////////////////
    // sys_cerrorhandler::sys_cerrorhandler(...)
    ////////////////////////////////////////////
    // Class constructor. bLogAndQuit specifies the mode of error processing;
    // whether an error should only be logged or be logged, displayed in a dialog
    // whereafter the program shuts down. strLogFilename is simply the filename of
    // the error log.
    sys_cerrorhandler::sys_cerrorhandler(const bool& bLogAndQuit,
    									 const string& strLogFilename)
    {
    	// Save the mode of error processing.
    	m_bLogAndQuit = bLogAndQuit;

    	// Save the error log's filename.
    	m_strLogFilename = strLogFilename;

    	// Log(...) hasn't had the chance to fail, yet.
    	m_bLogFailed = false;
    }

    ///////////////////////////////////////////////////////////////////////////////
    // sys_cerrorhandler::~sys_cerrorhandler()
    //////////////////////////////////////////
    // Class destructor.
    sys_cerrorhandler::~sys_cerrorhandler()
    {
    }

    ///////////////////////////////////////////////////////////////////////////////
    // void sys_cerrorhandler::Process(...)
    ///////////////////////////////////////
    // Processes the error. Either logging it to disk or logging it and showing it
    // in a dialog box, whereafter the program shuts down. That depends on the
    // boolean member variable m_bLogAndQuit.
    void sys_cerrorhandler::Process(const string& strFilename, const uint& nLine)
    {
    	// Check if an error has been registered.
    	if(SYS_FAILED(m_strErrorMessage.empty()))
    	{
    		// Log the error to disk.
    		Log(strFilename, nLine);

    		if(m_bLogAndQuit)
    		{
    			// Show the error dialog box and shutdown.
    			ShowErrorDialog(strFilename, nLine);
    		}

    		// Reset the error message.
    		Reset();
    	}
    }

    ///////////////////////////////////////////////////////////////////////////////
    // void sys_cerrorhandler::Log(...)
    ///////////////////////////////////
    // Log the error to disk. If the error log already exists append the error
    // message with the date of occurance and in which source file and line it
    // occured on.
    void sys_cerrorhandler::Log(const string& strFilename, const uint& nLine)
    {
    	// Open the error log for output only, and append the file.
    	fstream fErrorLog(m_strLogFilename.c_str(), ios::out | ios::app);
    	if(SYS_FAILED(fErrorLog.is_open()))
    	{
    		// Could not open the file. Something is seriously wrong.
    		m_bLogFailed	= true;
    		m_bLogAndQuit	= true;
    		return;
    	}

    	// Generate the error message.
    	string strErrorMessage = GenerateErrorMessage(strFilename, nLine);

    	// Output the error message.
    	fErrorLog << strErrorMessage << endl << endl;

    	// Close the file stream.
    	fErrorLog.close();
    }

    ///////////////////////////////////////////////////////////////////////////////
    // string sys_cerrorhandler::GenerateErrorMessage(...)
    //////////////////////////////////////////////////////
    // Returns an error message which includes when (date and time) and
    // where (source file and line) the error occured as well as the error
    // message itself.
    string sys_cerrorhandler::GenerateErrorMessage(const string& strFilename,
    											   const uint& nLine)
    {
    	// Declare a string  and a string stream object.
    	string			strErrorMessage;
    	stringstream	ssErrorMessage(strErrorMessage, ios::out);

    	// Retrieve the current date and time.
    	char szDate[16], szTime[16];
    	_strdate(szDate); _strtime(szTime);

    	// Generate the error message.
    	ssErrorMessage << "On:\t" << szDate << ", " << szTime;
    	ssErrorMessage << " the Mango, Game Engine encountered an error"<< endl;
    	ssErrorMessage << "in file:\t" << strFilename << "," << endl;
    	ssErrorMessage << "on line:\t" << nLine << "," << endl;
    	ssErrorMessage << "stating:\t" << m_strErrorMessage;

    	// Return the error message.
    	return ssErrorMessage.str();
    }

    ///////////////////////////////////////////////////////////////////////////////
    // void sys_cerrorhandler::ShowErrorDialog(...)
    ///////////////////////////////////////////////
    // Show the error message and additional information regarding the error
    // encountered in a dialog box, whereafter the program should shut down.
    void sys_cerrorhandler::ShowErrorDialog(const string& strFilename,
    										const uint& nLine)
    {
    	// Generate the error message.
    	string strDialogMessage = GenerateErrorMessage(strFilename, nLine);

    	// If the error message is saved on disk, ask the user if he can send the
    	// file. Otherwise, ask if the user can send the error message.
    	if(SYS_FAILED(m_bLogFailed))
    	{
    		strDialogMessage += "\n\nPlease, send the error log to";
    		strDialogMessage += "bernardo_quiroga@hotmail.com. It is located";
    		strDialogMessage += " at:\n\t\"";
    		strDialogMessage += m_strLogFilename;
    		strDialogMessage += "\"\nin the directory you installed this program.";
    	}
    	else
    	{
    		strDialogMessage += "\n\nPlease, send this error message to: ";
    		strDialogMessage += "bernardo_quiroga@hotmail.com.";
    	}

    	// Show the error dialog box and shut down the program.
    	MessageBox(NULL, strDialogMessage.c_str(), NULL, MB_OK | MB_ICONERROR |
    		MB_SYSTEMMODAL | MB_TOPMOST);
    	PostQuitMessage(0);
    }

    ///////////////////////////////////////////////////////////////////////////////
    // void sys_cerrorhandler::Reset()
    //////////////////////////////////
    // Flushes the error register. The error that was registered as last will be
    // ignored.
    void sys_cerrorhandler::Reset()
    {
    	// Reset the error message.
    	m_strErrorMessage.erase(0, m_strErrorMessage.length());
    }

    ///////////////////////////////////////////////////////////////////////////////
    // void sys_cerrorhandler::SetError(...)
    ////////////////////////////////////////
    // Register an error and set the error message.
    void sys_cerrorhandler::SetError(const string& strErrorMessage)
    {
    	// Copy the new error message.
    	m_strErrorMessage = strErrorMessage;
    }

    ///////////////////////////////////////////////////////////////////////////////
    // sys_cerrorhandler.cpp - End of file.
    ///////////////////////////////////////////////////////////////////////////////

Currently browsing [errorhandle.zip] (4,391 bytes) - [sys_cerrorhandler.h] - (2,224 bytes)

    /******************************************************************************
    	filename:		sys_cerrorhandler.h
    	date:			04/Nov/2000.
    	author:			Bernardo Quiroga.
    	contact at:		bernardo_quiroga@hotmail.com, ICQ UIN: 13639620.
    	website:		http://www.casema.net/~drago/projects/mango/index.html
    	---------------------------------------------------------------------------
    	This file is part of "Mango".

    	This class handles any error that might pop up.
    ******************************************************************************/
    #ifndef __SYS_CERRORHANDLER_H__
    #define __SYS_CERRORHANDLER_H__

    ///////////////////////////////////////////////////////////////////////////////
    // Included header files
    #include <windows.h>
    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <string>
    #include <time.h>
    #include "dll_interface.h"
    #include "sys_typedefs.h"
    #include "sys_macros.h"

    ///////////////////////////////////////////////////////////////////////////////
    // Definitions
    #define DEFAULT_LOG_FILENAME		"errors.log"
    #define DEFAULT_MODE_OF_PROCESSING	1

    ///////////////////////////////////////////////////////////////////////////////
    // Class sys_cerrorhandler
    class DLL_INTERFACE sys_cerrorhandler
    {
    public:
    	sys_cerrorhandler();
    	sys_cerrorhandler(const bool& bLogAndQuit, const string& strLogFilename);
    	virtual ~sys_cerrorhandler();

    	void		SetProcessMode(const bool& bLogAndQuit)
    				{m_bLogAndQuit = bLogAndQuit;};
    	static void	SetError(const string& strErrorMessage);
    	void		Process(const string& strFilename, const uint& nLine);
    	void		Reset();
    protected:
    	void		Log(const string& strFilename, const uint& nLine);
    	string		GenerateErrorMessage(const string& strFilename,
    									 const uint& nLine);
    	void		ShowErrorDialog(const string& strFilename, const uint& nLine);

    protected:
    	bool			m_bLogAndQuit;
    	static string	m_strErrorMessage;
    	string			m_strLogFilename;
    private:
    	bool			m_bLogFailed;
    };

    #endif // __SYS_CERRORHANDLER_H__

    ///////////////////////////////////////////////////////////////////////////////
    // sys_cerrorhandler.h - End of file.
    ///////////////////////////////////////////////////////////////////////////////

Currently browsing [errorhandle.zip] (4,391 bytes) - [sys_macros.h] - (1,223 bytes)

    /******************************************************************************
    	filename:		sys_macros.h
    	date:			05/Nov/2000.
    	author:			Bernardo Quiroga.
    	contact at:		bernardo_quiroga@hotmail.com, ICQ UIN: 13639620.
    	website:		http://www.casema.net/~drago/projects/mango/index.html
    	---------------------------------------------------------------------------
    	This file is part of "Mango".

    	This header file declares all the system macros used by this project.
    ******************************************************************************/
    #ifndef __SYS_MACROS_H__
    #define __SYS_MACROS_H__

    ///////////////////////////////////////////////////////////////////////////////
    // Macro declaration.
    #define SYS_DELETE(x)			if(x) delete x; x = NULL;
    #define SYS_DELETE_ARRAY(x)		if(x) delete [] x; x = NULL;
    #define SYS_RELEASE(x)			SYS_DELETE(x);
    #define SYS_RELEASE_ARRAY(x)	SYS_DELETE_ARRAY(x);
    #define SYS_FAILED(x)			((uint)x < 1)
    #define SYS_SUCCEEDED(x)		((uint)x > 0)

    #endif // __SYS_MACROS_H__

    ///////////////////////////////////////////////////////////////////////////////
    // sys_macros.h - End of file.
    ///////////////////////////////////////////////////////////////////////////////

Currently browsing [errorhandle.zip] (4,391 bytes) - [sys_typedefs.h] - (1,235 bytes)

    /******************************************************************************
    	filename:		sys_typedefs.h
    	date:			04/Nov/2000.
    	author:			Bernardo Quiroga.
    	contact at:		bernardo_quiroga@hotmail.com, ICQ UIN: 13639620.
    	website:		http://www.casema.net/~drago/projects/mango/index.html
    	---------------------------------------------------------------------------
    	This file is part of "Mango".

    	This header file declares all the type definitions that will be used in
    	this project.
    ******************************************************************************/
    #ifndef __SYS_TYPEDEFS_H__
    #define __SYS_TYPEDEFS_H__

    ///////////////////////////////////////////////////////////////////////////////
    // Type definitions namespace.
    namespace sys_nsTypedefs
    {
    	typedef unsigned char	ubyte;
    	typedef unsigned int	uint;
    };

    ///////////////////////////////////////////////////////////////////////////////
    // Namespace directives.
    using namespace std;
    using namespace sys_nsTypedefs;

    #endif // __SYS_TYPEDEFS_H__

    ///////////////////////////////////////////////////////////////////////////////
    // sys_typedefs.h - End of file.
    ///////////////////////////////////////////////////////////////////////////////

The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.

Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.