Skip to content

Commit

Permalink
feat: add boost/stacktrace (#114)
Browse files Browse the repository at this point in the history
* feat: initial add

* feat: cleaned up the functionality

* feat: add build targets for static and util lib

* fix: use static target for static build

* refactor: instance variable for what_
  • Loading branch information
kyle-cochran authored Jun 12, 2023
1 parent 78ccdd4 commit 3b37869
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 39 deletions.
11 changes: 7 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ ENDFOREACH ()
SET (Boost_USE_STATIC_LIBS ON)
SET (Boost_USE_MULTITHREADED ON)

FIND_PACKAGE ("Boost" "1.82" REQUIRED COMPONENTS "system" "filesystem" "regex" "log")
FIND_PACKAGE ("Boost" "1.82" REQUIRED COMPONENTS "system" "filesystem" "regex" "log" "stacktrace_basic")

IF (NOT Boost_FOUND)
MESSAGE (SEND_ERROR "[Boost] not found.")
Expand Down Expand Up @@ -269,7 +269,8 @@ IF (BUILD_SHARED_LIBRARY)

TARGET_LINK_LIBRARIES (${SHARED_LIBRARY_TARGET} "stdc++")
TARGET_LINK_LIBRARIES (${SHARED_LIBRARY_TARGET} "pthread")
TARGET_LINK_LIBRARIES (${SHARED_LIBRARY_TARGET} "Boost::filesystem" "Boost::log")
TARGET_LINK_LIBRARIES (${SHARED_LIBRARY_TARGET} "Boost::filesystem" "Boost::log" ${Boost_STACKTRACE_BASIC_LIBRARY})
TARGET_LINK_LIBRARIES (${SHARED_LIBRARY_TARGET} "dl")
TARGET_LINK_LIBRARIES (${SHARED_LIBRARY_TARGET} "yaml-cpp")

SET_TARGET_PROPERTIES ( ${SHARED_LIBRARY_TARGET} PROPERTIES
Expand Down Expand Up @@ -300,7 +301,8 @@ IF (BUILD_STATIC_LIBRARY)
TARGET_INCLUDE_DIRECTORIES (${STATIC_LIBRARY_TARGET} PUBLIC "${PROJECT_SOURCE_DIR}/include/")

TARGET_LINK_LIBRARIES (${STATIC_LIBRARY_TARGET} "pthread")
TARGET_LINK_LIBRARIES (${STATIC_LIBRARY_TARGET} "Boost::filesystem" "Boost::log")
TARGET_LINK_LIBRARIES (${STATIC_LIBRARY_TARGET} "Boost::filesystem" "Boost::log" ${Boost_STACKTRACE_BASIC_LIBRARY})
TARGET_LINK_LIBRARIES (${STATIC_LIBRARY_TARGET} "dl")
TARGET_LINK_LIBRARIES (${STATIC_LIBRARY_TARGET} "yaml-cpp")

SET_TARGET_PROPERTIES (${STATIC_LIBRARY_TARGET} PROPERTIES VERSION ${PROJECT_VERSION_STRING} OUTPUT_NAME ${SHARED_LIBRARY_NAME} CLEAN_DIRECT_OUTPUT 1 INSTALL_RPATH "$ORIGIN/../lib:$ORIGIN/")
Expand Down Expand Up @@ -328,7 +330,8 @@ IF (BUILD_UTILITY)
TARGET_INCLUDE_DIRECTORIES (${UTILITY_TARGET} PUBLIC "${PROJECT_SOURCE_DIR}/include/")

TARGET_LINK_LIBRARIES (${UTILITY_TARGET} "pthread")
TARGET_LINK_LIBRARIES (${UTILITY_TARGET} "Boost::filesystem" "Boost::log")
TARGET_LINK_LIBRARIES (${UTILITY_TARGET} "Boost::filesystem" "Boost::log" ${Boost_STACKTRACE_BASIC_LIBRARY})
TARGET_LINK_LIBRARIES (${UTILITY_TARGET} "dl")
TARGET_LINK_LIBRARIES (${UTILITY_TARGET} ${SHARED_LIBRARY_TARGET})

SET_TARGET_PROPERTIES (${UTILITY_TARGET} PROPERTIES VERSION ${PROJECT_VERSION_STRING} OUTPUT_NAME ${SHARED_LIBRARY_NAME} CLEAN_DIRECT_OUTPUT 1 INSTALL_RPATH "$ORIGIN/../lib:$ORIGIN/")
Expand Down
24 changes: 10 additions & 14 deletions include/OpenSpaceToolkit/Core/Error/RuntimeError.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include <stdexcept>

#include <boost/stacktrace.hpp>

#include <OpenSpaceToolkit/Core/Error/Exception.hpp>
#include <OpenSpaceToolkit/Core/Types/String.hpp>

Expand All @@ -24,32 +26,26 @@ class RuntimeError : public Exception
public:
RuntimeError(const String& aMessage);

// RuntimeError ( const String& aScope,
// const String& aMessage ) ;

template <typename... Args>
RuntimeError(const char* aFormat, Args... anArgumentList)
: Exception(String::Empty()),
message_(String::Format(aFormat, anArgumentList...))
message_(String::Format(aFormat, anArgumentList...)),
stackTrace_(boost::stacktrace::to_string(boost::stacktrace::stacktrace())),
what_(stackTrace_ + message_)
{
}

// template <typename ...Args>
// RuntimeError ( const String& aScope,
// const char* aFormat,
// Args... anArgumentList )
// : Exception(aScope),
// message_(String::Format(aFormat, anArgumentList...))
// {

// }

~RuntimeError();

String getMessage() const;
String getStackTrace() const;

virtual const char* what() const noexcept override;

private:
String message_;
String stackTrace_;
String what_;
};

} // namespace error
Expand Down
20 changes: 12 additions & 8 deletions src/OpenSpaceToolkit/Core/Error/RuntimeError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,27 @@ namespace error

