OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <stdlib.h> |
| 6 |
| 7 #include <sstream> |
| 8 #include <string> |
| 9 |
| 10 #include "mojo/public/cpp/environment/environment.h" |
| 11 #include "mojo/public/cpp/environment/logging.h" |
| 12 #include "mojo/public/cpp/system/macros.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 // A macro, so it can be automatically joined with other string literals. (Not |
| 16 // simply __FILE__, since that may contain a path.) |
| 17 #define OUR_FILENAME "logging_unittest.cc" |
| 18 |
| 19 namespace mojo { |
| 20 namespace { |
| 21 |
| 22 class LoggingTest : public testing::Test { |
| 23 public: |
| 24 LoggingTest() : environment_(NULL, &kMockLogger) { |
| 25 minimum_log_level_ = MOJO_LOG_LEVEL_INFO; |
| 26 ResetMockLogger(); |
| 27 } |
| 28 virtual ~LoggingTest() {} |
| 29 |
| 30 protected: |
| 31 // Note: Does not reset |minimum_log_level_|. |
| 32 static void ResetMockLogger() { |
| 33 log_message_was_called_ = false; |
| 34 last_log_level_ = MOJO_LOG_LEVEL_INFO; |
| 35 last_message_.clear(); |
| 36 } |
| 37 |
| 38 static bool log_message_was_called() { return log_message_was_called_; } |
| 39 static MojoLogLevel last_log_level() { return last_log_level_; } |
| 40 static const std::string& last_message() { return last_message_; } |
| 41 |
| 42 private: |
| 43 // Note: We record calls even if |log_level| is below |minimum_log_level_| |
| 44 // (since the macros should mostly avoid this, and we want to be able to check |
| 45 // that they do). |
| 46 static void MockLogMessage(MojoLogLevel log_level, const char* message) { |
| 47 log_message_was_called_ = true; |
| 48 last_log_level_ = log_level; |
| 49 last_message_ = message; |
| 50 } |
| 51 |
| 52 static MojoLogLevel MockGetMinimumLogLevel() { |
| 53 return minimum_log_level_; |
| 54 } |
| 55 |
| 56 static void MockSetMinimumLogLevel(MojoLogLevel minimum_log_level) { |
| 57 minimum_log_level_ = minimum_log_level; |
| 58 } |
| 59 |
| 60 Environment environment_; |
| 61 |
| 62 static const MojoLogger kMockLogger; |
| 63 static MojoLogLevel minimum_log_level_; |
| 64 static bool log_message_was_called_; |
| 65 static MojoLogLevel last_log_level_; |
| 66 static std::string last_message_; |
| 67 |
| 68 MOJO_DISALLOW_COPY_AND_ASSIGN(LoggingTest); |
| 69 }; |
| 70 |
| 71 // static |
| 72 const MojoLogger LoggingTest::kMockLogger = { |
| 73 &LoggingTest::MockLogMessage, |
| 74 &LoggingTest::MockGetMinimumLogLevel, |
| 75 &LoggingTest::MockSetMinimumLogLevel |
| 76 }; |
| 77 |
| 78 // static |
| 79 MojoLogLevel LoggingTest::minimum_log_level_ = MOJO_LOG_LEVEL_INFO; |
| 80 |
| 81 // static |
| 82 bool LoggingTest::log_message_was_called_ = MOJO_LOG_LEVEL_INFO; |
| 83 |
| 84 // static |
| 85 MojoLogLevel LoggingTest::last_log_level_ = MOJO_LOG_LEVEL_INFO; |
| 86 |
| 87 // static |
| 88 std::string LoggingTest::last_message_; |
| 89 |
| 90 std::string ExpectedLogMessage(int line, const char* message) { |
| 91 std::ostringstream s; |
| 92 s << OUR_FILENAME "(" << line << "): " << message; |
| 93 return s.str(); |
| 94 } |
| 95 |
| 96 // A function returning |bool| that shouldn't be called. |
| 97 bool NotCalled() { |
| 98 abort(); |
| 99 return true; |
| 100 } |
| 101 |
| 102 TEST_F(LoggingTest, InternalLogMessage) { |
| 103 internal::LogMessage("foo.cc", 123, MOJO_LOG_LEVEL_INFO).stream() |
| 104 << "hello " << "world"; |
| 105 EXPECT_TRUE(log_message_was_called()); |
| 106 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level()); |
| 107 EXPECT_EQ("foo.cc(123): hello world", last_message()); |
| 108 |
| 109 ResetMockLogger(); |
| 110 |
| 111 internal::LogMessage("./path/to/foo.cc", 123, MOJO_LOG_LEVEL_WARNING).stream() |
| 112 << "hello " << "world"; |
| 113 EXPECT_TRUE(log_message_was_called()); |
| 114 EXPECT_EQ(MOJO_LOG_LEVEL_WARNING, last_log_level()); |
| 115 EXPECT_EQ("foo.cc(123): hello world", last_message()); |
| 116 |
| 117 ResetMockLogger(); |
| 118 |
| 119 internal::LogMessage("/path/to/foo.cc", 123, MOJO_LOG_LEVEL_ERROR).stream() |
| 120 << "hello " << "world"; |
| 121 EXPECT_TRUE(log_message_was_called()); |
| 122 EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level()); |
| 123 EXPECT_EQ("foo.cc(123): hello world", last_message()); |
| 124 |
| 125 ResetMockLogger(); |
| 126 |
| 127 internal::LogMessage("path/to/foo.cc", 123, MOJO_LOG_LEVEL_FATAL).stream() |
| 128 << "hello " << "world"; |
| 129 EXPECT_TRUE(log_message_was_called()); |
| 130 EXPECT_EQ(MOJO_LOG_LEVEL_FATAL, last_log_level()); |
| 131 EXPECT_EQ("foo.cc(123): hello world", last_message()); |
| 132 |
| 133 ResetMockLogger(); |
| 134 |
| 135 internal::LogMessage(".\\xy\\foo.cc", 123, MOJO_LOG_LEVEL_VERBOSE).stream() |
| 136 << "hello " << "world"; |
| 137 EXPECT_TRUE(log_message_was_called()); |
| 138 EXPECT_EQ(MOJO_LOG_LEVEL_VERBOSE, last_log_level()); |
| 139 EXPECT_EQ("foo.cc(123): hello world", last_message()); |
| 140 |
| 141 ResetMockLogger(); |
| 142 |
| 143 internal::LogMessage("xy\\foo.cc", 123, MOJO_LOG_LEVEL_VERBOSE-1).stream() |
| 144 << "hello " << "world"; |
| 145 EXPECT_TRUE(log_message_was_called()); |
| 146 EXPECT_EQ(MOJO_LOG_LEVEL_VERBOSE-1, last_log_level()); |
| 147 EXPECT_EQ("foo.cc(123): hello world", last_message()); |
| 148 |
| 149 ResetMockLogger(); |
| 150 |
| 151 internal::LogMessage("C:\\xy\\foo.cc", 123, MOJO_LOG_LEVEL_VERBOSE-9).stream() |
| 152 << "hello " << "world"; |
| 153 EXPECT_TRUE(log_message_was_called()); |
| 154 EXPECT_EQ(MOJO_LOG_LEVEL_VERBOSE-9, last_log_level()); |
| 155 EXPECT_EQ("foo.cc(123): hello world", last_message()); |
| 156 |
| 157 ResetMockLogger(); |
| 158 |
| 159 internal::LogMessage(__FILE__, 123, MOJO_LOG_LEVEL_INFO).stream() |
| 160 << "hello " << "world"; |
| 161 EXPECT_TRUE(log_message_was_called()); |
| 162 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level()); |
| 163 EXPECT_EQ(OUR_FILENAME "(123): hello world", last_message()); |
| 164 } |
| 165 |
| 166 TEST_F(LoggingTest, LogStream) { |
| 167 MOJO_LOG_STREAM(INFO) << "hello"; |
| 168 EXPECT_TRUE(log_message_was_called()); |
| 169 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level()); |
| 170 EXPECT_EQ(ExpectedLogMessage(__LINE__-3, "hello"), last_message()); |
| 171 |
| 172 ResetMockLogger(); |
| 173 |
| 174 MOJO_LOG_STREAM(ERROR) << "hi " << 123; |
| 175 EXPECT_TRUE(log_message_was_called()); |
| 176 EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level()); |
| 177 EXPECT_EQ(ExpectedLogMessage(__LINE__-3, "hi 123"), last_message()); |
| 178 } |
| 179 |
| 180 TEST_F(LoggingTest, LazyLogStream) { |
| 181 MOJO_LAZY_LOG_STREAM(INFO, true) << "hello"; |
| 182 EXPECT_TRUE(log_message_was_called()); |
| 183 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level()); |
| 184 EXPECT_EQ(ExpectedLogMessage(__LINE__-3, "hello"), last_message()); |
| 185 |
| 186 ResetMockLogger(); |
| 187 |
| 188 MOJO_LAZY_LOG_STREAM(ERROR, true) << "hi " << 123; |
| 189 EXPECT_TRUE(log_message_was_called()); |
| 190 EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level()); |
| 191 EXPECT_EQ(ExpectedLogMessage(__LINE__-3, "hi 123"), last_message()); |
| 192 |
| 193 ResetMockLogger(); |
| 194 |
| 195 MOJO_LAZY_LOG_STREAM(INFO, false) << "hello"; |
| 196 EXPECT_FALSE(log_message_was_called()); |
| 197 |
| 198 ResetMockLogger(); |
| 199 |
| 200 MOJO_LAZY_LOG_STREAM(FATAL, false) << "hello"; |
| 201 EXPECT_FALSE(log_message_was_called()); |
| 202 |
| 203 ResetMockLogger(); |
| 204 |
| 205 bool x = false; |
| 206 // This probably fails to compile if we forget to parenthesize the condition |
| 207 // in the macro (= has low precedence, and needs an lvalue on the LHS). |
| 208 MOJO_LAZY_LOG_STREAM(ERROR, x = true) << "hello"; |
| 209 EXPECT_TRUE(log_message_was_called()); |
| 210 |
| 211 ResetMockLogger(); |
| 212 |
| 213 MOJO_LAZY_LOG_STREAM(WARNING, x = false) << "hello"; |
| 214 EXPECT_FALSE(log_message_was_called()); |
| 215 } |
| 216 |
| 217 TEST_F(LoggingTest, ShouldLog) { |
| 218 // We start at |MOJO_LOG_LEVEL_INFO|. |
| 219 EXPECT_FALSE(MOJO_SHOULD_LOG(VERBOSE)); |
| 220 EXPECT_TRUE(MOJO_SHOULD_LOG(INFO)); |
| 221 EXPECT_TRUE(MOJO_SHOULD_LOG(WARNING)); |
| 222 EXPECT_TRUE(MOJO_SHOULD_LOG(ERROR)); |
| 223 EXPECT_TRUE(MOJO_SHOULD_LOG(FATAL)); |
| 224 |
| 225 Environment::GetDefaultLogger()->SetMinimumLogLevel(MOJO_LOG_LEVEL_ERROR); |
| 226 EXPECT_FALSE(MOJO_SHOULD_LOG(VERBOSE)); |
| 227 EXPECT_FALSE(MOJO_SHOULD_LOG(INFO)); |
| 228 EXPECT_FALSE(MOJO_SHOULD_LOG(WARNING)); |
| 229 EXPECT_TRUE(MOJO_SHOULD_LOG(ERROR)); |
| 230 EXPECT_TRUE(MOJO_SHOULD_LOG(FATAL)); |
| 231 |
| 232 Environment::GetDefaultLogger()->SetMinimumLogLevel(MOJO_LOG_LEVEL_VERBOSE-1); |
| 233 EXPECT_TRUE(MOJO_SHOULD_LOG(VERBOSE)); |
| 234 EXPECT_TRUE(MOJO_SHOULD_LOG(INFO)); |
| 235 EXPECT_TRUE(MOJO_SHOULD_LOG(WARNING)); |
| 236 EXPECT_TRUE(MOJO_SHOULD_LOG(ERROR)); |
| 237 EXPECT_TRUE(MOJO_SHOULD_LOG(FATAL)); |
| 238 } |
| 239 |
| 240 TEST_F(LoggingTest, Log) { |
| 241 // We start at |MOJO_LOG_LEVEL_INFO|. |
| 242 MOJO_LOG(VERBOSE) << "hello"; |
| 243 EXPECT_FALSE(log_message_was_called()); |
| 244 |
| 245 ResetMockLogger(); |
| 246 |
| 247 MOJO_LOG(INFO) << "hello"; |
| 248 EXPECT_TRUE(log_message_was_called()); |
| 249 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level()); |
| 250 EXPECT_EQ(ExpectedLogMessage(__LINE__-3, "hello"), last_message()); |
| 251 |
| 252 ResetMockLogger(); |
| 253 |
| 254 MOJO_LOG(ERROR) << "hello"; |
| 255 EXPECT_TRUE(log_message_was_called()); |
| 256 EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level()); |
| 257 EXPECT_EQ(ExpectedLogMessage(__LINE__-3, "hello"), last_message()); |
| 258 |
| 259 ResetMockLogger(); |
| 260 |
| 261 Environment::GetDefaultLogger()->SetMinimumLogLevel(MOJO_LOG_LEVEL_ERROR); |
| 262 |
| 263 MOJO_LOG(VERBOSE) << "hello"; |
| 264 EXPECT_FALSE(log_message_was_called()); |
| 265 |
| 266 ResetMockLogger(); |
| 267 |
| 268 MOJO_LOG(INFO) << "hello"; |
| 269 EXPECT_FALSE(log_message_was_called()); |
| 270 |
| 271 ResetMockLogger(); |
| 272 |
| 273 MOJO_LOG(ERROR) << "hello"; |
| 274 EXPECT_TRUE(log_message_was_called()); |
| 275 EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level()); |
| 276 EXPECT_EQ(ExpectedLogMessage(__LINE__-3, "hello"), last_message()); |
| 277 } |
| 278 |
| 279 TEST_F(LoggingTest, LogIf) { |
| 280 // We start at |MOJO_LOG_LEVEL_INFO|. |
| 281 MOJO_LOG_IF(VERBOSE, true) << "hello"; |
| 282 EXPECT_FALSE(log_message_was_called()); |
| 283 |
| 284 ResetMockLogger(); |
| 285 |
| 286 MOJO_LOG_IF(VERBOSE, false) << "hello"; |
| 287 EXPECT_FALSE(log_message_was_called()); |
| 288 |
| 289 ResetMockLogger(); |
| 290 |
| 291 bool x = false; |
| 292 // Also try to make sure that we parenthesize the condition properly. |
| 293 MOJO_LOG_IF(INFO, x = true) << "hello"; |
| 294 EXPECT_TRUE(log_message_was_called()); |
| 295 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level()); |
| 296 EXPECT_EQ(ExpectedLogMessage(__LINE__-3, "hello"), last_message()); |
| 297 |
| 298 ResetMockLogger(); |
| 299 |
| 300 MOJO_LOG_IF(INFO, x = false) << "hello"; |
| 301 EXPECT_FALSE(log_message_was_called()); |
| 302 |
| 303 ResetMockLogger(); |
| 304 |
| 305 Environment::GetDefaultLogger()->SetMinimumLogLevel(MOJO_LOG_LEVEL_ERROR); |
| 306 |
| 307 ResetMockLogger(); |
| 308 |
| 309 MOJO_LOG_IF(INFO, 0 != 1) << "hello"; |
| 310 EXPECT_FALSE(log_message_was_called()); |
| 311 |
| 312 ResetMockLogger(); |
| 313 |
| 314 MOJO_LOG_IF(WARNING, 1+1 == 2) << "hello"; |
| 315 EXPECT_FALSE(log_message_was_called()); |
| 316 |
| 317 ResetMockLogger(); |
| 318 |
| 319 MOJO_LOG_IF(ERROR, 1*2 == 2) << "hello"; |
| 320 EXPECT_TRUE(log_message_was_called()); |
| 321 EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level()); |
| 322 EXPECT_EQ(ExpectedLogMessage(__LINE__-3, "hello"), last_message()); |
| 323 |
| 324 ResetMockLogger(); |
| 325 |
| 326 MOJO_LOG_IF(FATAL, 1*2 == 3) << "hello"; |
| 327 EXPECT_FALSE(log_message_was_called()); |
| 328 |
| 329 ResetMockLogger(); |
| 330 |
| 331 // |MOJO_LOG_IF()| shouldn't evaluate its condition if the level is below the |
| 332 // minimum. |
| 333 MOJO_LOG_IF(INFO, NotCalled()) << "hello"; |
| 334 EXPECT_FALSE(log_message_was_called()); |
| 335 } |
| 336 |
| 337 TEST_F(LoggingTest, Check) { |
| 338 MOJO_CHECK(true) << "hello"; |
| 339 EXPECT_FALSE(log_message_was_called()); |
| 340 |
| 341 ResetMockLogger(); |
| 342 |
| 343 bool x = true; |
| 344 // Also try to make sure that we parenthesize the condition properly. |
| 345 MOJO_CHECK(x = false) << "hello"; |
| 346 EXPECT_TRUE(log_message_was_called()); |
| 347 EXPECT_EQ(MOJO_LOG_LEVEL_FATAL, last_log_level()); |
| 348 EXPECT_EQ(ExpectedLogMessage(__LINE__-3, "Check failed: x = false. hello"), |
| 349 last_message()); |
| 350 |
| 351 ResetMockLogger(); |
| 352 |
| 353 // Also test a "naked" |MOJO_CHECK()|s. |
| 354 MOJO_CHECK(1+2 == 3); |
| 355 EXPECT_FALSE(log_message_was_called()); |
| 356 } |
| 357 |
| 358 TEST_F(LoggingTest, Dlog) { |
| 359 // We start at |MOJO_LOG_LEVEL_INFO|. |
| 360 MOJO_DLOG(VERBOSE) << "hello"; |
| 361 EXPECT_FALSE(log_message_was_called()); |
| 362 |
| 363 ResetMockLogger(); |
| 364 |
| 365 MOJO_DLOG(INFO) << "hello"; |
| 366 #ifdef NDEBUG |
| 367 EXPECT_FALSE(log_message_was_called()); |
| 368 #else |
| 369 EXPECT_TRUE(log_message_was_called()); |
| 370 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level()); |
| 371 EXPECT_EQ(ExpectedLogMessage(__LINE__-6, "hello"), last_message()); |
| 372 #endif |
| 373 } |
| 374 |
| 375 TEST_F(LoggingTest, DlogIf) { |
| 376 // We start at |MOJO_LOG_LEVEL_INFO|. It shouldn't evaluate the condition in |
| 377 // this case. |
| 378 MOJO_DLOG_IF(VERBOSE, NotCalled()) << "hello"; |
| 379 EXPECT_FALSE(log_message_was_called()); |
| 380 |
| 381 ResetMockLogger(); |
| 382 |
| 383 MOJO_DLOG_IF(INFO, 1 == 0) << "hello"; |
| 384 EXPECT_FALSE(log_message_was_called()); |
| 385 |
| 386 ResetMockLogger(); |
| 387 |
| 388 MOJO_DLOG_IF(INFO, 1 == 1) << "hello"; |
| 389 #ifdef NDEBUG |
| 390 EXPECT_FALSE(log_message_was_called()); |
| 391 #else |
| 392 EXPECT_TRUE(log_message_was_called()); |
| 393 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level()); |
| 394 EXPECT_EQ(ExpectedLogMessage(__LINE__-6, "hello"), last_message()); |
| 395 #endif |
| 396 |
| 397 ResetMockLogger(); |
| 398 |
| 399 // |MOJO_DLOG_IF()| shouldn't compile its condition for non-debug builds. |
| 400 #ifndef NDEBUG |
| 401 bool debug_only = true; |
| 402 #endif |
| 403 MOJO_DLOG_IF(WARNING, debug_only) << "hello"; |
| 404 #ifdef NDEBUG |
| 405 EXPECT_FALSE(log_message_was_called()); |
| 406 #else |
| 407 EXPECT_TRUE(log_message_was_called()); |
| 408 EXPECT_EQ(MOJO_LOG_LEVEL_WARNING, last_log_level()); |
| 409 EXPECT_EQ(ExpectedLogMessage(__LINE__-6, "hello"), last_message()); |
| 410 #endif |
| 411 } |
| 412 |
| 413 TEST_F(LoggingTest, Dcheck) { |
| 414 MOJO_DCHECK(true); |
| 415 EXPECT_FALSE(log_message_was_called()); |
| 416 |
| 417 ResetMockLogger(); |
| 418 |
| 419 MOJO_DCHECK(true) << "hello"; |
| 420 EXPECT_FALSE(log_message_was_called()); |
| 421 |
| 422 ResetMockLogger(); |
| 423 |
| 424 // |MOJO_DCHECK()| should compile (but not evaluate) its condition even for |
| 425 // non-debug builds. (Hopefully, we'll get an unused variable error if it |
| 426 // fails to compile the condition.) |
| 427 bool x = true; |
| 428 MOJO_DCHECK(x = false) << "hello"; |
| 429 #ifdef NDEBUG |
| 430 EXPECT_FALSE(log_message_was_called()); |
| 431 #else |
| 432 EXPECT_TRUE(log_message_was_called()); |
| 433 EXPECT_EQ(MOJO_LOG_LEVEL_FATAL, last_log_level()); |
| 434 EXPECT_EQ(ExpectedLogMessage(__LINE__-6, "Check failed: x = false. hello"), |
| 435 last_message()); |
| 436 #endif |
| 437 } |
| 438 |
| 439 } // namespace |
| 440 } // namespace mojo |
OLD | NEW |