feat: allow to obtain the stack trace as a string (#1075)

This commit is contained in:
Sergiu Deitsch 2024-02-17 18:41:40 +01:00 committed by GitHub
parent b24e4d9445
commit 7a807f3805
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 13 additions and 10 deletions

View File

@ -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

View File

@ -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

View File

@ -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};
@ -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

View File

@ -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);