RuntimeError::RuntimeError(const String& aMessage)
: Exception(String::Empty()),
message_(aMessage)
message_(aMessage),
stackTrace_(boost::stacktrace::to_string(boost::stacktrace::stacktrace())),
what_(stackTrace_ + message_)
{
}

// RuntimeError::RuntimeError ( const String& aScope,
// const String& aMessage )
// : Exception(aScope),
// message_(aMessage)
// {
String RuntimeError::getMessage() const
{
return message_.data();
}

// }
String RuntimeError::getStackTrace() const
{
return stackTrace_.data();
}

RuntimeError::~RuntimeError() {}

const char* RuntimeError::what() const noexcept
{
return message_.data();
return what_.data();
}

} // namespace error
Expand Down
25 changes: 12 additions & 13 deletions test/OpenSpaceToolkit/Core/Error/RuntimeError.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ TEST(OpenSpaceToolkit_Core_Error_RuntimeError, Constructor)

{
EXPECT_THROW(throw RuntimeError("This is a test."), RuntimeError);
// EXPECT_THROW(throw RuntimeError("This is a scope.", "This is a test."), RuntimeError) ;

EXPECT_THROW(throw RuntimeError("This is a list [{}, {}, {}].", 123, 456, 789), RuntimeError);
}
Expand All @@ -23,18 +22,13 @@ TEST(OpenSpaceToolkit_Core_Error_RuntimeError, Constructor)
catch (const RuntimeError& anError)
{
EXPECT_EQ("", anError.getScope());
EXPECT_EQ("This is a test.", std::string(anError.what()));
}
EXPECT_EQ("This is a test.", anError.getMessage());

EXPECT_FALSE(anError.getStackTrace().empty());
EXPECT_EQ(anError.getStackTrace().getHead(3), " 0#");

// try
// {
// throw RuntimeError("This is a scope.", "This is a test.") ;
// }
// catch (const RuntimeError& anError)
// {
// EXPECT_EQ("This is a scope.", anError.getScope()) ;
// EXPECT_EQ("This is a test.", std::string(anError.what())) ;
// }
EXPECT_EQ(anError.getStackTrace() + anError.getMessage(), std::string(anError.what()));
}

try
{
Expand All @@ -43,7 +37,12 @@ TEST(OpenSpaceToolkit_Core_Error_RuntimeError, Constructor)
catch (const RuntimeError& anError)
{
EXPECT_EQ("", anError.getScope());
EXPECT_EQ("This is a list [123, 456, 789].", std::string(anError.what()));
EXPECT_EQ("This is a list [123, 456, 789].", anError.getMessage());

EXPECT_FALSE(anError.getStackTrace().empty());
EXPECT_EQ(anError.getStackTrace().getHead(3), " 0#");

EXPECT_EQ(anError.getStackTrace() + anError.getMessage(), std::string(anError.what()));
}
}
}

0 comments on commit 3b37869

Please sign in to comment.