| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 1 | /* | 
 | 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 Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 17 | #include "android-base/logging.h" | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 18 |  | 
| Elliott Hughes | 2e5ae00 | 2015-08-13 20:09:29 -0700 | [diff] [blame] | 19 | #include <libgen.h> | 
 | 20 |  | 
| Spencer Low | 765ae6b | 2015-09-17 19:36:10 -0700 | [diff] [blame] | 21 | #if defined(_WIN32) | 
 | 22 | #include <signal.h> | 
 | 23 | #endif | 
 | 24 |  | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 25 | #include <regex> | 
 | 26 | #include <string> | 
 | 27 |  | 
| Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 28 | #include "android-base/file.h" | 
 | 29 | #include "android-base/stringprintf.h" | 
 | 30 | #include "android-base/test_utils.h" | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 31 |  | 
 | 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 Low | 765ae6b | 2015-09-17 19:36:10 -0700 | [diff] [blame] | 40 | #if defined(_WIN32) | 
 | 41 | static void ExitSignalAbortHandler(int) { | 
 | 42 |   _exit(3); | 
 | 43 | } | 
 | 44 | #endif | 
 | 45 |  | 
 | 46 | static 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 Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 60 | TEST(logging, CHECK) { | 
| Spencer Low | 765ae6b | 2015-09-17 19:36:10 -0700 | [diff] [blame] | 61 |   ASSERT_DEATH({SuppressAbortUI(); CHECK(false);}, "Check failed: false "); | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 62 |   CHECK(true); | 
 | 63 |  | 
| Spencer Low | 765ae6b | 2015-09-17 19:36:10 -0700 | [diff] [blame] | 64 |   ASSERT_DEATH({SuppressAbortUI(); CHECK_EQ(0, 1);}, "Check failed: 0 == 1 "); | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 65 |   CHECK_EQ(0, 0); | 
 | 66 |  | 
| Spencer Low | 765ae6b | 2015-09-17 19:36:10 -0700 | [diff] [blame] | 67 |   ASSERT_DEATH({SuppressAbortUI(); CHECK_STREQ("foo", "bar");}, | 
 | 68 |                R"(Check failed: "foo" == "bar")"); | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 69 |   CHECK_STREQ("foo", "foo"); | 
| Spencer Low | 765ae6b | 2015-09-17 19:36:10 -0700 | [diff] [blame] | 70 |  | 
 | 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 Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 85 | } | 
 | 86 |  | 
| Andreas Gampe | d8f26e2 | 2016-09-13 10:44:46 -0700 | [diff] [blame] | 87 | TEST(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 Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 109 |  | 
| Andreas Gampe | 1f5fb43 | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 110 | #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 Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 138 |  | 
 | 139 | TEST(logging, WOULD_LOG_FATAL) { | 
 | 140 |   CHECK_WOULD_LOG_ENABLED(FATAL); | 
 | 141 | } | 
 | 142 |  | 
| Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 143 | TEST(logging, WOULD_LOG_FATAL_WITHOUT_ABORT_enabled) { | 
 | 144 |   CHECK_WOULD_LOG_ENABLED(FATAL_WITHOUT_ABORT); | 
 | 145 | } | 
 | 146 |  | 
 | 147 | TEST(logging, WOULD_LOG_ERROR_disabled) { | 
 | 148 |   CHECK_WOULD_LOG_DISABLED(ERROR); | 
 | 149 | } | 
 | 150 |  | 
 | 151 | TEST(logging, WOULD_LOG_ERROR_enabled) { | 
 | 152 |   CHECK_WOULD_LOG_ENABLED(ERROR); | 
 | 153 | } | 
 | 154 |  | 
 | 155 | TEST(logging, WOULD_LOG_WARNING_disabled) { | 
 | 156 |   CHECK_WOULD_LOG_DISABLED(WARNING); | 
 | 157 | } | 
 | 158 |  | 
 | 159 | TEST(logging, WOULD_LOG_WARNING_enabled) { | 
 | 160 |   CHECK_WOULD_LOG_ENABLED(WARNING); | 
 | 161 | } | 
 | 162 |  | 
 | 163 | TEST(logging, WOULD_LOG_INFO_disabled) { | 
 | 164 |   CHECK_WOULD_LOG_DISABLED(INFO); | 
 | 165 | } | 
 | 166 |  | 
 | 167 | TEST(logging, WOULD_LOG_INFO_enabled) { | 
 | 168 |   CHECK_WOULD_LOG_ENABLED(INFO); | 
 | 169 | } | 
 | 170 |  | 
 | 171 | TEST(logging, WOULD_LOG_DEBUG_disabled) { | 
 | 172 |   CHECK_WOULD_LOG_DISABLED(DEBUG); | 
 | 173 | } | 
 | 174 |  | 
 | 175 | TEST(logging, WOULD_LOG_DEBUG_enabled) { | 
 | 176 |   CHECK_WOULD_LOG_ENABLED(DEBUG); | 
 | 177 | } | 
 | 178 |  | 
 | 179 | TEST(logging, WOULD_LOG_VERBOSE_disabled) { | 
 | 180 |   CHECK_WOULD_LOG_DISABLED(VERBOSE); | 
 | 181 | } | 
 | 182 |  | 
 | 183 | TEST(logging, WOULD_LOG_VERBOSE_enabled) { | 
 | 184 |   CHECK_WOULD_LOG_ENABLED(VERBOSE); | 
 | 185 | } | 
 | 186 |  | 
 | 187 | #undef CHECK_WOULD_LOG_DISABLED | 
 | 188 | #undef CHECK_WOULD_LOG_ENABLED | 
 | 189 |  | 
 | 190 |  | 
| Dan Willemsen | 528f144 | 2017-11-29 18:06:11 -0800 | [diff] [blame] | 191 | #if !defined(_WIN32) | 
| Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 192 | static std::string make_log_pattern(android::base::LogSeverity severity, | 
 | 193 |                                     const char* message) { | 
| Andreas Gampe | 550829d | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 194 |   static const char log_characters[] = "VDIWEFF"; | 
 | 195 |   static_assert(arraysize(log_characters) - 1 == android::base::FATAL + 1, | 
 | 196 |                 "Mismatch in size of log_characters and values in LogSeverity"); | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 197 |   char log_char = log_characters[severity]; | 
| Spencer Low | bdab59a | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 198 |   std::string holder(__FILE__); | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 199 |   return android::base::StringPrintf( | 
| Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 200 |       "%c \\d+-\\d+ \\d+:\\d+:\\d+ \\s*\\d+ \\s*\\d+ %s:\\d+] %s", | 
| Spencer Low | bdab59a | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 201 |       log_char, basename(&holder[0]), message); | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 202 | } | 
| Dan Willemsen | 528f144 | 2017-11-29 18:06:11 -0800 | [diff] [blame] | 203 | #endif | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 204 |  | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 205 | static void CheckMessage(const std::string& output, android::base::LogSeverity severity, | 
| Andreas Gampe | 1923e76 | 2018-03-05 10:00:19 -0800 | [diff] [blame] | 206 |                          const char* expected, const char* expected_tag = nullptr) { | 
| Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 207 |   // We can't usefully check the output of any of these on Windows because we | 
 | 208 |   // don't have std::regex, but we can at least make sure we printed at least as | 
 | 209 |   // many characters are in the log message. | 
| Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 210 |   ASSERT_GT(output.length(), strlen(expected)); | 
 | 211 |   ASSERT_NE(nullptr, strstr(output.c_str(), expected)) << output; | 
| Andreas Gampe | 1923e76 | 2018-03-05 10:00:19 -0800 | [diff] [blame] | 212 |   if (expected_tag != nullptr) { | 
 | 213 |     ASSERT_NE(nullptr, strstr(output.c_str(), expected_tag)) << output; | 
 | 214 |   } | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 215 |  | 
| Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 216 | #if !defined(_WIN32) | 
| Andreas Gampe | 1923e76 | 2018-03-05 10:00:19 -0800 | [diff] [blame] | 217 |   std::string regex_str; | 
 | 218 |   if (expected_tag != nullptr) { | 
 | 219 |     regex_str.append(expected_tag); | 
 | 220 |     regex_str.append(" "); | 
 | 221 |   } | 
 | 222 |   regex_str.append(make_log_pattern(severity, expected)); | 
 | 223 |   std::regex message_regex(regex_str); | 
| Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 224 |   ASSERT_TRUE(std::regex_search(output, message_regex)) << output; | 
| Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 225 | #endif | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 226 | } | 
 | 227 |  | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 228 | static void CheckMessage(CapturedStderr& cap, android::base::LogSeverity severity, | 
 | 229 |                          const char* expected, const char* expected_tag = nullptr) { | 
 | 230 |   cap.Stop(); | 
 | 231 |   std::string output = cap.str(); | 
 | 232 |   return CheckMessage(output, severity, expected, expected_tag); | 
 | 233 | } | 
| Andreas Gampe | 1f5fb43 | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 234 |  | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 235 | #define CHECK_LOG_STREAM_DISABLED(severity)                      \ | 
 | 236 |   {                                                              \ | 
| Andreas Gampe | 1f5fb43 | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 237 |     android::base::ScopedLogSeverity sls1(android::base::FATAL); \ | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 238 |     CapturedStderr cap1;                                         \ | 
 | 239 |     LOG_STREAM(severity) << "foo bar";                           \ | 
 | 240 |     cap1.Stop();                                                 \ | 
 | 241 |     ASSERT_EQ("", cap1.str());                                   \ | 
 | 242 |   }                                                              \ | 
 | 243 |   {                                                              \ | 
| Andreas Gampe | 1f5fb43 | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 244 |     android::base::ScopedLogSeverity sls1(android::base::FATAL); \ | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 245 |     CapturedStderr cap1;                                         \ | 
 | 246 |     LOG_STREAM(::android::base::severity) << "foo bar";          \ | 
 | 247 |     cap1.Stop();                                                 \ | 
 | 248 |     ASSERT_EQ("", cap1.str());                                   \ | 
 | 249 |   } | 
| Andreas Gampe | 19ff8f1 | 2016-09-23 13:31:52 -0700 | [diff] [blame] | 250 |  | 
 | 251 | #define CHECK_LOG_STREAM_ENABLED(severity) \ | 
| Andreas Gampe | 1f5fb43 | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 252 |   { \ | 
 | 253 |     android::base::ScopedLogSeverity sls2(android::base::severity); \ | 
 | 254 |     CapturedStderr cap2; \ | 
 | 255 |     LOG_STREAM(severity) << "foobar"; \ | 
 | 256 |     CheckMessage(cap2, android::base::severity, "foobar"); \ | 
 | 257 |   } \ | 
 | 258 |   { \ | 
 | 259 |     android::base::ScopedLogSeverity sls2(android::base::severity); \ | 
 | 260 |     CapturedStderr cap2; \ | 
 | 261 |     LOG_STREAM(::android::base::severity) << "foobar"; \ | 
 | 262 |     CheckMessage(cap2, android::base::severity, "foobar"); \ | 
 | 263 |   } \ | 
| Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 264 |  | 
| Andreas Gampe | 19ff8f1 | 2016-09-23 13:31:52 -0700 | [diff] [blame] | 265 | TEST(logging, LOG_STREAM_FATAL_WITHOUT_ABORT_enabled) { | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 266 |   ASSERT_NO_FATAL_FAILURE(CHECK_LOG_STREAM_ENABLED(FATAL_WITHOUT_ABORT)); | 
| Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 267 | } | 
 | 268 |  | 
| Andreas Gampe | 19ff8f1 | 2016-09-23 13:31:52 -0700 | [diff] [blame] | 269 | TEST(logging, LOG_STREAM_ERROR_disabled) { | 
 | 270 |   CHECK_LOG_STREAM_DISABLED(ERROR); | 
| Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 271 | } | 
 | 272 |  | 
| Andreas Gampe | 19ff8f1 | 2016-09-23 13:31:52 -0700 | [diff] [blame] | 273 | TEST(logging, LOG_STREAM_ERROR_enabled) { | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 274 |   ASSERT_NO_FATAL_FAILURE(CHECK_LOG_STREAM_ENABLED(ERROR)); | 
| Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 275 | } | 
 | 276 |  | 
| Andreas Gampe | 19ff8f1 | 2016-09-23 13:31:52 -0700 | [diff] [blame] | 277 | TEST(logging, LOG_STREAM_WARNING_disabled) { | 
 | 278 |   CHECK_LOG_STREAM_DISABLED(WARNING); | 
| Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 279 | } | 
 | 280 |  | 
| Andreas Gampe | 19ff8f1 | 2016-09-23 13:31:52 -0700 | [diff] [blame] | 281 | TEST(logging, LOG_STREAM_WARNING_enabled) { | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 282 |   ASSERT_NO_FATAL_FAILURE(CHECK_LOG_STREAM_ENABLED(WARNING)); | 
| Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 283 | } | 
 | 284 |  | 
| Andreas Gampe | 19ff8f1 | 2016-09-23 13:31:52 -0700 | [diff] [blame] | 285 | TEST(logging, LOG_STREAM_INFO_disabled) { | 
 | 286 |   CHECK_LOG_STREAM_DISABLED(INFO); | 
 | 287 | } | 
| Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 288 |  | 
| Andreas Gampe | 19ff8f1 | 2016-09-23 13:31:52 -0700 | [diff] [blame] | 289 | TEST(logging, LOG_STREAM_INFO_enabled) { | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 290 |   ASSERT_NO_FATAL_FAILURE(CHECK_LOG_STREAM_ENABLED(INFO)); | 
| Andreas Gampe | 19ff8f1 | 2016-09-23 13:31:52 -0700 | [diff] [blame] | 291 | } | 
 | 292 |  | 
 | 293 | TEST(logging, LOG_STREAM_DEBUG_disabled) { | 
 | 294 |   CHECK_LOG_STREAM_DISABLED(DEBUG); | 
 | 295 | } | 
 | 296 |  | 
 | 297 | TEST(logging, LOG_STREAM_DEBUG_enabled) { | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 298 |   ASSERT_NO_FATAL_FAILURE(CHECK_LOG_STREAM_ENABLED(DEBUG)); | 
| Andreas Gampe | 19ff8f1 | 2016-09-23 13:31:52 -0700 | [diff] [blame] | 299 | } | 
 | 300 |  | 
 | 301 | TEST(logging, LOG_STREAM_VERBOSE_disabled) { | 
 | 302 |   CHECK_LOG_STREAM_DISABLED(VERBOSE); | 
 | 303 | } | 
 | 304 |  | 
 | 305 | TEST(logging, LOG_STREAM_VERBOSE_enabled) { | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 306 |   ASSERT_NO_FATAL_FAILURE(CHECK_LOG_STREAM_ENABLED(VERBOSE)); | 
| Andreas Gampe | 19ff8f1 | 2016-09-23 13:31:52 -0700 | [diff] [blame] | 307 | } | 
 | 308 |  | 
 | 309 | #undef CHECK_LOG_STREAM_DISABLED | 
 | 310 | #undef CHECK_LOG_STREAM_ENABLED | 
 | 311 |  | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 312 | #define CHECK_LOG_DISABLED(severity)                             \ | 
 | 313 |   {                                                              \ | 
| Andreas Gampe | 1f5fb43 | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 314 |     android::base::ScopedLogSeverity sls1(android::base::FATAL); \ | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 315 |     CapturedStderr cap1;                                         \ | 
 | 316 |     LOG(severity) << "foo bar";                                  \ | 
 | 317 |     cap1.Stop();                                                 \ | 
 | 318 |     ASSERT_EQ("", cap1.str());                                   \ | 
 | 319 |   }                                                              \ | 
 | 320 |   {                                                              \ | 
| Andreas Gampe | 1f5fb43 | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 321 |     android::base::ScopedLogSeverity sls1(android::base::FATAL); \ | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 322 |     CapturedStderr cap1;                                         \ | 
 | 323 |     LOG(::android::base::severity) << "foo bar";                 \ | 
 | 324 |     cap1.Stop();                                                 \ | 
 | 325 |     ASSERT_EQ("", cap1.str());                                   \ | 
 | 326 |   } | 
| Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 327 |  | 
 | 328 | #define CHECK_LOG_ENABLED(severity) \ | 
| Andreas Gampe | 1f5fb43 | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 329 |   { \ | 
 | 330 |     android::base::ScopedLogSeverity sls2(android::base::severity); \ | 
 | 331 |     CapturedStderr cap2; \ | 
 | 332 |     LOG(severity) << "foobar"; \ | 
 | 333 |     CheckMessage(cap2, android::base::severity, "foobar"); \ | 
 | 334 |   } \ | 
 | 335 |   { \ | 
 | 336 |     android::base::ScopedLogSeverity sls2(android::base::severity); \ | 
 | 337 |     CapturedStderr cap2; \ | 
 | 338 |     LOG(::android::base::severity) << "foobar"; \ | 
 | 339 |     CheckMessage(cap2, android::base::severity, "foobar"); \ | 
 | 340 |   } \ | 
| Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 341 |  | 
| Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 342 | TEST(logging, LOG_FATAL) { | 
 | 343 |   ASSERT_DEATH({SuppressAbortUI(); LOG(FATAL) << "foobar";}, "foobar"); | 
| Andreas Gampe | 1f5fb43 | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 344 |   ASSERT_DEATH({SuppressAbortUI(); LOG(::android::base::FATAL) << "foobar";}, "foobar"); | 
| Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 345 | } | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 346 |  | 
| Andreas Gampe | 550829d | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 347 | TEST(logging, LOG_FATAL_WITHOUT_ABORT_enabled) { | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 348 |   ASSERT_NO_FATAL_FAILURE(CHECK_LOG_ENABLED(FATAL_WITHOUT_ABORT)); | 
| Andreas Gampe | 550829d | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 349 | } | 
 | 350 |  | 
| Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 351 | TEST(logging, LOG_ERROR_disabled) { | 
 | 352 |   CHECK_LOG_DISABLED(ERROR); | 
 | 353 | } | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 354 |  | 
| Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 355 | TEST(logging, LOG_ERROR_enabled) { | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 356 |   ASSERT_NO_FATAL_FAILURE(CHECK_LOG_ENABLED(ERROR)); | 
| Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 357 | } | 
 | 358 |  | 
 | 359 | TEST(logging, LOG_WARNING_disabled) { | 
 | 360 |   CHECK_LOG_DISABLED(WARNING); | 
 | 361 | } | 
 | 362 |  | 
 | 363 | TEST(logging, LOG_WARNING_enabled) { | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 364 |   ASSERT_NO_FATAL_FAILURE(CHECK_LOG_ENABLED(WARNING)); | 
| Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 365 | } | 
 | 366 |  | 
 | 367 | TEST(logging, LOG_INFO_disabled) { | 
 | 368 |   CHECK_LOG_DISABLED(INFO); | 
 | 369 | } | 
 | 370 |  | 
 | 371 | TEST(logging, LOG_INFO_enabled) { | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 372 |   ASSERT_NO_FATAL_FAILURE(CHECK_LOG_ENABLED(INFO)); | 
| Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 373 | } | 
 | 374 |  | 
 | 375 | TEST(logging, LOG_DEBUG_disabled) { | 
 | 376 |   CHECK_LOG_DISABLED(DEBUG); | 
 | 377 | } | 
 | 378 |  | 
 | 379 | TEST(logging, LOG_DEBUG_enabled) { | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 380 |   ASSERT_NO_FATAL_FAILURE(CHECK_LOG_ENABLED(DEBUG)); | 
| Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 381 | } | 
 | 382 |  | 
 | 383 | TEST(logging, LOG_VERBOSE_disabled) { | 
 | 384 |   CHECK_LOG_DISABLED(VERBOSE); | 
 | 385 | } | 
 | 386 |  | 
 | 387 | TEST(logging, LOG_VERBOSE_enabled) { | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 388 |   ASSERT_NO_FATAL_FAILURE(CHECK_LOG_ENABLED(VERBOSE)); | 
| Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 389 | } | 
 | 390 |  | 
| Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 391 | #undef CHECK_LOG_DISABLED | 
 | 392 | #undef CHECK_LOG_ENABLED | 
 | 393 |  | 
| Andreas Gampe | 1f5fb43 | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 394 | TEST(logging, LOG_complex_param) { | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 395 | #define CHECK_LOG_COMBINATION(use_scoped_log_severity_info, use_logging_severity_info)         \ | 
 | 396 |   {                                                                                            \ | 
 | 397 |     android::base::ScopedLogSeverity sls(                                                      \ | 
 | 398 |         (use_scoped_log_severity_info) ? ::android::base::INFO : ::android::base::WARNING);    \ | 
 | 399 |     CapturedStderr cap;                                                                        \ | 
 | 400 |     LOG((use_logging_severity_info) ? ::android::base::INFO : ::android::base::WARNING)        \ | 
 | 401 |         << "foobar";                                                                           \ | 
 | 402 |     if ((use_scoped_log_severity_info) || !(use_logging_severity_info)) {                      \ | 
 | 403 |       ASSERT_NO_FATAL_FAILURE(CheckMessage(                                                    \ | 
 | 404 |           cap, (use_logging_severity_info) ? ::android::base::INFO : ::android::base::WARNING, \ | 
 | 405 |           "foobar"));                                                                          \ | 
 | 406 |     } else {                                                                                   \ | 
 | 407 |       cap.Stop();                                                                              \ | 
 | 408 |       ASSERT_EQ("", cap.str());                                                                \ | 
 | 409 |     }                                                                                          \ | 
| Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 410 |   } | 
 | 411 |  | 
| Andreas Gampe | 1f5fb43 | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 412 |   CHECK_LOG_COMBINATION(false,false); | 
 | 413 |   CHECK_LOG_COMBINATION(false,true); | 
 | 414 |   CHECK_LOG_COMBINATION(true,false); | 
 | 415 |   CHECK_LOG_COMBINATION(true,true); | 
| Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 416 |  | 
| Andreas Gampe | 1f5fb43 | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 417 | #undef CHECK_LOG_COMBINATION | 
| Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 418 | } | 
 | 419 |  | 
 | 420 |  | 
| Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 421 | TEST(logging, LOG_does_not_clobber_errno) { | 
 | 422 |   CapturedStderr cap; | 
 | 423 |   errno = 12345; | 
 | 424 |   LOG(INFO) << (errno = 67890); | 
 | 425 |   EXPECT_EQ(12345, errno) << "errno was not restored"; | 
 | 426 |  | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 427 |   ASSERT_NO_FATAL_FAILURE(CheckMessage(cap, android::base::INFO, "67890")); | 
| Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 428 | } | 
 | 429 |  | 
 | 430 | TEST(logging, PLOG_does_not_clobber_errno) { | 
 | 431 |   CapturedStderr cap; | 
 | 432 |   errno = 12345; | 
 | 433 |   PLOG(INFO) << (errno = 67890); | 
 | 434 |   EXPECT_EQ(12345, errno) << "errno was not restored"; | 
 | 435 |  | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 436 |   ASSERT_NO_FATAL_FAILURE(CheckMessage(cap, android::base::INFO, "67890")); | 
| Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 437 | } | 
 | 438 |  | 
 | 439 | TEST(logging, LOG_does_not_have_dangling_if) { | 
 | 440 |   CapturedStderr cap; // So the logging below has no side-effects. | 
 | 441 |  | 
 | 442 |   // Do the test two ways: once where we hypothesize that LOG()'s if | 
 | 443 |   // will evaluate to true (when severity is high enough) and once when we | 
 | 444 |   // expect it to evaluate to false (when severity is not high enough). | 
 | 445 |   bool flag = false; | 
 | 446 |   if (true) | 
 | 447 |     LOG(INFO) << "foobar"; | 
 | 448 |   else | 
 | 449 |     flag = true; | 
 | 450 |  | 
 | 451 |   EXPECT_FALSE(flag) << "LOG macro probably has a dangling if with no else"; | 
 | 452 |  | 
 | 453 |   flag = false; | 
 | 454 |   if (true) | 
 | 455 |     LOG(VERBOSE) << "foobar"; | 
 | 456 |   else | 
 | 457 |     flag = true; | 
 | 458 |  | 
 | 459 |   EXPECT_FALSE(flag) << "LOG macro probably has a dangling if with no else"; | 
| Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 460 | } | 
| Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 461 |  | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 462 | #define CHECK_PLOG_DISABLED(severity)                            \ | 
 | 463 |   {                                                              \ | 
| Andreas Gampe | 1f5fb43 | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 464 |     android::base::ScopedLogSeverity sls1(android::base::FATAL); \ | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 465 |     CapturedStderr cap1;                                         \ | 
 | 466 |     PLOG(severity) << "foo bar";                                 \ | 
 | 467 |     cap1.Stop();                                                 \ | 
 | 468 |     ASSERT_EQ("", cap1.str());                                   \ | 
 | 469 |   }                                                              \ | 
 | 470 |   {                                                              \ | 
| Andreas Gampe | 1f5fb43 | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 471 |     android::base::ScopedLogSeverity sls1(android::base::FATAL); \ | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 472 |     CapturedStderr cap1;                                         \ | 
 | 473 |     PLOG(severity) << "foo bar";                                 \ | 
 | 474 |     cap1.Stop();                                                 \ | 
 | 475 |     ASSERT_EQ("", cap1.str());                                   \ | 
 | 476 |   } | 
| Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 477 |  | 
 | 478 | #define CHECK_PLOG_ENABLED(severity) \ | 
| Andreas Gampe | 1f5fb43 | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 479 |   { \ | 
 | 480 |     android::base::ScopedLogSeverity sls2(android::base::severity); \ | 
 | 481 |     CapturedStderr cap2; \ | 
 | 482 |     errno = ENOENT; \ | 
 | 483 |     PLOG(severity) << "foobar"; \ | 
 | 484 |     CheckMessage(cap2, android::base::severity, "foobar: No such file or directory"); \ | 
 | 485 |   } \ | 
 | 486 |   { \ | 
 | 487 |     android::base::ScopedLogSeverity sls2(android::base::severity); \ | 
 | 488 |     CapturedStderr cap2; \ | 
 | 489 |     errno = ENOENT; \ | 
 | 490 |     PLOG(severity) << "foobar"; \ | 
 | 491 |     CheckMessage(cap2, android::base::severity, "foobar: No such file or directory"); \ | 
 | 492 |   } \ | 
| Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 493 |  | 
| Andreas Gampe | 550829d | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 494 | TEST(logging, PLOG_FATAL) { | 
 | 495 |   ASSERT_DEATH({SuppressAbortUI(); PLOG(FATAL) << "foobar";}, "foobar"); | 
| Andreas Gampe | 1f5fb43 | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 496 |   ASSERT_DEATH({SuppressAbortUI(); PLOG(::android::base::FATAL) << "foobar";}, "foobar"); | 
| Andreas Gampe | 550829d | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 497 | } | 
 | 498 |  | 
| Andreas Gampe | 550829d | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 499 | TEST(logging, PLOG_FATAL_WITHOUT_ABORT_enabled) { | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 500 |   ASSERT_NO_FATAL_FAILURE(CHECK_PLOG_ENABLED(FATAL_WITHOUT_ABORT)); | 
| Andreas Gampe | 550829d | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 501 | } | 
 | 502 |  | 
| Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 503 | TEST(logging, PLOG_ERROR_disabled) { | 
 | 504 |   CHECK_PLOG_DISABLED(ERROR); | 
 | 505 | } | 
 | 506 |  | 
 | 507 | TEST(logging, PLOG_ERROR_enabled) { | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 508 |   ASSERT_NO_FATAL_FAILURE(CHECK_PLOG_ENABLED(ERROR)); | 
| Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 509 | } | 
 | 510 |  | 
 | 511 | TEST(logging, PLOG_WARNING_disabled) { | 
 | 512 |   CHECK_PLOG_DISABLED(WARNING); | 
 | 513 | } | 
 | 514 |  | 
 | 515 | TEST(logging, PLOG_WARNING_enabled) { | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 516 |   ASSERT_NO_FATAL_FAILURE(CHECK_PLOG_ENABLED(WARNING)); | 
| Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 517 | } | 
 | 518 |  | 
 | 519 | TEST(logging, PLOG_INFO_disabled) { | 
 | 520 |   CHECK_PLOG_DISABLED(INFO); | 
 | 521 | } | 
 | 522 |  | 
 | 523 | TEST(logging, PLOG_INFO_enabled) { | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 524 |   ASSERT_NO_FATAL_FAILURE(CHECK_PLOG_ENABLED(INFO)); | 
| Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 525 | } | 
 | 526 |  | 
 | 527 | TEST(logging, PLOG_DEBUG_disabled) { | 
 | 528 |   CHECK_PLOG_DISABLED(DEBUG); | 
 | 529 | } | 
 | 530 |  | 
 | 531 | TEST(logging, PLOG_DEBUG_enabled) { | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 532 |   ASSERT_NO_FATAL_FAILURE(CHECK_PLOG_ENABLED(DEBUG)); | 
| Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 533 | } | 
 | 534 |  | 
 | 535 | TEST(logging, PLOG_VERBOSE_disabled) { | 
 | 536 |   CHECK_PLOG_DISABLED(VERBOSE); | 
 | 537 | } | 
 | 538 |  | 
 | 539 | TEST(logging, PLOG_VERBOSE_enabled) { | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 540 |   ASSERT_NO_FATAL_FAILURE(CHECK_PLOG_ENABLED(VERBOSE)); | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 541 | } | 
 | 542 |  | 
| Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 543 | #undef CHECK_PLOG_DISABLED | 
 | 544 | #undef CHECK_PLOG_ENABLED | 
 | 545 |  | 
 | 546 |  | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 547 | TEST(logging, UNIMPLEMENTED) { | 
| Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 548 |   std::string expected = android::base::StringPrintf("%s unimplemented ", __PRETTY_FUNCTION__); | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 549 |  | 
| Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 550 |   CapturedStderr cap; | 
 | 551 |   errno = ENOENT; | 
 | 552 |   UNIMPLEMENTED(ERROR); | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 553 |   ASSERT_NO_FATAL_FAILURE(CheckMessage(cap, android::base::ERROR, expected.c_str())); | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 554 | } | 
| Andreas Gampe | 2691e33 | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 555 |  | 
 | 556 | static void NoopAborter(const char* msg ATTRIBUTE_UNUSED) { | 
 | 557 |   LOG(ERROR) << "called noop"; | 
 | 558 | } | 
 | 559 |  | 
 | 560 | TEST(logging, LOG_FATAL_NOOP_ABORTER) { | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 561 |   CapturedStderr cap; | 
| Andreas Gampe | 2691e33 | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 562 |   { | 
 | 563 |     android::base::SetAborter(NoopAborter); | 
 | 564 |  | 
 | 565 |     android::base::ScopedLogSeverity sls(android::base::ERROR); | 
| Andreas Gampe | 2691e33 | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 566 |     LOG(FATAL) << "foobar"; | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 567 |     cap.Stop(); | 
| Andreas Gampe | 2691e33 | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 568 |  | 
 | 569 |     android::base::SetAborter(android::base::DefaultAborter); | 
 | 570 |   } | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 571 |   std::string output = cap.str(); | 
 | 572 |   ASSERT_NO_FATAL_FAILURE(CheckMessage(output, android::base::FATAL, "foobar")); | 
 | 573 |   ASSERT_NO_FATAL_FAILURE(CheckMessage(output, android::base::ERROR, "called noop")); | 
| Andreas Gampe | 2691e33 | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 574 |  | 
 | 575 |   ASSERT_DEATH({SuppressAbortUI(); LOG(FATAL) << "foobar";}, "foobar"); | 
 | 576 | } | 
| Andreas Gampe | b4e32f3 | 2016-10-04 19:17:07 -0700 | [diff] [blame] | 577 |  | 
 | 578 | struct CountLineAborter { | 
 | 579 |   static void CountLineAborterFunction(const char* msg) { | 
 | 580 |     while (*msg != 0) { | 
 | 581 |       if (*msg == '\n') { | 
 | 582 |         newline_count++; | 
 | 583 |       } | 
 | 584 |       msg++; | 
 | 585 |     } | 
 | 586 |   } | 
 | 587 |   static size_t newline_count; | 
 | 588 | }; | 
 | 589 | size_t CountLineAborter::newline_count = 0; | 
 | 590 |  | 
 | 591 | TEST(logging, LOG_FATAL_ABORTER_MESSAGE) { | 
 | 592 |   CountLineAborter::newline_count = 0; | 
 | 593 |   android::base::SetAborter(CountLineAborter::CountLineAborterFunction); | 
 | 594 |  | 
 | 595 |   android::base::ScopedLogSeverity sls(android::base::ERROR); | 
 | 596 |   CapturedStderr cap; | 
 | 597 |   LOG(FATAL) << "foo\nbar"; | 
 | 598 |  | 
 | 599 |   EXPECT_EQ(CountLineAborter::newline_count, 1U + 1U);  // +1 for final '\n'. | 
 | 600 | } | 
| Yabin Cui | 0c68953 | 2017-01-23 10:29:23 -0800 | [diff] [blame] | 601 |  | 
 | 602 | __attribute__((constructor)) void TestLoggingInConstructor() { | 
 | 603 |   LOG(ERROR) << "foobar"; | 
 | 604 | } | 
| Andreas Gampe | 1923e76 | 2018-03-05 10:00:19 -0800 | [diff] [blame] | 605 |  | 
| Elliott Hughes | 1be0d14 | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 606 | TEST(logging, StdioLogger) { | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 607 |   CapturedStderr cap_err; | 
 | 608 |   CapturedStdout cap_out; | 
 | 609 |   android::base::SetLogger(android::base::StdioLogger); | 
 | 610 |   LOG(INFO) << "out"; | 
 | 611 |   LOG(ERROR) << "err"; | 
 | 612 |   cap_err.Stop(); | 
 | 613 |   cap_out.Stop(); | 
| Elliott Hughes | 1be0d14 | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 614 |  | 
 | 615 |   // For INFO we expect just the literal "out\n". | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 616 |   ASSERT_EQ("out\n", cap_out.str()); | 
| Elliott Hughes | 1be0d14 | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 617 |   // Whereas ERROR logging includes the program name. | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 618 |   ASSERT_EQ(android::base::Basename(android::base::GetExecutablePath()) + ": err\n", cap_err.str()); | 
| Elliott Hughes | 1be0d14 | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 619 | } |