Skip to content

Commit

Permalink
Add an option to show / hide filtered frames vs printing a placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-rifkin committed Feb 2, 2025
1 parent d1ce9c8 commit eb83ee2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 14 deletions.
1 change: 1 addition & 0 deletions include/cpptrace/formatting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace cpptrace {
formatter& set_snippets(bool);
formatter& set_snippet_context(int);
formatter& include_column(bool);
formatter& show_filtered_frames(bool);
formatter& set_filter(std::function<bool(const stacktrace_frame&)>);

std::string format(const stacktrace_frame&) const;
Expand Down
45 changes: 31 additions & 14 deletions src/formatting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace cpptrace {
bool snippets = false;
int context_lines = 2;
bool columns = true;
bool show_filtered_frames = true;
std::function<bool(const stacktrace_frame&)> filter;
} options;

Expand All @@ -43,6 +44,9 @@ namespace cpptrace {
void include_column(bool columns) {
options.columns = columns;
}
void show_filtered_frames(bool show) {
options.show_filtered_frames = show;
}
void set_filter(std::function<bool(const stacktrace_frame&)> filter) {
options.filter = filter;
}
Expand Down Expand Up @@ -140,20 +144,24 @@ namespace cpptrace {
const auto frame_number_width = detail::n_digits(static_cast<int>(frames.size()) - 1);
for(const auto& frame : frames) {
if(options.filter && !options.filter(frame)) {
counter++;
continue;
}
print_frame_internal(stream, frame, color, frame_number_width, counter);
if(frame.line.has_value() && !frame.filename.empty() && options.snippets) {
auto snippet = detail::get_snippet(
frame.filename,
frame.line.value(),
options.context_lines,
color
);
if(!snippet.empty()) {
stream << '\n';
stream << snippet;
if(!options.show_filtered_frames) {
counter++;
continue;
}
print_placeholder_frame(stream, frame_number_width, counter);
} else {
print_frame_internal(stream, frame, color, frame_number_width, counter);
if(frame.line.has_value() && !frame.filename.empty() && options.snippets) {
auto snippet = detail::get_snippet(
frame.filename,
frame.line.value(),
options.context_lines,
color
);
if(!snippet.empty()) {
stream << '\n';
stream << snippet;
}
}
}
if(newline_at_end || &frame != &frames.back()) {
Expand All @@ -174,6 +182,11 @@ namespace cpptrace {
stream << line;
}

void print_placeholder_frame(std::ostream& stream, unsigned frame_number_width, std::size_t counter) const {
std::string line = microfmt::format("#{<{}} (filtered)", frame_number_width, counter);
stream << line;
}

void print_frame_internal(
std::ostream& stream,
const stacktrace_frame& frame,
Expand Down Expand Up @@ -258,6 +271,10 @@ namespace cpptrace {
pimpl->include_column(columns);
return *this;
}
formatter& formatter::show_filtered_frames(bool show) {
pimpl->show_filtered_frames(show);
return *this;
}
formatter& formatter::set_filter(std::function<bool(const stacktrace_frame&)> filter) {
pimpl->set_filter(std::move(filter));
return *this;
Expand Down
22 changes: 22 additions & 0 deletions test/unit/lib/formatting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,24 @@ TEST(FormatterTest, Filtering) {
return frame.filename.find("foo.cpp") != std::string::npos;
});
auto res = split(formatter.format(make_test_stacktrace()), "\n");
EXPECT_THAT(
res,
ElementsAre(
"Stack trace (most recent call first):",
"#0 0x0000000000000001 in foo() at foo.cpp:20:30",
"#1 (filtered)",
"#2 0x0000000000000003 in main at foo.cpp:40:25"
)
);
}

TEST(FormatterTest, DontShowFilteredFrames) {
auto formatter = cpptrace::formatter{}
.set_filter([] (const cpptrace::stacktrace_frame& frame) -> bool {
return frame.filename.find("foo.cpp") != std::string::npos;
})
.show_filtered_frames(false);
auto res = split(formatter.format(make_test_stacktrace()), "\n");
EXPECT_THAT(
res,
ElementsAre(
Expand All @@ -213,6 +231,7 @@ TEST(FormatterTest, MoveSemantics) {
ElementsAre(
"Stack trace (most recent call first):",
"#0 0x0000000000000001 in foo() at foo.cpp:20:30",
"#1 (filtered)",
"#2 0x0000000000000003 in main at foo.cpp:40:25"
)
);
Expand All @@ -224,6 +243,7 @@ TEST(FormatterTest, MoveSemantics) {
ElementsAre(
"Stack trace (most recent call first):",
"#0 0x0000000000000001 in foo() at foo.cpp:20:30",
"#1 (filtered)",
"#2 0x0000000000000003 in main at foo.cpp:40:25"
)
);
Expand All @@ -241,6 +261,7 @@ TEST(FormatterTest, CopySemantics) {
ElementsAre(
"Stack trace (most recent call first):",
"#0 0x0000000000000001 in foo() at foo.cpp:20:30",
"#1 (filtered)",
"#2 0x0000000000000003 in main at foo.cpp:40:25"
)
);
Expand All @@ -252,6 +273,7 @@ TEST(FormatterTest, CopySemantics) {
ElementsAre(
"Stack trace (most recent call first):",
"#0 0x0000000000000001 in foo() at foo.cpp:20:30",
"#1 (filtered)",
"#2 0x0000000000000003 in main at foo.cpp:40:25"
)
);
Expand Down

0 comments on commit eb83ee2

Please sign in to comment.