Skip to content

Commit 81f4d83

Browse files
Abseil Teamcopybara-github
authored andcommitted
Add supporting code for absl::SourceLocation
PiperOrigin-RevId: 883256961 Change-Id: I536f9dc76ce4f248b5c87eaf27515cd8a4a484ca
1 parent 522a5a8 commit 81f4d83

20 files changed

Lines changed: 912 additions & 73 deletions

absl/log/BUILD.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ cc_library(
209209
"//absl/strings",
210210
"//absl/strings:internal",
211211
"//absl/types:optional",
212+
"//absl/types:source_location",
212213
"//absl/utility",
213214
],
214215
)
@@ -404,6 +405,7 @@ cc_test(
404405
"//absl/log/internal:test_helpers",
405406
"//absl/log/internal:test_matchers",
406407
"//absl/strings",
408+
"//absl/types:source_location",
407409
"@googletest//:gtest",
408410
"@googletest//:gtest_main",
409411
],
@@ -564,6 +566,7 @@ cc_test(
564566
"//absl/log/internal:test_helpers",
565567
"//absl/log/internal:test_matchers",
566568
"//absl/strings",
569+
"//absl/types:source_location",
567570
"@googletest//:gtest",
568571
"@googletest//:gtest_main",
569572
],
@@ -584,6 +587,7 @@ cc_test(
584587
"//absl/log/internal:test_matchers",
585588
"//absl/strings",
586589
"//absl/time",
590+
"//absl/types:source_location",
587591
"@googletest//:gtest",
588592
"@googletest//:gtest_main",
589593
],

