diff --git a/include/cpptrace/formatting.hpp b/include/cpptrace/formatting.hpp index 060c0eb3..cee21cb1 100644 --- a/include/cpptrace/formatting.hpp +++ b/include/cpptrace/formatting.hpp @@ -56,6 +56,7 @@ CPPTRACE_BEGIN_NAMESPACE formatter& filtered_frame_placeholders(bool); formatter& filter(std::function); formatter& transform(std::function); + formatter& interesting(std::function); std::string format(const stacktrace_frame&) const; std::string format(const stacktrace_frame&, bool color) const; diff --git a/src/formatting.cpp b/src/formatting.cpp index 02abad3e..302752e7 100644 --- a/src/formatting.cpp +++ b/src/formatting.cpp @@ -70,6 +70,7 @@ CPPTRACE_BEGIN_NAMESPACE bool show_filtered_frames = true; std::function filter; std::function transform; + std::function interesting; } options; public: @@ -106,6 +107,9 @@ CPPTRACE_BEGIN_NAMESPACE void transform(std::function transform) { options.transform = std::move(transform); } + void interesting(std::function interesting) { + options.interesting = std::move(interesting); + } std::string format( const stacktrace_frame& frame, @@ -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 color_override) const { @@ -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(), @@ -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 << ' '; @@ -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 maybe_stored_string; detail::string_view symbol; - switch(options.symbols) { + switch(symbols) { case symbol_mode::full: symbol = frame.symbol; break; @@ -399,6 +425,10 @@ CPPTRACE_BEGIN_NAMESPACE pimpl->transform(std::move(transform)); return *this; } + formatter& formatter::interesting(std::function interesting) { + pimpl->interesting(std::move(interesting)); + return *this; + } std::string formatter::format(const stacktrace_frame& frame) const { return pimpl->format(frame);