blob: 1ee181a42c92fd78153dc302b5e6d9e1300bfe3c [file] [log] [blame]
Dan Albert58310b42015-03-13 23:06:01 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
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 * http://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
Elliott Hughes4f713192015-12-04 22:00:26 -080017#include "android-base/logging.h"
Dan Albert58310b42015-03-13 23:06:01 -070018
Elliott Hughes2e5ae002015-08-13 20:09:29 -070019#include <libgen.h>
20
Spencer Low765ae6b2015-09-17 19:36:10 -070021#if defined(_WIN32)
22#include <signal.h>
23#endif
24
Dan Albert58310b42015-03-13 23:06:01 -070025#include <regex>
26#include <string>
27
Elliott Hughes4f713192015-12-04 22:00:26 -080028#include "android-base/file.h"
29#include "android-base/stringprintf.h"
30#include "android-base/test_utils.h"
Dan Albert58310b42015-03-13 23:06:01 -070031
32#include <gtest/gtest.h>
33
34#ifdef __ANDROID__
35#define HOST_TEST(suite, name) TEST(suite, DISABLED_ ## name)
36#else
37#define HOST_TEST(suite, name) TEST(suite, name)
38#endif
39
40class CapturedStderr {
41 public:
42 CapturedStderr() : old_stderr_(-1) {
43 init();
44 }
45
46 ~CapturedStderr() {
47 reset();
48 }
49
50 int fd() const {
51 return temp_file_.fd;
52 }
53
54 private:
55 void init() {
Spencer Low765ae6b2015-09-17 19:36:10 -070056#if defined(_WIN32)
57 // On Windows, stderr is often buffered, so make sure it is unbuffered so
58 // that we can immediately read back what was written to stderr.
59 ASSERT_EQ(0, setvbuf(stderr, NULL, _IONBF, 0));
60#endif
Dan Albert58310b42015-03-13 23:06:01 -070061 old_stderr_ = dup(STDERR_FILENO);
62 ASSERT_NE(-1, old_stderr_);
63 ASSERT_NE(-1, dup2(fd(), STDERR_FILENO));
64 }
65
66 void reset() {
67 ASSERT_NE(-1, dup2(old_stderr_, STDERR_FILENO));
68 ASSERT_EQ(0, close(old_stderr_));
Spencer Low765ae6b2015-09-17 19:36:10 -070069 // Note: cannot restore prior setvbuf() setting.
Dan Albert58310b42015-03-13 23:06:01 -070070 }
71
72 TemporaryFile temp_file_;
73 int old_stderr_;
74};
75
Spencer Low765ae6b2015-09-17 19:36:10 -070076#if defined(_WIN32)
77static void ExitSignalAbortHandler(int) {
78 _exit(3);
79}
80#endif
81
82static void SuppressAbortUI() {
83#if defined(_WIN32)
84 // We really just want to call _set_abort_behavior(0, _CALL_REPORTFAULT) to
85 // suppress the Windows Error Reporting dialog box, but that API is not
86 // available in the OS-supplied C Runtime, msvcrt.dll, that we currently
87 // use (it is available in the Visual Studio C runtime).
88 //
89 // Instead, we setup a SIGABRT handler, which is called in abort() right
90 // before calling Windows Error Reporting. In the handler, we exit the
91 // process just like abort() does.
92 ASSERT_NE(SIG_ERR, signal(SIGABRT, ExitSignalAbortHandler));
93#endif
94}
95
Dan Albertb547c852015-03-27 11:20:14 -070096TEST(logging, CHECK) {
Spencer Low765ae6b2015-09-17 19:36:10 -070097 ASSERT_DEATH({SuppressAbortUI(); CHECK(false);}, "Check failed: false ");
Dan Albert58310b42015-03-13 23:06:01 -070098 CHECK(true);
99
Spencer Low765ae6b2015-09-17 19:36:10 -0700100 ASSERT_DEATH({SuppressAbortUI(); CHECK_EQ(0, 1);}, "Check failed: 0 == 1 ");
Dan Albert58310b42015-03-13 23:06:01 -0700101 CHECK_EQ(0, 0);
102
Spencer Low765ae6b2015-09-17 19:36:10 -0700103 ASSERT_DEATH({SuppressAbortUI(); CHECK_STREQ("foo", "bar");},
104 R"(Check failed: "foo" == "bar")");
Dan Albert58310b42015-03-13 23:06:01 -0700105 CHECK_STREQ("foo", "foo");
Spencer Low765ae6b2015-09-17 19:36:10 -0700106
107 // Test whether CHECK() and CHECK_STREQ() have a dangling if with no else.
108 bool flag = false;
109 if (true)
110 CHECK(true);
111 else
112 flag = true;
113 EXPECT_FALSE(flag) << "CHECK macro probably has a dangling if with no else";
114
115 flag = false;
116 if (true)
117 CHECK_STREQ("foo", "foo");
118 else
119 flag = true;
120 EXPECT_FALSE(flag) << "CHECK_STREQ probably has a dangling if with no else";
Dan Albert58310b42015-03-13 23:06:01 -0700121}
122
Andreas Gamped8f26e22016-09-13 10:44:46 -0700123TEST(logging, DCHECK) {
124 if (android::base::kEnableDChecks) {
125 ASSERT_DEATH({SuppressAbortUI(); DCHECK(false);}, "DCheck failed: false ");
126 }
127 DCHECK(true);
128
129 if (android::base::kEnableDChecks) {
130 ASSERT_DEATH({SuppressAbortUI(); DCHECK_EQ(0, 1);}, "DCheck failed: 0 == 1 ");
131 }
132 DCHECK_EQ(0, 0);
133
134 if (android::base::kEnableDChecks) {
135 ASSERT_DEATH({SuppressAbortUI(); DCHECK_STREQ("foo", "bar");},
136 R"(DCheck failed: "foo" == "bar")");
137 }
138 DCHECK_STREQ("foo", "foo");
139
140 // No testing whether we have a dangling else, possibly. That's inherent to the if (constexpr)
141 // setup we intentionally chose to force type-checks of debug code even in release builds (so
142 // we don't get more bit-rot).
143}
144
Andreas Gampe436f5a02016-09-22 10:15:01 -0700145
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700146#define CHECK_WOULD_LOG_DISABLED(severity) \
147 static_assert(android::base::severity < android::base::FATAL, "Bad input"); \
148 for (size_t i = static_cast<size_t>(android::base::severity) + 1; \
149 i <= static_cast<size_t>(android::base::FATAL); \
150 ++i) { \
151 { \
152 android::base::ScopedLogSeverity sls2(static_cast<android::base::LogSeverity>(i)); \
153 EXPECT_FALSE(WOULD_LOG(severity)) << i; \
154 } \
155 { \
156 android::base::ScopedLogSeverity sls2(static_cast<android::base::LogSeverity>(i)); \
157 EXPECT_FALSE(WOULD_LOG(::android::base::severity)) << i; \
158 } \
159 } \
160
161#define CHECK_WOULD_LOG_ENABLED(severity) \
162 for (size_t i = static_cast<size_t>(android::base::VERBOSE); \
163 i <= static_cast<size_t>(android::base::severity); \
164 ++i) { \
165 { \
166 android::base::ScopedLogSeverity sls2(static_cast<android::base::LogSeverity>(i)); \
167 EXPECT_TRUE(WOULD_LOG(severity)) << i; \
168 } \
169 { \
170 android::base::ScopedLogSeverity sls2(static_cast<android::base::LogSeverity>(i)); \
171 EXPECT_TRUE(WOULD_LOG(::android::base::severity)) << i; \
172 } \
173 } \
Andreas Gampe436f5a02016-09-22 10:15:01 -0700174
175TEST(logging, WOULD_LOG_FATAL) {
176 CHECK_WOULD_LOG_ENABLED(FATAL);
177}
178
179TEST(logging, WOULD_LOG_FATAL_WITHOUT_ABORT_disabled) {
180 CHECK_WOULD_LOG_DISABLED(FATAL_WITHOUT_ABORT);
181}
182
183TEST(logging, WOULD_LOG_FATAL_WITHOUT_ABORT_enabled) {
184 CHECK_WOULD_LOG_ENABLED(FATAL_WITHOUT_ABORT);
185}
186
187TEST(logging, WOULD_LOG_ERROR_disabled) {
188 CHECK_WOULD_LOG_DISABLED(ERROR);
189}
190
191TEST(logging, WOULD_LOG_ERROR_enabled) {
192 CHECK_WOULD_LOG_ENABLED(ERROR);
193}
194
195TEST(logging, WOULD_LOG_WARNING_disabled) {
196 CHECK_WOULD_LOG_DISABLED(WARNING);
197}
198
199TEST(logging, WOULD_LOG_WARNING_enabled) {
200 CHECK_WOULD_LOG_ENABLED(WARNING);
201}
202
203TEST(logging, WOULD_LOG_INFO_disabled) {
204 CHECK_WOULD_LOG_DISABLED(INFO);
205}
206
207TEST(logging, WOULD_LOG_INFO_enabled) {
208 CHECK_WOULD_LOG_ENABLED(INFO);
209}
210
211TEST(logging, WOULD_LOG_DEBUG_disabled) {
212 CHECK_WOULD_LOG_DISABLED(DEBUG);
213}
214
215TEST(logging, WOULD_LOG_DEBUG_enabled) {
216 CHECK_WOULD_LOG_ENABLED(DEBUG);
217}
218
219TEST(logging, WOULD_LOG_VERBOSE_disabled) {
220 CHECK_WOULD_LOG_DISABLED(VERBOSE);
221}
222
223TEST(logging, WOULD_LOG_VERBOSE_enabled) {
224 CHECK_WOULD_LOG_ENABLED(VERBOSE);
225}
226
227#undef CHECK_WOULD_LOG_DISABLED
228#undef CHECK_WOULD_LOG_ENABLED
229
230
Elliott Hughes13d78e42016-09-07 16:22:40 -0700231static std::string make_log_pattern(android::base::LogSeverity severity,
232 const char* message) {
Andreas Gampe550829d2016-09-07 10:10:50 -0700233 static const char log_characters[] = "VDIWEFF";
234 static_assert(arraysize(log_characters) - 1 == android::base::FATAL + 1,
235 "Mismatch in size of log_characters and values in LogSeverity");
Dan Albert58310b42015-03-13 23:06:01 -0700236 char log_char = log_characters[severity];
Spencer Lowbdab59a2015-08-11 16:00:13 -0700237 std::string holder(__FILE__);
Dan Albert58310b42015-03-13 23:06:01 -0700238 return android::base::StringPrintf(
Elliott Hughes13d78e42016-09-07 16:22:40 -0700239 "%c \\d+-\\d+ \\d+:\\d+:\\d+ \\s*\\d+ \\s*\\d+ %s:\\d+] %s",
Spencer Lowbdab59a2015-08-11 16:00:13 -0700240 log_char, basename(&holder[0]), message);
Dan Albert58310b42015-03-13 23:06:01 -0700241}
242
Elliott Hughes13d78e42016-09-07 16:22:40 -0700243static void CheckMessage(const CapturedStderr& cap,
244 android::base::LogSeverity severity, const char* expected) {
245 std::string output;
246 ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_SET));
247 android::base::ReadFdToString(cap.fd(), &output);
Dan Albert58310b42015-03-13 23:06:01 -0700248
Dan Albert5c190402015-04-29 11:32:23 -0700249 // We can't usefully check the output of any of these on Windows because we
250 // don't have std::regex, but we can at least make sure we printed at least as
251 // many characters are in the log message.
Elliott Hughes13d78e42016-09-07 16:22:40 -0700252 ASSERT_GT(output.length(), strlen(expected));
253 ASSERT_NE(nullptr, strstr(output.c_str(), expected)) << output;
Dan Albert58310b42015-03-13 23:06:01 -0700254
Dan Albert5c190402015-04-29 11:32:23 -0700255#if !defined(_WIN32)
Elliott Hughes13d78e42016-09-07 16:22:40 -0700256 std::regex message_regex(make_log_pattern(severity, expected));
257 ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
Dan Albert5c190402015-04-29 11:32:23 -0700258#endif
Dan Albert58310b42015-03-13 23:06:01 -0700259}
260
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700261
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700262#define CHECK_LOG_STREAM_DISABLED(severity) \
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700263 { \
264 android::base::ScopedLogSeverity sls1(android::base::FATAL); \
265 CapturedStderr cap1; \
266 LOG_STREAM(severity) << "foo bar"; \
267 ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \
268 } \
269 { \
270 android::base::ScopedLogSeverity sls1(android::base::FATAL); \
271 CapturedStderr cap1; \
272 LOG_STREAM(::android::base::severity) << "foo bar"; \
273 ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \
274 } \
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700275
276#define CHECK_LOG_STREAM_ENABLED(severity) \
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700277 { \
278 android::base::ScopedLogSeverity sls2(android::base::severity); \
279 CapturedStderr cap2; \
280 LOG_STREAM(severity) << "foobar"; \
281 CheckMessage(cap2, android::base::severity, "foobar"); \
282 } \
283 { \
284 android::base::ScopedLogSeverity sls2(android::base::severity); \
285 CapturedStderr cap2; \
286 LOG_STREAM(::android::base::severity) << "foobar"; \
287 CheckMessage(cap2, android::base::severity, "foobar"); \
288 } \
Andreas Gampe436f5a02016-09-22 10:15:01 -0700289
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700290TEST(logging, LOG_STREAM_FATAL_WITHOUT_ABORT_disabled) {
291 CHECK_LOG_STREAM_DISABLED(FATAL_WITHOUT_ABORT);
Andreas Gampe436f5a02016-09-22 10:15:01 -0700292}
293
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700294TEST(logging, LOG_STREAM_FATAL_WITHOUT_ABORT_enabled) {
295 CHECK_LOG_STREAM_ENABLED(FATAL_WITHOUT_ABORT);
Andreas Gampe436f5a02016-09-22 10:15:01 -0700296}
297
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700298TEST(logging, LOG_STREAM_ERROR_disabled) {
299 CHECK_LOG_STREAM_DISABLED(ERROR);
Andreas Gampe436f5a02016-09-22 10:15:01 -0700300}
301
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700302TEST(logging, LOG_STREAM_ERROR_enabled) {
303 CHECK_LOG_STREAM_ENABLED(ERROR);
Andreas Gampe436f5a02016-09-22 10:15:01 -0700304}
305
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700306TEST(logging, LOG_STREAM_WARNING_disabled) {
307 CHECK_LOG_STREAM_DISABLED(WARNING);
Andreas Gampe436f5a02016-09-22 10:15:01 -0700308}
309
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700310TEST(logging, LOG_STREAM_WARNING_enabled) {
311 CHECK_LOG_STREAM_ENABLED(WARNING);
Andreas Gampe436f5a02016-09-22 10:15:01 -0700312}
313
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700314TEST(logging, LOG_STREAM_INFO_disabled) {
315 CHECK_LOG_STREAM_DISABLED(INFO);
316}
Andreas Gampe436f5a02016-09-22 10:15:01 -0700317
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700318TEST(logging, LOG_STREAM_INFO_enabled) {
319 CHECK_LOG_STREAM_ENABLED(INFO);
320}
321
322TEST(logging, LOG_STREAM_DEBUG_disabled) {
323 CHECK_LOG_STREAM_DISABLED(DEBUG);
324}
325
326TEST(logging, LOG_STREAM_DEBUG_enabled) {
327 CHECK_LOG_STREAM_ENABLED(DEBUG);
328}
329
330TEST(logging, LOG_STREAM_VERBOSE_disabled) {
331 CHECK_LOG_STREAM_DISABLED(VERBOSE);
332}
333
334TEST(logging, LOG_STREAM_VERBOSE_enabled) {
335 CHECK_LOG_STREAM_ENABLED(VERBOSE);
336}
337
338#undef CHECK_LOG_STREAM_DISABLED
339#undef CHECK_LOG_STREAM_ENABLED
340
Andreas Gampe436f5a02016-09-22 10:15:01 -0700341
342#define CHECK_LOG_DISABLED(severity) \
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700343 { \
344 android::base::ScopedLogSeverity sls1(android::base::FATAL); \
345 CapturedStderr cap1; \
346 LOG(severity) << "foo bar"; \
347 ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \
348 } \
349 { \
350 android::base::ScopedLogSeverity sls1(android::base::FATAL); \
351 CapturedStderr cap1; \
352 LOG(::android::base::severity) << "foo bar"; \
353 ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \
354 } \
Andreas Gampe436f5a02016-09-22 10:15:01 -0700355
356#define CHECK_LOG_ENABLED(severity) \
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700357 { \
358 android::base::ScopedLogSeverity sls2(android::base::severity); \
359 CapturedStderr cap2; \
360 LOG(severity) << "foobar"; \
361 CheckMessage(cap2, android::base::severity, "foobar"); \
362 } \
363 { \
364 android::base::ScopedLogSeverity sls2(android::base::severity); \
365 CapturedStderr cap2; \
366 LOG(::android::base::severity) << "foobar"; \
367 CheckMessage(cap2, android::base::severity, "foobar"); \
368 } \
Andreas Gampe436f5a02016-09-22 10:15:01 -0700369
Elliott Hughes13d78e42016-09-07 16:22:40 -0700370TEST(logging, LOG_FATAL) {
371 ASSERT_DEATH({SuppressAbortUI(); LOG(FATAL) << "foobar";}, "foobar");
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700372 ASSERT_DEATH({SuppressAbortUI(); LOG(::android::base::FATAL) << "foobar";}, "foobar");
Elliott Hughes13d78e42016-09-07 16:22:40 -0700373}
Dan Albert58310b42015-03-13 23:06:01 -0700374
Andreas Gampe550829d2016-09-07 10:10:50 -0700375TEST(logging, LOG_FATAL_WITHOUT_ABORT_disabled) {
376 CHECK_LOG_DISABLED(FATAL_WITHOUT_ABORT);
377}
378
379TEST(logging, LOG_FATAL_WITHOUT_ABORT_enabled) {
380 CHECK_LOG_ENABLED(FATAL_WITHOUT_ABORT);
381}
382
Elliott Hughes13d78e42016-09-07 16:22:40 -0700383TEST(logging, LOG_ERROR_disabled) {
384 CHECK_LOG_DISABLED(ERROR);
385}
Dan Albert58310b42015-03-13 23:06:01 -0700386
Elliott Hughes13d78e42016-09-07 16:22:40 -0700387TEST(logging, LOG_ERROR_enabled) {
388 CHECK_LOG_ENABLED(ERROR);
389}
390
391TEST(logging, LOG_WARNING_disabled) {
392 CHECK_LOG_DISABLED(WARNING);
393}
394
395TEST(logging, LOG_WARNING_enabled) {
396 CHECK_LOG_ENABLED(WARNING);
397}
398
399TEST(logging, LOG_INFO_disabled) {
400 CHECK_LOG_DISABLED(INFO);
401}
402
403TEST(logging, LOG_INFO_enabled) {
404 CHECK_LOG_ENABLED(INFO);
405}
406
407TEST(logging, LOG_DEBUG_disabled) {
408 CHECK_LOG_DISABLED(DEBUG);
409}
410
411TEST(logging, LOG_DEBUG_enabled) {
412 CHECK_LOG_ENABLED(DEBUG);
413}
414
415TEST(logging, LOG_VERBOSE_disabled) {
416 CHECK_LOG_DISABLED(VERBOSE);
417}
418
419TEST(logging, LOG_VERBOSE_enabled) {
420 CHECK_LOG_ENABLED(VERBOSE);
421}
422
Andreas Gampe436f5a02016-09-22 10:15:01 -0700423#undef CHECK_LOG_DISABLED
424#undef CHECK_LOG_ENABLED
425
Andreas Gampe436f5a02016-09-22 10:15:01 -0700426
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700427TEST(logging, LOG_complex_param) {
428#define CHECK_LOG_COMBINATION(use_scoped_log_severity_info, use_logging_severity_info) \
Andreas Gampe436f5a02016-09-22 10:15:01 -0700429 { \
430 android::base::ScopedLogSeverity sls( \
431 (use_scoped_log_severity_info) ? ::android::base::INFO : ::android::base::WARNING); \
432 CapturedStderr cap; \
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700433 LOG((use_logging_severity_info) ? ::android::base::INFO : ::android::base::WARNING) \
Andreas Gampe436f5a02016-09-22 10:15:01 -0700434 << "foobar"; \
435 if ((use_scoped_log_severity_info) || !(use_logging_severity_info)) { \
436 CheckMessage(cap, \
437 (use_logging_severity_info) ? ::android::base::INFO : ::android::base::WARNING, \
438 "foobar"); \
439 } else { \
440 ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_CUR)); \
441 } \
442 }
443
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700444 CHECK_LOG_COMBINATION(false,false);
445 CHECK_LOG_COMBINATION(false,true);
446 CHECK_LOG_COMBINATION(true,false);
447 CHECK_LOG_COMBINATION(true,true);
Andreas Gampe436f5a02016-09-22 10:15:01 -0700448
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700449#undef CHECK_LOG_COMBINATION
Andreas Gampe436f5a02016-09-22 10:15:01 -0700450}
451
452
Elliott Hughes13d78e42016-09-07 16:22:40 -0700453TEST(logging, LOG_does_not_clobber_errno) {
454 CapturedStderr cap;
455 errno = 12345;
456 LOG(INFO) << (errno = 67890);
457 EXPECT_EQ(12345, errno) << "errno was not restored";
458
459 CheckMessage(cap, android::base::INFO, "67890");
460}
461
462TEST(logging, PLOG_does_not_clobber_errno) {
463 CapturedStderr cap;
464 errno = 12345;
465 PLOG(INFO) << (errno = 67890);
466 EXPECT_EQ(12345, errno) << "errno was not restored";
467
468 CheckMessage(cap, android::base::INFO, "67890");
469}
470
471TEST(logging, LOG_does_not_have_dangling_if) {
472 CapturedStderr cap; // So the logging below has no side-effects.
473
474 // Do the test two ways: once where we hypothesize that LOG()'s if
475 // will evaluate to true (when severity is high enough) and once when we
476 // expect it to evaluate to false (when severity is not high enough).
477 bool flag = false;
478 if (true)
479 LOG(INFO) << "foobar";
480 else
481 flag = true;
482
483 EXPECT_FALSE(flag) << "LOG macro probably has a dangling if with no else";
484
485 flag = false;
486 if (true)
487 LOG(VERBOSE) << "foobar";
488 else
489 flag = true;
490
491 EXPECT_FALSE(flag) << "LOG macro probably has a dangling if with no else";
Andreas Gampe436f5a02016-09-22 10:15:01 -0700492}
Elliott Hughes13d78e42016-09-07 16:22:40 -0700493
494#define CHECK_PLOG_DISABLED(severity) \
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700495 { \
496 android::base::ScopedLogSeverity sls1(android::base::FATAL); \
497 CapturedStderr cap1; \
498 PLOG(severity) << "foo bar"; \
499 ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \
500 } \
501 { \
502 android::base::ScopedLogSeverity sls1(android::base::FATAL); \
503 CapturedStderr cap1; \
504 PLOG(severity) << "foo bar"; \
505 ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \
506 } \
Elliott Hughes13d78e42016-09-07 16:22:40 -0700507
508#define CHECK_PLOG_ENABLED(severity) \
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700509 { \
510 android::base::ScopedLogSeverity sls2(android::base::severity); \
511 CapturedStderr cap2; \
512 errno = ENOENT; \
513 PLOG(severity) << "foobar"; \
514 CheckMessage(cap2, android::base::severity, "foobar: No such file or directory"); \
515 } \
516 { \
517 android::base::ScopedLogSeverity sls2(android::base::severity); \
518 CapturedStderr cap2; \
519 errno = ENOENT; \
520 PLOG(severity) << "foobar"; \
521 CheckMessage(cap2, android::base::severity, "foobar: No such file or directory"); \
522 } \
Elliott Hughes13d78e42016-09-07 16:22:40 -0700523
Andreas Gampe550829d2016-09-07 10:10:50 -0700524TEST(logging, PLOG_FATAL) {
525 ASSERT_DEATH({SuppressAbortUI(); PLOG(FATAL) << "foobar";}, "foobar");
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700526 ASSERT_DEATH({SuppressAbortUI(); PLOG(::android::base::FATAL) << "foobar";}, "foobar");
Andreas Gampe550829d2016-09-07 10:10:50 -0700527}
528
529TEST(logging, PLOG_FATAL_WITHOUT_ABORT_disabled) {
530 CHECK_PLOG_DISABLED(FATAL_WITHOUT_ABORT);
531}
532
533TEST(logging, PLOG_FATAL_WITHOUT_ABORT_enabled) {
534 CHECK_PLOG_ENABLED(FATAL_WITHOUT_ABORT);
535}
536
Elliott Hughes13d78e42016-09-07 16:22:40 -0700537TEST(logging, PLOG_ERROR_disabled) {
538 CHECK_PLOG_DISABLED(ERROR);
539}
540
541TEST(logging, PLOG_ERROR_enabled) {
542 CHECK_PLOG_ENABLED(ERROR);
543}
544
545TEST(logging, PLOG_WARNING_disabled) {
546 CHECK_PLOG_DISABLED(WARNING);
547}
548
549TEST(logging, PLOG_WARNING_enabled) {
550 CHECK_PLOG_ENABLED(WARNING);
551}
552
553TEST(logging, PLOG_INFO_disabled) {
554 CHECK_PLOG_DISABLED(INFO);
555}
556
557TEST(logging, PLOG_INFO_enabled) {
558 CHECK_PLOG_ENABLED(INFO);
559}
560
561TEST(logging, PLOG_DEBUG_disabled) {
562 CHECK_PLOG_DISABLED(DEBUG);
563}
564
565TEST(logging, PLOG_DEBUG_enabled) {
566 CHECK_PLOG_ENABLED(DEBUG);
567}
568
569TEST(logging, PLOG_VERBOSE_disabled) {
570 CHECK_PLOG_DISABLED(VERBOSE);
571}
572
573TEST(logging, PLOG_VERBOSE_enabled) {
574 CHECK_PLOG_ENABLED(VERBOSE);
Dan Albert58310b42015-03-13 23:06:01 -0700575}
576
Andreas Gampe436f5a02016-09-22 10:15:01 -0700577#undef CHECK_PLOG_DISABLED
578#undef CHECK_PLOG_ENABLED
579
580
Dan Albertb547c852015-03-27 11:20:14 -0700581TEST(logging, UNIMPLEMENTED) {
Elliott Hughes13d78e42016-09-07 16:22:40 -0700582 std::string expected = android::base::StringPrintf("%s unimplemented ", __PRETTY_FUNCTION__);
Dan Albert58310b42015-03-13 23:06:01 -0700583
Elliott Hughes13d78e42016-09-07 16:22:40 -0700584 CapturedStderr cap;
585 errno = ENOENT;
586 UNIMPLEMENTED(ERROR);
587 CheckMessage(cap, android::base::ERROR, expected.c_str());
Dan Albert58310b42015-03-13 23:06:01 -0700588}
Andreas Gampe2691e332016-09-08 11:03:58 -0700589
590static void NoopAborter(const char* msg ATTRIBUTE_UNUSED) {
591 LOG(ERROR) << "called noop";
592}
593
594TEST(logging, LOG_FATAL_NOOP_ABORTER) {
595 {
596 android::base::SetAborter(NoopAborter);
597
598 android::base::ScopedLogSeverity sls(android::base::ERROR);
599 CapturedStderr cap;
600 LOG(FATAL) << "foobar";
601 CheckMessage(cap, android::base::FATAL, "foobar");
602 CheckMessage(cap, android::base::ERROR, "called noop");
603
604 android::base::SetAborter(android::base::DefaultAborter);
605 }
606
607 ASSERT_DEATH({SuppressAbortUI(); LOG(FATAL) << "foobar";}, "foobar");
608}
Andreas Gampeb4e32f32016-10-04 19:17:07 -0700609
610struct CountLineAborter {
611 static void CountLineAborterFunction(const char* msg) {
612 while (*msg != 0) {
613 if (*msg == '\n') {
614 newline_count++;
615 }
616 msg++;
617 }
618 }
619 static size_t newline_count;
620};
621size_t CountLineAborter::newline_count = 0;
622
623TEST(logging, LOG_FATAL_ABORTER_MESSAGE) {
624 CountLineAborter::newline_count = 0;
625 android::base::SetAborter(CountLineAborter::CountLineAborterFunction);
626
627 android::base::ScopedLogSeverity sls(android::base::ERROR);
628 CapturedStderr cap;
629 LOG(FATAL) << "foo\nbar";
630
631 EXPECT_EQ(CountLineAborter::newline_count, 1U + 1U); // +1 for final '\n'.
632}