Refactor stack trace frame printing

This commit is contained in:
Jeremy 2024-02-28 20:53:28 -06:00
parent 58992cbeb6
commit 0fda4a88ed
No known key found for this signature in database
GPG Key ID: BE03111EB7ED6E2E

View File

@ -166,26 +166,17 @@ namespace cpptrace {
print(stream, color, true, nullptr);
}
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))
)
void print_frame(
std::ostream& stream,
bool color,
unsigned frame_number_width,
std::size_t counter,
const stacktrace_frame& frame
) {
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<<"<empty trace>" << '\n';
return;
}
const auto reset = color ? ESC "0m" : "";
const auto green = color ? ESC "32m" : "";
const auto yellow = color ? ESC "33m" : "";
const auto blue = color ? ESC "34m" : "";
const auto frame_number_width = detail::n_digits(static_cast<int>(frames.size()) - 1);
for(const auto& frame : frames) {
stream
<< '#'
<< std::setw(static_cast<int>(frame_number_width))
@ -236,6 +227,25 @@ namespace cpptrace {
}
}
}
}
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<<"<empty trace>" << '\n';
return;
}
const auto frame_number_width = detail::n_digits(static_cast<int>(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';
}