diff --git a/CInterface/Core.hpp b/CInterface/Core.hpp index 8bde751..7e8e007 100644 --- a/CInterface/Core.hpp +++ b/CInterface/Core.hpp @@ -16,17 +16,16 @@ class ExceptionHandler { static std::string _creatureLibLastExceptionStacktrace; public: - static void SetLastArbUtException(const std::string& function, const ArbUt::Exception& e) { + static void SetLastArbUtException(const std::string& function, const ArbUt::Exception& e, size_t depth = 6) { std::stringstream ss; - ss << "[" << function << "] " << e.what() << std::endl; + ss << "[" << function << "] " << e.what(); _creatureLibLastException = ss.str(); - _creatureLibLastExceptionStacktrace = e.GetStacktrace(); + _creatureLibLastExceptionStacktrace = e.GetStacktrace(depth, true); } static void SetLastException(const std::string& function, const std::exception& e) { std::stringstream ss; ss << "[" << function << "] " << e.what(); _creatureLibLastException = ss.str(); - _creatureLibLastExceptionStacktrace = ""; } static const char* GetLastException() { return _creatureLibLastException.c_str(); } static const char* GetLastExceptionStacktrace() { return _creatureLibLastExceptionStacktrace.c_str(); } diff --git a/CMakeLists.txt b/CMakeLists.txt index 0b56d0c..cbeb3f1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,6 +43,9 @@ endif (WINDOWS) # Set up links to all relevant libraries. SET(_LIBRARYLINKS Arbutils) +if (NOT WINDOWS) + set(_LIBRARYLINKS ${_LIBRARYLINKS} -lbfd -ldl) +endif () # If we need to link the C libraries statically, do so. if (STATICC) diff --git a/tests/ExceptionTests.cpp b/tests/ExceptionTests.cpp index 334d6fa..f1a7d62 100644 --- a/tests/ExceptionTests.cpp +++ b/tests/ExceptionTests.cpp @@ -1,5 +1,6 @@ #ifdef TESTS_BUILD #include +#include "../CInterface/Core.hpp" #include "../extern/catch.hpp" #include "../src/Library/Exceptions/CreatureException.hpp" @@ -10,9 +11,16 @@ TEST_CASE("When throwing exception, what() is readable", "[Utilities]") { } catch (const ArbUt::Exception& e) { hasCaught = true; INFO(e.what()); - REQUIRE(std::string(e.what()) == "[ExceptionTests.cpp:9] foobar"); + REQUIRE(std::string(e.what()) == "[ExceptionTests.cpp:10] foobar"); } REQUIRE(hasCaught); } +TEST_CASE("C Interface exception", "[Utilities]") { + ExceptionHandler::SetLastArbUtException("foo", ArbUt::Exception("foobar"), 1); + REQUIRE(std::string(ExceptionHandler::GetLastException()) == "[foo] foobar"); + REQUIRE(std::string(ExceptionHandler::GetLastExceptionStacktrace()) == + "ExceptionTests.cpp[20] ____C_A_T_C_H____T_E_S_T____3()\n"); +} + #endif \ No newline at end of file