Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: base/logging_unittest.cc

Issue 2034393004: Allow multiple logging::LogMessage{Handler,Listener}s Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: revert slow, try adding back handlers Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/compiler_specific.h" 5 #include "base/compiler_specific.h"
6 #include "base/logging.h" 6 #include "base/logging.h"
7 #include "base/macros.h" 7 #include "base/macros.h"
8 8
9 #include "testing/gmock/include/gmock/gmock.h" 9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
11 11
12 namespace logging { 12 namespace logging {
13 13
14 namespace { 14 namespace {
15 15
16 using ::testing::HasSubstr;
16 using ::testing::Return; 17 using ::testing::Return;
18 using ::testing::_;
17 19
18 // Needs to be global since log assert handlers can't maintain state. 20 // Needs to be global since log assert handlers can't maintain state.
19 int log_sink_call_count = 0; 21 int log_sink_call_count = 0;
20 22
21 #if !defined(OFFICIAL_BUILD) || defined(DCHECK_ALWAYS_ON) || !defined(NDEBUG) 23 #if !defined(OFFICIAL_BUILD) || defined(DCHECK_ALWAYS_ON) || !defined(NDEBUG)
22 void LogSink(const std::string& str) { 24 void LogSink(const std::string& str) {
23 ++log_sink_call_count; 25 ++log_sink_call_count;
24 } 26 }
25 #endif 27 #endif
26 28
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 if (false) 321 if (false)
320 CHECK_EQ(false, true); // Unreached. 322 CHECK_EQ(false, true); // Unreached.
321 else 323 else
322 CHECK_EQ(true, reached = true); // Reached, passed. 324 CHECK_EQ(true, reached = true); // Reached, passed.
323 ASSERT_TRUE(reached); 325 ASSERT_TRUE(reached);
324 326
325 if (false) 327 if (false)
326 CHECK_EQ(false, true); // Unreached. 328 CHECK_EQ(false, true); // Unreached.
327 } 329 }
328 330
331 class MockLogMessageHandler : logging::LogMessageHandler {
332 public:
333 MOCK_METHOD5(OnMessage,
334 bool(int severity,
335 const char* file,
336 int line,
337 size_t message_start,
338 const std::string& str));
339 };
340
341 TEST_F(LoggingTest, LogHandlerSink) {
342 size_t count = LogMessageHandlerCountForTesting();
343 {
344 MockLogMessageHandler log, log2, log3;
345 EXPECT_EQ(count + 3, LogMessageHandlerCountForTesting());
346
347 testing::InSequence s;
348 EXPECT_CALL(log3, OnMessage(_, _, _, _, HasSubstr("testing1")))
349 .WillOnce(::testing::Return(false));
350 EXPECT_CALL(log2, OnMessage(_, _, _, _, HasSubstr("testing1")))
351 .WillOnce(::testing::Return(true));
352 EXPECT_CALL(log, OnMessage(_, _, _, _, HasSubstr("testing1"))).Times(0);
353
354 EXPECT_CALL(log3, OnMessage(_, _, _, _, HasSubstr("testing2")))
355 .WillOnce(::testing::Return(false));
356 EXPECT_CALL(log2, OnMessage(_, _, _, _, HasSubstr("testing2")))
357 .WillOnce(::testing::Return(false));
358 EXPECT_CALL(log, OnMessage(_, _, _, _, HasSubstr("testing2"))).Times(1);
359
360 LOG(WARNING) << "testing1";
361 LOG(WARNING) << "testing2";
362 }
363 EXPECT_EQ(count, LogMessageHandlerCountForTesting());
364 }
365
366 class MockLogMessageListener : logging::LogMessageListener {
367 public:
368 MockLogMessageListener(std::string& buffer) : buffer_(buffer) {}
369 void OnMessage(int severity,
370 const char* file,
371 int line,
372 size_t message_start,
373 const std::string& str) override {
374 buffer_.append(str);
375 }
376
377 private:
378 std::string& buffer_;
379 };
380
381 TEST_F(LoggingTest, LogListenerBasic) {
382 std::string buffer;
383 MockLogMessageListener log(buffer);
384
385 LOG(INFO) << "testing1";
386 LOG(WARNING) << "testing2";
387
388 EXPECT_NE(std::string::npos, buffer.find("testing1"));
389 EXPECT_NE(std::string::npos, buffer.find("testing2"));
390 }
391
392 TEST_F(LoggingTest, LogListenerLifespan) {
393 size_t count = LogMessageListenerCountForTesting();
394 std::string buf1, buf2;
395 MockLogMessageListener* log1 = nullptr;
396 {
397 MockLogMessageListener log2(buf2);
398 EXPECT_EQ(count + 1, LogMessageListenerCountForTesting());
399
400 LOG(WARNING) << "testing1";
401 log1 = new MockLogMessageListener(buf1);
402 EXPECT_EQ(count + 2, LogMessageListenerCountForTesting());
403 LOG(WARNING) << "testing2";
404 }
405 EXPECT_EQ(count + 1, LogMessageListenerCountForTesting());
406 LOG(WARNING) << "testing3";
407 delete log1;
408 EXPECT_EQ(count, LogMessageListenerCountForTesting());
409
410 EXPECT_EQ(std::string::npos, buf1.find("testing1"));
411 EXPECT_NE(std::string::npos, buf1.find("testing2"));
412 EXPECT_NE(std::string::npos, buf1.find("testing3"));
413 EXPECT_NE(std::string::npos, buf2.find("testing1"));
414 EXPECT_NE(std::string::npos, buf2.find("testing2"));
415 EXPECT_EQ(std::string::npos, buf2.find("testing3"));
416 }
417
418 class ChattyLogMessageListener : logging::LogMessageListener {
419 public:
420 ChattyLogMessageListener() : count_(0) {}
421 void OnMessage(int severity,
422 const char* file,
423 int line,
424 size_t message_start,
425 const std::string& str) override {
426 count_++;
427 if (count_ == 1) {
428 LOG(ERROR) << "message inside OnMessage()";
429 }
430 }
431 int Count() { return count_; }
432
433 private:
434 int count_;
435 };
436
437 TEST_F(LoggingTest, LogListenerReentrant) {
438 ChattyLogMessageListener log;
439
440 LOG(INFO) << "testing1";
441 EXPECT_EQ(2, log.Count());
442 }
443
329 // Test that defining an operator<< for a type in a namespace doesn't prevent 444 // Test that defining an operator<< for a type in a namespace doesn't prevent
330 // other code in that namespace from calling the operator<<(ostream, wstring) 445 // other code in that namespace from calling the operator<<(ostream, wstring)
331 // defined by logging.h. This can fail if operator<<(ostream, wstring) can't be 446 // defined by logging.h. This can fail if operator<<(ostream, wstring) can't be
332 // found by ADL, since defining another operator<< prevents name lookup from 447 // found by ADL, since defining another operator<< prevents name lookup from
333 // looking in the global namespace. 448 // looking in the global namespace.
334 namespace nested_test { 449 namespace nested_test {
335 class Streamable {}; 450 class Streamable {};
336 ALLOW_UNUSED_TYPE std::ostream& operator<<(std::ostream& out, 451 ALLOW_UNUSED_TYPE std::ostream& operator<<(std::ostream& out,
337 const Streamable&) { 452 const Streamable&) {
338 return out << "Streamable"; 453 return out << "Streamable";
339 } 454 }
340 TEST_F(LoggingTest, StreamingWstringFindsCorrectOperator) { 455 TEST_F(LoggingTest, StreamingWstringFindsCorrectOperator) {
341 std::wstring wstr = L"Hello World"; 456 std::wstring wstr = L"Hello World";
342 std::ostringstream ostr; 457 std::ostringstream ostr;
343 ostr << wstr; 458 ostr << wstr;
344 EXPECT_EQ("Hello World", ostr.str()); 459 EXPECT_EQ("Hello World", ostr.str());
345 } 460 }
346 } // namespace nested_test 461 } // namespace nested_test
347 462
348 } // namespace 463 } // namespace
349 464
350 } // namespace logging 465 } // namespace logging
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698