blob: 75b4ea0453f819ef539c75f2c6b26937bd0bf0fa [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
Spencer Low765ae6b2015-09-17 19:36:10 -070040#if defined(_WIN32)
41static void ExitSignalAbortHandler(int) {
42 _exit(3);
43}
44#endif
45
46static void SuppressAbortUI() {
47#if defined(_WIN32)
48 // We really just want to call _set_abort_behavior(0, _CALL_REPORTFAULT) to
49 // suppress the Windows Error Reporting dialog box, but that API is not
50 // available in the OS-supplied C Runtime, msvcrt.dll, that we currently
51 // use (it is available in the Visual Studio C runtime).
52 //
53 // Instead, we setup a SIGABRT handler, which is called in abort() right
54 // before calling Windows Error Reporting. In the handler, we exit the
55 // process just like abort() does.
56 ASSERT_NE(SIG_ERR, signal(SIGABRT, ExitSignalAbortHandler));
57#endif
58}
59
Dan Albertb547c852015-03-27 11:20:14 -070060TEST(logging, CHECK) {
Spencer Low765ae6b2015-09-17 19:36:10 -070061 ASSERT_DEATH({SuppressAbortUI(); CHECK(false);}, "Check failed: false ");
Dan Albert58310b42015-03-13 23:06:01 -070062 CHECK(true);
63
Spencer Low765ae6b2015-09-17 19:36:10 -070064 ASSERT_DEATH({SuppressAbortUI(); CHECK_EQ(0, 1);}, "Check failed: 0 == 1 ");
Dan Albert58310b42015-03-13 23:06:01 -070065 CHECK_EQ(0, 0);
66
Spencer Low765ae6b2015-09-17 19:36:10 -070067 ASSERT_DEATH({SuppressAbortUI(); CHECK_STREQ("foo", "bar");},
68 R"(Check failed: "foo" == "bar")");
Dan Albert58310b42015-03-13 23:06:01 -070069 CHECK_STREQ("foo", "foo");
Spencer Low765ae6b2015-09-17 19:36:10 -070070
71 // Test whether CHECK() and CHECK_STREQ() have a dangling if with no else.
72 bool flag = false;
73 if (true)
74 CHECK(true);
75 else
76 flag = true;
77 EXPECT_FALSE(flag) << "CHECK macro probably has a dangling if with no else";
78
79 flag = false;
80 if (true)
81 CHECK_STREQ("foo", "foo");
82 else
83 flag = true;
84 EXPECT_FALSE(flag) << "CHECK_STREQ probably has a dangling if with no else";
Dan Albert58310b42015-03-13 23:06:01 -070085}
86
Andreas Gamped8f26e22016-09-13 10:44:46 -070087TEST(logging, DCHECK) {
88 if (android::base::kEnableDChecks) {
89 ASSERT_DEATH({SuppressAbortUI(); DCHECK(false);}, "DCheck failed: false ");
90 }
91 DCHECK(true);
92
93 if (android::base::kEnableDChecks) {
94 ASSERT_DEATH({SuppressAbortUI(); DCHECK_EQ(0, 1);}, "DCheck failed: 0 == 1 ");
95 }
96 DCHECK_EQ(0, 0);
97
98 if (android::base::kEnableDChecks) {
99 ASSERT_DEATH({SuppressAbortUI(); DCHECK_STREQ("foo", "bar");},
100 R"(DCheck failed: "foo" == "bar")");
101 }
102 DCHECK_STREQ("foo", "foo");
103
104 // No testing whether we have a dangling else, possibly. That's inherent to the if (constexpr)
105 // setup we intentionally chose to force type-checks of debug code even in release builds (so
106 // we don't get more bit-rot).
107}
108
Andreas Gampe436f5a02016-09-22 10:15:01 -0700109
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700110#define CHECK_WOULD_LOG_DISABLED(severity) \
111 static_assert(android::base::severity < android::base::FATAL, "Bad input"); \
112 for (size_t i = static_cast<size_t>(android::base::severity) + 1; \
113 i <= static_cast<size_t>(android::base::FATAL); \
114 ++i) { \
115 { \
116 android::base::ScopedLogSeverity sls2(static_cast<android::base::LogSeverity>(i)); \
117 EXPECT_FALSE(WOULD_LOG(severity)) << i; \
118 } \
119 { \
120 android::base::ScopedLogSeverity sls2(static_cast<android::base::LogSeverity>(i)); \
121 EXPECT_FALSE(WOULD_LOG(::android::base::severity)) << i; \
122 } \
123 } \
124
125#define CHECK_WOULD_LOG_ENABLED(severity) \
126 for (size_t i = static_cast<size_t>(android::base::VERBOSE); \
127 i <= static_cast<size_t>(android::base::severity); \
128 ++i) { \
129 { \
130 android::base::ScopedLogSeverity sls2(static_cast<android::base::LogSeverity>(i)); \
131 EXPECT_TRUE(WOULD_LOG(severity)) << i; \
132 } \
133 { \
134 android::base::ScopedLogSeverity sls2(static_cast<android::base::LogSeverity>(i)); \
135 EXPECT_TRUE(WOULD_LOG(::android::base::severity)) << i; \
136 } \
137 } \
Andreas Gampe436f5a02016-09-22 10:15:01 -0700138
139TEST(logging, WOULD_LOG_FATAL) {
140 CHECK_WOULD_LOG_ENABLED(FATAL);
141}
142
143TEST(logging, WOULD_LOG_FATAL_WITHOUT_ABORT_disabled) {
144 CHECK_WOULD_LOG_DISABLED(FATAL_WITHOUT_ABORT);
145}
146
147TEST(logging, WOULD_LOG_FATAL_WITHOUT_ABORT_enabled) {
148 CHECK_WOULD_LOG_ENABLED(FATAL_WITHOUT_ABORT);
149}
150
151TEST(logging, WOULD_LOG_ERROR_disabled) {
152 CHECK_WOULD_LOG_DISABLED(ERROR);
153}
154
155TEST(logging, WOULD_LOG_ERROR_enabled) {
156 CHECK_WOULD_LOG_ENABLED(ERROR);
157}
158
159TEST(logging, WOULD_LOG_WARNING_disabled) {
160 CHECK_WOULD_LOG_DISABLED(WARNING);
161}
162
163TEST(logging, WOULD_LOG_WARNING_enabled) {
164 CHECK_WOULD_LOG_ENABLED(WARNING);
165}
166
167TEST(logging, WOULD_LOG_INFO_disabled) {
168 CHECK_WOULD_LOG_DISABLED(INFO);
169}
170
171TEST(logging, WOULD_LOG_INFO_enabled) {
172 CHECK_WOULD_LOG_ENABLED(INFO);
173}
174
175TEST(logging, WOULD_LOG_DEBUG_disabled) {
176 CHECK_WOULD_LOG_DISABLED(DEBUG);
177}
178
179TEST(logging, WOULD_LOG_DEBUG_enabled) {
180 CHECK_WOULD_LOG_ENABLED(DEBUG);
181}
182
183TEST(logging, WOULD_LOG_VERBOSE_disabled) {
184 CHECK_WOULD_LOG_DISABLED(VERBOSE);
185}
186
187TEST(logging, WOULD_LOG_VERBOSE_enabled) {
188 CHECK_WOULD_LOG_ENABLED(VERBOSE);
189}
190
191#undef CHECK_WOULD_LOG_DISABLED
192#undef CHECK_WOULD_LOG_ENABLED
193
194
Dan Willemsen528f1442017-11-29 18:06:11 -0800195#if !defined(_WIN32)
Elliott Hughes13d78e42016-09-07 16:22:40 -0700196static std::string make_log_pattern(android::base::LogSeverity severity,
197 const char* message) {
Andreas Gampe550829d2016-09-07 10:10:50 -0700198 static const char log_characters[] = "VDIWEFF";
199 static_assert(arraysize(log_characters) - 1 == android::base::FATAL + 1,
200 "Mismatch in size of log_characters and values in LogSeverity");
Dan Albert58310b42015-03-13 23:06:01 -0700201 char log_char = log_characters[severity];
Spencer Lowbdab59a2015-08-11 16:00:13 -0700202 std::string holder(__FILE__);
Dan Albert58310b42015-03-13 23:06:01 -0700203 return android::base::StringPrintf(
Elliott Hughes13d78e42016-09-07 16:22:40 -0700204 "%c \\d+-\\d+ \\d+:\\d+:\\d+ \\s*\\d+ \\s*\\d+ %s:\\d+] %s",
Spencer Lowbdab59a2015-08-11 16:00:13 -0700205 log_char, basename(&holder[0]), message);
Dan Albert58310b42015-03-13 23:06:01 -0700206}
Dan Willemsen528f1442017-11-29 18:06:11 -0800207#endif
Dan Albert58310b42015-03-13 23:06:01 -0700208
Elliott Hughes1be0d142018-05-23 09:16:46 -0700209static void CheckMessage(CapturedStderr& cap, android::base::LogSeverity severity,
Andreas Gampe1923e762018-03-05 10:00:19 -0800210 const char* expected, const char* expected_tag = nullptr) {
Elliott Hughes1be0d142018-05-23 09:16:46 -0700211 std::string output = cap.str();
Dan Albert58310b42015-03-13 23:06:01 -0700212
Dan Albert5c190402015-04-29 11:32:23 -0700213 // We can't usefully check the output of any of these on Windows because we
214 // don't have std::regex, but we can at least make sure we printed at least as
215 // many characters are in the log message.
Elliott Hughes13d78e42016-09-07 16:22:40 -0700216 ASSERT_GT(output.length(), strlen(expected));
217 ASSERT_NE(nullptr, strstr(output.c_str(), expected)) << output;
Andreas Gampe1923e762018-03-05 10:00:19 -0800218 if (expected_tag != nullptr) {
219 ASSERT_NE(nullptr, strstr(output.c_str(), expected_tag)) << output;
220 }
Dan Albert58310b42015-03-13 23:06:01 -0700221
Dan Albert5c190402015-04-29 11:32:23 -0700222#if !defined(_WIN32)
Andreas Gampe1923e762018-03-05 10:00:19 -0800223 std::string regex_str;
224 if (expected_tag != nullptr) {
225 regex_str.append(expected_tag);
226 regex_str.append(" ");
227 }
228 regex_str.append(make_log_pattern(severity, expected));
229 std::regex message_regex(regex_str);
Elliott Hughes13d78e42016-09-07 16:22:40 -0700230 ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
Dan Albert5c190402015-04-29 11:32:23 -0700231#endif
Dan Albert58310b42015-03-13 23:06:01 -0700232}
233
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700234
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700235#define CHECK_LOG_STREAM_DISABLED(severity) \
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700236 { \
237 android::base::ScopedLogSeverity sls1(android::base::FATAL); \
238 CapturedStderr cap1; \
239 LOG_STREAM(severity) << "foo bar"; \
240 ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \
241 } \
242 { \
243 android::base::ScopedLogSeverity sls1(android::base::FATAL); \
244 CapturedStderr cap1; \
245 LOG_STREAM(::android::base::severity) << "foo bar"; \
246 ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \
247 } \
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700248
249#define CHECK_LOG_STREAM_ENABLED(severity) \
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700250 { \
251 android::base::ScopedLogSeverity sls2(android::base::severity); \
252 CapturedStderr cap2; \
253 LOG_STREAM(severity) << "foobar"; \
254 CheckMessage(cap2, android::base::severity, "foobar"); \
255 } \
256 { \
257 android::base::ScopedLogSeverity sls2(android::base::severity); \
258 CapturedStderr cap2; \
259 LOG_STREAM(::android::base::severity) << "foobar"; \
260 CheckMessage(cap2, android::base::severity, "foobar"); \
261 } \
Andreas Gampe436f5a02016-09-22 10:15:01 -0700262
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700263TEST(logging, LOG_STREAM_FATAL_WITHOUT_ABORT_disabled) {
264 CHECK_LOG_STREAM_DISABLED(FATAL_WITHOUT_ABORT);
Andreas Gampe436f5a02016-09-22 10:15:01 -0700265}
266
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700267TEST(logging, LOG_STREAM_FATAL_WITHOUT_ABORT_enabled) {
268 CHECK_LOG_STREAM_ENABLED(FATAL_WITHOUT_ABORT);
Andreas Gampe436f5a02016-09-22 10:15:01 -0700269}
270
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700271TEST(logging, LOG_STREAM_ERROR_disabled) {
272 CHECK_LOG_STREAM_DISABLED(ERROR);
Andreas Gampe436f5a02016-09-22 10:15:01 -0700273}
274
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700275TEST(logging, LOG_STREAM_ERROR_enabled) {
276 CHECK_LOG_STREAM_ENABLED(ERROR);
Andreas Gampe436f5a02016-09-22 10:15:01 -0700277}
278
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700279TEST(logging, LOG_STREAM_WARNING_disabled) {
280 CHECK_LOG_STREAM_DISABLED(WARNING);
Andreas Gampe436f5a02016-09-22 10:15:01 -0700281}
282
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700283TEST(logging, LOG_STREAM_WARNING_enabled) {
284 CHECK_LOG_STREAM_ENABLED(WARNING);
Andreas Gampe436f5a02016-09-22 10:15:01 -0700285}
286
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700287TEST(logging, LOG_STREAM_INFO_disabled) {
288 CHECK_LOG_STREAM_DISABLED(INFO);
289}
Andreas Gampe436f5a02016-09-22 10:15:01 -0700290
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700291TEST(logging, LOG_STREAM_INFO_enabled) {
292 CHECK_LOG_STREAM_ENABLED(INFO);
293}
294
295TEST(logging, LOG_STREAM_DEBUG_disabled) {
296 CHECK_LOG_STREAM_DISABLED(DEBUG);
297}
298
299TEST(logging, LOG_STREAM_DEBUG_enabled) {
300 CHECK_LOG_STREAM_ENABLED(DEBUG);
301}
302
303TEST(logging, LOG_STREAM_VERBOSE_disabled) {
304 CHECK_LOG_STREAM_DISABLED(VERBOSE);
305}
306
307TEST(logging, LOG_STREAM_VERBOSE_enabled) {
308 CHECK_LOG_STREAM_ENABLED(VERBOSE);
309}
310
311#undef CHECK_LOG_STREAM_DISABLED
312#undef CHECK_LOG_STREAM_ENABLED
313
Andreas Gampe436f5a02016-09-22 10:15:01 -0700314
315#define CHECK_LOG_DISABLED(severity) \
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700316 { \
317 android::base::ScopedLogSeverity sls1(android::base::FATAL); \
318 CapturedStderr cap1; \
319 LOG(severity) << "foo bar"; \
320 ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \
321 } \
322 { \
323 android::base::ScopedLogSeverity sls1(android::base::FATAL); \
324 CapturedStderr cap1; \
325 LOG(::android::base::severity) << "foo bar"; \
326 ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \
327 } \
Andreas Gampe436f5a02016-09-22 10:15:01 -0700328
329#define CHECK_LOG_ENABLED(severity) \
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700330 { \
331 android::base::ScopedLogSeverity sls2(android::base::severity); \
332 CapturedStderr cap2; \
333 LOG(severity) << "foobar"; \
334 CheckMessage(cap2, android::base::severity, "foobar"); \
335 } \
336 { \
337 android::base::ScopedLogSeverity sls2(android::base::severity); \
338 CapturedStderr cap2; \
339 LOG(::android::base::severity) << "foobar"; \
340 CheckMessage(cap2, android::base::severity, "foobar"); \
341 } \
Andreas Gampe436f5a02016-09-22 10:15:01 -0700342
Elliott Hughes13d78e42016-09-07 16:22:40 -0700343TEST(logging, LOG_FATAL) {
344 ASSERT_DEATH({SuppressAbortUI(); LOG(FATAL) << "foobar";}, "foobar");
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700345 ASSERT_DEATH({SuppressAbortUI(); LOG(::android::base::FATAL) << "foobar";}, "foobar");
Elliott Hughes13d78e42016-09-07 16:22:40 -0700346}
Dan Albert58310b42015-03-13 23:06:01 -0700347
Andreas Gampe550829d2016-09-07 10:10:50 -0700348TEST(logging, LOG_FATAL_WITHOUT_ABORT_disabled) {
349 CHECK_LOG_DISABLED(FATAL_WITHOUT_ABORT);
350}
351
352TEST(logging, LOG_FATAL_WITHOUT_ABORT_enabled) {
353 CHECK_LOG_ENABLED(FATAL_WITHOUT_ABORT);
354}
355
Elliott Hughes13d78e42016-09-07 16:22:40 -0700356TEST(logging, LOG_ERROR_disabled) {
357 CHECK_LOG_DISABLED(ERROR);
358}
Dan Albert58310b42015-03-13 23:06:01 -0700359
Elliott Hughes13d78e42016-09-07 16:22:40 -0700360TEST(logging, LOG_ERROR_enabled) {
361 CHECK_LOG_ENABLED(ERROR);
362}
363
364TEST(logging, LOG_WARNING_disabled) {
365 CHECK_LOG_DISABLED(WARNING);
366}
367
368TEST(logging, LOG_WARNING_enabled) {
369 CHECK_LOG_ENABLED(WARNING);
370}
371
372TEST(logging, LOG_INFO_disabled) {
373 CHECK_LOG_DISABLED(INFO);
374}
375
376TEST(logging, LOG_INFO_enabled) {
377 CHECK_LOG_ENABLED(INFO);
378}
379
380TEST(logging, LOG_DEBUG_disabled) {
381 CHECK_LOG_DISABLED(DEBUG);
382}
383
384TEST(logging, LOG_DEBUG_enabled) {
385 CHECK_LOG_ENABLED(DEBUG);
386}
387
388TEST(logging, LOG_VERBOSE_disabled) {
389 CHECK_LOG_DISABLED(VERBOSE);
390}
391
392TEST(logging, LOG_VERBOSE_enabled) {
393 CHECK_LOG_ENABLED(VERBOSE);
394}
395
Andreas Gampe436f5a02016-09-22 10:15:01 -0700396#undef CHECK_LOG_DISABLED
397#undef CHECK_LOG_ENABLED
398
Andreas Gampe436f5a02016-09-22 10:15:01 -0700399
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700400TEST(logging, LOG_complex_param) {
401#define CHECK_LOG_COMBINATION(use_scoped_log_severity_info, use_logging_severity_info) \
Andreas Gampe436f5a02016-09-22 10:15:01 -0700402 { \
403 android::base::ScopedLogSeverity sls( \
404 (use_scoped_log_severity_info) ? ::android::base::INFO : ::android::base::WARNING); \
405 CapturedStderr cap; \
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700406 LOG((use_logging_severity_info) ? ::android::base::INFO : ::android::base::WARNING) \
Andreas Gampe436f5a02016-09-22 10:15:01 -0700407 << "foobar"; \
408 if ((use_scoped_log_severity_info) || !(use_logging_severity_info)) { \
409 CheckMessage(cap, \
410 (use_logging_severity_info) ? ::android::base::INFO : ::android::base::WARNING, \
411 "foobar"); \
412 } else { \
413 ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_CUR)); \
414 } \
415 }
416
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700417 CHECK_LOG_COMBINATION(false,false);
418 CHECK_LOG_COMBINATION(false,true);
419 CHECK_LOG_COMBINATION(true,false);
420 CHECK_LOG_COMBINATION(true,true);
Andreas Gampe436f5a02016-09-22 10:15:01 -0700421
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700422#undef CHECK_LOG_COMBINATION
Andreas Gampe436f5a02016-09-22 10:15:01 -0700423}
424
425
Elliott Hughes13d78e42016-09-07 16:22:40 -0700426TEST(logging, LOG_does_not_clobber_errno) {
427 CapturedStderr cap;
428 errno = 12345;
429 LOG(INFO) << (errno = 67890);
430 EXPECT_EQ(12345, errno) << "errno was not restored";
431
432 CheckMessage(cap, android::base::INFO, "67890");
433}
434
435TEST(logging, PLOG_does_not_clobber_errno) {
436 CapturedStderr cap;
437 errno = 12345;
438 PLOG(INFO) << (errno = 67890);
439 EXPECT_EQ(12345, errno) << "errno was not restored";
440
441 CheckMessage(cap, android::base::INFO, "67890");
442}
443
444TEST(logging, LOG_does_not_have_dangling_if) {
445 CapturedStderr cap; // So the logging below has no side-effects.
446
447 // Do the test two ways: once where we hypothesize that LOG()'s if
448 // will evaluate to true (when severity is high enough) and once when we
449 // expect it to evaluate to false (when severity is not high enough).
450 bool flag = false;
451 if (true)
452 LOG(INFO) << "foobar";
453 else
454 flag = true;
455
456 EXPECT_FALSE(flag) << "LOG macro probably has a dangling if with no else";
457
458 flag = false;
459 if (true)
460 LOG(VERBOSE) << "foobar";
461 else
462 flag = true;
463
464 EXPECT_FALSE(flag) << "LOG macro probably has a dangling if with no else";
Andreas Gampe436f5a02016-09-22 10:15:01 -0700465}
Elliott Hughes13d78e42016-09-07 16:22:40 -0700466
467#define CHECK_PLOG_DISABLED(severity) \
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700468 { \
469 android::base::ScopedLogSeverity sls1(android::base::FATAL); \
470 CapturedStderr cap1; \
471 PLOG(severity) << "foo bar"; \
472 ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \
473 } \
474 { \
475 android::base::ScopedLogSeverity sls1(android::base::FATAL); \
476 CapturedStderr cap1; \
477 PLOG(severity) << "foo bar"; \
478 ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \
479 } \
Elliott Hughes13d78e42016-09-07 16:22:40 -0700480
481#define CHECK_PLOG_ENABLED(severity) \
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700482 { \
483 android::base::ScopedLogSeverity sls2(android::base::severity); \
484 CapturedStderr cap2; \
485 errno = ENOENT; \
486 PLOG(severity) << "foobar"; \
487 CheckMessage(cap2, android::base::severity, "foobar: No such file or directory"); \
488 } \
489 { \
490 android::base::ScopedLogSeverity sls2(android::base::severity); \
491 CapturedStderr cap2; \
492 errno = ENOENT; \
493 PLOG(severity) << "foobar"; \
494 CheckMessage(cap2, android::base::severity, "foobar: No such file or directory"); \
495 } \
Elliott Hughes13d78e42016-09-07 16:22:40 -0700496
Andreas Gampe550829d2016-09-07 10:10:50 -0700497TEST(logging, PLOG_FATAL) {
498 ASSERT_DEATH({SuppressAbortUI(); PLOG(FATAL) << "foobar";}, "foobar");
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700499 ASSERT_DEATH({SuppressAbortUI(); PLOG(::android::base::FATAL) << "foobar";}, "foobar");
Andreas Gampe550829d2016-09-07 10:10:50 -0700500}
501
502TEST(logging, PLOG_FATAL_WITHOUT_ABORT_disabled) {
503 CHECK_PLOG_DISABLED(FATAL_WITHOUT_ABORT);
504}
505
506TEST(logging, PLOG_FATAL_WITHOUT_ABORT_enabled) {
507 CHECK_PLOG_ENABLED(FATAL_WITHOUT_ABORT);
508}
509
Elliott Hughes13d78e42016-09-07 16:22:40 -0700510TEST(logging, PLOG_ERROR_disabled) {
511 CHECK_PLOG_DISABLED(ERROR);
512}
513
514TEST(logging, PLOG_ERROR_enabled) {
515 CHECK_PLOG_ENABLED(ERROR);
516}
517
518TEST(logging, PLOG_WARNING_disabled) {
519 CHECK_PLOG_DISABLED(WARNING);
520}
521
522TEST(logging, PLOG_WARNING_enabled) {
523 CHECK_PLOG_ENABLED(WARNING);
524}
525
526TEST(logging, PLOG_INFO_disabled) {
527 CHECK_PLOG_DISABLED(INFO);
528}
529
530TEST(logging, PLOG_INFO_enabled) {
531 CHECK_PLOG_ENABLED(INFO);
532}
533
534TEST(logging, PLOG_DEBUG_disabled) {
535 CHECK_PLOG_DISABLED(DEBUG);
536}
537
538TEST(logging, PLOG_DEBUG_enabled) {
539 CHECK_PLOG_ENABLED(DEBUG);
540}
541
542TEST(logging, PLOG_VERBOSE_disabled) {
543 CHECK_PLOG_DISABLED(VERBOSE);
544}
545
546TEST(logging, PLOG_VERBOSE_enabled) {
547 CHECK_PLOG_ENABLED(VERBOSE);
Dan Albert58310b42015-03-13 23:06:01 -0700548}
549
Andreas Gampe436f5a02016-09-22 10:15:01 -0700550#undef CHECK_PLOG_DISABLED
551#undef CHECK_PLOG_ENABLED
552
553
Dan Albertb547c852015-03-27 11:20:14 -0700554TEST(logging, UNIMPLEMENTED) {
Elliott Hughes13d78e42016-09-07 16:22:40 -0700555 std::string expected = android::base::StringPrintf("%s unimplemented ", __PRETTY_FUNCTION__);
Dan Albert58310b42015-03-13 23:06:01 -0700556
Elliott Hughes13d78e42016-09-07 16:22:40 -0700557 CapturedStderr cap;
558 errno = ENOENT;
559 UNIMPLEMENTED(ERROR);
560 CheckMessage(cap, android::base::ERROR, expected.c_str());
Dan Albert58310b42015-03-13 23:06:01 -0700561}
Andreas Gampe2691e332016-09-08 11:03:58 -0700562
563static void NoopAborter(const char* msg ATTRIBUTE_UNUSED) {
564 LOG(ERROR) << "called noop";
565}
566
567TEST(logging, LOG_FATAL_NOOP_ABORTER) {
568 {
569 android::base::SetAborter(NoopAborter);
570
571 android::base::ScopedLogSeverity sls(android::base::ERROR);
572 CapturedStderr cap;
573 LOG(FATAL) << "foobar";
574 CheckMessage(cap, android::base::FATAL, "foobar");
575 CheckMessage(cap, android::base::ERROR, "called noop");
576
577 android::base::SetAborter(android::base::DefaultAborter);
578 }
579
580 ASSERT_DEATH({SuppressAbortUI(); LOG(FATAL) << "foobar";}, "foobar");
581}
Andreas Gampeb4e32f32016-10-04 19:17:07 -0700582
583struct CountLineAborter {
584 static void CountLineAborterFunction(const char* msg) {
585 while (*msg != 0) {
586 if (*msg == '\n') {
587 newline_count++;
588 }
589 msg++;
590 }
591 }
592 static size_t newline_count;
593};
594size_t CountLineAborter::newline_count = 0;
595
596TEST(logging, LOG_FATAL_ABORTER_MESSAGE) {
597 CountLineAborter::newline_count = 0;
598 android::base::SetAborter(CountLineAborter::CountLineAborterFunction);
599
600 android::base::ScopedLogSeverity sls(android::base::ERROR);
601 CapturedStderr cap;
602 LOG(FATAL) << "foo\nbar";
603
604 EXPECT_EQ(CountLineAborter::newline_count, 1U + 1U); // +1 for final '\n'.
605}
Yabin Cui0c689532017-01-23 10:29:23 -0800606
607__attribute__((constructor)) void TestLoggingInConstructor() {
608 LOG(ERROR) << "foobar";
609}
Andreas Gampe1923e762018-03-05 10:00:19 -0800610
611TEST(logging, SetDefaultTag) {
612 constexpr const char* expected_tag = "test_tag";
613 constexpr const char* expected_msg = "foobar";
614 CapturedStderr cap;
615 {
616 std::string old_default_tag = android::base::GetDefaultTag();
617 android::base::SetDefaultTag(expected_tag);
618 android::base::ScopedLogSeverity sls(android::base::LogSeverity::INFO);
619 LOG(INFO) << expected_msg;
620 android::base::SetDefaultTag(old_default_tag);
621 }
622 CheckMessage(cap, android::base::LogSeverity::INFO, expected_msg, expected_tag);
623}
Elliott Hughes1be0d142018-05-23 09:16:46 -0700624
625TEST(logging, StdioLogger) {
626 std::string err_str;
627 std::string out_str;
628 {
629 CapturedStderr cap_err;
630 CapturedStdout cap_out;
631 android::base::SetLogger(android::base::StdioLogger);
632 LOG(INFO) << "out";
633 LOG(ERROR) << "err";
634 err_str = cap_err.str();
635 out_str = cap_out.str();
636 }
637
638 // For INFO we expect just the literal "out\n".
639 ASSERT_EQ("out\n", out_str) << out_str;
640 // Whereas ERROR logging includes the program name.
641 ASSERT_EQ(android::base::Basename(android::base::GetExecutablePath()) + ": err\n", err_str)
642 << err_str;
643}