diff --git a/CMakeLists.txt b/CMakeLists.txt index 2ea33e7..02b8a91 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -113,6 +113,7 @@ target_sources( src/ctrace.cpp src/exceptions.cpp src/from_current.cpp + src/formatting.cpp src/options.cpp src/utils.cpp src/demangle/demangle_with_cxxabi.cpp diff --git a/include/cpptrace/basic.hpp b/include/cpptrace/basic.hpp index d6240a6..b5fb4ea 100644 --- a/include/cpptrace/basic.hpp +++ b/include/cpptrace/basic.hpp @@ -182,8 +182,6 @@ namespace cpptrace { inline const_iterator cbegin() const noexcept { return frames.cbegin(); } inline const_iterator cend() const noexcept { return frames.cend(); } private: - void print(std::ostream& stream, bool color, bool newline_at_end, const char* header) const; - void print_with_snippets(std::ostream& stream, bool color, bool newline_at_end, const char* header) const; friend void print_terminate_trace(); }; diff --git a/include/cpptrace/formatting.hpp b/include/cpptrace/formatting.hpp new file mode 100644 index 0000000..c697e3d --- /dev/null +++ b/include/cpptrace/formatting.hpp @@ -0,0 +1,66 @@ +#ifndef CPPTRACE_FORMATTING_HPP +#define CPPTRACE_FORMATTING_HPP + +#include + +#include +#include +#include + +namespace cpptrace { + class CPPTRACE_EXPORT formatter { + class impl; + std::unique_ptr pimpl; + + public: + formatter(); + ~formatter(); + + formatter(formatter&&) = default; + formatter(const formatter&); + formatter& operator=(formatter&&) = default; + formatter& operator=(const formatter&); + + formatter& set_header(std::string); + enum class color_mode { + always, + none, + automatic, + }; + formatter& set_color_mode(color_mode); + enum class address_mode { + raw, + object, + none, + }; + formatter& set_address_mode(address_mode); + formatter& set_snippets(bool); + formatter& set_snippet_context(int); + formatter& include_column(bool); + formatter& set_filter(std::function); + + std::string format(const stacktrace_frame&) const; + std::string format(const stacktrace_frame&, bool color) const; + + std::string format(const stacktrace&) const; + std::string format(const stacktrace&, bool color) const; + + void print(const stacktrace_frame&) const; + void print(const stacktrace_frame&, bool color) const; + void print(std::ostream&, const stacktrace_frame&) const; + void print(std::ostream&, const stacktrace_frame&, bool color) const; + void print(std::FILE*, const stacktrace_frame&) const; + void print(std::FILE*, const stacktrace_frame&, bool color) const; + + void print(const stacktrace&) const; + void print(const stacktrace&, bool color) const; + void print(std::ostream&, const stacktrace&) const; + void print(std::ostream&, const stacktrace&, bool color) const; + void print(std::FILE*, const stacktrace&) const; + void print(std::FILE*, const stacktrace&, bool color) const; + }; + + CPPTRACE_EXPORT const formatter& get_default_formatter(); +} + +#endif diff --git a/src/cpptrace.cpp b/src/cpptrace.cpp index a30bd5c..ed445b0 100644 --- a/src/cpptrace.cpp +++ b/src/cpptrace.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -9,6 +10,7 @@ #include #include +#include "cpptrace/basic.hpp" #include "symbols/symbols.hpp" #include "unwind/unwind.hpp" #include "demangle/demangle.hpp" @@ -129,41 +131,13 @@ namespace cpptrace { return detail::get_frame_object_info(raw_address); } - static std::string frame_to_string( - bool color, - const stacktrace_frame& frame - ) { - const auto reset = color ? RESET : ""; - const auto green = color ? GREEN : ""; - const auto yellow = color ? YELLOW : ""; - const auto blue = color ? BLUE : ""; - std::string str; - if(frame.is_inline) { - str += microfmt::format("{<{}}", 2 * sizeof(frame_ptr) + 2, "(inlined)"); - } else { - str += microfmt::format("{}0x{>{}:0h}{}", blue, 2 * sizeof(frame_ptr), frame.raw_address, reset); - } - if(!frame.symbol.empty()) { - str += microfmt::format(" in {}{}{}", yellow, frame.symbol, reset); - } - if(!frame.filename.empty()) { - str += microfmt::format(" at {}{}{}", green, frame.filename, reset); - if(frame.line.has_value()) { - str += microfmt::format(":{}{}{}", blue, frame.line.value(), reset); - if(frame.column.has_value()) { - str += microfmt::format(":{}{}{}", blue, frame.column.value(), reset); - } - } - } - return str; - } - std::string stacktrace_frame::to_string() const { return to_string(false); } std::string stacktrace_frame::to_string(bool color) const { - return frame_to_string(color, *this); + // return frame_to_string(color, *this); + return get_default_formatter().format(*this, color); } std::ostream& operator<<(std::ostream& stream, const stacktrace_frame& frame) { @@ -195,89 +169,34 @@ namespace cpptrace { } void stacktrace::print() const { - print(std::cerr, true); + get_default_formatter().print(*this); } void stacktrace::print(std::ostream& stream) const { - print(stream, true); + get_default_formatter().print(stream, *this); } void stacktrace::print(std::ostream& stream, bool color) const { - print(stream, color, true, nullptr); + get_default_formatter().print(stream, *this, color); } - static void print_frame( - std::ostream& stream, - bool color, - unsigned frame_number_width, - std::size_t counter, - const stacktrace_frame& frame - ) { - std::string line = microfmt::format("#{<{}} {}", frame_number_width, counter, frame.to_string(color)); - stream << line; - } - - void stacktrace::print(std::ostream& stream, bool color, bool newline_at_end, const char* header) const { - if( - color && ( - (&stream == &std::cout && isatty(stdout_fileno)) || (&stream == &std::cerr && isatty(stderr_fileno)) - ) - ) { - detail::enable_virtual_terminal_processing_if_needed(); - } - stream << (header ? header : "Stack trace (most recent call first):") << '\n'; - std::size_t counter = 0; - if(frames.empty()) { - stream << "\n"; - return; - } - const auto frame_number_width = detail::n_digits(static_cast(frames.size()) - 1); - for(const auto& frame : frames) { - print_frame(stream, color, frame_number_width, counter, frame); - if(newline_at_end || &frame != &frames.back()) { - stream << '\n'; - } - counter++; + namespace detail { + const formatter& get_default_snippet_formatter() { + static formatter snippet_formatter = formatter{}.set_snippets(true); + return snippet_formatter; } } void stacktrace::print_with_snippets() const { - print_with_snippets(std::cerr, true); + detail::get_default_snippet_formatter().print(*this); } void stacktrace::print_with_snippets(std::ostream& stream) const { - print_with_snippets(stream, true); + detail::get_default_snippet_formatter().print(stream, *this); } void stacktrace::print_with_snippets(std::ostream& stream, bool color) const { - print_with_snippets(stream, color, true, nullptr); - } - - void stacktrace::print_with_snippets(std::ostream& stream, bool color, bool newline_at_end, const char* header) const { - if( - color && ( - (&stream == &std::cout && isatty(stdout_fileno)) || (&stream == &std::cerr && isatty(stderr_fileno)) - ) - ) { - detail::enable_virtual_terminal_processing_if_needed(); - } - stream << (header ? header : "Stack trace (most recent call first):") << '\n'; - std::size_t counter = 0; - if(frames.empty()) { - stream << "" << '\n'; - return; - } - const auto frame_number_width = detail::n_digits(static_cast(frames.size()) - 1); - for(const auto& frame : frames) { - print_frame(stream, color, frame_number_width, counter, frame); - if(newline_at_end || &frame != &frames.back()) { - stream << '\n'; - } - if(frame.line.has_value() && !frame.filename.empty()) { - stream << detail::get_snippet(frame.filename, frame.line.value(), 2, color); - } - counter++; - } + detail::get_default_snippet_formatter().print(stream, *this, color); } void stacktrace::clear() { @@ -289,13 +208,12 @@ namespace cpptrace { } std::string stacktrace::to_string(bool color) const { - std::ostringstream oss; - print(oss, color, false, nullptr); - return std::move(oss).str(); + return get_default_formatter().format(*this, color); } std::ostream& operator<<(std::ostream& stream, const stacktrace& trace) { - return stream << trace.to_string(); + get_default_formatter().print(stream, trace); + return stream; } CPPTRACE_FORCE_NO_INLINE diff --git a/src/formatting.cpp b/src/formatting.cpp new file mode 100644 index 0000000..16f40d9 --- /dev/null +++ b/src/formatting.cpp @@ -0,0 +1,294 @@ +#include +#include + +#include "utils/optional.hpp" +#include "utils/utils.hpp" +#include "snippets/snippet.hpp" + +#include +#include +#include +#include +#include +#include + +namespace cpptrace { + class formatter::impl { + std::string header = "Stack trace (most recent call first):"; + color_mode color = color_mode::automatic; + address_mode addresses = address_mode::raw; + bool snippets = false; + int context_lines = 2; + bool columns = true; + std::function filter = [] (const stacktrace_frame&) { return true; }; + + public: + void set_header(std::string header) { + this->header = std::move(header); + } + void set_color_mode(formatter::color_mode mode) { + this->color = mode; + } + void set_address_mode(formatter::address_mode mode) { + this->addresses = mode; + } + void set_snippets(bool snippets) { + this->snippets = snippets; + } + void set_snippet_context(int lines) { + this->context_lines = lines; + } + void include_column(bool columns) { + this->columns = columns; + } + void set_filter(std::function filter) { + this->filter = filter; + } + + std::string format(const stacktrace_frame& frame, detail::optional color_override = detail::nullopt) const { + return frame_to_string(frame, color_override.value_or(color == color_mode::always)); + } + + std::string format(const stacktrace& trace, detail::optional color_override = detail::nullopt) const { + std::ostringstream oss; + print_internal(oss, trace, false, color_override); + return std::move(oss).str(); + } + + void print(const stacktrace_frame& frame, detail::optional color_override = detail::nullopt) const { + print(std::cout, frame, color_override); + } + void print( + std::ostream& stream, + const stacktrace_frame& frame, + detail::optional color_override = detail::nullopt + ) const { + print_frame_internal(stream, frame, color_override); + } + void print( + std::FILE* file, + const stacktrace_frame& frame, + detail::optional color_override = detail::nullopt + ) const { + auto str = format(frame, color_override); + std::fwrite(str.data(), 1, str.size(), file); + } + + void print(const stacktrace& trace, detail::optional color_override = detail::nullopt) const { + print(std::cout, trace, color_override); + } + void print( + std::ostream& stream, + const stacktrace& trace, + detail::optional color_override = detail::nullopt + ) const { + print_internal(stream, trace, true, color_override); + } + void print( + std::FILE* file, + const stacktrace& trace, + detail::optional color_override = detail::nullopt + ) const { + auto str = format(trace, color_override); + std::fwrite(str.data(), 1, str.size(), file); + } + + private: + bool stream_is_tty(std::ostream& stream) const { + // not great, but it'll have to do + return (&stream == &std::cout && isatty(stdout_fileno)) + || (&stream == &std::cerr && isatty(stderr_fileno)); + } + + void maybe_ensure_virtual_terminal_processing(std::ostream& stream, bool color) const { + if(color && stream_is_tty(stream)) { + detail::enable_virtual_terminal_processing_if_needed(); + } + } + + bool should_do_color(std::ostream& stream, detail::optional color_override) const { + bool do_color = color == color_mode::always || color_override.value_or(false); + if( + (color == color_mode::automatic || color == color_mode::always) && + (!color_override || color_override.unwrap() != false) && + stream_is_tty(stream) + ) { + detail::enable_virtual_terminal_processing_if_needed(); + do_color = true; + } + return do_color; + } + + void print_internal(std::ostream& stream, const stacktrace& trace, bool newline_at_end, detail::optional color_override) const { + bool do_color = should_do_color(stream, color_override); + maybe_ensure_virtual_terminal_processing(stream, do_color); + print_internal(stream, trace, newline_at_end, do_color); + } + + void print_internal(std::ostream& stream, const stacktrace& trace, bool newline_at_end, bool color) const { + if(!header.empty()) { + stream << header << '\n'; + } + std::size_t counter = 0; + const auto& frames = trace.frames; + if(frames.empty()) { + stream << "\n"; + return; + } + const auto frame_number_width = detail::n_digits(static_cast(frames.size()) - 1); + for(const auto& frame : frames) { + print_frame_internal(stream, frame, color, frame_number_width, counter); + if(newline_at_end || &frame != &frames.back()) { + stream << '\n'; + } + if(frame.line.has_value() && !frame.filename.empty() && snippets) { + stream << detail::get_snippet(frame.filename, frame.line.value(), context_lines, color); + } + counter++; + } + } + + void print_frame_internal( + std::ostream& stream, + const stacktrace_frame& frame, + bool color, + unsigned frame_number_width, + std::size_t counter + ) const { + std::string line = microfmt::format("#{<{}} {}", frame_number_width, counter, frame_to_string(frame, color)); + stream << line; + } + + void print_frame_internal( + std::ostream& stream, + const stacktrace_frame& frame, + detail::optional color_override + ) const { + bool do_color = should_do_color(stream, color_override); + maybe_ensure_virtual_terminal_processing(stream, do_color); + stream << frame_to_string(frame, do_color); + } + + std::string frame_to_string(const stacktrace_frame& frame, bool color) const { + const auto reset = color ? RESET : ""; + const auto green = color ? GREEN : ""; + const auto yellow = color ? YELLOW : ""; + const auto blue = color ? BLUE : ""; + std::string str; + if(frame.is_inline) { + str += microfmt::format("{<{}}", 2 * sizeof(frame_ptr) + 2, "(inlined)"); + } else { + auto address = addresses == address_mode::raw ? frame.raw_address : frame.object_address; + str += microfmt::format("{}0x{>{}:0h}{}", blue, 2 * sizeof(frame_ptr), address, reset); + } + if(!frame.symbol.empty()) { + str += microfmt::format(" in {}{}{}", yellow, frame.symbol, reset); + } + if(!frame.filename.empty()) { + str += microfmt::format(" at {}{}{}", green, frame.filename, reset); + if(frame.line.has_value()) { + str += microfmt::format(":{}{}{}", blue, frame.line.value(), reset); + if(frame.column.has_value() && columns) { + str += microfmt::format(":{}{}{}", blue, frame.column.value(), reset); + } + } + } + return str; + } + }; + + formatter::formatter() : pimpl(std::make_unique()) {} + formatter::~formatter() = default; + + formatter::formatter(const formatter& other) : pimpl(std::make_unique(*other.pimpl)) {} + formatter& formatter::operator=(const formatter& other) { + pimpl = std::make_unique(*other.pimpl); + return *this; + } + + formatter& formatter::set_header(std::string header) { + pimpl->set_header(std::move(header)); + return *this; + } + formatter& formatter::set_color_mode(color_mode mode) { + pimpl->set_color_mode(mode); + return *this; + } + formatter& formatter::set_address_mode(address_mode mode) { + pimpl->set_address_mode(mode); + return *this; + } + formatter& formatter::set_snippets(bool snippets) { + pimpl->set_snippets(snippets); + return *this; + } + formatter& formatter::set_snippet_context(int lines) { + pimpl->set_snippet_context(lines); + return *this; + } + formatter& formatter::include_column(bool columns) { + pimpl->include_column(columns); + return *this; + } + formatter& formatter::set_filter(std::function filter) { + pimpl->set_filter(std::move(filter)); + return *this; + } + + std::string formatter::format(const stacktrace_frame& frame) const { + return pimpl->format(frame); + } + std::string formatter::format(const stacktrace_frame& frame, bool color) const { + return pimpl->format(frame, color); + } + + std::string formatter::format(const stacktrace& trace) const { + return pimpl->format(trace); + } + std::string formatter::format(const stacktrace& trace, bool color) const { + return pimpl->format(trace, color); + } + + void formatter::print(const stacktrace& trace) const { + pimpl->print(trace); + } + void formatter::print(const stacktrace& trace, bool color) const { + pimpl->print(trace, color); + } + void formatter::print(std::ostream& stream, const stacktrace& trace) const { + pimpl->print(stream, trace); + } + void formatter::print(std::ostream& stream, const stacktrace& trace, bool color) const { + pimpl->print(stream, trace, color); + } + void formatter::print(std::FILE* file, const stacktrace& trace) const { + pimpl->print(file, trace); + } + void formatter::print(std::FILE* file, const stacktrace& trace, bool color) const { + pimpl->print(file, trace, color); + } + + void formatter::print(const stacktrace_frame& frame) const { + pimpl->print(frame); + } + void formatter::print(const stacktrace_frame& frame, bool color) const { + pimpl->print(frame, color); + } + void formatter::print(std::ostream& stream, const stacktrace_frame& frame) const { + pimpl->print(stream, frame); + } + void formatter::print(std::ostream& stream, const stacktrace_frame& frame, bool color) const { + pimpl->print(stream, frame, color); + } + void formatter::print(std::FILE* file, const stacktrace_frame& frame) const { + pimpl->print(file, frame); + } + void formatter::print(std::FILE* file, const stacktrace_frame& frame, bool color) const { + pimpl->print(file, frame, color); + } + + const formatter& get_default_formatter() { + static formatter formatter; + return formatter; + } +} diff --git a/src/utils.cpp b/src/utils.cpp index 0841841..c0a76bb 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -1,5 +1,6 @@ #include #include +#include #include @@ -27,12 +28,9 @@ namespace cpptrace { CPPTRACE_FORCE_NO_INLINE void print_terminate_trace() { try { // try/catch can never be hit but it's needed to prevent TCO - generate_trace(1).print( - std::cerr, - isatty(stderr_fileno), - true, - "Stack trace to reach terminate handler (most recent call first):" - ); + formatter{} + .set_header("Stack trace to reach terminate handler (most recent call first):") + .print(std::cerr, generate_trace(1)); } catch(...) { if(!detail::should_absorb_trace_exceptions()) { throw;