absl/log/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ absl_cc_library(
215215
absl::memory
216216
absl::nullability
217217
absl::raw_logging_internal
218+
absl::source_location
218219
absl::span
219220
absl::strerror
220221
absl::strings
@@ -632,6 +633,7 @@ absl_cc_library(
632633
absl::absl_log
633634
absl::log_severity
634635
absl::optional
636+
absl::source_location
635637
absl::strings
636638
absl::strings_internal
637639
absl::utility
@@ -1079,6 +1081,7 @@ absl_cc_test(
10791081
absl::log_streamer
10801082
absl::log_severity
10811083
absl::scoped_mock_log
1084+
absl::source_location
10821085
absl::strings
10831086
GTest::gmock_main
10841087
)
@@ -1099,6 +1102,7 @@ absl_cc_test(
10991102
absl::log_internal_test_matchers
11001103
absl::log_sink
11011104
absl::scoped_mock_log
1105+
absl::source_location
11021106
absl::strings
11031107
absl::time
11041108
GTest::gmock_main

absl/log/internal/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ cc_library(
216216
"//absl/strings",
217217
"//absl/strings:internal",
218218
"//absl/time",
219+
"//absl/types:source_location",
219220
"//absl/types:span",
220221
],
221222
)

absl/log/internal/log_message.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
#include "absl/strings/has_absl_stringify.h"
5151
#include "absl/strings/string_view.h"
5252
#include "absl/time/time.h"
53+
#include "absl/types/source_location.h"
54+
#include "absl/types/span.h"
5355

5456
namespace absl {
5557
ABSL_NAMESPACE_BEGIN
@@ -86,6 +88,11 @@ class LogMessage {
8688
// Overrides the location inferred from the callsite. The string pointed to
8789
// by `file` must be valid until the end of the statement.
8890
LogMessage& AtLocation(absl::string_view file, int line);
91+
// `loc` doesn't default to `absl::SourceLocation::current()` here since the
92+
// callsite is already the default location for `LOG` statements.
93+
LogMessage& AtLocation(absl::SourceLocation loc) {
94+
return AtLocation(loc.file_name(), static_cast<int>(loc.line()));
95+
}
8996
// Omits the prefix from this line. The prefix includes metadata about the
9097
// logged data such as source code location and timestamp.
9198
LogMessage& NoPrefix();

absl/log/log_modifier_methods_test.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "absl/strings/match.h"
2929
#include "absl/strings/string_view.h"
3030
#include "absl/time/time.h"
31+
#include "absl/types/source_location.h"
3132

3233
namespace {
3334
#if GTEST_HAS_DEATH_TEST
@@ -88,6 +89,23 @@ TEST(TailCallsModifiesTest, AtLocationFileLineLifetime) {
8889
<< "hello world";
8990
}
9091

92+
TEST(TailCallsModifiesTest, AtLocationSourceLocation) {
93+
absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected);
94+
EXPECT_CALL(test_sink, Send).Times(0);
95+
96+
const int log_line = __LINE__ + 1;
97+
constexpr absl::SourceLocation loc = absl::SourceLocation::current();
98+
auto do_log = [loc] { LOG(INFO).AtLocation(loc) << "hello world"; };
99+
100+
EXPECT_CALL(test_sink,
101+
Send(AllOf(SourceFilename(Eq(__FILE__)),
102+
SourceBasename(Eq("log_modifier_methods_test.cc")),
103+
SourceLine(Eq(log_line)))));
104+
105+
test_sink.StartCapturingLogs();
106+
do_log();
107+
}
108+
91109
TEST(TailCallsModifiesTest, NoPrefix) {
92110
absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected);
93111
EXPECT_CALL(test_sink, Send).Times(0);

absl/log/log_streamer.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "absl/strings/internal/ostringstream.h"
3636
#include "absl/strings/string_view.h"
3737
#include "absl/types/optional.h"
38+
#include "absl/types/source_location.h"
3839
#include "absl/utility/utility.h"
3940

4041
namespace absl {
@@ -87,6 +88,10 @@ class LogStreamer final {
8788
// To match `LOG`'s defaults:
8889
stream_->setf(std::ios_base::showbase | std::ios_base::boolalpha);
8990
}
91+
explicit LogStreamer(
92+
absl::LogSeverity severity,
93+
absl::SourceLocation loc = absl::SourceLocation::current())
94+
: LogStreamer(severity, loc.file_name(), static_cast<int>(loc.line())) {}
9095

9196
// A moved-from `absl::LogStreamer` does not `LOG` when destroyed,
9297
// and a program that streams into one has undefined behavior.
@@ -176,6 +181,27 @@ inline LogStreamer LogDebugFatalStreamer(absl::string_view file, int line) {
176181
return absl::LogStreamer(absl::kLogDebugFatal, file, line);
177182
}
178183

184+
inline LogStreamer LogInfoStreamer(
185+
absl::SourceLocation loc = absl::SourceLocation::current()) {
186+
return absl::LogStreamer(absl::LogSeverity::kInfo, loc);
187+
}
188+
inline LogStreamer LogWarningStreamer(
189+
absl::SourceLocation loc = absl::SourceLocation::current()) {
190+
return absl::LogStreamer(absl::LogSeverity::kWarning, loc);
191+
}
192+
inline LogStreamer LogErrorStreamer(
193+
absl::SourceLocation loc = absl::SourceLocation::current()) {
194+
return absl::LogStreamer(absl::LogSeverity::kError, loc);
195+
}
196+
inline LogStreamer LogFatalStreamer(
197+
absl::SourceLocation loc = absl::SourceLocation::current()) {
198+
return absl::LogStreamer(absl::LogSeverity::kFatal, loc);
199+
}
200+
inline LogStreamer LogDebugFatalStreamer(
201+
absl::SourceLocation loc = absl::SourceLocation::current()) {
202+
return absl::LogStreamer(absl::kLogDebugFatal, loc);
203+
}
204+
179205
ABSL_NAMESPACE_END
180206
} // namespace absl
181207

absl/log/log_streamer_test.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "absl/log/log.h"
3131
#include "absl/log/scoped_mock_log.h"
3232
#include "absl/strings/string_view.h"
33+
#include "absl/types/source_location.h"
3334

3435
namespace {
3536
using ::absl::log_internal::DeathTestExpectedLogging;
@@ -448,4 +449,21 @@ TEST(LogStreamerTest, CorrectDefaultFlags) {
448449
LOG(INFO) << false << std::hex << 0xdeadbeef;
449450
}
450451

452+
TEST(LogStreamerTest, AtSourceLocation) {
453+
const int log_line = __LINE__ + 2;
454+
auto do_log = [] {
455+
WriteToStream("foo", &absl::LogInfoStreamer().stream()); //
456+
};
457+
absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected);
458+
EXPECT_CALL(test_sink, Send).Times(0);
459+
460+
EXPECT_CALL(test_sink,
461+
Send(AllOf(SourceFilename(
462+
Eq(absl::SourceLocation::current().file_name())),
463+
SourceLine(Eq(log_line)))));
464+
465+
test_sink.StartCapturingLogs();
466+
do_log();
467+
}
468+
451469
} // namespace

absl/status/BUILD.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,13 @@ cc_library(
6565
"//absl/debugging:stacktrace",
6666
"//absl/debugging:symbolize",
6767
"//absl/functional:function_ref",
68+
"//absl/hash",
6869
"//absl/memory",
6970
"//absl/strings",
7071
"//absl/strings:cord",
7172
"//absl/strings:str_format",
7273
"//absl/types:optional",
74+
"//absl/types:source_location",
7375
"//absl/types:span",
7476
],
7577
)
@@ -84,6 +86,7 @@ cc_test(
8486
"//absl/strings",
8587
"//absl/strings:cord",
8688
"//absl/strings:str_format",
89+
"//absl/types:source_location",
8790
"@googletest//:gtest",
8891
"@googletest//:gtest_main",
8992
],
@@ -96,6 +99,7 @@ cc_binary(
9699
tags = ["benchmark"],
97100
deps = [
98101
":status",
102+
"//absl/types:source_location",
99103
"@google_benchmark//:benchmark_main",
100104
],
101105
)
@@ -122,6 +126,8 @@ cc_library(
122126
"//absl/strings",
123127
"//absl/strings:has_ostream_operator",
124128
"//absl/strings:str_format",
129+
"//absl/types:source_location",
130+
"//absl/types:span",
125131
"//absl/types:variant",
126132
"//absl/utility",
127133
],
@@ -139,6 +145,7 @@ cc_test(
139145
"//absl/memory",
140146
"//absl/strings",
141147
"//absl/types:any",
148+
"//absl/types:source_location",
142149
"//absl/types:variant",
143150
"//absl/utility",
144151
"@googletest//:gtest",

absl/status/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ absl_cc_library(
4141
absl::nullability
4242
absl::optional
4343
absl::raw_logging_internal
44+
absl::source_location
4445
absl::span
4546
absl::stacktrace
4647
absl::str_format
@@ -59,6 +60,7 @@ absl_cc_test(
5960
${ABSL_TEST_COPTS}
6061
DEPS
6162
absl::status
63+
absl::source_location
6264
absl::str_format
6365
absl::strings
6466
GTest::gmock_main
@@ -82,6 +84,7 @@ absl_cc_library(
8284
absl::nullability
8385
absl::raw_logging_internal
8486
absl::status
87+
absl::source_location
8588
absl::str_format
8689
absl::strings
8790
absl::type_traits
@@ -101,6 +104,7 @@ absl_cc_test(
101104
absl::status
102105
absl::status_matchers
103106
absl::statusor
107+
absl::source_location
104108
absl::strings
105109
GTest::gmock_main
106110
)

absl/status/internal/status_internal.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
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"
@@ -41,6 +42,8 @@
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

4548
namespace absl {
4649
ABSL_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+
133144
std::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

Comments
 (0)