1 //
2 // Copyright 2022 The Abseil Authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // https://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 #include "absl/log/scoped_mock_log.h"
17
18 #include <atomic>
19 #include <string>
20
21 #include "gmock/gmock.h"
22 #include "absl/base/config.h"
23 #include "absl/base/internal/raw_logging.h"
24 #include "absl/log/log_entry.h"
25 #include "absl/log/log_sink.h"
26 #include "absl/log/log_sink_registry.h"
27 #include "absl/strings/string_view.h"
28
29 namespace absl {
30 ABSL_NAMESPACE_BEGIN
31
ScopedMockLog(MockLogDefault default_exp)32 ScopedMockLog::ScopedMockLog(MockLogDefault default_exp)
33 : sink_(this), is_capturing_logs_(false), is_triggered_(false) {
34 if (default_exp == MockLogDefault::kIgnoreUnexpected) {
35 // Ignore all calls to Log we did not set expectations for.
36 EXPECT_CALL(*this, Log).Times(::testing::AnyNumber());
37 } else {
38 // Disallow all calls to Log we did not set expectations for.
39 EXPECT_CALL(*this, Log).Times(0);
40 }
41 // By default Send mock forwards to Log mock.
42 EXPECT_CALL(*this, Send)
43 .Times(::testing::AnyNumber())
44 .WillRepeatedly([this](const absl::LogEntry& entry) {
45 is_triggered_.store(true, std::memory_order_relaxed);
46 Log(entry.log_severity(), std::string(entry.source_filename()),
47 std::string(entry.text_message()));
48 });
49
50 // By default We ignore all Flush calls.
51 EXPECT_CALL(*this, Flush).Times(::testing::AnyNumber());
52 }
53
~ScopedMockLog()54 ScopedMockLog::~ScopedMockLog() {
55 ABSL_RAW_CHECK(is_triggered_.load(std::memory_order_relaxed),
56 "Did you forget to call StartCapturingLogs()?");
57
58 if (is_capturing_logs_) StopCapturingLogs();
59 }
60
StartCapturingLogs()61 void ScopedMockLog::StartCapturingLogs() {
62 ABSL_RAW_CHECK(!is_capturing_logs_,
63 "StartCapturingLogs() can be called only when the "
64 "absl::ScopedMockLog object is not capturing logs.");
65
66 is_capturing_logs_ = true;
67 is_triggered_.store(true, std::memory_order_relaxed);
68 absl::AddLogSink(&sink_);
69 }
70
StopCapturingLogs()71 void ScopedMockLog::StopCapturingLogs() {
72 ABSL_RAW_CHECK(is_capturing_logs_,
73 "StopCapturingLogs() can be called only when the "
74 "absl::ScopedMockLog object is capturing logs.");
75
76 is_capturing_logs_ = false;
77 absl::RemoveLogSink(&sink_);
78 }
79
UseAsLocalSink()80 absl::LogSink& ScopedMockLog::UseAsLocalSink() {
81 is_triggered_.store(true, std::memory_order_relaxed);
82 return sink_;
83 }
84
85 ABSL_NAMESPACE_END
86 } // namespace absl
87