blob: 3113fb47a41155f7724ab9c473bc807f6d26586f [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
Christopher Ferriseea85c92018-08-30 13:31:45 -0700209static void CheckMessage(const std::string& output, android::base::LogSeverity severity,
Andreas Gampe1923e762018-03-05 10:00:19 -0800210 const char* expected, const char* expected_tag = nullptr) {
Dan Albert5c190402015-04-29 11:32:23 -0700211 // We can't usefully check the output of any of these on Windows because we
212 // don't have std::regex, but we can at least make sure we printed at least as
213 // many characters are in the log message.
Elliott Hughes13d78e42016-09-07 16:22:40 -0700214 ASSERT_GT(output.length(), strlen(expected));
215 ASSERT_NE(nullptr, strstr(output.c_str(), expected)) << output;
Andreas Gampe1923e762018-03-05 10:00:19 -0800216 if (expected_tag != nullptr) {
217 ASSERT_NE(nullptr, strstr(output.c_str(), expected_tag)) << output;
218 }
Dan Albert58310b42015-03-13 23:06:01 -0700219
Dan Albert5c190402015-04-29 11:32:23 -0700220#if !defined(_WIN32)
Andreas Gampe1923e762018-03-05 10:00:19 -0800221 std::string regex_str;
222 if (expected_tag != nullptr) {
223 regex_str.append(expected_tag);
224 regex_str.append(" ");
225 }
226 regex_str.append(make_log_pattern(severity, expected));
227 std::regex message_regex(regex_str);
Elliott Hughes13d78e42016-09-07 16:22:40 -0700228 ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
Dan Albert5c190402015-04-29 11:32:23 -0700229#endif
Dan Albert58310b42015-03-13 23:06:01 -0700230}
231
Christopher Ferriseea85c92018-08-30 13:31:45 -0700232static void CheckMessage(CapturedStderr& cap, android::base::LogSeverity severity,
233 const char* expected, const char* expected_tag = nullptr) {
234 cap.Stop();
235 std::string output = cap.str();
236 return CheckMessage(output, severity, expected, expected_tag);
237}
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700238
Christopher Ferriseea85c92018-08-30 13:31:45 -0700239#define CHECK_LOG_STREAM_DISABLED(severity) \
240 { \
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700241 android::base::ScopedLogSeverity sls1(android::base::FATAL); \
Christopher Ferriseea85c92018-08-30 13:31:45 -0700242 CapturedStderr cap1; \
243 LOG_STREAM(severity) << "foo bar"; \
244 cap1.Stop(); \
245 ASSERT_EQ("", cap1.str()); \
246 } \
247 { \
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700248 android::base::ScopedLogSeverity sls1(android::base::FATAL); \
Christopher Ferriseea85c92018-08-30 13:31:45 -0700249 CapturedStderr cap1; \
250 LOG_STREAM(::android::base::severity) << "foo bar"; \
251 cap1.Stop(); \
252 ASSERT_EQ("", cap1.str()); \
253 }
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700254
255#define CHECK_LOG_STREAM_ENABLED(severity) \
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700256 { \
257 android::base::ScopedLogSeverity sls2(android::base::severity); \
258 CapturedStderr cap2; \
259 LOG_STREAM(severity) << "foobar"; \
260 CheckMessage(cap2, android::base::severity, "foobar"); \
261 } \
262 { \
263 android::base::ScopedLogSeverity sls2(android::base::severity); \
264 CapturedStderr cap2; \
265 LOG_STREAM(::android::base::severity) << "foobar"; \
266 CheckMessage(cap2, android::base::severity, "foobar"); \
267 } \
Andreas Gampe436f5a02016-09-22 10:15:01 -0700268
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700269TEST(logging, LOG_STREAM_FATAL_WITHOUT_ABORT_disabled) {
270 CHECK_LOG_STREAM_DISABLED(FATAL_WITHOUT_ABORT);
Andreas Gampe436f5a02016-09-22 10:15:01 -0700271}
272
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700273TEST(logging, LOG_STREAM_FATAL_WITHOUT_ABORT_enabled) {
Christopher Ferriseea85c92018-08-30 13:31:45 -0700274 ASSERT_NO_FATAL_FAILURE(CHECK_LOG_STREAM_ENABLED(FATAL_WITHOUT_ABORT));
Andreas Gampe436f5a02016-09-22 10:15:01 -0700275}
276
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700277TEST(logging, LOG_STREAM_ERROR_disabled) {
278 CHECK_LOG_STREAM_DISABLED(ERROR);
Andreas Gampe436f5a02016-09-22 10:15:01 -0700279}
280
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700281TEST(logging, LOG_STREAM_ERROR_enabled) {
Christopher Ferriseea85c92018-08-30 13:31:45 -0700282 ASSERT_NO_FATAL_FAILURE(CHECK_LOG_STREAM_ENABLED(ERROR));
Andreas Gampe436f5a02016-09-22 10:15:01 -0700283}
284
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700285TEST(logging, LOG_STREAM_WARNING_disabled) {
286 CHECK_LOG_STREAM_DISABLED(WARNING);
Andreas Gampe436f5a02016-09-22 10:15:01 -0700287}
288
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700289TEST(logging, LOG_STREAM_WARNING_enabled) {
Christopher Ferriseea85c92018-08-30 13:31:45 -0700290 ASSERT_NO_FATAL_FAILURE(CHECK_LOG_STREAM_ENABLED(WARNING));
Andreas Gampe436f5a02016-09-22 10:15:01 -0700291}
292
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700293TEST(logging, LOG_STREAM_INFO_disabled) {
294 CHECK_LOG_STREAM_DISABLED(INFO);
295}
Andreas Gampe436f5a02016-09-22 10:15:01 -0700296
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700297TEST(logging, LOG_STREAM_INFO_enabled) {
Christopher Ferriseea85c92018-08-30 13:31:45 -0700298 ASSERT_NO_FATAL_FAILURE(CHECK_LOG_STREAM_ENABLED(INFO));
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700299}
300
301TEST(logging, LOG_STREAM_DEBUG_disabled) {
302 CHECK_LOG_STREAM_DISABLED(DEBUG);
303}
304
305TEST(logging, LOG_STREAM_DEBUG_enabled) {
Christopher Ferriseea85c92018-08-30 13:31:45 -0700306 ASSERT_NO_FATAL_FAILURE(CHECK_LOG_STREAM_ENABLED(DEBUG));
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700307}
308
309TEST(logging, LOG_STREAM_VERBOSE_disabled) {
310 CHECK_LOG_STREAM_DISABLED(VERBOSE);
311}
312
313TEST(logging, LOG_STREAM_VERBOSE_enabled) {
Christopher Ferriseea85c92018-08-30 13:31:45 -0700314 ASSERT_NO_FATAL_FAILURE(CHECK_LOG_STREAM_ENABLED(VERBOSE));
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700315}
316
317#undef CHECK_LOG_STREAM_DISABLED
318#undef CHECK_LOG_STREAM_ENABLED
319
Christopher Ferriseea85c92018-08-30 13:31:45 -0700320#define CHECK_LOG_DISABLED(severity) \
321 { \
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700322 android::base::ScopedLogSeverity sls1(android::base::FATAL); \
Christopher Ferriseea85c92018-08-30 13:31:45 -0700323 CapturedStderr cap1; \
324 LOG(severity) << "foo bar"; \
325 cap1.Stop(); \
326 ASSERT_EQ("", cap1.str()); \
327 } \
328 { \
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700329 android::base::ScopedLogSeverity sls1(android::base::FATAL); \
Christopher Ferriseea85c92018-08-30 13:31:45 -0700330 CapturedStderr cap1; \
331 LOG(::android::base::severity) << "foo bar"; \
332 cap1.Stop(); \
333 ASSERT_EQ("", cap1.str()); \
334 }
Andreas Gampe436f5a02016-09-22 10:15:01 -0700335
336#define CHECK_LOG_ENABLED(severity) \
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700337 { \
338 android::base::ScopedLogSeverity sls2(android::base::severity); \
339 CapturedStderr cap2; \
340 LOG(severity) << "foobar"; \
341 CheckMessage(cap2, android::base::severity, "foobar"); \
342 } \
343 { \
344 android::base::ScopedLogSeverity sls2(android::base::severity); \
345 CapturedStderr cap2; \
346 LOG(::android::base::severity) << "foobar"; \
347 CheckMessage(cap2, android::base::severity, "foobar"); \
348 } \
Andreas Gampe436f5a02016-09-22 10:15:01 -0700349
Elliott Hughes13d78e42016-09-07 16:22:40 -0700350TEST(logging, LOG_FATAL) {
351 ASSERT_DEATH({SuppressAbortUI(); LOG(FATAL) << "foobar";}, "foobar");
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700352 ASSERT_DEATH({SuppressAbortUI(); LOG(::android::base::FATAL) << "foobar";}, "foobar");
Elliott Hughes13d78e42016-09-07 16:22:40 -0700353}
Dan Albert58310b42015-03-13 23:06:01 -0700354
Andreas Gampe550829d2016-09-07 10:10:50 -0700355TEST(logging, LOG_FATAL_WITHOUT_ABORT_disabled) {
356 CHECK_LOG_DISABLED(FATAL_WITHOUT_ABORT);
357}
358
359TEST(logging, LOG_FATAL_WITHOUT_ABORT_enabled) {
Christopher Ferriseea85c92018-08-30 13:31:45 -0700360 ASSERT_NO_FATAL_FAILURE(CHECK_LOG_ENABLED(FATAL_WITHOUT_ABORT));
Andreas Gampe550829d2016-09-07 10:10:50 -0700361}
362
Elliott Hughes13d78e42016-09-07 16:22:40 -0700363TEST(logging, LOG_ERROR_disabled) {
364 CHECK_LOG_DISABLED(ERROR);
365}
Dan Albert58310b42015-03-13 23:06:01 -0700366
Elliott Hughes13d78e42016-09-07 16:22:40 -0700367TEST(logging, LOG_ERROR_enabled) {
Christopher Ferriseea85c92018-08-30 13:31:45 -0700368 ASSERT_NO_FATAL_FAILURE(CHECK_LOG_ENABLED(ERROR));
Elliott Hughes13d78e42016-09-07 16:22:40 -0700369}
370
371TEST(logging, LOG_WARNING_disabled) {
372 CHECK_LOG_DISABLED(WARNING);
373}
374
375TEST(logging, LOG_WARNING_enabled) {
Christopher Ferriseea85c92018-08-30 13:31:45 -0700376 ASSERT_NO_FATAL_FAILURE(CHECK_LOG_ENABLED(WARNING));
Elliott Hughes13d78e42016-09-07 16:22:40 -0700377}
378
379TEST(logging, LOG_INFO_disabled) {
380 CHECK_LOG_DISABLED(INFO);
381}
382
383TEST(logging, LOG_INFO_enabled) {
Christopher Ferriseea85c92018-08-30 13:31:45 -0700384 ASSERT_NO_FATAL_FAILURE(CHECK_LOG_ENABLED(INFO));
Elliott Hughes13d78e42016-09-07 16:22:40 -0700385}
386
387TEST(logging, LOG_DEBUG_disabled) {
388 CHECK_LOG_DISABLED(DEBUG);
389}
390
391TEST(logging, LOG_DEBUG_enabled) {
Christopher Ferriseea85c92018-08-30 13:31:45 -0700392 ASSERT_NO_FATAL_FAILURE(CHECK_LOG_ENABLED(DEBUG));
Elliott Hughes13d78e42016-09-07 16:22:40 -0700393}
394
395TEST(logging, LOG_VERBOSE_disabled) {
396 CHECK_LOG_DISABLED(VERBOSE);
397}
398
399TEST(logging, LOG_VERBOSE_enabled) {
Christopher Ferriseea85c92018-08-30 13:31:45 -0700400 ASSERT_NO_FATAL_FAILURE(CHECK_LOG_ENABLED(VERBOSE));
Elliott Hughes13d78e42016-09-07 16:22:40 -0700401}
402
Andreas Gampe436f5a02016-09-22 10:15:01 -0700403#undef CHECK_LOG_DISABLED
404#undef CHECK_LOG_ENABLED
405
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700406TEST(logging, LOG_complex_param) {
Christopher Ferriseea85c92018-08-30 13:31:45 -0700407#define CHECK_LOG_COMBINATION(use_scoped_log_severity_info, use_logging_severity_info) \
408 { \
409 android::base::ScopedLogSeverity sls( \
410 (use_scoped_log_severity_info) ? ::android::base::INFO : ::android::base::WARNING); \
411 CapturedStderr cap; \
412 LOG((use_logging_severity_info) ? ::android::base::INFO : ::android::base::WARNING) \
413 << "foobar"; \
414 if ((use_scoped_log_severity_info) || !(use_logging_severity_info)) { \
415 ASSERT_NO_FATAL_FAILURE(CheckMessage( \
416 cap, (use_logging_severity_info) ? ::android::base::INFO : ::android::base::WARNING, \
417 "foobar")); \
418 } else { \
419 cap.Stop(); \
420 ASSERT_EQ("", cap.str()); \
421 } \
Andreas Gampe436f5a02016-09-22 10:15:01 -0700422 }
423
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700424 CHECK_LOG_COMBINATION(false,false);
425 CHECK_LOG_COMBINATION(false,true);
426 CHECK_LOG_COMBINATION(true,false);
427 CHECK_LOG_COMBINATION(true,true);
Andreas Gampe436f5a02016-09-22 10:15:01 -0700428
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700429#undef CHECK_LOG_COMBINATION
Andreas Gampe436f5a02016-09-22 10:15:01 -0700430}
431
432
Elliott Hughes13d78e42016-09-07 16:22:40 -0700433TEST(logging, LOG_does_not_clobber_errno) {
434 CapturedStderr cap;
435 errno = 12345;
436 LOG(INFO) << (errno = 67890);
437 EXPECT_EQ(12345, errno) << "errno was not restored";
438
Christopher Ferriseea85c92018-08-30 13:31:45 -0700439 ASSERT_NO_FATAL_FAILURE(CheckMessage(cap, android::base::INFO, "67890"));
Elliott Hughes13d78e42016-09-07 16:22:40 -0700440}
441
442TEST(logging, PLOG_does_not_clobber_errno) {
443 CapturedStderr cap;
444 errno = 12345;
445 PLOG(INFO) << (errno = 67890);
446 EXPECT_EQ(12345, errno) << "errno was not restored";
447
Christopher Ferriseea85c92018-08-30 13:31:45 -0700448 ASSERT_NO_FATAL_FAILURE(CheckMessage(cap, android::base::INFO, "67890"));
Elliott Hughes13d78e42016-09-07 16:22:40 -0700449}
450
451TEST(logging, LOG_does_not_have_dangling_if) {
452 CapturedStderr cap; // So the logging below has no side-effects.
453
454 // Do the test two ways: once where we hypothesize that LOG()'s if
455 // will evaluate to true (when severity is high enough) and once when we
456 // expect it to evaluate to false (when severity is not high enough).
457 bool flag = false;
458 if (true)
459 LOG(INFO) << "foobar";
460 else
461 flag = true;
462
463 EXPECT_FALSE(flag) << "LOG macro probably has a dangling if with no else";
464
465 flag = false;
466 if (true)
467 LOG(VERBOSE) << "foobar";
468 else
469 flag = true;
470
471 EXPECT_FALSE(flag) << "LOG macro probably has a dangling if with no else";
Andreas Gampe436f5a02016-09-22 10:15:01 -0700472}
Elliott Hughes13d78e42016-09-07 16:22:40 -0700473
Christopher Ferriseea85c92018-08-30 13:31:45 -0700474#define CHECK_PLOG_DISABLED(severity) \
475 { \
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700476 android::base::ScopedLogSeverity sls1(android::base::FATAL); \
Christopher Ferriseea85c92018-08-30 13:31:45 -0700477 CapturedStderr cap1; \
478 PLOG(severity) << "foo bar"; \
479 cap1.Stop(); \
480 ASSERT_EQ("", cap1.str()); \
481 } \
482 { \
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700483 android::base::ScopedLogSeverity sls1(android::base::FATAL); \
Christopher Ferriseea85c92018-08-30 13:31:45 -0700484 CapturedStderr cap1; \
485 PLOG(severity) << "foo bar"; \
486 cap1.Stop(); \
487 ASSERT_EQ("", cap1.str()); \
488 }
Elliott Hughes13d78e42016-09-07 16:22:40 -0700489
490#define CHECK_PLOG_ENABLED(severity) \
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700491 { \
492 android::base::ScopedLogSeverity sls2(android::base::severity); \
493 CapturedStderr cap2; \
494 errno = ENOENT; \
495 PLOG(severity) << "foobar"; \
496 CheckMessage(cap2, android::base::severity, "foobar: No such file or directory"); \
497 } \
498 { \
499 android::base::ScopedLogSeverity sls2(android::base::severity); \
500 CapturedStderr cap2; \
501 errno = ENOENT; \
502 PLOG(severity) << "foobar"; \
503 CheckMessage(cap2, android::base::severity, "foobar: No such file or directory"); \
504 } \
Elliott Hughes13d78e42016-09-07 16:22:40 -0700505
Andreas Gampe550829d2016-09-07 10:10:50 -0700506TEST(logging, PLOG_FATAL) {
507 ASSERT_DEATH({SuppressAbortUI(); PLOG(FATAL) << "foobar";}, "foobar");
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700508 ASSERT_DEATH({SuppressAbortUI(); PLOG(::android::base::FATAL) << "foobar";}, "foobar");
Andreas Gampe550829d2016-09-07 10:10:50 -0700509}
510
511TEST(logging, PLOG_FATAL_WITHOUT_ABORT_disabled) {
512 CHECK_PLOG_DISABLED(FATAL_WITHOUT_ABORT);
513}
514
515TEST(logging, PLOG_FATAL_WITHOUT_ABORT_enabled) {
Christopher Ferriseea85c92018-08-30 13:31:45 -0700516 ASSERT_NO_FATAL_FAILURE(CHECK_PLOG_ENABLED(FATAL_WITHOUT_ABORT));
Andreas Gampe550829d2016-09-07 10:10:50 -0700517}
518
Elliott Hughes13d78e42016-09-07 16:22:40 -0700519TEST(logging, PLOG_ERROR_disabled) {
520 CHECK_PLOG_DISABLED(ERROR);
521}
522
523TEST(logging, PLOG_ERROR_enabled) {
Christopher Ferriseea85c92018-08-30 13:31:45 -0700524 ASSERT_NO_FATAL_FAILURE(CHECK_PLOG_ENABLED(ERROR));
Elliott Hughes13d78e42016-09-07 16:22:40 -0700525}
526
527TEST(logging, PLOG_WARNING_disabled) {
528 CHECK_PLOG_DISABLED(WARNING);
529}
530
531TEST(logging, PLOG_WARNING_enabled) {
Christopher Ferriseea85c92018-08-30 13:31:45 -0700532 ASSERT_NO_FATAL_FAILURE(CHECK_PLOG_ENABLED(WARNING));
Elliott Hughes13d78e42016-09-07 16:22:40 -0700533}
534
535TEST(logging, PLOG_INFO_disabled) {
536 CHECK_PLOG_DISABLED(INFO);
537}
538
539TEST(logging, PLOG_INFO_enabled) {
Christopher Ferriseea85c92018-08-30 13:31:45 -0700540 ASSERT_NO_FATAL_FAILURE(CHECK_PLOG_ENABLED(INFO));
Elliott Hughes13d78e42016-09-07 16:22:40 -0700541}
542
543TEST(logging, PLOG_DEBUG_disabled) {
544 CHECK_PLOG_DISABLED(DEBUG);
545}
546
547TEST(logging, PLOG_DEBUG_enabled) {
Christopher Ferriseea85c92018-08-30 13:31:45 -0700548 ASSERT_NO_FATAL_FAILURE(CHECK_PLOG_ENABLED(DEBUG));
Elliott Hughes13d78e42016-09-07 16:22:40 -0700549}
550
551TEST(logging, PLOG_VERBOSE_disabled) {
552 CHECK_PLOG_DISABLED(VERBOSE);
553}
554
555TEST(logging, PLOG_VERBOSE_enabled) {
Christopher Ferriseea85c92018-08-30 13:31:45 -0700556 ASSERT_NO_FATAL_FAILURE(CHECK_PLOG_ENABLED(VERBOSE));
Dan Albert58310b42015-03-13 23:06:01 -0700557}
558
Andreas Gampe436f5a02016-09-22 10:15:01 -0700559#undef CHECK_PLOG_DISABLED
560#undef CHECK_PLOG_ENABLED
561
562
Dan Albertb547c852015-03-27 11:20:14 -0700563TEST(logging, UNIMPLEMENTED) {
Elliott Hughes13d78e42016-09-07 16:22:40 -0700564 std::string expected = android::base::StringPrintf("%s unimplemented ", __PRETTY_FUNCTION__);
Dan Albert58310b42015-03-13 23:06:01 -0700565
Elliott Hughes13d78e42016-09-07 16:22:40 -0700566 CapturedStderr cap;
567 errno = ENOENT;
568 UNIMPLEMENTED(ERROR);
Christopher Ferriseea85c92018-08-30 13:31:45 -0700569 ASSERT_NO_FATAL_FAILURE(CheckMessage(cap, android::base::ERROR, expected.c_str()));
Dan Albert58310b42015-03-13 23:06:01 -0700570}
Andreas Gampe2691e332016-09-08 11:03:58 -0700571
572static void NoopAborter(const char* msg ATTRIBUTE_UNUSED) {
573 LOG(ERROR) << "called noop";
574}
575
576TEST(logging, LOG_FATAL_NOOP_ABORTER) {
Christopher Ferriseea85c92018-08-30 13:31:45 -0700577 CapturedStderr cap;
Andreas Gampe2691e332016-09-08 11:03:58 -0700578 {
579 android::base::SetAborter(NoopAborter);
580
581 android::base::ScopedLogSeverity sls(android::base::ERROR);
Andreas Gampe2691e332016-09-08 11:03:58 -0700582 LOG(FATAL) << "foobar";
Christopher Ferriseea85c92018-08-30 13:31:45 -0700583 cap.Stop();
Andreas Gampe2691e332016-09-08 11:03:58 -0700584
585 android::base::SetAborter(android::base::DefaultAborter);
586 }
Christopher Ferriseea85c92018-08-30 13:31:45 -0700587 std::string output = cap.str();
588 ASSERT_NO_FATAL_FAILURE(CheckMessage(output, android::base::FATAL, "foobar"));
589 ASSERT_NO_FATAL_FAILURE(CheckMessage(output, android::base::ERROR, "called noop"));
Andreas Gampe2691e332016-09-08 11:03:58 -0700590
591 ASSERT_DEATH({SuppressAbortUI(); LOG(FATAL) << "foobar";}, "foobar");
592}
Andreas Gampeb4e32f32016-10-04 19:17:07 -0700593
594struct CountLineAborter {
595 static void CountLineAborterFunction(const char* msg) {
596 while (*msg != 0) {
597 if (*msg == '\n') {
598 newline_count++;
599 }
600 msg++;
601 }
602 }
603 static size_t newline_count;
604};
605size_t CountLineAborter::newline_count = 0;
606
607TEST(logging, LOG_FATAL_ABORTER_MESSAGE) {
608 CountLineAborter::newline_count = 0;
609 android::base::SetAborter(CountLineAborter::CountLineAborterFunction);
610
611 android::base::ScopedLogSeverity sls(android::base::ERROR);
612 CapturedStderr cap;
613 LOG(FATAL) << "foo\nbar";
614
615 EXPECT_EQ(CountLineAborter::newline_count, 1U + 1U); // +1 for final '\n'.
616}
Yabin Cui0c689532017-01-23 10:29:23 -0800617
618__attribute__((constructor)) void TestLoggingInConstructor() {
619 LOG(ERROR) << "foobar";
620}
Andreas Gampe1923e762018-03-05 10:00:19 -0800621
622TEST(logging, SetDefaultTag) {
623 constexpr const char* expected_tag = "test_tag";
624 constexpr const char* expected_msg = "foobar";
625 CapturedStderr cap;
626 {
627 std::string old_default_tag = android::base::GetDefaultTag();
628 android::base::SetDefaultTag(expected_tag);
629 android::base::ScopedLogSeverity sls(android::base::LogSeverity::INFO);
630 LOG(INFO) << expected_msg;
631 android::base::SetDefaultTag(old_default_tag);
632 }
Christopher Ferriseea85c92018-08-30 13:31:45 -0700633 ASSERT_NO_FATAL_FAILURE(
634 CheckMessage(cap, android::base::LogSeverity::INFO, expected_msg, expected_tag));
Andreas Gampe1923e762018-03-05 10:00:19 -0800635}
Elliott Hughes1be0d142018-05-23 09:16:46 -0700636
637TEST(logging, StdioLogger) {
Christopher Ferriseea85c92018-08-30 13:31:45 -0700638 CapturedStderr cap_err;
639 CapturedStdout cap_out;
640 android::base::SetLogger(android::base::StdioLogger);
641 LOG(INFO) << "out";
642 LOG(ERROR) << "err";
643 cap_err.Stop();
644 cap_out.Stop();
Elliott Hughes1be0d142018-05-23 09:16:46 -0700645
646 // For INFO we expect just the literal "out\n".
Christopher Ferriseea85c92018-08-30 13:31:45 -0700647 ASSERT_EQ("out\n", cap_out.str());
Elliott Hughes1be0d142018-05-23 09:16:46 -0700648 // Whereas ERROR logging includes the program name.
Christopher Ferriseea85c92018-08-30 13:31:45 -0700649 ASSERT_EQ(android::base::Basename(android::base::GetExecutablePath()) + ": err\n", cap_err.str());
Elliott Hughes1be0d142018-05-23 09:16:46 -0700650}