Skip to content

Commit bd03ddd

Browse files
committed
fix(Exception): add stack trace at exception construction
1 parent ce66844 commit bd03ddd

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

bindings/python/src/model/mixin/core/vertex_identifier.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ namespace geode
3333
.def( pybind11::init<>() )
3434
.def( "nb_unique_vertices", &VertexIdentifier::nb_unique_vertices )
3535
.def( "component_mesh_vertices",
36-
static_cast< const std::vector< ComponentMeshVertex >& (
37-
VertexIdentifier::*) ( index_t ) const >(
38-
&VertexIdentifier::component_mesh_vertices ) )
36+
&VertexIdentifier::component_mesh_vertices )
3937
.def( "unique_vertex", &VertexIdentifier::unique_vertex );
4038

4139
pybind11::class_< ComponentMeshVertex >( module, "ComponentMeshVertex" )

include/geode/basic/assert.hpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include <string>
2929

3030
#include <absl/base/optimization.h>
31+
#include <absl/debugging/stacktrace.h>
32+
#include <absl/debugging/symbolize.h>
3133
#include <absl/strings/str_cat.h>
3234

3335
#include <geode/basic/opengeode_basic_export.hpp>
@@ -49,13 +51,42 @@ namespace geode
4951
*/
5052
class OpenGeodeException : public std::runtime_error
5153
{
54+
static constexpr int MAX_STACK_DEPTH = 10;
55+
static constexpr int NB_SKIPPED_STACKS = 1;
56+
5257
public:
5358
template < typename... Args >
5459
explicit OpenGeodeException( const Args&... message )
55-
: std::runtime_error{ absl::StrCat( message... ) }
60+
: std::runtime_error{ absl::StrCat( message... ) },
61+
stack_size_{ absl::GetStackTrace(
62+
stack_, MAX_STACK_DEPTH, NB_SKIPPED_STACKS ) }
63+
{
64+
}
65+
66+
virtual ~OpenGeodeException() noexcept = default;
67+
68+
std::string stack_trace() const
5669
{
70+
std::string stack_string;
71+
for( auto frame = 0; frame < stack_size_; ++frame )
72+
{
73+
absl::StrAppend( &stack_string, " ", frame, ": " );
74+
if( char symbol[1024];
75+
absl::Symbolize( stack_[frame], symbol, sizeof( symbol ) ) )
76+
{
77+
absl::StrAppend( &stack_string, symbol, "\n" );
78+
}
79+
else
80+
{
81+
absl::StrAppend( &stack_string, "Unknown \n" );
82+
}
83+
}
84+
return stack_string;
5785
}
58-
virtual ~OpenGeodeException() noexcept {}
86+
87+
private:
88+
void* stack_[MAX_STACK_DEPTH];
89+
int stack_size_;
5990
};
6091

6192
void opengeode_basic_api geode_assertion_failed( std::string_view condition,

src/geode/basic/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ add_geode_library(
9797
PUBLIC_DEPENDENCIES
9898
absl::flat_hash_map
9999
absl::strings
100+
absl::stacktrace
101+
absl::symbolize
100102
Bitsery::bitsery
101103
PRIVATE_DEPENDENCIES
102104
Async++

src/geode/basic/assert.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ namespace geode
4848
}
4949
catch( const OpenGeodeException& e )
5050
{
51-
Logger::critical( "OpenGeodeException: ", e.what() );
51+
Logger::critical(
52+
"OpenGeodeException: ", e.what(), "\n", e.stack_trace() );
5253
}
5354
catch( const std::exception& e )
5455
{

0 commit comments

Comments
 (0)