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