Exception.cpp

00001 /***************************************************************************
00002  *                                                                         *
00003  *   (c) Art Tevs, MPI Informatik Saarbruecken                             *
00004  *       mailto: <tevs@mpi-sb.mpg.de>                                      *
00005  *                                                                         *
00006  *   This program is free software; you can redistribute it and/or modify  *
00007  *   it under the terms of the GNU General Public License as published by  *
00008  *   the Free Software Foundation; either version 2 of the License, or     *
00009  *   (at your option) any later version.                                   *
00010  *                                                                         *
00011  ***************************************************************************/
00012 
00013 //----------------------------------------------------------------------------------
00014 // Includes
00015 //----------------------------------------------------------------------------------
00016 #include "Exception.h"
00017 #include "Engine.h"
00018 
00019 namespace nrEngine{
00020 
00021         //-------------------------------------------------------------------------
00022         Exception::Exception( Result number, const std::string& description, const std::string& source )
00023         {
00024                 this->number = number;
00025                 this->description = description;
00026                 this->source = source;
00027         }
00028 
00029         //-------------------------------------------------------------------------
00030         Exception::Exception( Result number, const std::string& description, const std::string& source, char* file, long line )
00031         {
00032                 this->number = number;
00033                 this->description = description;
00034                 this->source = source;
00035                 this->lineNumber = line;
00036                 this->file = file;
00037 
00038                 // log the exception
00039                 _log(description.c_str(), file, source.c_str(), line, "Exception thrown!");
00040         }
00041 
00042         //-------------------------------------------------------------------------
00043         Exception::Exception(const Exception& rhs)
00044         {
00045                 this->number = rhs.number;
00046                 this->description = rhs.description;
00047                 this->source = rhs.source;
00048                 this->lineNumber = rhs.lineNumber;
00049                 this->file = rhs.file;
00050         }
00051 
00052         //-------------------------------------------------------------------------
00053         void Exception::_log(const char *szExp, const char *szFilename, const char* szFuncName, int iLineNum, const char* msg)
00054         {
00055                 #if NR_PLATFORM != NR_PLATFORM_WIN32
00056 
00057                         // Give message on the console
00058                         fprintf(stderr,"================================================\n");
00059                         fprintf(stderr,"%s!\n", msg);
00060                         if (strlen(szFuncName)) fprintf(stderr,"Func: %s\n)", szFuncName);
00061                         if (strlen(szExp))              fprintf(stderr,"Expr: %s\n", szExp);
00062                         if (strlen(szFilename)) fprintf(stderr,"File: %s\n", szFilename);
00063                         if (iLineNum)                   fprintf(stderr,"Line: %d\n", iLineNum);
00064                         fprintf(stderr,"================================================\n");
00065 
00066                 #endif
00067                 
00068                 // give message to log engine
00069                 if (Engine::valid())
00070                 {
00071                         NR_Log(Log::LOG_ENGINE, Log::LL_ERROR, "================================================");
00072                         NR_Log(Log::LOG_ENGINE, Log::LL_ERROR, "%s", msg);
00073                         if (strlen(szFuncName)) NR_Log(Log::LOG_ENGINE, Log::LL_ERROR, "Func: %s", szFuncName);
00074                         if (strlen(szExp))              NR_Log(Log::LOG_ENGINE, Log::LL_ERROR, "Expr: %s", szExp);
00075                         if (strlen(szFilename)) NR_Log(Log::LOG_ENGINE, Log::LL_ERROR, "File: %s", szFilename);
00076                         if (iLineNum)                   NR_Log(Log::LOG_ENGINE, Log::LL_ERROR, "Line: %d", iLineNum);
00077                         NR_Log(Log::LOG_ENGINE, Log::LL_ERROR, "================================================");
00078                 }
00079         
00080 
00081         }
00082 
00083         //-------------------------------------------------------------------------
00084         void Exception::Assert(bool exp, const char *szExp, const char *szFilename, const char* szFuncName, int iLineNum)
00085         {
00086                 if (!exp) _assertMsg(szExp, szFilename, szFuncName, iLineNum);
00087         }
00088 
00089         //-------------------------------------------------------------------------
00090         void Exception::_assertMsg(const char *szExp, const char *szFilename, const char* szFuncName, int iLineNum){
00091 
00092                 // give some log information
00093                 _log(szExp, szFilename, szFuncName, iLineNum, "Assertion failed!");
00094 
00095                 // Try now to delete the engine
00096                 if (Engine::valid())
00097                 {
00098                         Engine::instance()->stopEngine();
00099                         Engine::release();
00100                 }
00101 
00102                 // if wondows, so show a message box
00103                 #if NR_PLATFORM == NR_PLATFORM_WIN32
00104                         std::string msg;
00105                         msg += std::string("Assertion Failed!\n");
00106                         if (strlen(szFuncName)) msg += std::string("Func: ") +  std::string(szFuncName) + "\n";
00107                         if (strlen(szExp))              msg += "Expr: " + std::string(szExp) + "\n";
00108                         if (strlen(szFilename)) msg += "File: " + std::string(szFilename) + "\n";
00109                         if (iLineNum)                   msg += "Line: " + boost::lexical_cast<std::string>(iLineNum)+ "\n";
00110                         MessageBox(NULL, msg.c_str(), (std::string("nrEngine v")  + std::string(" ") + NR_VERSION_NAME).c_str(), MB_OK | MB_ICONERROR);
00111                 #endif
00112 
00113                 // Now exit the application
00114                 exit(1);
00115         }
00116 
00117 }; // end namespace
00118 

Generated on Wed Sep 12 23:19:42 2007 for nrEngine by  doxygen 1.5.1