blob: a1ed05b823fd0e3a2313a6f09723695d2ee560f6 [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 Gampe436f5a02016-09-22 10:15:01 -0700320#define CHECK_LOG_STREAM(severity) \
321 android::base::ScopedLogSeverity sls2(android::base::severity); \
322 CapturedStderr cap2; \
323 LOG_STREAM(severity) << "foobar"; \
324 CheckMessage(cap2, android::base::severity, "foobar"); \
325
326TEST(logging, LOG_STREAM_FATAL_WITHOUT_ABORT) {
327 CHECK_LOG_STREAM(FATAL_WITHOUT_ABORT);
328}
329
330TEST(logging, LOG_STREAM_ERROR) {
331 CHECK_LOG_STREAM(ERROR);
332}
333
334TEST(logging, LOG_STREAM_WARNING) {
335 CHECK_LOG_STREAM(WARNING);
336}
337
338TEST(logging, LOG_STREAM_INFO) {
339 CHECK_LOG_STREAM(INFO);
340}
341
342TEST(logging, LOG_STREAM_DEBUG) {
343 CHECK_LOG_STREAM(DEBUG);
344}
345
346TEST(logging, LOG_STREAM_VERBOSE) {
347 CHECK_LOG_STREAM(VERBOSE);
348}
349
350#undef CHECK_LOG_STREAM
351
352#define CHECK_LOG_STREAM_S(severity) \
353 android::base::ScopedLogSeverity sls2(android::base::severity); \
354 CapturedStderr cap2; \
355 LOG_STREAM_S(android::base::severity) << "foobar"; \
356 CheckMessage(cap2, android::base::severity, "foobar"); \
357
358TEST(logging, LOG_STREAM_S_FATAL_WITHOUT_ABORT) {
359 CHECK_LOG_STREAM_S(FATAL_WITHOUT_ABORT);
360}
361
362TEST(logging, LOG_STREAM_S_ERROR) {
363 CHECK_LOG_STREAM_S(ERROR);
364}
365
366TEST(logging, LOG_STREAM_S_WARNING) {
367 CHECK_LOG_STREAM_S(WARNING);
368}
369
370TEST(logging, LOG_STREAM_S_INFO) {
371 CHECK_LOG_STREAM_S(INFO);
372}
373
374TEST(logging, LOG_STREAM_S_DEBUG) {
375 CHECK_LOG_STREAM_S(DEBUG);
376}
377
378TEST(logging, LOG_STREAM_S_VERBOSE) {
379 CHECK_LOG_STREAM_S(VERBOSE);
380}
381
382#undef CHECK_LOG_STREAM_S
383
384
385#define CHECK_LOG_DISABLED(severity) \
386 android::base::ScopedLogSeverity sls1(android::base::FATAL); \
387 CapturedStderr cap1; \
388 LOG(severity) << "foo bar"; \
389 ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \
390
391#define CHECK_LOG_ENABLED(severity) \
392 android::base::ScopedLogSeverity sls2(android::base::severity); \
393 CapturedStderr cap2; \
394 LOG(severity) << "foobar"; \
395 CheckMessage(cap2, android::base::severity, "foobar"); \
396
Elliott Hughes13d78e42016-09-07 16:22:40 -0700397TEST(logging, LOG_FATAL) {
398 ASSERT_DEATH({SuppressAbortUI(); LOG(FATAL) << "foobar";}, "foobar");
399}
Dan Albert58310b42015-03-13 23:06:01 -0700400
Andreas Gampe550829d2016-09-07 10:10:50 -0700401TEST(logging, LOG_FATAL_WITHOUT_ABORT_disabled) {
402 CHECK_LOG_DISABLED(FATAL_WITHOUT_ABORT);
403}
404
405TEST(logging, LOG_FATAL_WITHOUT_ABORT_enabled) {
406 CHECK_LOG_ENABLED(FATAL_WITHOUT_ABORT);
407}
408
Elliott Hughes13d78e42016-09-07 16:22:40 -0700409TEST(logging, LOG_ERROR_disabled) {
410 CHECK_LOG_DISABLED(ERROR);
411}
Dan Albert58310b42015-03-13 23:06:01 -0700412
Elliott Hughes13d78e42016-09-07 16:22:40 -0700413TEST(logging, LOG_ERROR_enabled) {
414 CHECK_LOG_ENABLED(ERROR);
415}
416
417TEST(logging, LOG_WARNING_disabled) {
418 CHECK_LOG_DISABLED(WARNING);
419}
420
421TEST(logging, LOG_WARNING_enabled) {
422 CHECK_LOG_ENABLED(WARNING);
423}
424
425TEST(logging, LOG_INFO_disabled) {
426 CHECK_LOG_DISABLED(INFO);
427}
428
429TEST(logging, LOG_INFO_enabled) {
430 CHECK_LOG_ENABLED(INFO);
431}
432
433TEST(logging, LOG_DEBUG_disabled) {
434 CHECK_LOG_DISABLED(DEBUG);
435}
436
437TEST(logging, LOG_DEBUG_enabled) {
438 CHECK_LOG_ENABLED(DEBUG);
439}
440
441TEST(logging, LOG_VERBOSE_disabled) {
442 CHECK_LOG_DISABLED(VERBOSE);
443}
444
445TEST(logging, LOG_VERBOSE_enabled) {
446 CHECK_LOG_ENABLED(VERBOSE);
447}
448
Andreas Gampe436f5a02016-09-22 10:15:01 -0700449#undef CHECK_LOG_DISABLED
450#undef CHECK_LOG_ENABLED
451
452#define CHECK_LOG_S_DISABLED(severity) \
453 android::base::ScopedLogSeverity sls1(android::base::FATAL); \
454 CapturedStderr cap1; \
455 LOG_S(::android::base::severity) << "foo bar"; \
456 ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \
457
458#define CHECK_LOG_S_ENABLED(severity) \
459 android::base::ScopedLogSeverity sls2(android::base::severity); \
460 CapturedStderr cap2; \
461 LOG_S(::android::base::severity) << "foobar"; \
462 CheckMessage(cap2, android::base::severity, "foobar"); \
463
464TEST(logging, LOG_S_FATAL) {
465 ASSERT_DEATH({SuppressAbortUI(); LOG_S(::android::base::FATAL) << "foobar";}, "foobar");
466}
467
468TEST(logging, LOG_S_FATAL_WITHOUT_ABORT_disabled) {
469 CHECK_LOG_S_DISABLED(FATAL_WITHOUT_ABORT);
470}
471
472TEST(logging, LOG_S_FATAL_WITHOUT_ABORT_enabled) {
473 CHECK_LOG_S_ENABLED(FATAL_WITHOUT_ABORT);
474}
475
476TEST(logging, LOG_S_ERROR_disabled) {
477 CHECK_LOG_S_DISABLED(ERROR);
478}
479
480TEST(logging, LOG_S_ERROR_enabled) {
481 CHECK_LOG_S_ENABLED(ERROR);
482}
483
484TEST(logging, LOG_S_WARNING_disabled) {
485 CHECK_LOG_S_DISABLED(WARNING);
486}
487
488TEST(logging, LOG_S_WARNING_enabled) {
489 CHECK_LOG_S_ENABLED(WARNING);
490}
491
492TEST(logging, LOG_S_INFO_disabled) {
493 CHECK_LOG_S_DISABLED(INFO);
494}
495
496TEST(logging, LOG_S_INFO_enabled) {
497 CHECK_LOG_S_ENABLED(INFO);
498}
499
500TEST(logging, LOG_S_DEBUG_disabled) {
501 CHECK_LOG_S_DISABLED(DEBUG);
502}
503
504TEST(logging, LOG_S_DEBUG_enabled) {
505 CHECK_LOG_S_ENABLED(DEBUG);
506}
507
508TEST(logging, LOG_S_VERBOSE_disabled) {
509 CHECK_LOG_S_DISABLED(VERBOSE);
510}
511
512TEST(logging, LOG_S_VERBOSE_enabled) {
513 CHECK_LOG_S_ENABLED(VERBOSE);
514}
515
516#undef CHECK_LOG_S_DISABLED
517#undef CHECK_LOG_S_ENABLED
518
519TEST(logging, LOG_S_complex_param) {
520#define CHECK_LOG_S_COMBINATION(use_scoped_log_severity_info, use_logging_severity_info) \
521 { \
522 android::base::ScopedLogSeverity sls( \
523 (use_scoped_log_severity_info) ? ::android::base::INFO : ::android::base::WARNING); \
524 CapturedStderr cap; \
525 LOG_S((use_logging_severity_info) ? ::android::base::INFO : ::android::base::WARNING) \
526 << "foobar"; \
527 if ((use_scoped_log_severity_info) || !(use_logging_severity_info)) { \
528 CheckMessage(cap, \
529 (use_logging_severity_info) ? ::android::base::INFO : ::android::base::WARNING, \
530 "foobar"); \
531 } else { \
532 ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_CUR)); \
533 } \
534 }
535
536 CHECK_LOG_S_COMBINATION(false,false);
537 CHECK_LOG_S_COMBINATION(false,true);
538 CHECK_LOG_S_COMBINATION(true,false);
539 CHECK_LOG_S_COMBINATION(true,true);
540
541#undef CHECK_LOG_S_COMBINATION
542}
543
544
Elliott Hughes13d78e42016-09-07 16:22:40 -0700545TEST(logging, LOG_does_not_clobber_errno) {
546 CapturedStderr cap;
547 errno = 12345;
548 LOG(INFO) << (errno = 67890);
549 EXPECT_EQ(12345, errno) << "errno was not restored";
550
551 CheckMessage(cap, android::base::INFO, "67890");
552}
553
Andreas Gampe436f5a02016-09-22 10:15:01 -0700554TEST(logging, LOG_S_does_not_clobber_errno) {
555 CapturedStderr cap;
556 errno = 12345;
557 LOG_S(::android::base::INFO) << (errno = 67890);
558 EXPECT_EQ(12345, errno) << "errno was not restored";
559
560 CheckMessage(cap, android::base::INFO, "67890");
561}
562
Elliott Hughes13d78e42016-09-07 16:22:40 -0700563TEST(logging, PLOG_does_not_clobber_errno) {
564 CapturedStderr cap;
565 errno = 12345;
566 PLOG(INFO) << (errno = 67890);
567 EXPECT_EQ(12345, errno) << "errno was not restored";
568
569 CheckMessage(cap, android::base::INFO, "67890");
570}
571
Andreas Gampe436f5a02016-09-22 10:15:01 -0700572TEST(logging, PLOG_S_does_not_clobber_errno) {
573 CapturedStderr cap;
574 errno = 12345;
575 PLOG_S(::android::base::INFO) << (errno = 67890);
576 EXPECT_EQ(12345, errno) << "errno was not restored";
577
578 CheckMessage(cap, android::base::INFO, "67890");
579}
580
581
Elliott Hughes13d78e42016-09-07 16:22:40 -0700582TEST(logging, LOG_does_not_have_dangling_if) {
583 CapturedStderr cap; // So the logging below has no side-effects.
584
585 // Do the test two ways: once where we hypothesize that LOG()'s if
586 // will evaluate to true (when severity is high enough) and once when we
587 // expect it to evaluate to false (when severity is not high enough).
588 bool flag = false;
589 if (true)
590 LOG(INFO) << "foobar";
591 else
592 flag = true;
593
594 EXPECT_FALSE(flag) << "LOG macro probably has a dangling if with no else";
595
596 flag = false;
597 if (true)
598 LOG(VERBOSE) << "foobar";
599 else
600 flag = true;
601
602 EXPECT_FALSE(flag) << "LOG macro probably has a dangling if with no else";
Elliott Hughes13d78e42016-09-07 16:22:40 -0700603
Andreas Gampe436f5a02016-09-22 10:15:01 -0700604 flag = false;
605 if (true)
606 LOG_S(::android::base::INFO) << "foobar";
607 else
608 flag = true;
609
610 EXPECT_FALSE(flag) << "LOG_S macro probably has a dangling if with no else";
611
612 flag = false;
613 if (true)
614 LOG_S(::android::base::VERBOSE) << "foobar";
615 else
616 flag = true;
617
618 EXPECT_FALSE(flag) << "LOG_S macro probably has a dangling if with no else";
619}
Elliott Hughes13d78e42016-09-07 16:22:40 -0700620
621#define CHECK_PLOG_DISABLED(severity) \
622 android::base::ScopedLogSeverity sls1(android::base::FATAL); \
623 CapturedStderr cap1; \
624 PLOG(severity) << "foo bar"; \
625 ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \
626
627#define CHECK_PLOG_ENABLED(severity) \
628 android::base::ScopedLogSeverity sls2(android::base::severity); \
629 CapturedStderr cap2; \
630 errno = ENOENT; \
631 PLOG(severity) << "foobar"; \
632 CheckMessage(cap2, android::base::severity, "foobar: No such file or directory"); \
633
Andreas Gampe550829d2016-09-07 10:10:50 -0700634TEST(logging, PLOG_FATAL) {
635 ASSERT_DEATH({SuppressAbortUI(); PLOG(FATAL) << "foobar";}, "foobar");
636}
637
638TEST(logging, PLOG_FATAL_WITHOUT_ABORT_disabled) {
639 CHECK_PLOG_DISABLED(FATAL_WITHOUT_ABORT);
640}
641
642TEST(logging, PLOG_FATAL_WITHOUT_ABORT_enabled) {
643 CHECK_PLOG_ENABLED(FATAL_WITHOUT_ABORT);
644}
645
Elliott Hughes13d78e42016-09-07 16:22:40 -0700646TEST(logging, PLOG_ERROR_disabled) {
647 CHECK_PLOG_DISABLED(ERROR);
648}
649
650TEST(logging, PLOG_ERROR_enabled) {
651 CHECK_PLOG_ENABLED(ERROR);
652}
653
654TEST(logging, PLOG_WARNING_disabled) {
655 CHECK_PLOG_DISABLED(WARNING);
656}
657
658TEST(logging, PLOG_WARNING_enabled) {
659 CHECK_PLOG_ENABLED(WARNING);
660}
661
662TEST(logging, PLOG_INFO_disabled) {
663 CHECK_PLOG_DISABLED(INFO);
664}
665
666TEST(logging, PLOG_INFO_enabled) {
667 CHECK_PLOG_ENABLED(INFO);
668}
669
670TEST(logging, PLOG_DEBUG_disabled) {
671 CHECK_PLOG_DISABLED(DEBUG);
672}
673
674TEST(logging, PLOG_DEBUG_enabled) {
675 CHECK_PLOG_ENABLED(DEBUG);
676}
677
678TEST(logging, PLOG_VERBOSE_disabled) {
679 CHECK_PLOG_DISABLED(VERBOSE);
680}
681
682TEST(logging, PLOG_VERBOSE_enabled) {
683 CHECK_PLOG_ENABLED(VERBOSE);
Dan Albert58310b42015-03-13 23:06:01 -0700684}
685
Andreas Gampe436f5a02016-09-22 10:15:01 -0700686#undef CHECK_PLOG_DISABLED
687#undef CHECK_PLOG_ENABLED
688
689
690#define CHECK_PLOG_S_DISABLED(severity) \
691 android::base::ScopedLogSeverity sls1(android::base::FATAL); \
692 CapturedStderr cap1; \
693 PLOG_S(android::base::severity) << "foo bar"; \
694 ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \
695
696#define CHECK_PLOG_S_ENABLED(severity) \
697 android::base::ScopedLogSeverity sls2(android::base::severity); \
698 CapturedStderr cap2; \
699 errno = ENOENT; \
700 PLOG_S(android::base::severity) << "foobar"; \
701 CheckMessage(cap2, android::base::severity, "foobar: No such file or directory"); \
702
703TEST(logging, PLOG_S_FATAL) {
704 ASSERT_DEATH({SuppressAbortUI(); PLOG_S(::android::base::FATAL) << "foobar";}, "foobar");
705}
706
707TEST(logging, PLOG_S_FATAL_WITHOUT_ABORT_disabled) {
708 CHECK_PLOG_S_DISABLED(FATAL_WITHOUT_ABORT);
709}
710
711TEST(logging, PLOG_S_FATAL_WITHOUT_ABORT_enabled) {
712 CHECK_PLOG_S_ENABLED(FATAL_WITHOUT_ABORT);
713}
714
715TEST(logging, PLOG_S_ERROR_disabled) {
716 CHECK_PLOG_S_DISABLED(ERROR);
717}
718
719TEST(logging, PLOG_S_ERROR_enabled) {
720 CHECK_PLOG_S_ENABLED(ERROR);
721}
722
723TEST(logging, PLOG_S_WARNING_disabled) {
724 CHECK_PLOG_S_DISABLED(WARNING);
725}
726
727TEST(logging, PLOG_S_WARNING_enabled) {
728 CHECK_PLOG_S_ENABLED(WARNING);
729}
730
731TEST(logging, PLOG_S_INFO_disabled) {
732 CHECK_PLOG_S_DISABLED(INFO);
733}
734
735TEST(logging, PLOG_S_INFO_enabled) {
736 CHECK_PLOG_S_ENABLED(INFO);
737}
738
739TEST(logging, PLOG_S_DEBUG_disabled) {
740 CHECK_PLOG_S_DISABLED(DEBUG);
741}
742
743TEST(logging, PLOG_S_DEBUG_enabled) {
744 CHECK_PLOG_S_ENABLED(DEBUG);
745}
746
747TEST(logging, PLOG_S_VERBOSE_disabled) {
748 CHECK_PLOG_S_DISABLED(VERBOSE);
749}
750
751TEST(logging, PLOG_S_VERBOSE_enabled) {
752 CHECK_PLOG_S_ENABLED(VERBOSE);
753}
754
755#undef CHECK_PLOG_S_DISABLED
756#undef CHECK_PLOG_S_ENABLED
757
758
Dan Albertb547c852015-03-27 11:20:14 -0700759TEST(logging, UNIMPLEMENTED) {
Elliott Hughes13d78e42016-09-07 16:22:40 -0700760 std::string expected = android::base::StringPrintf("%s unimplemented ", __PRETTY_FUNCTION__);
Dan Albert58310b42015-03-13 23:06:01 -0700761
Elliott Hughes13d78e42016-09-07 16:22:40 -0700762 CapturedStderr cap;
763 errno = ENOENT;
764 UNIMPLEMENTED(ERROR);
765 CheckMessage(cap, android::base::ERROR, expected.c_str());
Dan Albert58310b42015-03-13 23:06:01 -0700766}
Andreas Gampe2691e332016-09-08 11:03:58 -0700767
768static void NoopAborter(const char* msg ATTRIBUTE_UNUSED) {
769 LOG(ERROR) << "called noop";
770}
771
772TEST(logging, LOG_FATAL_NOOP_ABORTER) {
773 {
774 android::base::SetAborter(NoopAborter);
775
776 android::base::ScopedLogSeverity sls(android::base::ERROR);
777 CapturedStderr cap;
778 LOG(FATAL) << "foobar";
779 CheckMessage(cap, android::base::FATAL, "foobar");
780 CheckMessage(cap, android::base::ERROR, "called noop");
781
782 android::base::SetAborter(android::base::DefaultAborter);
783 }
784
785 ASSERT_DEATH({SuppressAbortUI(); LOG(FATAL) << "foobar";}, "foobar");
786}