Skip to content

Commit c6bc233

Browse files
committed
Add libassert::print_stacktrace
1 parent 29d1f87 commit c6bc233

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,12 @@ namespace libassert {
439439
const color_scheme& scheme = get_color_scheme(),
440440
std::size_t skip = 0
441441
);
442+
[[nodiscard]] std::string print_stacktrace(
443+
const cpptrace::stacktrace& trace,
444+
int width = 0,
445+
const color_scheme& scheme = get_color_scheme(),
446+
path_mode = get_path_mode()
447+
);
442448
template<typename T> [[nodiscard]] std::string_view type_name() noexcept;
443449
template<typename T> [[nodiscard]] std::string pretty_type_name() noexcept;
444450
template<typename T> [[nodiscard]] std::string stringify(const T& value);
@@ -455,6 +461,7 @@ namespace libassert {
455461
```
456462

457463
- `stacktrace`: Generates a stack trace, formats to the given width (0 for no width formatting)
464+
- `print_stacktrace`: Formats a provided stack trace with libassert's internal trace formatting
458465
- `type_name`: Returns the type name of T
459466
- `pretty_type_name`: Returns the prettified type name for T
460467
- `stringify`: Produces a debug stringification of a value
@@ -565,10 +572,12 @@ namespace libassert {
565572
basename, // only the file name is used
566573
};
567574
LIBASSERT_EXPORT void set_path_mode(path_mode mode);
575+
path_mode get_path_mode();
568576
}
569577
```
570578

571579
- `set_path_mode`: Sets the path shortening mode for assertion output. Default: `path_mode::disambiguated`.
580+
- `get_path_mode`: Gets the path shortening mode for assertion output.
572581

573582
## Assertion information
574583

include/libassert/assert.hpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,6 @@ LIBASSERT_BEGIN_NAMESPACE
117117
return highlight(stringify(t), scheme);
118118
}
119119

120-
// generates a stack trace, formats to the given width
121-
[[nodiscard]] LIBASSERT_ATTR_NOINLINE LIBASSERT_EXPORT
122-
std::string stacktrace(int width = 0, const color_scheme& scheme = get_color_scheme(), std::size_t skip = 0);
123-
124120
enum class literal_format : unsigned {
125121
// integers and floats are decimal by default, chars are of course chars, and everything else only has one
126122
// format that makes sense
@@ -162,6 +158,20 @@ LIBASSERT_BEGIN_NAMESPACE
162158
basename,
163159
};
164160
LIBASSERT_EXPORT void set_path_mode(path_mode mode);
161+
LIBASSERT_EXPORT path_mode get_path_mode();
162+
163+
// generates a stack trace, formats to the given width
164+
[[nodiscard]] LIBASSERT_ATTR_NOINLINE LIBASSERT_EXPORT
165+
std::string stacktrace(int width = 0, const color_scheme& scheme = get_color_scheme(), std::size_t skip = 0);
166+
167+
// formats a stacktrace
168+
[[nodiscard]] LIBASSERT_EXPORT
169+
std::string print_stacktrace(
170+
const cpptrace::stacktrace& trace,
171+
int width = 0,
172+
const color_scheme& scheme = get_color_scheme(),
173+
path_mode = get_path_mode()
174+
);
165175

166176
enum class assert_type {
167177
debug_assertion,

src/assert.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,12 @@ LIBASSERT_BEGIN_NAMESPACE
391391
current_path_mode = mode;
392392
}
393393

394+
path_mode get_path_mode() {
395+
return current_path_mode;
396+
}
397+
394398
namespace detail {
395-
std::unique_ptr<detail::path_handler> new_path_handler() {
396-
auto mode = current_path_mode.load();
399+
std::unique_ptr<detail::path_handler> new_path_handler(path_mode mode = get_path_mode()) {
397400
switch(mode) {
398401
case path_mode::disambiguated:
399402
return std::make_unique<disambiguating_path_handler>();
@@ -404,7 +407,6 @@ LIBASSERT_BEGIN_NAMESPACE
404407
return std::make_unique<identity_path_handler>();
405408
}
406409
}
407-
408410
}
409411

410412
[[noreturn]] LIBASSERT_EXPORT
@@ -714,4 +716,22 @@ LIBASSERT_BEGIN_NAMESPACE
714716
detail::identity_path_handler handler;
715717
return print_stacktrace(trace, width, scheme, &handler);
716718
}
719+
720+
[[nodiscard]]
721+
std::string print_stacktrace(
722+
const cpptrace::stacktrace& trace,
723+
int width,
724+
const color_scheme& scheme,
725+
path_mode mode
726+
) {
727+
auto path_handler = new_path_handler(mode);
728+
// if this is a disambiguating handler or similar it needs to be fed all paths
729+
if(path_handler->has_add_path()) {
730+
for(const auto& frame : trace.frames) {
731+
path_handler->add_path(frame.filename);
732+
}
733+
path_handler->finalize();
734+
}
735+
return print_stacktrace(trace, width, scheme, path_handler.get());
736+
}
717737
LIBASSERT_END_NAMESPACE

src/libassert.cppm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ LIBASSERT_BEGIN_NAMESPACE
2525
export using libassert::highlight;
2626
export using libassert::highlight_stringify;
2727
export using libassert::stacktrace;
28+
export using libassert::print_stacktrace;
2829
export using libassert::literal_format;
2930
export using libassert::operator|;
3031
export using libassert::literal_format_mode;
3132
export using libassert::set_literal_format_mode;
3233
export using libassert::set_fixed_literal_format;
3334
export using libassert::path_mode;
3435
export using libassert::set_path_mode;
36+
export using libassert::get_path_mode;
3537
export using libassert::assert_type;
3638
export using libassert::default_failure_handler;
3739
export using libassert::handler_ptr;

0 commit comments

Comments
 (0)