3232#include " absl/debugging/leak_check.h"
3333#include " absl/debugging/stacktrace.h"
3434#include " absl/debugging/symbolize.h"
35+ #include " absl/hash/hash.h"
3536#include " absl/memory/memory.h"
3637#include " absl/status/status.h"
3738#include " absl/status/status_payload_printer.h"
4142#include " absl/strings/str_format.h"
4243#include " absl/strings/str_split.h"
4344#include " absl/strings/string_view.h"
45+ #include " absl/types/source_location.h"
46+ #include " absl/types/span.h"
4447
4548namespace absl {
4649ABSL_NAMESPACE_BEGIN
@@ -130,6 +133,14 @@ void StatusRep::ForEachPayload(
130133 }
131134}
132135
136+ absl::Span<const SourceLocation> StatusRep::GetSourceLocations () const {
137+ return absl::MakeSpan (source_locations_);
138+ }
139+
140+ void StatusRep::AddSourceLocation (absl::SourceLocation loc) {
141+ source_locations_.push_back (loc);
142+ }
143+
133144std::string StatusRep::ToString (StatusToStringMode mode) const {
134145 std::string text;
135146 absl::StrAppend (&text, absl::StatusCodeToString (code ()), " : " , message ());
@@ -150,6 +161,17 @@ std::string StatusRep::ToString(StatusToStringMode mode) const {
150161 " ']" );
151162 });
152163 }
164+ const bool with_source_location =
165+ (mode & StatusToStringMode::kWithSourceLocation ) ==
166+ StatusToStringMode::kWithSourceLocation ;
167+ if (with_source_location && !source_locations_.empty ()) {
168+ absl::string_view whitespace = (absl::Hash<int >{}(42 ) % 2 == 0 ) ? " " : " " ;
169+ absl::StrAppend (&text, " \n === Source Location Trace: ===" , whitespace,
170+ " \n " );
171+ for (const absl::SourceLocation loc : GetSourceLocations ()) {
172+ absl::StrAppend (&text, loc.file_name (), " :" , loc.line (), " \n " );
173+ }
174+ }
153175
154176 return text;
155177}
@@ -204,6 +226,7 @@ StatusRep* absl_nonnull StatusRep::CloneAndUnref() const {
204226 payloads = absl::make_unique<status_internal::Payloads>(*payloads_);
205227 }
206228 auto * new_rep = new StatusRep (code_, message_, std::move (payloads));
229+ new_rep->source_locations_ = source_locations_;
207230 Unref ();
208231 return new_rep;
209232}
0 commit comments