Skip to content

Commit dca4061

Browse files
Merge pull request #987 from Geode-solutions/fix/stack-trace
fix(Exception): add stack trace at exception construction
2 parents ce66844 + 00461ab commit dca4061

File tree

5 files changed

+42
-7
lines changed

5 files changed

+42
-7
lines changed

bindings/python/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
# This file is autogenerated by pip-compile with Python 3.10
33
# by the following command:
44
#
5-
# pip-compile bindings/python/requirements.in
5+
# pip-compile --pre bindings/python/requirements.in
66
#

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: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@
2323

2424
#pragma once
2525

26+
#include <array>
2627
#include <sstream>
2728
#include <stdexcept>
2829
#include <string>
2930

3031
#include <absl/base/optimization.h>
32+
#include <absl/debugging/stacktrace.h>
33+
#include <absl/debugging/symbolize.h>
3134
#include <absl/strings/str_cat.h>
3235

3336
#include <geode/basic/opengeode_basic_export.hpp>
@@ -49,13 +52,44 @@ namespace geode
4952
*/
5053
class OpenGeodeException : public std::runtime_error
5154
{
55+
static constexpr int MAX_STACK_DEPTH = 10;
56+
static constexpr int NB_SKIPPED_STACKS = 1;
57+
static constexpr int SYMBOL_SIZE = 1024;
58+
5259
public:
5360
template < typename... Args >
5461
explicit OpenGeodeException( const Args&... message )
55-
: std::runtime_error{ absl::StrCat( message... ) }
62+
: std::runtime_error{ absl::StrCat( message... ) },
63+
stack_size_{ absl::GetStackTrace(
64+
stack_.data(), MAX_STACK_DEPTH, NB_SKIPPED_STACKS ) }
65+
{
66+
}
67+
68+
~OpenGeodeException() noexcept override = default;
69+
70+
std::string stack_trace() const
5671
{
72+
std::string stack_string;
73+
for( auto frame = 0; frame < stack_size_; ++frame )
74+
{
75+
absl::StrAppend( &stack_string, " ", frame, ": " );
76+
if( std::array< char, SYMBOL_SIZE > symbol; absl::Symbolize(
77+
stack_[frame], symbol.data(), sizeof( symbol ) ) )
78+
{
79+
absl::StrAppend( &stack_string, symbol.data() );
80+
}
81+
else
82+
{
83+
absl::StrAppend( &stack_string, "Unknown" );
84+
}
85+
absl::StrAppend( &stack_string, "\n" );
86+
}
87+
return stack_string;
5788
}
58-
virtual ~OpenGeodeException() noexcept {}
89+
90+
private:
91+
std::array< void*, MAX_STACK_DEPTH > stack_;
92+
int stack_size_;
5993
};
6094

6195
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)