blob: dccb7748827b8b4a228ea85be4166bbe3f6c5897 [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#define CHECK_WOULD_LOG_DISABLED(severity) \
146 static_assert(android::base::severity < android::base::FATAL, "Bad input"); \
147 for (size_t i = static_cast<size_t>(android::base::severity) + 1; \
148 i <= static_cast<size_t>(android::base::FATAL); \
149 ++i) { \
150 android::base::ScopedLogSeverity sls2(static_cast<android::base::LogSeverity>(i)); \
151 EXPECT_FALSE(WOULD_LOG(severity)) << i; \
152 }
153
154#define CHECK_WOULD_LOG_ENABLED(severity) \
155 for (size_t i = static_cast<size_t>(android::base::VERBOSE); \
156 i <= static_cast<size_t>(android::base::severity); \
157 ++i) { \
158 android::base::ScopedLogSeverity sls2(static_cast<android::base::LogSeverity>(i)); \
159 EXPECT_TRUE(WOULD_LOG(severity)) << i; \
160 }
161
162TEST(logging, WOULD_LOG_FATAL) {
163 CHECK_WOULD_LOG_ENABLED(FATAL);
164}
165
166TEST(logging, WOULD_LOG_FATAL_WITHOUT_ABORT_disabled) {
167 CHECK_WOULD_LOG_DISABLED(FATAL_WITHOUT_ABORT);
168}
169
170TEST(logging, WOULD_LOG_FATAL_WITHOUT_ABORT_enabled) {
171 CHECK_WOULD_LOG_ENABLED(FATAL_WITHOUT_ABORT);
172}
173
174TEST(logging, WOULD_LOG_ERROR_disabled) {
175 CHECK_WOULD_LOG_DISABLED(ERROR);
176}
177
178TEST(logging, WOULD_LOG_ERROR_enabled) {
179 CHECK_WOULD_LOG_ENABLED(ERROR);
180}
181
182TEST(logging, WOULD_LOG_WARNING_disabled) {
183 CHECK_WOULD_LOG_DISABLED(WARNING);
184}
185
186TEST(logging, WOULD_LOG_WARNING_enabled) {
187 CHECK_WOULD_LOG_ENABLED(WARNING);
188}
189
190TEST(logging, WOULD_LOG_INFO_disabled) {
191 CHECK_WOULD_LOG_DISABLED(INFO);
192}
193
194TEST(logging, WOULD_LOG_INFO_enabled) {
195 CHECK_WOULD_LOG_ENABLED(INFO);
196}
197
198TEST(logging, WOULD_LOG_DEBUG_disabled) {
199 CHECK_WOULD_LOG_DISABLED(DEBUG);
200}
201
202TEST(logging, WOULD_LOG_DEBUG_enabled) {
203 CHECK_WOULD_LOG_ENABLED(DEBUG);
204}
205
206TEST(logging, WOULD_LOG_VERBOSE_disabled) {
207 CHECK_WOULD_LOG_DISABLED(VERBOSE);
208}
209
210TEST(logging, WOULD_LOG_VERBOSE_enabled) {
211 CHECK_WOULD_LOG_ENABLED(VERBOSE);
212}
213
214#undef CHECK_WOULD_LOG_DISABLED
215#undef CHECK_WOULD_LOG_ENABLED
216
217
218#define CHECK_WOULD_LOG_SEVERITY_DISABLED(severity) \
219 static_assert(android::base::severity < android::base::FATAL, "Bad input"); \
220 for (size_t i = static_cast<size_t>(android::base::severity) + 1; \
221 i <= static_cast<size_t>(android::base::FATAL); \
222 ++i) { \
223 android::base::ScopedLogSeverity sls2(static_cast<android::base::LogSeverity>(i)); \
224 EXPECT_FALSE(WOULD_LOG_SEVERITY(::android::base::severity)) << i; \
225 }
226
227#define CHECK_WOULD_LOG_SEVERITY_ENABLED(severity) \
228 for (size_t i = static_cast<size_t>(android::base::VERBOSE); \
229 i <= static_cast<size_t>(android::base::severity); \
230 ++i) { \
231 android::base::ScopedLogSeverity sls2(static_cast<android::base::LogSeverity>(i)); \
232 EXPECT_TRUE(WOULD_LOG_SEVERITY(::android::base::severity)) << i; \
233 }
234
235TEST(logging,WOULD_LOG_SEVERITY_FATAL) {
236 CHECK_WOULD_LOG_SEVERITY_ENABLED(FATAL);
237}
238
239TEST(logging,WOULD_LOG_SEVERITY_FATAL_WITHOUT_ABORT_disabled) {
240 CHECK_WOULD_LOG_SEVERITY_DISABLED(FATAL_WITHOUT_ABORT);
241}
242
243TEST(logging, WOULD_LOG_SEVERITY_FATAL_WITHOUT_ABORT_enabled) {
244 CHECK_WOULD_LOG_SEVERITY_ENABLED(FATAL_WITHOUT_ABORT);
245}
246
247TEST(logging, WOULD_LOG_SEVERITY_ERROR_disabled) {
248 CHECK_WOULD_LOG_SEVERITY_DISABLED(ERROR);
249}
250
251TEST(logging, WOULD_LOG_SEVERITY_ERROR_enabled) {
252 CHECK_WOULD_LOG_SEVERITY_ENABLED(ERROR);
253}
254
255TEST(logging, WOULD_LOG_SEVERITY_WARNING_disabled) {
256 CHECK_WOULD_LOG_SEVERITY_DISABLED(WARNING);
257}
258
259TEST(logging, WOULD_LOG_SEVERITY_WARNING_enabled) {
260 CHECK_WOULD_LOG_SEVERITY_ENABLED(WARNING);
261}
262
263TEST(logging, WOULD_LOG_SEVERITY_INFO_disabled) {
264 CHECK_WOULD_LOG_SEVERITY_DISABLED(INFO);
265}
266
267TEST(logging, WOULD_LOG_SEVERITY_INFO_enabled) {
268 CHECK_WOULD_LOG_SEVERITY_ENABLED(INFO);
269}
270
271TEST(logging, WOULD_LOG_SEVERITY_DEBUG_disabled) {
272 CHECK_WOULD_LOG_SEVERITY_DISABLED(DEBUG);
273}
274
275TEST(logging, WOULD_LOG_SEVERITY_DEBUG_enabled) {
276 CHECK_WOULD_LOG_SEVERITY_ENABLED(DEBUG);
277}
278
279TEST(logging, WOULD_LOG_SEVERITYVERBOSE_disabled) {
280 CHECK_WOULD_LOG_SEVERITY_DISABLED(VERBOSE);
281}
282
283TEST(logging, WOULD_LOG_SEVERITY_VERBOSE_enabled) {
284 CHECK_WOULD_LOG_SEVERITY_ENABLED(VERBOSE);
285}
286
287#undef CHECK_WOULD_LOG_SEVERITY_DISABLED
288#undef CHECK_WOULD_LOG_SEVERITY_ENABLED
289
Elliott Hughes13d78e42016-09-07 16:22:40 -0700290static std::string make_log_pattern(android::base::LogSeverity severity,
291 const char* message) {
Andreas Gampe550829d2016-09-07 10:10:50 -0700292 static const char log_characters[] = "VDIWEFF";
293 static_assert(arraysize(log_characters) - 1 == android::base::FATAL + 1,
294 "Mismatch in size of log_characters and values in LogSeverity");
Dan Albert58310b42015-03-13 23:06:01 -0700295 char log_char = log_characters[severity];
Spencer Lowbdab59a2015-08-11 16:00:13 -0700296 std::string holder(__FILE__);
Dan Albert58310b42015-03-13 23:06:01 -0700297 return android::base::StringPrintf(
Elliott Hughes13d78e42016-09-07 16:22:40 -0700298 "%c \\d+-\\d+ \\d+:\\d+:\\d+ \\s*\\d+ \\s*\\d+ %s:\\d+] %s",
Spencer Lowbdab59a2015-08-11 16:00:13 -0700299 log_char, basename(&holder[0]), message);
Dan Albert58310b42015-03-13 23:06:01 -0700300}
301
Elliott Hughes13d78e42016-09-07 16:22:40 -0700302static void CheckMessage(const CapturedStderr& cap,
303 android::base::LogSeverity severity, const char* expected) {
304 std::string output;
305 ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_SET));
306 android::base::ReadFdToString(cap.fd(), &output);
Dan Albert58310b42015-03-13 23:06:01 -0700307
Dan Albert5c190402015-04-29 11:32:23 -0700308 // We can't usefully check the output of any of these on Windows because we
309 // don't have std::regex, but we can at least make sure we printed at least as
310 // many characters are in the log message.
Elliott Hughes13d78e42016-09-07 16:22:40 -0700311 ASSERT_GT(output.length(), strlen(expected));
312 ASSERT_NE(nullptr, strstr(output.c_str(), expected)) << output;
Dan Albert58310b42015-03-13 23:06:01 -0700313
Dan Albert5c190402015-04-29 11:32:23 -0700314#if !defined(_WIN32)
Elliott Hughes13d78e42016-09-07 16:22:40 -0700315 std::regex message_regex(make_log_pattern(severity, expected));
316 ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
Dan Albert5c190402015-04-29 11:32:23 -0700317#endif
Dan Albert58310b42015-03-13 23:06:01 -0700318}
319
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700320#define CHECK_LOG_STREAM_DISABLED(severity) \
321 android::base::ScopedLogSeverity sls1(android::base::FATAL); \
322 CapturedStderr cap1; \
323 LOG_STREAM(severity) << "foo bar"; \
324 ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR));
325
326#define CHECK_LOG_STREAM_ENABLED(severity) \
Andreas Gampe436f5a02016-09-22 10:15:01 -0700327 android::base::ScopedLogSeverity sls2(android::base::severity); \
328 CapturedStderr cap2; \
329 LOG_STREAM(severity) << "foobar"; \
330 CheckMessage(cap2, android::base::severity, "foobar"); \
331
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700332TEST(logging, LOG_STREAM_FATAL_WITHOUT_ABORT_disabled) {
333 CHECK_LOG_STREAM_DISABLED(FATAL_WITHOUT_ABORT);
Andreas Gampe436f5a02016-09-22 10:15:01 -0700334}
335
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700336TEST(logging, LOG_STREAM_FATAL_WITHOUT_ABORT_enabled) {
337 CHECK_LOG_STREAM_ENABLED(FATAL_WITHOUT_ABORT);
Andreas Gampe436f5a02016-09-22 10:15:01 -0700338}
339
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700340TEST(logging, LOG_STREAM_ERROR_disabled) {
341 CHECK_LOG_STREAM_DISABLED(ERROR);
Andreas Gampe436f5a02016-09-22 10:15:01 -0700342}
343
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700344TEST(logging, LOG_STREAM_ERROR_enabled) {
345 CHECK_LOG_STREAM_ENABLED(ERROR);
Andreas Gampe436f5a02016-09-22 10:15:01 -0700346}
347
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700348TEST(logging, LOG_STREAM_WARNING_disabled) {
349 CHECK_LOG_STREAM_DISABLED(WARNING);
Andreas Gampe436f5a02016-09-22 10:15:01 -0700350}
351
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700352TEST(logging, LOG_STREAM_WARNING_enabled) {
353 CHECK_LOG_STREAM_ENABLED(WARNING);
Andreas Gampe436f5a02016-09-22 10:15:01 -0700354}
355
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700356TEST(logging, LOG_STREAM_INFO_disabled) {
357 CHECK_LOG_STREAM_DISABLED(INFO);
358}
Andreas Gampe436f5a02016-09-22 10:15:01 -0700359
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700360TEST(logging, LOG_STREAM_INFO_enabled) {
361 CHECK_LOG_STREAM_ENABLED(INFO);
362}
363
364TEST(logging, LOG_STREAM_DEBUG_disabled) {
365 CHECK_LOG_STREAM_DISABLED(DEBUG);
366}
367
368TEST(logging, LOG_STREAM_DEBUG_enabled) {
369 CHECK_LOG_STREAM_ENABLED(DEBUG);
370}
371
372TEST(logging, LOG_STREAM_VERBOSE_disabled) {
373 CHECK_LOG_STREAM_DISABLED(VERBOSE);
374}
375
376TEST(logging, LOG_STREAM_VERBOSE_enabled) {
377 CHECK_LOG_STREAM_ENABLED(VERBOSE);
378}
379
380#undef CHECK_LOG_STREAM_DISABLED
381#undef CHECK_LOG_STREAM_ENABLED
382
383#define CHECK_LOG_STREAM_S_DISABLED(severity) \
384 android::base::ScopedLogSeverity sls1(android::base::FATAL); \
385 CapturedStderr cap1; \
386 LOG_STREAM_S(android::base::severity) << "foobar"; \
387 ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR));
388
389#define CHECK_LOG_STREAM_S_ENABLED(severity) \
Andreas Gampe436f5a02016-09-22 10:15:01 -0700390 android::base::ScopedLogSeverity sls2(android::base::severity); \
391 CapturedStderr cap2; \
392 LOG_STREAM_S(android::base::severity) << "foobar"; \
393 CheckMessage(cap2, android::base::severity, "foobar"); \
394
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700395TEST(logging, LOG_STREAM_S_FATAL_WITHOUT_ABORT_disabled) {
396 CHECK_LOG_STREAM_S_DISABLED(FATAL_WITHOUT_ABORT);
Andreas Gampe436f5a02016-09-22 10:15:01 -0700397}
398
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700399TEST(logging, LOG_STREAM_S_FATAL_WITHOUT_ABORT_enabled) {
400 CHECK_LOG_STREAM_S_ENABLED(FATAL_WITHOUT_ABORT);
Andreas Gampe436f5a02016-09-22 10:15:01 -0700401}
402
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700403TEST(logging, LOG_STREAM_S_ERROR_disabled) {
404 CHECK_LOG_STREAM_S_DISABLED(ERROR);
Andreas Gampe436f5a02016-09-22 10:15:01 -0700405}
406
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700407TEST(logging, LOG_STREAM_S_ERROR_enabled) {
408 CHECK_LOG_STREAM_S_ENABLED(ERROR);
Andreas Gampe436f5a02016-09-22 10:15:01 -0700409}
410
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700411TEST(logging, LOG_STREAM_S_WARNING_disabled) {
412 CHECK_LOG_STREAM_S_DISABLED(WARNING);
Andreas Gampe436f5a02016-09-22 10:15:01 -0700413}
414
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700415TEST(logging, LOG_STREAM_S_WARNING_enabled) {
416 CHECK_LOG_STREAM_S_ENABLED(WARNING);
Andreas Gampe436f5a02016-09-22 10:15:01 -0700417}
418
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700419TEST(logging, LOG_STREAM_S_INFO_disabled) {
420 CHECK_LOG_STREAM_S_DISABLED(INFO);
421}
422
423TEST(logging, LOG_STREAM_S_INFO_enabled) {
424 CHECK_LOG_STREAM_S_ENABLED(INFO);
425}
426
427TEST(logging, LOG_STREAM_S_DEBUG_disabled) {
428 CHECK_LOG_STREAM_S_DISABLED(DEBUG);
429}
430
431TEST(logging, LOG_STREAM_S_DEBUG_enabled) {
432 CHECK_LOG_STREAM_S_ENABLED(DEBUG);
433}
434
435TEST(logging, LOG_STREAM_S_VERBOSE_disabled) {
436 CHECK_LOG_STREAM_S_DISABLED(VERBOSE);
437}
438
439TEST(logging, LOG_STREAM_S_VERBOSE_enabled) {
440 CHECK_LOG_STREAM_S_ENABLED(VERBOSE);
441}
442
443#undef CHECK_LOG_STREAM_S_DISABLED
444#undef CHECK_LOG_STREAM_S_ENABLED
Andreas Gampe436f5a02016-09-22 10:15:01 -0700445
446
447#define CHECK_LOG_DISABLED(severity) \
448 android::base::ScopedLogSeverity sls1(android::base::FATAL); \
449 CapturedStderr cap1; \
450 LOG(severity) << "foo bar"; \
451 ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \
452
453#define CHECK_LOG_ENABLED(severity) \
454 android::base::ScopedLogSeverity sls2(android::base::severity); \
455 CapturedStderr cap2; \
456 LOG(severity) << "foobar"; \
457 CheckMessage(cap2, android::base::severity, "foobar"); \
458
Elliott Hughes13d78e42016-09-07 16:22:40 -0700459TEST(logging, LOG_FATAL) {
460 ASSERT_DEATH({SuppressAbortUI(); LOG(FATAL) << "foobar";}, "foobar");
461}
Dan Albert58310b42015-03-13 23:06:01 -0700462
Andreas Gampe550829d2016-09-07 10:10:50 -0700463TEST(logging, LOG_FATAL_WITHOUT_ABORT_disabled) {
464 CHECK_LOG_DISABLED(FATAL_WITHOUT_ABORT);
465}
466
467TEST(logging, LOG_FATAL_WITHOUT_ABORT_enabled) {
468 CHECK_LOG_ENABLED(FATAL_WITHOUT_ABORT);
469}
470
Elliott Hughes13d78e42016-09-07 16:22:40 -0700471TEST(logging, LOG_ERROR_disabled) {
472 CHECK_LOG_DISABLED(ERROR);
473}
Dan Albert58310b42015-03-13 23:06:01 -0700474
Elliott Hughes13d78e42016-09-07 16:22:40 -0700475TEST(logging, LOG_ERROR_enabled) {
476 CHECK_LOG_ENABLED(ERROR);
477}
478
479TEST(logging, LOG_WARNING_disabled) {
480 CHECK_LOG_DISABLED(WARNING);
481}
482
483TEST(logging, LOG_WARNING_enabled) {
484 CHECK_LOG_ENABLED(WARNING);
485}
486
487TEST(logging, LOG_INFO_disabled) {
488 CHECK_LOG_DISABLED(INFO);
489}
490
491TEST(logging, LOG_INFO_enabled) {
492 CHECK_LOG_ENABLED(INFO);
493}
494
495TEST(logging, LOG_DEBUG_disabled) {
496 CHECK_LOG_DISABLED(DEBUG);
497}
498
499TEST(logging, LOG_DEBUG_enabled) {
500 CHECK_LOG_ENABLED(DEBUG);
501}
502
503TEST(logging, LOG_VERBOSE_disabled) {
504 CHECK_LOG_DISABLED(VERBOSE);
505}
506
507TEST(logging, LOG_VERBOSE_enabled) {
508 CHECK_LOG_ENABLED(VERBOSE);
509}
510
Andreas Gampe436f5a02016-09-22 10:15:01 -0700511#undef CHECK_LOG_DISABLED
512#undef CHECK_LOG_ENABLED
513
514#define CHECK_LOG_S_DISABLED(severity) \
515 android::base::ScopedLogSeverity sls1(android::base::FATAL); \
516 CapturedStderr cap1; \
517 LOG_S(::android::base::severity) << "foo bar"; \
518 ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \
519
520#define CHECK_LOG_S_ENABLED(severity) \
521 android::base::ScopedLogSeverity sls2(android::base::severity); \
522 CapturedStderr cap2; \
523 LOG_S(::android::base::severity) << "foobar"; \
524 CheckMessage(cap2, android::base::severity, "foobar"); \
525
526TEST(logging, LOG_S_FATAL) {
527 ASSERT_DEATH({SuppressAbortUI(); LOG_S(::android::base::FATAL) << "foobar";}, "foobar");
528}
529
530TEST(logging, LOG_S_FATAL_WITHOUT_ABORT_disabled) {
531 CHECK_LOG_S_DISABLED(FATAL_WITHOUT_ABORT);
532}
533
534TEST(logging, LOG_S_FATAL_WITHOUT_ABORT_enabled) {
535 CHECK_LOG_S_ENABLED(FATAL_WITHOUT_ABORT);
536}
537
538TEST(logging, LOG_S_ERROR_disabled) {
539 CHECK_LOG_S_DISABLED(ERROR);
540}
541
542TEST(logging, LOG_S_ERROR_enabled) {
543 CHECK_LOG_S_ENABLED(ERROR);
544}
545
546TEST(logging, LOG_S_WARNING_disabled) {
547 CHECK_LOG_S_DISABLED(WARNING);
548}
549
550TEST(logging, LOG_S_WARNING_enabled) {
551 CHECK_LOG_S_ENABLED(WARNING);
552}
553
554TEST(logging, LOG_S_INFO_disabled) {
555 CHECK_LOG_S_DISABLED(INFO);
556}
557
558TEST(logging, LOG_S_INFO_enabled) {
559 CHECK_LOG_S_ENABLED(INFO);
560}
561
562TEST(logging, LOG_S_DEBUG_disabled) {
563 CHECK_LOG_S_DISABLED(DEBUG);
564}
565
566TEST(logging, LOG_S_DEBUG_enabled) {
567 CHECK_LOG_S_ENABLED(DEBUG);
568}
569
570TEST(logging, LOG_S_VERBOSE_disabled) {
571 CHECK_LOG_S_DISABLED(VERBOSE);
572}
573
574TEST(logging, LOG_S_VERBOSE_enabled) {
575 CHECK_LOG_S_ENABLED(VERBOSE);
576}
577
578#undef CHECK_LOG_S_DISABLED
579#undef CHECK_LOG_S_ENABLED
580
581TEST(logging, LOG_S_complex_param) {
582#define CHECK_LOG_S_COMBINATION(use_scoped_log_severity_info, use_logging_severity_info) \
583 { \
584 android::base::ScopedLogSeverity sls( \
585 (use_scoped_log_severity_info) ? ::android::base::INFO : ::android::base::WARNING); \
586 CapturedStderr cap; \
587 LOG_S((use_logging_severity_info) ? ::android::base::INFO : ::android::base::WARNING) \
588 << "foobar"; \
589 if ((use_scoped_log_severity_info) || !(use_logging_severity_info)) { \
590 CheckMessage(cap, \
591 (use_logging_severity_info) ? ::android::base::INFO : ::android::base::WARNING, \
592 "foobar"); \
593 } else { \
594 ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_CUR)); \
595 } \
596 }
597
598 CHECK_LOG_S_COMBINATION(false,false);
599 CHECK_LOG_S_COMBINATION(false,true);
600 CHECK_LOG_S_COMBINATION(true,false);
601 CHECK_LOG_S_COMBINATION(true,true);
602
603#undef CHECK_LOG_S_COMBINATION
604}
605
606
Elliott Hughes13d78e42016-09-07 16:22:40 -0700607TEST(logging, LOG_does_not_clobber_errno) {
608 CapturedStderr cap;
609 errno = 12345;
610 LOG(INFO) << (errno = 67890);
611 EXPECT_EQ(12345, errno) << "errno was not restored";
612
613 CheckMessage(cap, android::base::INFO, "67890");
614}
615
Andreas Gampe436f5a02016-09-22 10:15:01 -0700616TEST(logging, LOG_S_does_not_clobber_errno) {
617 CapturedStderr cap;
618 errno = 12345;
619 LOG_S(::android::base::INFO) << (errno = 67890);
620 EXPECT_EQ(12345, errno) << "errno was not restored";
621
622 CheckMessage(cap, android::base::INFO, "67890");
623}
624
Elliott Hughes13d78e42016-09-07 16:22:40 -0700625TEST(logging, PLOG_does_not_clobber_errno) {
626 CapturedStderr cap;
627 errno = 12345;
628 PLOG(INFO) << (errno = 67890);
629 EXPECT_EQ(12345, errno) << "errno was not restored";
630
631 CheckMessage(cap, android::base::INFO, "67890");
632}
633
Andreas Gampe436f5a02016-09-22 10:15:01 -0700634TEST(logging, PLOG_S_does_not_clobber_errno) {
635 CapturedStderr cap;
636 errno = 12345;
637 PLOG_S(::android::base::INFO) << (errno = 67890);
638 EXPECT_EQ(12345, errno) << "errno was not restored";
639
640 CheckMessage(cap, android::base::INFO, "67890");
641}
642
643
Elliott Hughes13d78e42016-09-07 16:22:40 -0700644TEST(logging, LOG_does_not_have_dangling_if) {
645 CapturedStderr cap; // So the logging below has no side-effects.
646
647 // Do the test two ways: once where we hypothesize that LOG()'s if
648 // will evaluate to true (when severity is high enough) and once when we
649 // expect it to evaluate to false (when severity is not high enough).
650 bool flag = false;
651 if (true)
652 LOG(INFO) << "foobar";
653 else
654 flag = true;
655
656 EXPECT_FALSE(flag) << "LOG macro probably has a dangling if with no else";
657
658 flag = false;
659 if (true)
660 LOG(VERBOSE) << "foobar";
661 else
662 flag = true;
663
664 EXPECT_FALSE(flag) << "LOG macro probably has a dangling if with no else";
Elliott Hughes13d78e42016-09-07 16:22:40 -0700665
Andreas Gampe436f5a02016-09-22 10:15:01 -0700666 flag = false;
667 if (true)
668 LOG_S(::android::base::INFO) << "foobar";
669 else
670 flag = true;
671
672 EXPECT_FALSE(flag) << "LOG_S macro probably has a dangling if with no else";
673
674 flag = false;
675 if (true)
676 LOG_S(::android::base::VERBOSE) << "foobar";
677 else
678 flag = true;
679
680 EXPECT_FALSE(flag) << "LOG_S macro probably has a dangling if with no else";
681}
Elliott Hughes13d78e42016-09-07 16:22:40 -0700682
683#define CHECK_PLOG_DISABLED(severity) \
684 android::base::ScopedLogSeverity sls1(android::base::FATAL); \
685 CapturedStderr cap1; \
686 PLOG(severity) << "foo bar"; \
687 ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \
688
689#define CHECK_PLOG_ENABLED(severity) \
690 android::base::ScopedLogSeverity sls2(android::base::severity); \
691 CapturedStderr cap2; \
692 errno = ENOENT; \
693 PLOG(severity) << "foobar"; \
694 CheckMessage(cap2, android::base::severity, "foobar: No such file or directory"); \
695
Andreas Gampe550829d2016-09-07 10:10:50 -0700696TEST(logging, PLOG_FATAL) {
697 ASSERT_DEATH({SuppressAbortUI(); PLOG(FATAL) << "foobar";}, "foobar");
698}
699
700TEST(logging, PLOG_FATAL_WITHOUT_ABORT_disabled) {
701 CHECK_PLOG_DISABLED(FATAL_WITHOUT_ABORT);
702}
703
704TEST(logging, PLOG_FATAL_WITHOUT_ABORT_enabled) {
705 CHECK_PLOG_ENABLED(FATAL_WITHOUT_ABORT);
706}
707
Elliott Hughes13d78e42016-09-07 16:22:40 -0700708TEST(logging, PLOG_ERROR_disabled) {
709 CHECK_PLOG_DISABLED(ERROR);
710}
711
712TEST(logging, PLOG_ERROR_enabled) {
713 CHECK_PLOG_ENABLED(ERROR);
714}
715
716TEST(logging, PLOG_WARNING_disabled) {
717 CHECK_PLOG_DISABLED(WARNING);
718}
719
720TEST(logging, PLOG_WARNING_enabled) {
721 CHECK_PLOG_ENABLED(WARNING);
722}
723
724TEST(logging, PLOG_INFO_disabled) {
725 CHECK_PLOG_DISABLED(INFO);
726}
727
728TEST(logging, PLOG_INFO_enabled) {
729 CHECK_PLOG_ENABLED(INFO);
730}
731
732TEST(logging, PLOG_DEBUG_disabled) {
733 CHECK_PLOG_DISABLED(DEBUG);
734}
735
736TEST(logging, PLOG_DEBUG_enabled) {
737 CHECK_PLOG_ENABLED(DEBUG);
738}
739
740TEST(logging, PLOG_VERBOSE_disabled) {
741 CHECK_PLOG_DISABLED(VERBOSE);
742}
743
744TEST(logging, PLOG_VERBOSE_enabled) {
745 CHECK_PLOG_ENABLED(VERBOSE);
Dan Albert58310b42015-03-13 23:06:01 -0700746}
747
Andreas Gampe436f5a02016-09-22 10:15:01 -0700748#undef CHECK_PLOG_DISABLED
749#undef CHECK_PLOG_ENABLED
750
751
752#define CHECK_PLOG_S_DISABLED(severity) \
753 android::base::ScopedLogSeverity sls1(android::base::FATAL); \
754 CapturedStderr cap1; \
755 PLOG_S(android::base::severity) << "foo bar"; \
756 ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \
757
758#define CHECK_PLOG_S_ENABLED(severity) \
759 android::base::ScopedLogSeverity sls2(android::base::severity); \
760 CapturedStderr cap2; \
761 errno = ENOENT; \
762 PLOG_S(android::base::severity) << "foobar"; \
763 CheckMessage(cap2, android::base::severity, "foobar: No such file or directory"); \
764
765TEST(logging, PLOG_S_FATAL) {
766 ASSERT_DEATH({SuppressAbortUI(); PLOG_S(::android::base::FATAL) << "foobar";}, "foobar");
767}
768
769TEST(logging, PLOG_S_FATAL_WITHOUT_ABORT_disabled) {
770 CHECK_PLOG_S_DISABLED(FATAL_WITHOUT_ABORT);
771}
772
773TEST(logging, PLOG_S_FATAL_WITHOUT_ABORT_enabled) {
774 CHECK_PLOG_S_ENABLED(FATAL_WITHOUT_ABORT);
775}
776
777TEST(logging, PLOG_S_ERROR_disabled) {
778 CHECK_PLOG_S_DISABLED(ERROR);
779}
780
781TEST(logging, PLOG_S_ERROR_enabled) {
782 CHECK_PLOG_S_ENABLED(ERROR);
783}
784
785TEST(logging, PLOG_S_WARNING_disabled) {
786 CHECK_PLOG_S_DISABLED(WARNING);
787}
788
789TEST(logging, PLOG_S_WARNING_enabled) {
790 CHECK_PLOG_S_ENABLED(WARNING);
791}
792
793TEST(logging, PLOG_S_INFO_disabled) {
794 CHECK_PLOG_S_DISABLED(INFO);
795}
796
797TEST(logging, PLOG_S_INFO_enabled) {
798 CHECK_PLOG_S_ENABLED(INFO);
799}
800
801TEST(logging, PLOG_S_DEBUG_disabled) {
802 CHECK_PLOG_S_DISABLED(DEBUG);
803}
804
805TEST(logging, PLOG_S_DEBUG_enabled) {
806 CHECK_PLOG_S_ENABLED(DEBUG);
807}
808
809TEST(logging, PLOG_S_VERBOSE_disabled) {
810 CHECK_PLOG_S_DISABLED(VERBOSE);
811}
812
813TEST(logging, PLOG_S_VERBOSE_enabled) {
814 CHECK_PLOG_S_ENABLED(VERBOSE);
815}
816
817#undef CHECK_PLOG_S_DISABLED
818#undef CHECK_PLOG_S_ENABLED
819
820
Dan Albertb547c852015-03-27 11:20:14 -0700821TEST(logging, UNIMPLEMENTED) {
Elliott Hughes13d78e42016-09-07 16:22:40 -0700822 std::string expected = android::base::StringPrintf("%s unimplemented ", __PRETTY_FUNCTION__);
Dan Albert58310b42015-03-13 23:06:01 -0700823
Elliott Hughes13d78e42016-09-07 16:22:40 -0700824 CapturedStderr cap;
825 errno = ENOENT;
826 UNIMPLEMENTED(ERROR);
827 CheckMessage(cap, android::base::ERROR, expected.c_str());
Dan Albert58310b42015-03-13 23:06:01 -0700828}
Andreas Gampe2691e332016-09-08 11:03:58 -0700829
830static void NoopAborter(const char* msg ATTRIBUTE_UNUSED) {
831 LOG(ERROR) << "called noop";
832}
833
834TEST(logging, LOG_FATAL_NOOP_ABORTER) {
835 {
836 android::base::SetAborter(NoopAborter);
837
838 android::base::ScopedLogSeverity sls(android::base::ERROR);
839 CapturedStderr cap;
840 LOG(FATAL) << "foobar";
841 CheckMessage(cap, android::base::FATAL, "foobar");
842 CheckMessage(cap, android::base::ERROR, "called noop");
843
844 android::base::SetAborter(android::base::DefaultAborter);
845 }
846
847 ASSERT_DEATH({SuppressAbortUI(); LOG(FATAL) << "foobar";}, "foobar");
848}