Skip to content
Closed
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
31 changes: 28 additions & 3 deletions src/runtime/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
#include <tvm/runtime/logging.h>

#include <stdexcept>
#include <string>

#if TVM_LOG_STACK_TRACE
Expand All @@ -28,6 +29,7 @@

#include <iomanip>
#include <iostream>
#include <rang.hpp>
#include <sstream>
#include <unordered_map>
#include <vector>
Expand Down Expand Up @@ -95,14 +97,22 @@ int BacktraceFullCallback(void* data, uintptr_t pc, const char* filename, int li
backtrace_syminfo(_bt_state, pc, BacktraceSyminfoCallback, BacktraceErrorCallback,
symbol_str.get());
}
s << *symbol_str;

if (rang::rang_implementation::isTerminal(std::cout.rdbuf())) {
// This will eventually find its way to stdout, but rang is printing to a
// regular stringstream so pipe through the isatty() result manually
rang::setControlMode(rang::control::Force);
}
s << rang::fg::yellow << *symbol_str << rang::style::reset;

if (filename != nullptr) {
s << std::endl << " at " << filename;
s << std::endl << " at " << rang::fg::green << filename;
s << rang::style::reset;
if (lineno != 0) {
s << ":" << lineno;
}
}
rang::setControlMode(rang::control::Auto);
// Skip tvm::backtrace and tvm::LogFatal::~LogFatal at the beginning of the trace as they don't
// add anything useful to the backtrace.
if (!(stack_trace->lines.size() == 0 &&
Expand All @@ -120,7 +130,22 @@ int BacktraceFullCallback(void* data, uintptr_t pc, const char* filename, int li

std::string Backtrace() {
BacktraceInfo bt;
bt.max_size = 500;

// Limit backtrace length based on TVM_BACKTRACE_LIMIT env variable
auto user_limit_s = getenv("TVM_BACKTRACE_LIMIT");
const auto default_limit = 500;

if (user_limit_s == nullptr) {
bt.max_size = default_limit;
} else {
// Parse out the user-set backtrace limit
try {
bt.max_size = std::stoi(user_limit_s);
} catch (const std::invalid_argument& e) {
bt.max_size = default_limit;
}
}

Comment on lines +134 to +148
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a separate PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved into #10025

if (_bt_state == nullptr) {
return "";
}
Expand Down