Skip to content

Commit

Permalink
feat: allow to obtain the stack trace as a string (#1075)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiud authored Feb 17, 2024
1 parent b24e4d9 commit 7a807f3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/glog/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -1736,6 +1736,9 @@ GLOG_EXPORT bool IsFailureSignalHandlerInstalled();
GLOG_EXPORT void InstallFailureWriter(void (*writer)(const char* data,
size_t size));

// Dump stack trace as a string.
GLOG_EXPORT std::string GetStackTrace();

} // namespace google

#endif // GLOG_LOGGING_H
3 changes: 1 addition & 2 deletions src/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1705,8 +1705,7 @@ void LogMessage::Init(const char* file, int line, LogSeverity severity,
std::snprintf(fileline, sizeof(fileline), "%s:%d", data_->basename_, line);
#ifdef HAVE_STACKTRACE
if (FLAGS_log_backtrace_at == fileline) {
string stacktrace;
DumpStackTraceToString(&stacktrace);
string stacktrace = GetStackTrace();
stream() << " (stacktrace:\n" << stacktrace << ") ";
}
#endif
Expand Down
15 changes: 9 additions & 6 deletions src/utilities.cc
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,6 @@ static void MyUserNameInitializer() {
}
REGISTER_MODULE_INITIALIZER(utilities, MyUserNameInitializer())

#ifdef HAVE_STACKTRACE
void DumpStackTraceToString(string* stacktrace) {
DumpStackTrace(1, DebugWriteToString, stacktrace);
}
#endif

// We use an atomic operation to prevent problems with calling CrashReason
// from inside the Mutex implementation (potentially through RAW_CHECK).
static std::atomic<const logging::internal::CrashReason*> g_reason{nullptr};
Expand Down Expand Up @@ -323,4 +317,13 @@ void ShutdownGoogleLoggingUtilities() {
}

} // namespace glog_internal_namespace_

#ifdef HAVE_STACKTRACE
std::string GetStackTrace() {
std::string stacktrace;
DumpStackTrace(1, DebugWriteToString, &stacktrace);
return stacktrace;
}
#endif

} // namespace google
2 changes: 0 additions & 2 deletions src/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,6 @@ const std::string& MyUserName();
// (Doesn't modify filepath, contrary to basename() in libgen.h.)
const char* const_basename(const char* filepath);

void DumpStackTraceToString(std::string* stacktrace);

void SetCrashReason(const logging::internal::CrashReason* r);

void InitGoogleLoggingUtilities(const char* argv0);
Expand Down

0 comments on commit 7a807f3

Please sign in to comment.