Fix issue with trying to call detail::enable_virtual_terminal_processing_if_needed unconditionally on print

This commit is contained in:
Jeremy 2024-02-04 13:49:10 -06:00
parent ab2d440a00
commit a3e3916daa
No known key found for this signature in database
GPG Key ID: BE03111EB7ED6E2E
2 changed files with 11 additions and 2 deletions

View File

@ -167,7 +167,11 @@ namespace cpptrace {
}
void stacktrace::print(std::ostream& stream, bool color, bool newline_at_end, const char* header) const {
if(color) {
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):")<<std::endl;

View File

@ -300,7 +300,12 @@ extern "C" {
}
void ctrace_stacktrace_print(const ctrace_stacktrace* trace, FILE* to, ctrace_bool use_color) {
if(use_color) cpptrace::detail::enable_virtual_terminal_processing_if_needed();
if(
use_color && (
(to == stdout && cpptrace::isatty(cpptrace::stdout_fileno)) ||
(to == stderr && cpptrace::isatty(cpptrace::stderr_fileno))
)
) cpptrace::detail::enable_virtual_terminal_processing_if_needed();
ctrace::ffprintf(to, "Stack trace (most recent call first):\n");
if(trace->count == 0 || !trace->frames) {
ctrace::ffprintf(to, "<empty trace>\n");