blob: 84018b3f97092889d742f70041044e86ffa96077 [file] [log] [blame]
mark a. foltzd6a5a222020-07-28 17:28:06 -07001// Copyright 2020 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "platform/api/logging.h"
6
7#include <string>
8#include <vector>
9
10#include "absl/strings/match.h"
11#include "absl/strings/str_cat.h"
12#include "absl/strings/string_view.h"
13#include "gtest/gtest.h"
14#include "platform/impl/logging.h"
15#include "platform/impl/logging_test.h"
16#include "util/osp_logging.h"
17
18namespace openscreen {
19
20class LoggingTest : public ::testing::Test {
21 public:
22 LoggingTest() {}
23
24 void SetUp() {
25 previous_log_level = GetLogLevel();
26 SetLogLevel(LogLevel::kInfo);
27 SetLogBufferForTest(&log_messages);
28 }
29
30 void TearDown() {
31 SetLogLevel(previous_log_level);
32 SetLogBufferForTest(nullptr);
33 DisableBreakForTest(nullptr);
34 }
35
36 protected:
37 void DisableBreak() { DisableBreakForTest(&break_was_called); }
38
39 void ExpectLog(LogLevel level, absl::string_view message) {
40 const char* level_string = "";
41 switch (level) {
42 case LogLevel::kVerbose:
43 level_string = "VERBOSE";
44 break;
45 case LogLevel::kInfo:
46 level_string = "INFO";
47 break;
48 case LogLevel::kWarning:
49 level_string = "WARNING";
50 break;
51 case LogLevel::kError:
52 level_string = "ERROR";
53 break;
54 case LogLevel::kFatal:
55 level_string = "FATAL";
56 break;
57 }
58 expected_messages.push_back({level_string, std::string(message)});
59 }
60
61 void VerifyNoLogs() { EXPECT_TRUE(log_messages.empty()); }
62
63 void VerifyLogs() {
64 ASSERT_EQ(expected_messages.size(), log_messages.size());
65 auto expected_it = expected_messages.begin();
66 auto actual_it = log_messages.begin();
67 // NOTE: This is somewhat brittle; it relies on details of how
68 // logging_posix.cc formats log messages.
69 while (expected_it != expected_messages.end()) {
70 EXPECT_TRUE(
71 absl::StartsWith(*actual_it, absl::StrCat("[", expected_it->level)));
72 EXPECT_TRUE(absl::EndsWith(
73 *actual_it, absl::StrCat("] ", expected_it->message, "\n")));
74 actual_it++;
75 expected_it++;
76 }
77 expected_messages.clear();
78 log_messages.clear();
79 }
80
81 void ExpectBreakAndClear() {
82 EXPECT_TRUE(break_was_called);
83 break_was_called = false;
84 }
85
86 void ExpectDebugBreakAndClear() {
87#if OSP_DCHECK_IS_ON()
88 EXPECT_TRUE(break_was_called);
89 break_was_called = false;
90#else
91 EXPECT_FALSE(break_was_called);
92#endif // OSP_DCHECK_IS_ON()
93 }
94
95 private:
96 struct LogMessage {
97 std::string level;
98 std::string message;
99 };
100 std::vector<std::string> log_messages;
101 std::vector<LogMessage> expected_messages;
102 bool break_was_called = false;
103 LogLevel previous_log_level = LogLevel::kWarning;
104};
105
106TEST_F(LoggingTest, UnconditionalLogging) {
107 SetLogLevel(LogLevel::kVerbose);
108 ExpectLog(LogLevel::kVerbose, "Verbose");
109 ExpectLog(LogLevel::kInfo, "Info");
110 ExpectLog(LogLevel::kWarning, "Warning");
111 ExpectLog(LogLevel::kError, "Error");
112
113 OSP_VLOG << "Verbose";
114 OSP_LOG_INFO << "Info";
115 OSP_LOG_WARN << "Warning";
116 OSP_LOG_ERROR << "Error";
117
118 VerifyLogs();
119
120 SetLogLevel(LogLevel::kWarning);
121 ExpectLog(LogLevel::kWarning, "Warning");
122 ExpectLog(LogLevel::kError, "Error");
123
124 OSP_VLOG << "Verbose";
125 OSP_LOG_INFO << "Info";
126 OSP_LOG_WARN << "Warning";
127 OSP_LOG_ERROR << "Error";
128
129 VerifyLogs();
130}
131
132TEST_F(LoggingTest, ConditionalLogging) {
133 SetLogLevel(LogLevel::kVerbose);
134
135 ExpectLog(LogLevel::kVerbose, "Verbose");
136 ExpectLog(LogLevel::kInfo, "Info");
137 ExpectLog(LogLevel::kWarning, "Warning");
138 ExpectLog(LogLevel::kError, "Error");
139
140 OSP_VLOG_IF(true) << "Verbose";
141 OSP_LOG_IF(INFO, true) << "Info";
142 OSP_LOG_IF(WARN, true) << "Warning";
143 OSP_LOG_IF(ERROR, true) << "Error";
144 VerifyLogs();
145
146 OSP_VLOG_IF(false) << "Verbose";
147 OSP_LOG_IF(INFO, false) << "Info";
148 OSP_LOG_IF(WARN, false) << "Warning";
149 OSP_LOG_IF(ERROR, false) << "Error";
150 VerifyNoLogs();
151}
152
153TEST_F(LoggingTest, DebugUnconditionalLogging) {
154 SetLogLevel(LogLevel::kVerbose);
155 OSP_DVLOG << "Verbose";
156 OSP_DLOG_INFO << "Info";
157 OSP_DLOG_WARN << "Warning";
158 OSP_DLOG_ERROR << "Error";
159
160#if OSP_DCHECK_IS_ON()
161 ExpectLog(LogLevel::kVerbose, "Verbose");
162 ExpectLog(LogLevel::kInfo, "Info");
163 ExpectLog(LogLevel::kWarning, "Warning");
164 ExpectLog(LogLevel::kError, "Error");
165 VerifyLogs();
166#else
167 VerifyNoLogs();
168#endif // OSP_DCHECK_IS_ON()
169
170 SetLogLevel(LogLevel::kWarning);
171
172 OSP_DVLOG << "Verbose";
173 OSP_DLOG_INFO << "Info";
174 OSP_DLOG_WARN << "Warning";
175 OSP_DLOG_ERROR << "Error";
176
177#if OSP_DCHECK_IS_ON()
178 ExpectLog(LogLevel::kWarning, "Warning");
179 ExpectLog(LogLevel::kError, "Error");
180 VerifyLogs();
181#else
182 VerifyNoLogs();
183#endif // OSP_DCHECK_IS_ON()
184}
185
186TEST_F(LoggingTest, DebugConditionalLogging) {
187 SetLogLevel(LogLevel::kVerbose);
188
189 OSP_DVLOG_IF(true) << "Verbose";
190 OSP_DLOG_IF(INFO, true) << "Info";
191 OSP_DLOG_IF(WARN, true) << "Warning";
192 OSP_DLOG_IF(ERROR, true) << "Error";
193
194#if OSP_DCHECK_IS_ON()
195 ExpectLog(LogLevel::kVerbose, "Verbose");
196 ExpectLog(LogLevel::kInfo, "Info");
197 ExpectLog(LogLevel::kWarning, "Warning");
198 ExpectLog(LogLevel::kError, "Error");
199 VerifyLogs();
200#else
201 VerifyNoLogs();
202#endif // OSP_DCHECK_IS_ON()
203
204 OSP_DVLOG_IF(false) << "Verbose";
205 OSP_DLOG_IF(INFO, false) << "Info";
206 OSP_DLOG_IF(WARN, false) << "Warning";
207 OSP_DLOG_IF(ERROR, false) << "Error";
208 VerifyNoLogs();
209}
210
211TEST_F(LoggingTest, CheckAndLogFatal) {
212 DisableBreak();
213
214 ExpectLog(LogLevel::kFatal, "OSP_CHECK(false) failed: ");
215 OSP_CHECK(false);
216 ExpectBreakAndClear();
217
218 ExpectLog(LogLevel::kFatal, "OSP_CHECK((1) == (2)) failed: 1 vs. 2: ");
219 OSP_CHECK_EQ(1, 2);
220 ExpectBreakAndClear();
221
222 ExpectLog(LogLevel::kFatal, "OSP_CHECK((1) != (1)) failed: 1 vs. 1: ");
223 OSP_CHECK_NE(1, 1);
224 ExpectBreakAndClear();
225
226 ExpectLog(LogLevel::kFatal, "OSP_CHECK((2) < (1)) failed: 2 vs. 1: ");
227 OSP_CHECK_LT(2, 1);
228 ExpectBreakAndClear();
229
230 ExpectLog(LogLevel::kFatal, "OSP_CHECK((2) <= (1)) failed: 2 vs. 1: ");
231 OSP_CHECK_LE(2, 1);
232 ExpectBreakAndClear();
233
234 ExpectLog(LogLevel::kFatal, "OSP_CHECK((1) > (2)) failed: 1 vs. 2: ");
235 OSP_CHECK_GT(1, 2);
236 ExpectBreakAndClear();
237
238 ExpectLog(LogLevel::kFatal, "OSP_CHECK((1) >= (2)) failed: 1 vs. 2: ");
239 OSP_CHECK_GE(1, 2);
240 ExpectBreakAndClear();
241
242 ExpectLog(LogLevel::kFatal, "Fatal");
243 OSP_LOG_FATAL << "Fatal";
244 ExpectBreakAndClear();
245
246 VerifyLogs();
247}
248
249TEST_F(LoggingTest, DCheckAndDLogFatal) {
250 DisableBreak();
251
252 ExpectLog(LogLevel::kFatal, "OSP_CHECK(false) failed: ");
253 OSP_DCHECK(false);
254 ExpectDebugBreakAndClear();
255
256 ExpectLog(LogLevel::kFatal, "OSP_CHECK((1) == (2)) failed: 1 vs. 2: ");
257 OSP_DCHECK_EQ(1, 2);
258 ExpectDebugBreakAndClear();
259
260 ExpectLog(LogLevel::kFatal, "OSP_CHECK((1) != (1)) failed: 1 vs. 1: ");
261 OSP_DCHECK_NE(1, 1);
262 ExpectDebugBreakAndClear();
263
264 ExpectLog(LogLevel::kFatal, "OSP_CHECK((2) < (1)) failed: 2 vs. 1: ");
265 OSP_DCHECK_LT(2, 1);
266 ExpectDebugBreakAndClear();
267
268 ExpectLog(LogLevel::kFatal, "OSP_CHECK((2) <= (1)) failed: 2 vs. 1: ");
269 OSP_DCHECK_LE(2, 1);
270 ExpectDebugBreakAndClear();
271
272 ExpectLog(LogLevel::kFatal, "OSP_CHECK((1) > (2)) failed: 1 vs. 2: ");
273 OSP_DCHECK_GT(1, 2);
274 ExpectDebugBreakAndClear();
275
276 ExpectLog(LogLevel::kFatal, "OSP_CHECK((1) >= (2)) failed: 1 vs. 2: ");
277 OSP_DCHECK_GE(1, 2);
278 ExpectDebugBreakAndClear();
279
280 ExpectLog(LogLevel::kFatal, "Fatal");
281 OSP_DLOG_FATAL << "Fatal";
282 ExpectDebugBreakAndClear();
283
284#if OSP_DCHECK_IS_ON()
285 VerifyLogs();
286#else
287 VerifyNoLogs();
288#endif // OSP_DCHECK_IS_ON()
289}
290
291TEST_F(LoggingTest, OspUnimplemented) {
292 // Default is to log once per process if the level >= kWarning.
293 SetLogLevel(LogLevel::kWarning);
294 ExpectLog(LogLevel::kWarning, "TestBody: UNIMPLEMENTED() hit.");
295 for (int i = 0; i < 2; i++) {
296 OSP_UNIMPLEMENTED();
297 }
298 VerifyLogs();
299
300 // Setting the level to kVerbose logs every time.
301 SetLogLevel(LogLevel::kVerbose);
302 std::string message("TestBody: UNIMPLEMENTED() hit.");
303 ExpectLog(LogLevel::kVerbose, message);
304 ExpectLog(LogLevel::kVerbose, message);
305 for (int i = 0; i < 2; i++) {
306 OSP_UNIMPLEMENTED();
307 }
308 VerifyLogs();
309}
310
311TEST_F(LoggingTest, OspNotReached) {
312 DisableBreak();
313 ExpectLog(LogLevel::kFatal, "TestBody: NOTREACHED() hit.");
314
315 OSP_NOTREACHED();
316
317 VerifyLogs();
318 ExpectBreakAndClear();
319}
320
321} // namespace openscreen