From 0b9ad4c457dde2d88e372bc2246ba072c9b557ad Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Fri, 20 Jul 2012 23:51:10 +0000 Subject: [PATCH] - added support for throwing known errors and skipping stack traces (For friendlier error messages) --- source/glest_game/game/game.cpp | 17 ++++++++++ source/glest_game/world/world.cpp | 34 +++++++++++++++++-- .../include/platform/sdl/platform_util.h | 6 +++- .../sources/platform/sdl/platform_util.cpp | 4 +-- .../sources/platform/win32/platform_util.cpp | 4 +-- 5 files changed, 57 insertions(+), 8 deletions(-) diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index 81f801e8..55b68503 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -918,6 +918,23 @@ void Game::init(bool initForPreviewOnly) { try { world.init(this, gameSettings.getDefaultUnits()); } + catch(const megaglest_runtime_error &ex) { + string sErrBuf = ""; + if(ex.wantStackTrace() == true) { + char szErrBuf[8096]=""; + sprintf(szErrBuf,"In [%s::%s %d]",__FILE__,__FUNCTION__,__LINE__); + sErrBuf = string(szErrBuf) + string("\nerror [") + string(ex.what()) + string("]\n"); + } + else { + sErrBuf = ex.what(); + } + SystemFlags::OutputDebug(SystemFlags::debugError,sErrBuf.c_str()); + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,sErrBuf.c_str()); + + if(errorMessageBox.getEnabled() == false) { + ErrorDisplayMessage(ex.what(),true); + } + } catch(const exception &ex) { char szErrBuf[8096]=""; sprintf(szErrBuf,"In [%s::%s %d]",__FILE__,__FUNCTION__,__LINE__); diff --git a/source/glest_game/world/world.cpp b/source/glest_game/world/world.cpp index 83a3e41a..0d87b9ce 100644 --- a/source/glest_game/world/world.cpp +++ b/source/glest_game/world/world.cpp @@ -259,6 +259,7 @@ void World::init(Game *game, bool createUnits, bool initFactions){ initMinimap(); bool gotError = false; + bool skipStackTrace = false; string sErrBuf = ""; try { @@ -266,6 +267,19 @@ void World::init(Game *game, bool createUnits, bool initFactions){ initUnits(); } } + catch(const megaglest_runtime_error &ex) { + gotError = true; + if(ex.wantStackTrace() == true) { + char szErrBuf[8096]=""; + sprintf(szErrBuf,"In [%s::%s %d]",__FILE__,__FUNCTION__,__LINE__); + sErrBuf = string(szErrBuf) + string("\nerror [") + string(ex.what()) + string("]\n"); + } + else { + skipStackTrace = true; + sErrBuf = ex.what(); + } + SystemFlags::OutputDebug(SystemFlags::debugError,sErrBuf.c_str()); + } catch(const std::exception &ex) { gotError = true; char szErrBuf[8096]=""; @@ -283,7 +297,7 @@ void World::init(Game *game, bool createUnits, bool initFactions){ computeFow(); if(gotError == true) { - throw megaglest_runtime_error(sErrBuf); + throw megaglest_runtime_error(sErrBuf,!skipStackTrace); } if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -1622,7 +1636,7 @@ void World::placeUnitAtLocation(const Vec2i &location, int radius, Unit *unit, b char szBuf[4096]=""; sprintf(szBuf,"Unit: [%s] can't be placed, this error is caused because there\nis not enough room to put all units near their start location.\nmake a better/larger map. Faction: #%d name: [%s]", unitName.c_str(),unitFactionIndex,unitFactionName.c_str()); - throw megaglest_runtime_error(szBuf); + throw megaglest_runtime_error(szBuf,false); } if (unit->getType()->hasSkillClass(scBeBuilt)) { map.flatternTerrain(unit); @@ -1640,6 +1654,7 @@ void World::initUnits() { Logger::getInstance().add(Lang::getInstance().get("LogScreenGameLoadingGenerateGameElements","",true), true); bool gotError = false; + bool skipStackTrace = false; string sErrBuf=""; try { //put starting units @@ -1676,6 +1691,19 @@ void World::initUnits() { } } } + catch(const megaglest_runtime_error &ex) { + gotError = true; + if(ex.wantStackTrace() == true) { + char szErrBuf[8096]=""; + sprintf(szErrBuf,"In [%s::%s %d]",__FILE__,__FUNCTION__,__LINE__); + sErrBuf = string(szErrBuf) + string("\nerror [") + string(ex.what()) + string("]\n"); + } + else { + skipStackTrace = true; + sErrBuf = ex.what(); + } + SystemFlags::OutputDebug(SystemFlags::debugError,sErrBuf.c_str()); + } catch(const std::exception &ex) { gotError = true; char szErrBuf[8096]=""; @@ -1688,7 +1716,7 @@ void World::initUnits() { map.computeInterpolatedHeights(); if(gotError == true) { - throw megaglest_runtime_error(sErrBuf); + throw megaglest_runtime_error(sErrBuf,!skipStackTrace); } if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } diff --git a/source/shared_lib/include/platform/sdl/platform_util.h b/source/shared_lib/include/platform/sdl/platform_util.h index 10817a65..b48153d6 100644 --- a/source/shared_lib/include/platform/sdl/platform_util.h +++ b/source/shared_lib/include/platform/sdl/platform_util.h @@ -29,8 +29,12 @@ using std::exception; namespace Shared { namespace Platform { class megaglest_runtime_error : public runtime_error { +protected: + bool noStackTrace; public: - megaglest_runtime_error(const string& __arg); + megaglest_runtime_error(const string& __arg,bool noStackTrace=false); + + bool wantStackTrace() const { return noStackTrace; } }; #ifndef WIN32 diff --git a/source/shared_lib/sources/platform/sdl/platform_util.cpp b/source/shared_lib/sources/platform/sdl/platform_util.cpp index 31b46e64..e59f1eac 100644 --- a/source/shared_lib/sources/platform/sdl/platform_util.cpp +++ b/source/shared_lib/sources/platform/sdl/platform_util.cpp @@ -256,8 +256,8 @@ string PlatformExceptionHandler::getStackTrace() { return errMsg; } -megaglest_runtime_error::megaglest_runtime_error(const string& __arg) - : std::runtime_error(__arg + PlatformExceptionHandler::getStackTrace()) { +megaglest_runtime_error::megaglest_runtime_error(const string& __arg,bool noStackTrace) + : std::runtime_error(noStackTrace ? __arg + PlatformExceptionHandler::getStackTrace() : __arg), noStackTrace(noStackTrace) { } }}//end namespace diff --git a/source/shared_lib/sources/platform/win32/platform_util.cpp b/source/shared_lib/sources/platform/win32/platform_util.cpp index e80bb741..0d4bc0d3 100644 --- a/source/shared_lib/sources/platform/win32/platform_util.cpp +++ b/source/shared_lib/sources/platform/win32/platform_util.cpp @@ -265,8 +265,8 @@ string PlatformExceptionHandler::getStackTrace() { return result; } -megaglest_runtime_error::megaglest_runtime_error(const string& __arg) - : std::runtime_error(__arg + PlatformExceptionHandler::getStackTrace()) { +megaglest_runtime_error::megaglest_runtime_error(const string& __arg,bool noStackTrace) +: std::runtime_error(noStackTrace ? __arg + PlatformExceptionHandler::getStackTrace() : __arg), noStackTrace(noStackTrace) { } // =====================================================