diff --git a/CInterface/Core.cpp b/CInterface/Core.cpp index b12294d..75f7a32 100644 --- a/CInterface/Core.cpp +++ b/CInterface/Core.cpp @@ -1,4 +1,3 @@ #include "Core.hpp" -char* ExceptionHandler::_lastException = 0; export const char* CreatureLib_C_GetLastException() { return ExceptionHandler::GetLastException(); } diff --git a/CInterface/Core.hpp b/CInterface/Core.hpp index fb903cd..e6ddf34 100644 --- a/CInterface/Core.hpp +++ b/CInterface/Core.hpp @@ -3,17 +3,23 @@ #include #include +#include #include #define export extern "C" [[maybe_unused]] #define CreatureLibException 1; +#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) class ExceptionHandler { - static char* _lastException; + static std::string _lastException; public: - static void SetLastException(const std::exception& e) { _lastException = strcpy(_lastException, e.what()); } - static const char* GetLastException() { return _lastException; } + static void SetLastException(std::string file, std::string function, int line, const std::exception& e) { + std::stringstream ss; + ss << "[" << file << ", " << function << "-" << line << "] " << e.what(); + _lastException = ss.str(); + } + static const char* GetLastException() { return _lastException.c_str(); } }; #define Try(data) \ @@ -21,7 +27,7 @@ public: data; \ return 0; \ } catch (const std::exception& e) { \ - ExceptionHandler::SetLastException(e); \ + ExceptionHandler::SetLastException(__FILENAME__, __FUNCTION__, __LINE__, e); \ return CreatureLibException; \ }