Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/cpptrace/formatting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ CPPTRACE_BEGIN_NAMESPACE
formatter& filtered_frame_placeholders(bool);
formatter& filter(std::function<bool(const stacktrace_frame&)>);
formatter& transform(std::function<stacktrace_frame(stacktrace_frame)>);
formatter& interesting(std::function<bool(const stacktrace_frame&)>);

std::string format(const stacktrace_frame&) const;
std::string format(const stacktrace_frame&, bool color) const;
Expand Down
44 changes: 37 additions & 7 deletions src/formatting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ CPPTRACE_BEGIN_NAMESPACE
bool show_filtered_frames = true;
std::function<bool(const stacktrace_frame&)> filter;
std::function<stacktrace_frame(stacktrace_frame)> transform;
std::function<bool(const stacktrace_frame&)> interesting;
} options;

public:
Expand Down Expand Up @@ -106,6 +107,9 @@ CPPTRACE_BEGIN_NAMESPACE
void transform(std::function<stacktrace_frame(stacktrace_frame)> transform) {
options.transform = std::move(transform);
}
void interesting(std::function<bool(const stacktrace_frame&)> interesting) {
options.interesting = std::move(interesting);
}

std::string format(
const stacktrace_frame& frame,
Expand Down Expand Up @@ -214,7 +218,13 @@ CPPTRACE_BEGIN_NAMESPACE
transformed_frame = options.transform(input_frame);
}
const stacktrace_frame& frame = options.transform ? transformed_frame.unwrap() : input_frame;
write_frame(stream, frame, color);
auto interesting = options.interesting ? options.interesting(frame) : true;
write_frame(
stream,
frame,
interesting ? color : false,
interesting ? options.symbols : symbol_mode::pruned
);
}

void print_internal(std::ostream& stream, const stacktrace& trace, detail::optional<bool> color_override) const {
Expand Down Expand Up @@ -249,8 +259,14 @@ CPPTRACE_BEGIN_NAMESPACE
if(filter_out_frame) {
microfmt::print(stream, "(filtered)");
} else {
write_frame(stream, frame, color);
if(frame.line.has_value() && !frame.filename.empty() && options.snippets) {
auto interesting = options.interesting ? options.interesting(frame) : true;
write_frame(
stream,
frame,
interesting ? color : false,
interesting ? options.symbols : symbol_mode::pruned
);
if(frame.line.has_value() && !frame.filename.empty() && options.snippets && interesting) {
auto snippet = detail::get_snippet(
frame.filename,
frame.line.value(),
Expand All @@ -270,13 +286,18 @@ CPPTRACE_BEGIN_NAMESPACE
}
}

void write_frame(std::ostream& stream, const stacktrace_frame& frame, color_setting color) const {
void write_frame(
std::ostream& stream,
const stacktrace_frame& frame,
color_setting color,
symbol_mode symbols
) const {
write_address(stream, frame, color);
if(frame.is_inline || options.addresses != address_mode::none) {
stream << ' ';
}
if(!frame.symbol.empty()) {
write_symbol(stream, frame, color);
write_symbol(stream, frame, color, symbols);
}
if(!frame.symbol.empty() && !frame.filename.empty()) {
stream << ' ';
Expand All @@ -295,10 +316,15 @@ CPPTRACE_BEGIN_NAMESPACE
}
}

void write_symbol(std::ostream& stream, const stacktrace_frame& frame, color_setting color) const {
void write_symbol(
std::ostream& stream,
const stacktrace_frame& frame,
color_setting color,
symbol_mode symbols
) const {
detail::optional<std::string> maybe_stored_string;
detail::string_view symbol;
switch(options.symbols) {
switch(symbols) {
case symbol_mode::full:
symbol = frame.symbol;
break;
Expand Down Expand Up @@ -399,6 +425,10 @@ CPPTRACE_BEGIN_NAMESPACE
pimpl->transform(std::move(transform));
return *this;
}
formatter& formatter::interesting(std::function<bool(const stacktrace_frame&)> interesting) {
pimpl->interesting(std::move(interesting));
return *this;
}

std::string formatter::format(const stacktrace_frame& frame) const {
return pimpl->format(frame);
Expand Down
Loading