1*8f0ba417SAndroid Build Coastguard Worker /*
2*8f0ba417SAndroid Build Coastguard Worker * Copyright (C) 2015 The Android Open Source Project
3*8f0ba417SAndroid Build Coastguard Worker *
4*8f0ba417SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*8f0ba417SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*8f0ba417SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*8f0ba417SAndroid Build Coastguard Worker *
8*8f0ba417SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*8f0ba417SAndroid Build Coastguard Worker *
10*8f0ba417SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*8f0ba417SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*8f0ba417SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8f0ba417SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*8f0ba417SAndroid Build Coastguard Worker * limitations under the License.
15*8f0ba417SAndroid Build Coastguard Worker */
16*8f0ba417SAndroid Build Coastguard Worker
17*8f0ba417SAndroid Build Coastguard Worker #include "android-base/logging.h"
18*8f0ba417SAndroid Build Coastguard Worker
19*8f0ba417SAndroid Build Coastguard Worker #include <libgen.h>
20*8f0ba417SAndroid Build Coastguard Worker
21*8f0ba417SAndroid Build Coastguard Worker #if defined(_WIN32)
22*8f0ba417SAndroid Build Coastguard Worker #include <signal.h>
23*8f0ba417SAndroid Build Coastguard Worker #endif
24*8f0ba417SAndroid Build Coastguard Worker
25*8f0ba417SAndroid Build Coastguard Worker #include <memory>
26*8f0ba417SAndroid Build Coastguard Worker #include <regex>
27*8f0ba417SAndroid Build Coastguard Worker #include <string>
28*8f0ba417SAndroid Build Coastguard Worker #include <thread>
29*8f0ba417SAndroid Build Coastguard Worker
30*8f0ba417SAndroid Build Coastguard Worker #include "android-base/file.h"
31*8f0ba417SAndroid Build Coastguard Worker #include "android-base/scopeguard.h"
32*8f0ba417SAndroid Build Coastguard Worker #include "android-base/stringprintf.h"
33*8f0ba417SAndroid Build Coastguard Worker #include "android-base/test_utils.h"
34*8f0ba417SAndroid Build Coastguard Worker
35*8f0ba417SAndroid Build Coastguard Worker #include <gtest/gtest.h>
36*8f0ba417SAndroid Build Coastguard Worker
37*8f0ba417SAndroid Build Coastguard Worker #ifdef __ANDROID__
38*8f0ba417SAndroid Build Coastguard Worker #define HOST_TEST(suite, name) TEST(suite, DISABLED_ ## name)
39*8f0ba417SAndroid Build Coastguard Worker #else
40*8f0ba417SAndroid Build Coastguard Worker #define HOST_TEST(suite, name) TEST(suite, name)
41*8f0ba417SAndroid Build Coastguard Worker #endif
42*8f0ba417SAndroid Build Coastguard Worker
43*8f0ba417SAndroid Build Coastguard Worker #if defined(_WIN32)
ExitSignalAbortHandler(int)44*8f0ba417SAndroid Build Coastguard Worker static void ExitSignalAbortHandler(int) {
45*8f0ba417SAndroid Build Coastguard Worker _exit(3);
46*8f0ba417SAndroid Build Coastguard Worker }
47*8f0ba417SAndroid Build Coastguard Worker #endif
48*8f0ba417SAndroid Build Coastguard Worker
SuppressAbortUI()49*8f0ba417SAndroid Build Coastguard Worker static void SuppressAbortUI() {
50*8f0ba417SAndroid Build Coastguard Worker #if defined(_WIN32)
51*8f0ba417SAndroid Build Coastguard Worker // We really just want to call _set_abort_behavior(0, _CALL_REPORTFAULT) to
52*8f0ba417SAndroid Build Coastguard Worker // suppress the Windows Error Reporting dialog box, but that API is not
53*8f0ba417SAndroid Build Coastguard Worker // available in the OS-supplied C Runtime, msvcrt.dll, that we currently
54*8f0ba417SAndroid Build Coastguard Worker // use (it is available in the Visual Studio C runtime).
55*8f0ba417SAndroid Build Coastguard Worker //
56*8f0ba417SAndroid Build Coastguard Worker // Instead, we setup a SIGABRT handler, which is called in abort() right
57*8f0ba417SAndroid Build Coastguard Worker // before calling Windows Error Reporting. In the handler, we exit the
58*8f0ba417SAndroid Build Coastguard Worker // process just like abort() does.
59*8f0ba417SAndroid Build Coastguard Worker ASSERT_NE(SIG_ERR, signal(SIGABRT, ExitSignalAbortHandler));
60*8f0ba417SAndroid Build Coastguard Worker #endif
61*8f0ba417SAndroid Build Coastguard Worker }
62*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,CHECK)63*8f0ba417SAndroid Build Coastguard Worker TEST(logging, CHECK) {
64*8f0ba417SAndroid Build Coastguard Worker ASSERT_DEATH({SuppressAbortUI(); CHECK(false);}, "Check failed: false ");
65*8f0ba417SAndroid Build Coastguard Worker CHECK(true);
66*8f0ba417SAndroid Build Coastguard Worker
67*8f0ba417SAndroid Build Coastguard Worker ASSERT_DEATH({SuppressAbortUI(); CHECK_EQ(0, 1);}, "Check failed: 0 == 1 ");
68*8f0ba417SAndroid Build Coastguard Worker CHECK_EQ(0, 0);
69*8f0ba417SAndroid Build Coastguard Worker
70*8f0ba417SAndroid Build Coastguard Worker std::unique_ptr<int> p;
71*8f0ba417SAndroid Build Coastguard Worker ASSERT_DEATH(CHECK_NE(p, nullptr), "Check failed");
72*8f0ba417SAndroid Build Coastguard Worker CHECK_EQ(p, nullptr);
73*8f0ba417SAndroid Build Coastguard Worker CHECK_EQ(p, p);
74*8f0ba417SAndroid Build Coastguard Worker
75*8f0ba417SAndroid Build Coastguard Worker const char* kText = "Some text";
76*8f0ba417SAndroid Build Coastguard Worker ASSERT_DEATH(CHECK_NE(kText, kText), "Check failed");
77*8f0ba417SAndroid Build Coastguard Worker ASSERT_DEATH(CHECK_EQ(kText, nullptr), "Check failed.*null");
78*8f0ba417SAndroid Build Coastguard Worker CHECK_EQ(kText, kText);
79*8f0ba417SAndroid Build Coastguard Worker
80*8f0ba417SAndroid Build Coastguard Worker ASSERT_DEATH({SuppressAbortUI(); CHECK_STREQ("foo", "bar");},
81*8f0ba417SAndroid Build Coastguard Worker R"(Check failed: "foo" == "bar")");
82*8f0ba417SAndroid Build Coastguard Worker CHECK_STREQ("foo", "foo");
83*8f0ba417SAndroid Build Coastguard Worker
84*8f0ba417SAndroid Build Coastguard Worker // Test whether CHECK() and CHECK_STREQ() have a dangling if with no else.
85*8f0ba417SAndroid Build Coastguard Worker bool flag = false;
86*8f0ba417SAndroid Build Coastguard Worker if (true)
87*8f0ba417SAndroid Build Coastguard Worker CHECK(true);
88*8f0ba417SAndroid Build Coastguard Worker else
89*8f0ba417SAndroid Build Coastguard Worker flag = true;
90*8f0ba417SAndroid Build Coastguard Worker EXPECT_FALSE(flag) << "CHECK macro probably has a dangling if with no else";
91*8f0ba417SAndroid Build Coastguard Worker
92*8f0ba417SAndroid Build Coastguard Worker flag = false;
93*8f0ba417SAndroid Build Coastguard Worker if (true)
94*8f0ba417SAndroid Build Coastguard Worker CHECK_STREQ("foo", "foo");
95*8f0ba417SAndroid Build Coastguard Worker else
96*8f0ba417SAndroid Build Coastguard Worker flag = true;
97*8f0ba417SAndroid Build Coastguard Worker EXPECT_FALSE(flag) << "CHECK_STREQ probably has a dangling if with no else";
98*8f0ba417SAndroid Build Coastguard Worker }
99*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,DCHECK)100*8f0ba417SAndroid Build Coastguard Worker TEST(logging, DCHECK) {
101*8f0ba417SAndroid Build Coastguard Worker if (android::base::kEnableDChecks) {
102*8f0ba417SAndroid Build Coastguard Worker ASSERT_DEATH({SuppressAbortUI(); DCHECK(false);}, "Check failed: false ");
103*8f0ba417SAndroid Build Coastguard Worker }
104*8f0ba417SAndroid Build Coastguard Worker DCHECK(true);
105*8f0ba417SAndroid Build Coastguard Worker
106*8f0ba417SAndroid Build Coastguard Worker if (android::base::kEnableDChecks) {
107*8f0ba417SAndroid Build Coastguard Worker ASSERT_DEATH({SuppressAbortUI(); DCHECK_EQ(0, 1);}, "Check failed: 0 == 1 ");
108*8f0ba417SAndroid Build Coastguard Worker }
109*8f0ba417SAndroid Build Coastguard Worker DCHECK_EQ(0, 0);
110*8f0ba417SAndroid Build Coastguard Worker
111*8f0ba417SAndroid Build Coastguard Worker std::unique_ptr<int> p;
112*8f0ba417SAndroid Build Coastguard Worker if (android::base::kEnableDChecks) {
113*8f0ba417SAndroid Build Coastguard Worker ASSERT_DEATH(DCHECK_NE(p, nullptr), "Check failed");
114*8f0ba417SAndroid Build Coastguard Worker }
115*8f0ba417SAndroid Build Coastguard Worker DCHECK_EQ(p, nullptr);
116*8f0ba417SAndroid Build Coastguard Worker DCHECK_EQ(p, p);
117*8f0ba417SAndroid Build Coastguard Worker
118*8f0ba417SAndroid Build Coastguard Worker if (android::base::kEnableDChecks) {
119*8f0ba417SAndroid Build Coastguard Worker ASSERT_DEATH({SuppressAbortUI(); DCHECK_STREQ("foo", "bar");},
120*8f0ba417SAndroid Build Coastguard Worker R"(Check failed: "foo" == "bar")");
121*8f0ba417SAndroid Build Coastguard Worker }
122*8f0ba417SAndroid Build Coastguard Worker DCHECK_STREQ("foo", "foo");
123*8f0ba417SAndroid Build Coastguard Worker
124*8f0ba417SAndroid Build Coastguard Worker // No testing whether we have a dangling else, possibly. That's inherent to the if (constexpr)
125*8f0ba417SAndroid Build Coastguard Worker // setup we intentionally chose to force type-checks of debug code even in release builds (so
126*8f0ba417SAndroid Build Coastguard Worker // we don't get more bit-rot).
127*8f0ba417SAndroid Build Coastguard Worker }
128*8f0ba417SAndroid Build Coastguard Worker
129*8f0ba417SAndroid Build Coastguard Worker
130*8f0ba417SAndroid Build Coastguard Worker #define CHECK_WOULD_LOG_DISABLED(severity) \
131*8f0ba417SAndroid Build Coastguard Worker static_assert(android::base::severity < android::base::FATAL, "Bad input"); \
132*8f0ba417SAndroid Build Coastguard Worker for (size_t i = static_cast<size_t>(android::base::severity) + 1; \
133*8f0ba417SAndroid Build Coastguard Worker i <= static_cast<size_t>(android::base::FATAL); \
134*8f0ba417SAndroid Build Coastguard Worker ++i) { \
135*8f0ba417SAndroid Build Coastguard Worker { \
136*8f0ba417SAndroid Build Coastguard Worker android::base::ScopedLogSeverity sls2(static_cast<android::base::LogSeverity>(i)); \
137*8f0ba417SAndroid Build Coastguard Worker EXPECT_FALSE(WOULD_LOG(severity)) << i; \
138*8f0ba417SAndroid Build Coastguard Worker } \
139*8f0ba417SAndroid Build Coastguard Worker { \
140*8f0ba417SAndroid Build Coastguard Worker android::base::ScopedLogSeverity sls2(static_cast<android::base::LogSeverity>(i)); \
141*8f0ba417SAndroid Build Coastguard Worker EXPECT_FALSE(WOULD_LOG(::android::base::severity)) << i; \
142*8f0ba417SAndroid Build Coastguard Worker } \
143*8f0ba417SAndroid Build Coastguard Worker } \
144*8f0ba417SAndroid Build Coastguard Worker
145*8f0ba417SAndroid Build Coastguard Worker #define CHECK_WOULD_LOG_ENABLED(severity) \
146*8f0ba417SAndroid Build Coastguard Worker for (size_t i = static_cast<size_t>(android::base::VERBOSE); \
147*8f0ba417SAndroid Build Coastguard Worker i <= static_cast<size_t>(android::base::severity); \
148*8f0ba417SAndroid Build Coastguard Worker ++i) { \
149*8f0ba417SAndroid Build Coastguard Worker { \
150*8f0ba417SAndroid Build Coastguard Worker android::base::ScopedLogSeverity sls2(static_cast<android::base::LogSeverity>(i)); \
151*8f0ba417SAndroid Build Coastguard Worker EXPECT_TRUE(WOULD_LOG(severity)) << i; \
152*8f0ba417SAndroid Build Coastguard Worker } \
153*8f0ba417SAndroid Build Coastguard Worker { \
154*8f0ba417SAndroid Build Coastguard Worker android::base::ScopedLogSeverity sls2(static_cast<android::base::LogSeverity>(i)); \
155*8f0ba417SAndroid Build Coastguard Worker EXPECT_TRUE(WOULD_LOG(::android::base::severity)) << i; \
156*8f0ba417SAndroid Build Coastguard Worker } \
157*8f0ba417SAndroid Build Coastguard Worker } \
158*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,WOULD_LOG_FATAL)159*8f0ba417SAndroid Build Coastguard Worker TEST(logging, WOULD_LOG_FATAL) {
160*8f0ba417SAndroid Build Coastguard Worker CHECK_WOULD_LOG_ENABLED(FATAL);
161*8f0ba417SAndroid Build Coastguard Worker }
162*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,WOULD_LOG_FATAL_WITHOUT_ABORT_enabled)163*8f0ba417SAndroid Build Coastguard Worker TEST(logging, WOULD_LOG_FATAL_WITHOUT_ABORT_enabled) {
164*8f0ba417SAndroid Build Coastguard Worker CHECK_WOULD_LOG_ENABLED(FATAL_WITHOUT_ABORT);
165*8f0ba417SAndroid Build Coastguard Worker }
166*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,WOULD_LOG_ERROR_disabled)167*8f0ba417SAndroid Build Coastguard Worker TEST(logging, WOULD_LOG_ERROR_disabled) {
168*8f0ba417SAndroid Build Coastguard Worker CHECK_WOULD_LOG_DISABLED(ERROR);
169*8f0ba417SAndroid Build Coastguard Worker }
170*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,WOULD_LOG_ERROR_enabled)171*8f0ba417SAndroid Build Coastguard Worker TEST(logging, WOULD_LOG_ERROR_enabled) {
172*8f0ba417SAndroid Build Coastguard Worker CHECK_WOULD_LOG_ENABLED(ERROR);
173*8f0ba417SAndroid Build Coastguard Worker }
174*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,WOULD_LOG_WARNING_disabled)175*8f0ba417SAndroid Build Coastguard Worker TEST(logging, WOULD_LOG_WARNING_disabled) {
176*8f0ba417SAndroid Build Coastguard Worker CHECK_WOULD_LOG_DISABLED(WARNING);
177*8f0ba417SAndroid Build Coastguard Worker }
178*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,WOULD_LOG_WARNING_enabled)179*8f0ba417SAndroid Build Coastguard Worker TEST(logging, WOULD_LOG_WARNING_enabled) {
180*8f0ba417SAndroid Build Coastguard Worker CHECK_WOULD_LOG_ENABLED(WARNING);
181*8f0ba417SAndroid Build Coastguard Worker }
182*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,WOULD_LOG_INFO_disabled)183*8f0ba417SAndroid Build Coastguard Worker TEST(logging, WOULD_LOG_INFO_disabled) {
184*8f0ba417SAndroid Build Coastguard Worker CHECK_WOULD_LOG_DISABLED(INFO);
185*8f0ba417SAndroid Build Coastguard Worker }
186*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,WOULD_LOG_INFO_enabled)187*8f0ba417SAndroid Build Coastguard Worker TEST(logging, WOULD_LOG_INFO_enabled) {
188*8f0ba417SAndroid Build Coastguard Worker CHECK_WOULD_LOG_ENABLED(INFO);
189*8f0ba417SAndroid Build Coastguard Worker }
190*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,WOULD_LOG_DEBUG_disabled)191*8f0ba417SAndroid Build Coastguard Worker TEST(logging, WOULD_LOG_DEBUG_disabled) {
192*8f0ba417SAndroid Build Coastguard Worker CHECK_WOULD_LOG_DISABLED(DEBUG);
193*8f0ba417SAndroid Build Coastguard Worker }
194*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,WOULD_LOG_DEBUG_enabled)195*8f0ba417SAndroid Build Coastguard Worker TEST(logging, WOULD_LOG_DEBUG_enabled) {
196*8f0ba417SAndroid Build Coastguard Worker CHECK_WOULD_LOG_ENABLED(DEBUG);
197*8f0ba417SAndroid Build Coastguard Worker }
198*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,WOULD_LOG_VERBOSE_disabled)199*8f0ba417SAndroid Build Coastguard Worker TEST(logging, WOULD_LOG_VERBOSE_disabled) {
200*8f0ba417SAndroid Build Coastguard Worker CHECK_WOULD_LOG_DISABLED(VERBOSE);
201*8f0ba417SAndroid Build Coastguard Worker }
202*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,WOULD_LOG_VERBOSE_enabled)203*8f0ba417SAndroid Build Coastguard Worker TEST(logging, WOULD_LOG_VERBOSE_enabled) {
204*8f0ba417SAndroid Build Coastguard Worker CHECK_WOULD_LOG_ENABLED(VERBOSE);
205*8f0ba417SAndroid Build Coastguard Worker }
206*8f0ba417SAndroid Build Coastguard Worker
207*8f0ba417SAndroid Build Coastguard Worker #undef CHECK_WOULD_LOG_DISABLED
208*8f0ba417SAndroid Build Coastguard Worker #undef CHECK_WOULD_LOG_ENABLED
209*8f0ba417SAndroid Build Coastguard Worker
210*8f0ba417SAndroid Build Coastguard Worker
211*8f0ba417SAndroid Build Coastguard Worker #if !defined(_WIN32)
make_log_pattern(const char * expected_tag,android::base::LogSeverity severity,const char * message)212*8f0ba417SAndroid Build Coastguard Worker static std::string make_log_pattern(const char* expected_tag, android::base::LogSeverity severity,
213*8f0ba417SAndroid Build Coastguard Worker const char* message) {
214*8f0ba417SAndroid Build Coastguard Worker static const char log_characters[] = "VDIWEFF";
215*8f0ba417SAndroid Build Coastguard Worker static_assert(arraysize(log_characters) - 1 == android::base::FATAL + 1,
216*8f0ba417SAndroid Build Coastguard Worker "Mismatch in size of log_characters and values in LogSeverity");
217*8f0ba417SAndroid Build Coastguard Worker char log_char = log_characters[severity];
218*8f0ba417SAndroid Build Coastguard Worker std::string holder(__FILE__);
219*8f0ba417SAndroid Build Coastguard Worker
220*8f0ba417SAndroid Build Coastguard Worker // `message` can have a function name like "TestBody()". The parentheses should be escaped,
221*8f0ba417SAndroid Build Coastguard Worker // otherwise it will be interpreted as a capturing group when it is used as a regex. Below
222*8f0ba417SAndroid Build Coastguard Worker // replaces either '(' or ')' to '\(' or '\)', respectively.
223*8f0ba417SAndroid Build Coastguard Worker std::regex parentheses(R"(\(|\))");
224*8f0ba417SAndroid Build Coastguard Worker std::string message_escaped = std::regex_replace(message, parentheses, R"(\$&)");
225*8f0ba417SAndroid Build Coastguard Worker
226*8f0ba417SAndroid Build Coastguard Worker const char* tag_pattern = expected_tag != nullptr ? expected_tag : ".+";
227*8f0ba417SAndroid Build Coastguard Worker return android::base::StringPrintf(
228*8f0ba417SAndroid Build Coastguard Worker R"(\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3} \s*\d+ \s*\d+ %c %s\s*: %s:\d+ %s)", log_char,
229*8f0ba417SAndroid Build Coastguard Worker tag_pattern, basename(&holder[0]), message_escaped.c_str());
230*8f0ba417SAndroid Build Coastguard Worker }
231*8f0ba417SAndroid Build Coastguard Worker #endif
232*8f0ba417SAndroid Build Coastguard Worker
CheckMessage(const std::string & output,android::base::LogSeverity severity,const char * expected,const char * expected_tag=nullptr)233*8f0ba417SAndroid Build Coastguard Worker static void CheckMessage(const std::string& output, android::base::LogSeverity severity,
234*8f0ba417SAndroid Build Coastguard Worker const char* expected, const char* expected_tag = nullptr) {
235*8f0ba417SAndroid Build Coastguard Worker // We can't usefully check the output of any of these on Windows because we
236*8f0ba417SAndroid Build Coastguard Worker // don't have std::regex, but we can at least make sure we printed at least as
237*8f0ba417SAndroid Build Coastguard Worker // many characters are in the log message.
238*8f0ba417SAndroid Build Coastguard Worker ASSERT_GT(output.length(), strlen(expected));
239*8f0ba417SAndroid Build Coastguard Worker ASSERT_NE(nullptr, strstr(output.c_str(), expected)) << output;
240*8f0ba417SAndroid Build Coastguard Worker if (expected_tag != nullptr) {
241*8f0ba417SAndroid Build Coastguard Worker ASSERT_NE(nullptr, strstr(output.c_str(), expected_tag)) << output;
242*8f0ba417SAndroid Build Coastguard Worker }
243*8f0ba417SAndroid Build Coastguard Worker
244*8f0ba417SAndroid Build Coastguard Worker #if !defined(_WIN32)
245*8f0ba417SAndroid Build Coastguard Worker std::string regex_str = make_log_pattern(expected_tag, severity, expected);
246*8f0ba417SAndroid Build Coastguard Worker std::regex message_regex(regex_str);
247*8f0ba417SAndroid Build Coastguard Worker ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
248*8f0ba417SAndroid Build Coastguard Worker #endif
249*8f0ba417SAndroid Build Coastguard Worker }
250*8f0ba417SAndroid Build Coastguard Worker
CheckMessage(CapturedStderr & cap,android::base::LogSeverity severity,const char * expected,const char * expected_tag=nullptr)251*8f0ba417SAndroid Build Coastguard Worker static void CheckMessage(CapturedStderr& cap, android::base::LogSeverity severity,
252*8f0ba417SAndroid Build Coastguard Worker const char* expected, const char* expected_tag = nullptr) {
253*8f0ba417SAndroid Build Coastguard Worker cap.Stop();
254*8f0ba417SAndroid Build Coastguard Worker std::string output = cap.str();
255*8f0ba417SAndroid Build Coastguard Worker return CheckMessage(output, severity, expected, expected_tag);
256*8f0ba417SAndroid Build Coastguard Worker }
257*8f0ba417SAndroid Build Coastguard Worker
258*8f0ba417SAndroid Build Coastguard Worker #define CHECK_LOG_STREAM_DISABLED(severity) \
259*8f0ba417SAndroid Build Coastguard Worker { \
260*8f0ba417SAndroid Build Coastguard Worker android::base::ScopedLogSeverity sls1(android::base::FATAL); \
261*8f0ba417SAndroid Build Coastguard Worker CapturedStderr cap1; \
262*8f0ba417SAndroid Build Coastguard Worker LOG_STREAM(severity) << "foo bar"; \
263*8f0ba417SAndroid Build Coastguard Worker cap1.Stop(); \
264*8f0ba417SAndroid Build Coastguard Worker ASSERT_EQ("", cap1.str()); \
265*8f0ba417SAndroid Build Coastguard Worker } \
266*8f0ba417SAndroid Build Coastguard Worker { \
267*8f0ba417SAndroid Build Coastguard Worker android::base::ScopedLogSeverity sls1(android::base::FATAL); \
268*8f0ba417SAndroid Build Coastguard Worker CapturedStderr cap1; \
269*8f0ba417SAndroid Build Coastguard Worker LOG_STREAM(::android::base::severity) << "foo bar"; \
270*8f0ba417SAndroid Build Coastguard Worker cap1.Stop(); \
271*8f0ba417SAndroid Build Coastguard Worker ASSERT_EQ("", cap1.str()); \
272*8f0ba417SAndroid Build Coastguard Worker }
273*8f0ba417SAndroid Build Coastguard Worker
274*8f0ba417SAndroid Build Coastguard Worker #define CHECK_LOG_STREAM_ENABLED(severity) \
275*8f0ba417SAndroid Build Coastguard Worker { \
276*8f0ba417SAndroid Build Coastguard Worker android::base::ScopedLogSeverity sls2(android::base::severity); \
277*8f0ba417SAndroid Build Coastguard Worker CapturedStderr cap2; \
278*8f0ba417SAndroid Build Coastguard Worker LOG_STREAM(severity) << "foobar"; \
279*8f0ba417SAndroid Build Coastguard Worker CheckMessage(cap2, android::base::severity, "foobar"); \
280*8f0ba417SAndroid Build Coastguard Worker } \
281*8f0ba417SAndroid Build Coastguard Worker { \
282*8f0ba417SAndroid Build Coastguard Worker android::base::ScopedLogSeverity sls2(android::base::severity); \
283*8f0ba417SAndroid Build Coastguard Worker CapturedStderr cap2; \
284*8f0ba417SAndroid Build Coastguard Worker LOG_STREAM(::android::base::severity) << "foobar"; \
285*8f0ba417SAndroid Build Coastguard Worker CheckMessage(cap2, android::base::severity, "foobar"); \
286*8f0ba417SAndroid Build Coastguard Worker } \
287*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,LOG_STREAM_FATAL_WITHOUT_ABORT_enabled)288*8f0ba417SAndroid Build Coastguard Worker TEST(logging, LOG_STREAM_FATAL_WITHOUT_ABORT_enabled) {
289*8f0ba417SAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(CHECK_LOG_STREAM_ENABLED(FATAL_WITHOUT_ABORT));
290*8f0ba417SAndroid Build Coastguard Worker }
291*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,LOG_STREAM_ERROR_disabled)292*8f0ba417SAndroid Build Coastguard Worker TEST(logging, LOG_STREAM_ERROR_disabled) {
293*8f0ba417SAndroid Build Coastguard Worker CHECK_LOG_STREAM_DISABLED(ERROR);
294*8f0ba417SAndroid Build Coastguard Worker }
295*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,LOG_STREAM_ERROR_enabled)296*8f0ba417SAndroid Build Coastguard Worker TEST(logging, LOG_STREAM_ERROR_enabled) {
297*8f0ba417SAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(CHECK_LOG_STREAM_ENABLED(ERROR));
298*8f0ba417SAndroid Build Coastguard Worker }
299*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,LOG_STREAM_WARNING_disabled)300*8f0ba417SAndroid Build Coastguard Worker TEST(logging, LOG_STREAM_WARNING_disabled) {
301*8f0ba417SAndroid Build Coastguard Worker CHECK_LOG_STREAM_DISABLED(WARNING);
302*8f0ba417SAndroid Build Coastguard Worker }
303*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,LOG_STREAM_WARNING_enabled)304*8f0ba417SAndroid Build Coastguard Worker TEST(logging, LOG_STREAM_WARNING_enabled) {
305*8f0ba417SAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(CHECK_LOG_STREAM_ENABLED(WARNING));
306*8f0ba417SAndroid Build Coastguard Worker }
307*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,LOG_STREAM_INFO_disabled)308*8f0ba417SAndroid Build Coastguard Worker TEST(logging, LOG_STREAM_INFO_disabled) {
309*8f0ba417SAndroid Build Coastguard Worker CHECK_LOG_STREAM_DISABLED(INFO);
310*8f0ba417SAndroid Build Coastguard Worker }
311*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,LOG_STREAM_INFO_enabled)312*8f0ba417SAndroid Build Coastguard Worker TEST(logging, LOG_STREAM_INFO_enabled) {
313*8f0ba417SAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(CHECK_LOG_STREAM_ENABLED(INFO));
314*8f0ba417SAndroid Build Coastguard Worker }
315*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,LOG_STREAM_DEBUG_disabled)316*8f0ba417SAndroid Build Coastguard Worker TEST(logging, LOG_STREAM_DEBUG_disabled) {
317*8f0ba417SAndroid Build Coastguard Worker CHECK_LOG_STREAM_DISABLED(DEBUG);
318*8f0ba417SAndroid Build Coastguard Worker }
319*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,LOG_STREAM_DEBUG_enabled)320*8f0ba417SAndroid Build Coastguard Worker TEST(logging, LOG_STREAM_DEBUG_enabled) {
321*8f0ba417SAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(CHECK_LOG_STREAM_ENABLED(DEBUG));
322*8f0ba417SAndroid Build Coastguard Worker }
323*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,LOG_STREAM_VERBOSE_disabled)324*8f0ba417SAndroid Build Coastguard Worker TEST(logging, LOG_STREAM_VERBOSE_disabled) {
325*8f0ba417SAndroid Build Coastguard Worker CHECK_LOG_STREAM_DISABLED(VERBOSE);
326*8f0ba417SAndroid Build Coastguard Worker }
327*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,LOG_STREAM_VERBOSE_enabled)328*8f0ba417SAndroid Build Coastguard Worker TEST(logging, LOG_STREAM_VERBOSE_enabled) {
329*8f0ba417SAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(CHECK_LOG_STREAM_ENABLED(VERBOSE));
330*8f0ba417SAndroid Build Coastguard Worker }
331*8f0ba417SAndroid Build Coastguard Worker
332*8f0ba417SAndroid Build Coastguard Worker #undef CHECK_LOG_STREAM_DISABLED
333*8f0ba417SAndroid Build Coastguard Worker #undef CHECK_LOG_STREAM_ENABLED
334*8f0ba417SAndroid Build Coastguard Worker
335*8f0ba417SAndroid Build Coastguard Worker #define CHECK_LOG_DISABLED(severity) \
336*8f0ba417SAndroid Build Coastguard Worker { \
337*8f0ba417SAndroid Build Coastguard Worker android::base::ScopedLogSeverity sls1(android::base::FATAL); \
338*8f0ba417SAndroid Build Coastguard Worker CapturedStderr cap1; \
339*8f0ba417SAndroid Build Coastguard Worker LOG(severity) << "foo bar"; \
340*8f0ba417SAndroid Build Coastguard Worker cap1.Stop(); \
341*8f0ba417SAndroid Build Coastguard Worker ASSERT_EQ("", cap1.str()); \
342*8f0ba417SAndroid Build Coastguard Worker } \
343*8f0ba417SAndroid Build Coastguard Worker { \
344*8f0ba417SAndroid Build Coastguard Worker android::base::ScopedLogSeverity sls1(android::base::FATAL); \
345*8f0ba417SAndroid Build Coastguard Worker CapturedStderr cap1; \
346*8f0ba417SAndroid Build Coastguard Worker LOG(::android::base::severity) << "foo bar"; \
347*8f0ba417SAndroid Build Coastguard Worker cap1.Stop(); \
348*8f0ba417SAndroid Build Coastguard Worker ASSERT_EQ("", cap1.str()); \
349*8f0ba417SAndroid Build Coastguard Worker }
350*8f0ba417SAndroid Build Coastguard Worker
351*8f0ba417SAndroid Build Coastguard Worker #define CHECK_LOG_ENABLED(severity) \
352*8f0ba417SAndroid Build Coastguard Worker { \
353*8f0ba417SAndroid Build Coastguard Worker android::base::ScopedLogSeverity sls2(android::base::severity); \
354*8f0ba417SAndroid Build Coastguard Worker CapturedStderr cap2; \
355*8f0ba417SAndroid Build Coastguard Worker LOG(severity) << "foobar"; \
356*8f0ba417SAndroid Build Coastguard Worker CheckMessage(cap2, android::base::severity, "foobar"); \
357*8f0ba417SAndroid Build Coastguard Worker } \
358*8f0ba417SAndroid Build Coastguard Worker { \
359*8f0ba417SAndroid Build Coastguard Worker android::base::ScopedLogSeverity sls2(android::base::severity); \
360*8f0ba417SAndroid Build Coastguard Worker CapturedStderr cap2; \
361*8f0ba417SAndroid Build Coastguard Worker LOG(::android::base::severity) << "foobar"; \
362*8f0ba417SAndroid Build Coastguard Worker CheckMessage(cap2, android::base::severity, "foobar"); \
363*8f0ba417SAndroid Build Coastguard Worker } \
364*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,LOG_FATAL)365*8f0ba417SAndroid Build Coastguard Worker TEST(logging, LOG_FATAL) {
366*8f0ba417SAndroid Build Coastguard Worker ASSERT_DEATH({SuppressAbortUI(); LOG(FATAL) << "foobar";}, "foobar");
367*8f0ba417SAndroid Build Coastguard Worker ASSERT_DEATH({SuppressAbortUI(); LOG(::android::base::FATAL) << "foobar";}, "foobar");
368*8f0ba417SAndroid Build Coastguard Worker }
369*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,LOG_FATAL_WITHOUT_ABORT_enabled)370*8f0ba417SAndroid Build Coastguard Worker TEST(logging, LOG_FATAL_WITHOUT_ABORT_enabled) {
371*8f0ba417SAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(CHECK_LOG_ENABLED(FATAL_WITHOUT_ABORT));
372*8f0ba417SAndroid Build Coastguard Worker }
373*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,LOG_ERROR_disabled)374*8f0ba417SAndroid Build Coastguard Worker TEST(logging, LOG_ERROR_disabled) {
375*8f0ba417SAndroid Build Coastguard Worker CHECK_LOG_DISABLED(ERROR);
376*8f0ba417SAndroid Build Coastguard Worker }
377*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,LOG_ERROR_enabled)378*8f0ba417SAndroid Build Coastguard Worker TEST(logging, LOG_ERROR_enabled) {
379*8f0ba417SAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(CHECK_LOG_ENABLED(ERROR));
380*8f0ba417SAndroid Build Coastguard Worker }
381*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,LOG_WARNING_disabled)382*8f0ba417SAndroid Build Coastguard Worker TEST(logging, LOG_WARNING_disabled) {
383*8f0ba417SAndroid Build Coastguard Worker CHECK_LOG_DISABLED(WARNING);
384*8f0ba417SAndroid Build Coastguard Worker }
385*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,LOG_WARNING_enabled)386*8f0ba417SAndroid Build Coastguard Worker TEST(logging, LOG_WARNING_enabled) {
387*8f0ba417SAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(CHECK_LOG_ENABLED(WARNING));
388*8f0ba417SAndroid Build Coastguard Worker }
389*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,LOG_INFO_disabled)390*8f0ba417SAndroid Build Coastguard Worker TEST(logging, LOG_INFO_disabled) {
391*8f0ba417SAndroid Build Coastguard Worker CHECK_LOG_DISABLED(INFO);
392*8f0ba417SAndroid Build Coastguard Worker }
393*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,LOG_INFO_enabled)394*8f0ba417SAndroid Build Coastguard Worker TEST(logging, LOG_INFO_enabled) {
395*8f0ba417SAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(CHECK_LOG_ENABLED(INFO));
396*8f0ba417SAndroid Build Coastguard Worker }
397*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,LOG_DEBUG_disabled)398*8f0ba417SAndroid Build Coastguard Worker TEST(logging, LOG_DEBUG_disabled) {
399*8f0ba417SAndroid Build Coastguard Worker CHECK_LOG_DISABLED(DEBUG);
400*8f0ba417SAndroid Build Coastguard Worker }
401*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,LOG_DEBUG_enabled)402*8f0ba417SAndroid Build Coastguard Worker TEST(logging, LOG_DEBUG_enabled) {
403*8f0ba417SAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(CHECK_LOG_ENABLED(DEBUG));
404*8f0ba417SAndroid Build Coastguard Worker }
405*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,LOG_VERBOSE_disabled)406*8f0ba417SAndroid Build Coastguard Worker TEST(logging, LOG_VERBOSE_disabled) {
407*8f0ba417SAndroid Build Coastguard Worker CHECK_LOG_DISABLED(VERBOSE);
408*8f0ba417SAndroid Build Coastguard Worker }
409*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,LOG_VERBOSE_enabled)410*8f0ba417SAndroid Build Coastguard Worker TEST(logging, LOG_VERBOSE_enabled) {
411*8f0ba417SAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(CHECK_LOG_ENABLED(VERBOSE));
412*8f0ba417SAndroid Build Coastguard Worker }
413*8f0ba417SAndroid Build Coastguard Worker
414*8f0ba417SAndroid Build Coastguard Worker #undef CHECK_LOG_DISABLED
415*8f0ba417SAndroid Build Coastguard Worker #undef CHECK_LOG_ENABLED
416*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,LOG_complex_param)417*8f0ba417SAndroid Build Coastguard Worker TEST(logging, LOG_complex_param) {
418*8f0ba417SAndroid Build Coastguard Worker #define CHECK_LOG_COMBINATION(use_scoped_log_severity_info, use_logging_severity_info) \
419*8f0ba417SAndroid Build Coastguard Worker { \
420*8f0ba417SAndroid Build Coastguard Worker android::base::ScopedLogSeverity sls( \
421*8f0ba417SAndroid Build Coastguard Worker (use_scoped_log_severity_info) ? ::android::base::INFO : ::android::base::WARNING); \
422*8f0ba417SAndroid Build Coastguard Worker CapturedStderr cap; \
423*8f0ba417SAndroid Build Coastguard Worker LOG((use_logging_severity_info) ? ::android::base::INFO : ::android::base::WARNING) \
424*8f0ba417SAndroid Build Coastguard Worker << "foobar"; \
425*8f0ba417SAndroid Build Coastguard Worker if ((use_scoped_log_severity_info) || !(use_logging_severity_info)) { \
426*8f0ba417SAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(CheckMessage( \
427*8f0ba417SAndroid Build Coastguard Worker cap, (use_logging_severity_info) ? ::android::base::INFO : ::android::base::WARNING, \
428*8f0ba417SAndroid Build Coastguard Worker "foobar")); \
429*8f0ba417SAndroid Build Coastguard Worker } else { \
430*8f0ba417SAndroid Build Coastguard Worker cap.Stop(); \
431*8f0ba417SAndroid Build Coastguard Worker ASSERT_EQ("", cap.str()); \
432*8f0ba417SAndroid Build Coastguard Worker } \
433*8f0ba417SAndroid Build Coastguard Worker }
434*8f0ba417SAndroid Build Coastguard Worker
435*8f0ba417SAndroid Build Coastguard Worker CHECK_LOG_COMBINATION(false,false);
436*8f0ba417SAndroid Build Coastguard Worker CHECK_LOG_COMBINATION(false,true);
437*8f0ba417SAndroid Build Coastguard Worker CHECK_LOG_COMBINATION(true,false);
438*8f0ba417SAndroid Build Coastguard Worker CHECK_LOG_COMBINATION(true,true);
439*8f0ba417SAndroid Build Coastguard Worker
440*8f0ba417SAndroid Build Coastguard Worker #undef CHECK_LOG_COMBINATION
441*8f0ba417SAndroid Build Coastguard Worker }
442*8f0ba417SAndroid Build Coastguard Worker
443*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,LOG_does_not_clobber_errno)444*8f0ba417SAndroid Build Coastguard Worker TEST(logging, LOG_does_not_clobber_errno) {
445*8f0ba417SAndroid Build Coastguard Worker CapturedStderr cap;
446*8f0ba417SAndroid Build Coastguard Worker errno = 12345;
447*8f0ba417SAndroid Build Coastguard Worker LOG(INFO) << (errno = 67890);
448*8f0ba417SAndroid Build Coastguard Worker EXPECT_EQ(12345, errno) << "errno was not restored";
449*8f0ba417SAndroid Build Coastguard Worker
450*8f0ba417SAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(CheckMessage(cap, android::base::INFO, "67890"));
451*8f0ba417SAndroid Build Coastguard Worker }
452*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,PLOG_does_not_clobber_errno)453*8f0ba417SAndroid Build Coastguard Worker TEST(logging, PLOG_does_not_clobber_errno) {
454*8f0ba417SAndroid Build Coastguard Worker CapturedStderr cap;
455*8f0ba417SAndroid Build Coastguard Worker errno = 12345;
456*8f0ba417SAndroid Build Coastguard Worker PLOG(INFO) << (errno = 67890);
457*8f0ba417SAndroid Build Coastguard Worker EXPECT_EQ(12345, errno) << "errno was not restored";
458*8f0ba417SAndroid Build Coastguard Worker
459*8f0ba417SAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(CheckMessage(cap, android::base::INFO, "67890"));
460*8f0ba417SAndroid Build Coastguard Worker }
461*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,LOG_does_not_have_dangling_if)462*8f0ba417SAndroid Build Coastguard Worker TEST(logging, LOG_does_not_have_dangling_if) {
463*8f0ba417SAndroid Build Coastguard Worker CapturedStderr cap; // So the logging below has no side-effects.
464*8f0ba417SAndroid Build Coastguard Worker
465*8f0ba417SAndroid Build Coastguard Worker // Do the test two ways: once where we hypothesize that LOG()'s if
466*8f0ba417SAndroid Build Coastguard Worker // will evaluate to true (when severity is high enough) and once when we
467*8f0ba417SAndroid Build Coastguard Worker // expect it to evaluate to false (when severity is not high enough).
468*8f0ba417SAndroid Build Coastguard Worker bool flag = false;
469*8f0ba417SAndroid Build Coastguard Worker if (true)
470*8f0ba417SAndroid Build Coastguard Worker LOG(INFO) << "foobar";
471*8f0ba417SAndroid Build Coastguard Worker else
472*8f0ba417SAndroid Build Coastguard Worker flag = true;
473*8f0ba417SAndroid Build Coastguard Worker
474*8f0ba417SAndroid Build Coastguard Worker EXPECT_FALSE(flag) << "LOG macro probably has a dangling if with no else";
475*8f0ba417SAndroid Build Coastguard Worker
476*8f0ba417SAndroid Build Coastguard Worker flag = false;
477*8f0ba417SAndroid Build Coastguard Worker if (true)
478*8f0ba417SAndroid Build Coastguard Worker LOG(VERBOSE) << "foobar";
479*8f0ba417SAndroid Build Coastguard Worker else
480*8f0ba417SAndroid Build Coastguard Worker flag = true;
481*8f0ba417SAndroid Build Coastguard Worker
482*8f0ba417SAndroid Build Coastguard Worker EXPECT_FALSE(flag) << "LOG macro probably has a dangling if with no else";
483*8f0ba417SAndroid Build Coastguard Worker }
484*8f0ba417SAndroid Build Coastguard Worker
485*8f0ba417SAndroid Build Coastguard Worker #define CHECK_PLOG_DISABLED(severity) \
486*8f0ba417SAndroid Build Coastguard Worker { \
487*8f0ba417SAndroid Build Coastguard Worker android::base::ScopedLogSeverity sls1(android::base::FATAL); \
488*8f0ba417SAndroid Build Coastguard Worker CapturedStderr cap1; \
489*8f0ba417SAndroid Build Coastguard Worker PLOG(severity) << "foo bar"; \
490*8f0ba417SAndroid Build Coastguard Worker cap1.Stop(); \
491*8f0ba417SAndroid Build Coastguard Worker ASSERT_EQ("", cap1.str()); \
492*8f0ba417SAndroid Build Coastguard Worker } \
493*8f0ba417SAndroid Build Coastguard Worker { \
494*8f0ba417SAndroid Build Coastguard Worker android::base::ScopedLogSeverity sls1(android::base::FATAL); \
495*8f0ba417SAndroid Build Coastguard Worker CapturedStderr cap1; \
496*8f0ba417SAndroid Build Coastguard Worker PLOG(severity) << "foo bar"; \
497*8f0ba417SAndroid Build Coastguard Worker cap1.Stop(); \
498*8f0ba417SAndroid Build Coastguard Worker ASSERT_EQ("", cap1.str()); \
499*8f0ba417SAndroid Build Coastguard Worker }
500*8f0ba417SAndroid Build Coastguard Worker
501*8f0ba417SAndroid Build Coastguard Worker #define CHECK_PLOG_ENABLED(severity) \
502*8f0ba417SAndroid Build Coastguard Worker { \
503*8f0ba417SAndroid Build Coastguard Worker android::base::ScopedLogSeverity sls2(android::base::severity); \
504*8f0ba417SAndroid Build Coastguard Worker CapturedStderr cap2; \
505*8f0ba417SAndroid Build Coastguard Worker errno = ENOENT; \
506*8f0ba417SAndroid Build Coastguard Worker PLOG(severity) << "foobar"; \
507*8f0ba417SAndroid Build Coastguard Worker CheckMessage(cap2, android::base::severity, "foobar: No such file or directory"); \
508*8f0ba417SAndroid Build Coastguard Worker } \
509*8f0ba417SAndroid Build Coastguard Worker { \
510*8f0ba417SAndroid Build Coastguard Worker android::base::ScopedLogSeverity sls2(android::base::severity); \
511*8f0ba417SAndroid Build Coastguard Worker CapturedStderr cap2; \
512*8f0ba417SAndroid Build Coastguard Worker errno = ENOENT; \
513*8f0ba417SAndroid Build Coastguard Worker PLOG(severity) << "foobar"; \
514*8f0ba417SAndroid Build Coastguard Worker CheckMessage(cap2, android::base::severity, "foobar: No such file or directory"); \
515*8f0ba417SAndroid Build Coastguard Worker } \
516*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,PLOG_FATAL)517*8f0ba417SAndroid Build Coastguard Worker TEST(logging, PLOG_FATAL) {
518*8f0ba417SAndroid Build Coastguard Worker ASSERT_DEATH({SuppressAbortUI(); PLOG(FATAL) << "foobar";}, "foobar");
519*8f0ba417SAndroid Build Coastguard Worker ASSERT_DEATH({SuppressAbortUI(); PLOG(::android::base::FATAL) << "foobar";}, "foobar");
520*8f0ba417SAndroid Build Coastguard Worker }
521*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,PLOG_FATAL_WITHOUT_ABORT_enabled)522*8f0ba417SAndroid Build Coastguard Worker TEST(logging, PLOG_FATAL_WITHOUT_ABORT_enabled) {
523*8f0ba417SAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(CHECK_PLOG_ENABLED(FATAL_WITHOUT_ABORT));
524*8f0ba417SAndroid Build Coastguard Worker }
525*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,PLOG_ERROR_disabled)526*8f0ba417SAndroid Build Coastguard Worker TEST(logging, PLOG_ERROR_disabled) {
527*8f0ba417SAndroid Build Coastguard Worker CHECK_PLOG_DISABLED(ERROR);
528*8f0ba417SAndroid Build Coastguard Worker }
529*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,PLOG_ERROR_enabled)530*8f0ba417SAndroid Build Coastguard Worker TEST(logging, PLOG_ERROR_enabled) {
531*8f0ba417SAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(CHECK_PLOG_ENABLED(ERROR));
532*8f0ba417SAndroid Build Coastguard Worker }
533*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,PLOG_WARNING_disabled)534*8f0ba417SAndroid Build Coastguard Worker TEST(logging, PLOG_WARNING_disabled) {
535*8f0ba417SAndroid Build Coastguard Worker CHECK_PLOG_DISABLED(WARNING);
536*8f0ba417SAndroid Build Coastguard Worker }
537*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,PLOG_WARNING_enabled)538*8f0ba417SAndroid Build Coastguard Worker TEST(logging, PLOG_WARNING_enabled) {
539*8f0ba417SAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(CHECK_PLOG_ENABLED(WARNING));
540*8f0ba417SAndroid Build Coastguard Worker }
541*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,PLOG_INFO_disabled)542*8f0ba417SAndroid Build Coastguard Worker TEST(logging, PLOG_INFO_disabled) {
543*8f0ba417SAndroid Build Coastguard Worker CHECK_PLOG_DISABLED(INFO);
544*8f0ba417SAndroid Build Coastguard Worker }
545*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,PLOG_INFO_enabled)546*8f0ba417SAndroid Build Coastguard Worker TEST(logging, PLOG_INFO_enabled) {
547*8f0ba417SAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(CHECK_PLOG_ENABLED(INFO));
548*8f0ba417SAndroid Build Coastguard Worker }
549*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,PLOG_DEBUG_disabled)550*8f0ba417SAndroid Build Coastguard Worker TEST(logging, PLOG_DEBUG_disabled) {
551*8f0ba417SAndroid Build Coastguard Worker CHECK_PLOG_DISABLED(DEBUG);
552*8f0ba417SAndroid Build Coastguard Worker }
553*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,PLOG_DEBUG_enabled)554*8f0ba417SAndroid Build Coastguard Worker TEST(logging, PLOG_DEBUG_enabled) {
555*8f0ba417SAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(CHECK_PLOG_ENABLED(DEBUG));
556*8f0ba417SAndroid Build Coastguard Worker }
557*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,PLOG_VERBOSE_disabled)558*8f0ba417SAndroid Build Coastguard Worker TEST(logging, PLOG_VERBOSE_disabled) {
559*8f0ba417SAndroid Build Coastguard Worker CHECK_PLOG_DISABLED(VERBOSE);
560*8f0ba417SAndroid Build Coastguard Worker }
561*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,PLOG_VERBOSE_enabled)562*8f0ba417SAndroid Build Coastguard Worker TEST(logging, PLOG_VERBOSE_enabled) {
563*8f0ba417SAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(CHECK_PLOG_ENABLED(VERBOSE));
564*8f0ba417SAndroid Build Coastguard Worker }
565*8f0ba417SAndroid Build Coastguard Worker
566*8f0ba417SAndroid Build Coastguard Worker #undef CHECK_PLOG_DISABLED
567*8f0ba417SAndroid Build Coastguard Worker #undef CHECK_PLOG_ENABLED
568*8f0ba417SAndroid Build Coastguard Worker
569*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,UNIMPLEMENTED)570*8f0ba417SAndroid Build Coastguard Worker TEST(logging, UNIMPLEMENTED) {
571*8f0ba417SAndroid Build Coastguard Worker std::string expected = android::base::StringPrintf("%s unimplemented ", __PRETTY_FUNCTION__);
572*8f0ba417SAndroid Build Coastguard Worker
573*8f0ba417SAndroid Build Coastguard Worker CapturedStderr cap;
574*8f0ba417SAndroid Build Coastguard Worker errno = ENOENT;
575*8f0ba417SAndroid Build Coastguard Worker UNIMPLEMENTED(ERROR);
576*8f0ba417SAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(CheckMessage(cap, android::base::ERROR, expected.c_str()));
577*8f0ba417SAndroid Build Coastguard Worker }
578*8f0ba417SAndroid Build Coastguard Worker
NoopAborter(const char * msg ATTRIBUTE_UNUSED)579*8f0ba417SAndroid Build Coastguard Worker static void NoopAborter(const char* msg ATTRIBUTE_UNUSED) {
580*8f0ba417SAndroid Build Coastguard Worker LOG(ERROR) << "called noop";
581*8f0ba417SAndroid Build Coastguard Worker }
582*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,LOG_FATAL_NOOP_ABORTER)583*8f0ba417SAndroid Build Coastguard Worker TEST(logging, LOG_FATAL_NOOP_ABORTER) {
584*8f0ba417SAndroid Build Coastguard Worker CapturedStderr cap;
585*8f0ba417SAndroid Build Coastguard Worker {
586*8f0ba417SAndroid Build Coastguard Worker android::base::SetAborter(NoopAborter);
587*8f0ba417SAndroid Build Coastguard Worker
588*8f0ba417SAndroid Build Coastguard Worker android::base::ScopedLogSeverity sls(android::base::ERROR);
589*8f0ba417SAndroid Build Coastguard Worker LOG(FATAL) << "foobar";
590*8f0ba417SAndroid Build Coastguard Worker cap.Stop();
591*8f0ba417SAndroid Build Coastguard Worker
592*8f0ba417SAndroid Build Coastguard Worker android::base::SetAborter(android::base::DefaultAborter);
593*8f0ba417SAndroid Build Coastguard Worker }
594*8f0ba417SAndroid Build Coastguard Worker std::string output = cap.str();
595*8f0ba417SAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(CheckMessage(output, android::base::FATAL, "foobar"));
596*8f0ba417SAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(CheckMessage(output, android::base::ERROR, "called noop"));
597*8f0ba417SAndroid Build Coastguard Worker
598*8f0ba417SAndroid Build Coastguard Worker ASSERT_DEATH({SuppressAbortUI(); LOG(FATAL) << "foobar";}, "foobar");
599*8f0ba417SAndroid Build Coastguard Worker }
600*8f0ba417SAndroid Build Coastguard Worker
601*8f0ba417SAndroid Build Coastguard Worker struct CountLineAborter {
CountLineAborterFunctionCountLineAborter602*8f0ba417SAndroid Build Coastguard Worker static void CountLineAborterFunction(const char* msg) {
603*8f0ba417SAndroid Build Coastguard Worker while (*msg != 0) {
604*8f0ba417SAndroid Build Coastguard Worker if (*msg == '\n') {
605*8f0ba417SAndroid Build Coastguard Worker newline_count++;
606*8f0ba417SAndroid Build Coastguard Worker }
607*8f0ba417SAndroid Build Coastguard Worker msg++;
608*8f0ba417SAndroid Build Coastguard Worker }
609*8f0ba417SAndroid Build Coastguard Worker }
610*8f0ba417SAndroid Build Coastguard Worker static size_t newline_count;
611*8f0ba417SAndroid Build Coastguard Worker };
612*8f0ba417SAndroid Build Coastguard Worker size_t CountLineAborter::newline_count = 0;
613*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,LOG_FATAL_ABORTER_MESSAGE)614*8f0ba417SAndroid Build Coastguard Worker TEST(logging, LOG_FATAL_ABORTER_MESSAGE) {
615*8f0ba417SAndroid Build Coastguard Worker CountLineAborter::newline_count = 0;
616*8f0ba417SAndroid Build Coastguard Worker android::base::SetAborter(CountLineAborter::CountLineAborterFunction);
617*8f0ba417SAndroid Build Coastguard Worker
618*8f0ba417SAndroid Build Coastguard Worker android::base::ScopedLogSeverity sls(android::base::ERROR);
619*8f0ba417SAndroid Build Coastguard Worker CapturedStderr cap;
620*8f0ba417SAndroid Build Coastguard Worker LOG(FATAL) << "foo\nbar";
621*8f0ba417SAndroid Build Coastguard Worker
622*8f0ba417SAndroid Build Coastguard Worker EXPECT_EQ(CountLineAborter::newline_count, 1U);
623*8f0ba417SAndroid Build Coastguard Worker }
624*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,SetAborterReturnsOldFunction)625*8f0ba417SAndroid Build Coastguard Worker TEST(logging, SetAborterReturnsOldFunction) {
626*8f0ba417SAndroid Build Coastguard Worker // std::function is not comparable, it only supports a null check.
627*8f0ba417SAndroid Build Coastguard Worker android::base::AbortFunction old_aborter;
628*8f0ba417SAndroid Build Coastguard Worker EXPECT_FALSE(old_aborter);
629*8f0ba417SAndroid Build Coastguard Worker old_aborter = android::base::SetAborter(android::base::DefaultAborter);
630*8f0ba417SAndroid Build Coastguard Worker EXPECT_TRUE(old_aborter);
631*8f0ba417SAndroid Build Coastguard Worker }
632*8f0ba417SAndroid Build Coastguard Worker
TestLoggingInConstructor()633*8f0ba417SAndroid Build Coastguard Worker __attribute__((constructor)) void TestLoggingInConstructor() {
634*8f0ba417SAndroid Build Coastguard Worker LOG(ERROR) << "foobar";
635*8f0ba417SAndroid Build Coastguard Worker }
636*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,StdioLogger)637*8f0ba417SAndroid Build Coastguard Worker TEST(logging, StdioLogger) {
638*8f0ba417SAndroid Build Coastguard Worker CapturedStderr cap_err;
639*8f0ba417SAndroid Build Coastguard Worker CapturedStdout cap_out;
640*8f0ba417SAndroid Build Coastguard Worker android::base::SetLogger(android::base::StdioLogger);
641*8f0ba417SAndroid Build Coastguard Worker LOG(INFO) << "out";
642*8f0ba417SAndroid Build Coastguard Worker LOG(ERROR) << "err";
643*8f0ba417SAndroid Build Coastguard Worker cap_err.Stop();
644*8f0ba417SAndroid Build Coastguard Worker cap_out.Stop();
645*8f0ba417SAndroid Build Coastguard Worker
646*8f0ba417SAndroid Build Coastguard Worker // For INFO we expect just the literal "out\n".
647*8f0ba417SAndroid Build Coastguard Worker ASSERT_EQ("out\n", cap_out.str());
648*8f0ba417SAndroid Build Coastguard Worker // Whereas ERROR logging includes the program name.
649*8f0ba417SAndroid Build Coastguard Worker ASSERT_EQ(android::base::Basename(android::base::GetExecutablePath()) + ": err\n", cap_err.str());
650*8f0ba417SAndroid Build Coastguard Worker }
651*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,SetLoggerReturnsOldFunction)652*8f0ba417SAndroid Build Coastguard Worker TEST(logging, SetLoggerReturnsOldFunction) {
653*8f0ba417SAndroid Build Coastguard Worker // std::function is not comparable, it only supports a null check.
654*8f0ba417SAndroid Build Coastguard Worker android::base::LogFunction old_function;
655*8f0ba417SAndroid Build Coastguard Worker EXPECT_FALSE(old_function);
656*8f0ba417SAndroid Build Coastguard Worker old_function = android::base::SetLogger(android::base::StdioLogger);
657*8f0ba417SAndroid Build Coastguard Worker EXPECT_TRUE(old_function);
658*8f0ba417SAndroid Build Coastguard Worker }
659*8f0ba417SAndroid Build Coastguard Worker
TEST(logging,ForkSafe)660*8f0ba417SAndroid Build Coastguard Worker TEST(logging, ForkSafe) {
661*8f0ba417SAndroid Build Coastguard Worker #if !defined(_WIN32)
662*8f0ba417SAndroid Build Coastguard Worker using namespace android::base;
663*8f0ba417SAndroid Build Coastguard Worker SetLogger(
664*8f0ba417SAndroid Build Coastguard Worker [&](LogId, LogSeverity, const char*, const char*, unsigned int, const char*) { sleep(3); });
665*8f0ba417SAndroid Build Coastguard Worker
666*8f0ba417SAndroid Build Coastguard Worker auto guard = make_scope_guard([&] {
667*8f0ba417SAndroid Build Coastguard Worker #ifdef __ANDROID__
668*8f0ba417SAndroid Build Coastguard Worker SetLogger(LogdLogger());
669*8f0ba417SAndroid Build Coastguard Worker #else
670*8f0ba417SAndroid Build Coastguard Worker SetLogger(StderrLogger);
671*8f0ba417SAndroid Build Coastguard Worker #endif
672*8f0ba417SAndroid Build Coastguard Worker });
673*8f0ba417SAndroid Build Coastguard Worker
674*8f0ba417SAndroid Build Coastguard Worker auto thread = std::thread([] {
675*8f0ba417SAndroid Build Coastguard Worker LOG(ERROR) << "This should sleep for 3 seconds, long enough to fork another process, if there "
676*8f0ba417SAndroid Build Coastguard Worker "is no intervention";
677*8f0ba417SAndroid Build Coastguard Worker });
678*8f0ba417SAndroid Build Coastguard Worker thread.detach();
679*8f0ba417SAndroid Build Coastguard Worker
680*8f0ba417SAndroid Build Coastguard Worker auto pid = fork();
681*8f0ba417SAndroid Build Coastguard Worker ASSERT_NE(-1, pid);
682*8f0ba417SAndroid Build Coastguard Worker
683*8f0ba417SAndroid Build Coastguard Worker if (pid == 0) {
684*8f0ba417SAndroid Build Coastguard Worker // Reset the logger, so the next message doesn't sleep().
685*8f0ba417SAndroid Build Coastguard Worker SetLogger([](LogId, LogSeverity, const char*, const char*, unsigned int, const char*) {});
686*8f0ba417SAndroid Build Coastguard Worker LOG(ERROR) << "This should succeed in the child, only if libbase is forksafe.";
687*8f0ba417SAndroid Build Coastguard Worker _exit(EXIT_SUCCESS);
688*8f0ba417SAndroid Build Coastguard Worker }
689*8f0ba417SAndroid Build Coastguard Worker
690*8f0ba417SAndroid Build Coastguard Worker // Wait for up to 3 seconds for the child to exit.
691*8f0ba417SAndroid Build Coastguard Worker int tries = 3;
692*8f0ba417SAndroid Build Coastguard Worker bool found_child = false;
693*8f0ba417SAndroid Build Coastguard Worker while (tries-- > 0) {
694*8f0ba417SAndroid Build Coastguard Worker auto result = waitpid(pid, nullptr, WNOHANG);
695*8f0ba417SAndroid Build Coastguard Worker EXPECT_NE(-1, result);
696*8f0ba417SAndroid Build Coastguard Worker if (result == pid) {
697*8f0ba417SAndroid Build Coastguard Worker found_child = true;
698*8f0ba417SAndroid Build Coastguard Worker break;
699*8f0ba417SAndroid Build Coastguard Worker }
700*8f0ba417SAndroid Build Coastguard Worker sleep(1);
701*8f0ba417SAndroid Build Coastguard Worker }
702*8f0ba417SAndroid Build Coastguard Worker
703*8f0ba417SAndroid Build Coastguard Worker EXPECT_TRUE(found_child);
704*8f0ba417SAndroid Build Coastguard Worker
705*8f0ba417SAndroid Build Coastguard Worker // Kill the child if it did not exit.
706*8f0ba417SAndroid Build Coastguard Worker if (!found_child) {
707*8f0ba417SAndroid Build Coastguard Worker kill(pid, SIGKILL);
708*8f0ba417SAndroid Build Coastguard Worker }
709*8f0ba417SAndroid Build Coastguard Worker #endif
710*8f0ba417SAndroid Build Coastguard Worker }
711