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

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: address comments, deque for listeners, reentrant test Created 4 years, 4 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 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 if (false) 288 if (false)
287 CHECK_EQ(false, true); // Unreached. 289 CHECK_EQ(false, true); // Unreached.
288 else 290 else
289 CHECK_EQ(true, reached = true); // Reached, passed. 291 CHECK_EQ(true, reached = true); // Reached, passed.
290 ASSERT_TRUE(reached); 292 ASSERT_TRUE(reached);
291 293
292 if (false) 294 if (false)
293 CHECK_EQ(false, true); // Unreached. 295 CHECK_EQ(false, true); // Unreached.
294 } 296 }
295 297
298 class MockLogMessageHandler : logging::LogMessageHandler {
299 public:
300 MOCK_METHOD5(OnMessage,
301 bool(int severity,
302 const char* file,
303 int line,
304 size_t message_start,
305 const std::string& str));
306 };
307
308 TEST_F(LoggingTest, LogHandlerSink) {
309 size_t count = LogMessageHandlerCountForTesting();
310 {
311 MockLogMessageHandler log, log2, log3;
312 EXPECT_EQ(count + 3, LogMessageHandlerCountForTesting());
313
314 testing::InSequence s;
315 EXPECT_CALL(log3, OnMessage(_, _, _, _, HasSubstr("testing1")))
316 .WillOnce(::testing::Return(false));
317 EXPECT_CALL(log2, OnMessage(_, _, _, _, HasSubstr("testing1")))
318 .WillOnce(::testing::Return(true));
319 EXPECT_CALL(log, OnMessage(_, _, _, _, HasSubstr("testing1"))).Times(0);
320
321 EXPECT_CALL(log3, OnMessage(_, _, _, _, HasSubstr("testing2")))
322 .WillOnce(::testing::Return(false));
323 EXPECT_CALL(log2, OnMessage(_, _, _, _, HasSubstr("testing2")))
324 .WillOnce(::testing::Return(false));
325 EXPECT_CALL(log, OnMessage(_, _, _, _, HasSubstr("testing2"))).Times(1);
326
327 LOG(WARNING) << "testing1";
328 LOG(WARNING) << "testing2";
329 }
330 EXPECT_EQ(count, LogMessageHandlerCountForTesting());
331 }
332
333 class MockLogMessageListener : logging::LogMessageListener {
334 public:
335 MockLogMessageListener(std::string& buffer) : buffer_(buffer) {}
336 void OnMessage(int severity,
337 const char* file,
338 int line,
339 size_t message_start,
340 const std::string& str) override {
341 buffer_.append(str);
342 }
343
344 private:
345 std::string& buffer_;
346 };
347
348 TEST_F(LoggingTest, LogListenerBasic) {
349 std::string buffer;
350 MockLogMessageListener log(buffer);
351
352 LOG(INFO) << "testing1";
353 LOG(WARNING) << "testing2";
354
355 EXPECT_NE(std::string::npos, buffer.find("testing1"));
356 EXPECT_NE(std::string::npos, buffer.find("testing2"));
357 }
358
359 TEST_F(LoggingTest, LogListenerLifespan) {
360 size_t count = LogMessageListenerCountForTesting();
361 std::string buf1, buf2;
362 MockLogMessageListener* log1 = nullptr;
363 {
364 MockLogMessageListener log2(buf2);
365 EXPECT_EQ(count + 1, LogMessageListenerCountForTesting());
366
367 LOG(WARNING) << "testing1";
368 log1 = new MockLogMessageListener(buf1);
369 EXPECT_EQ(count + 2, LogMessageListenerCountForTesting());
370 LOG(WARNING) << "testing2";
371 }
372 EXPECT_EQ(count + 1, LogMessageListenerCountForTesting());
373 LOG(WARNING) << "testing3";
374 delete log1;
375 EXPECT_EQ(count, LogMessageListenerCountForTesting());
376
377 EXPECT_EQ(std::string::npos, buf1.find("testing1"));
378 EXPECT_NE(std::string::npos, buf1.find("testing2"));
379 EXPECT_NE(std::string::npos, buf1.find("testing3"));
380 EXPECT_NE(std::string::npos, buf2.find("testing1"));
381 EXPECT_NE(std::string::npos, buf2.find("testing2"));
382 EXPECT_EQ(std::string::npos, buf2.find("testing3"));
383 }
384
385 class ChattyLogMessageListener : logging::LogMessageListener {
386 public:
387 ChattyLogMessageListener() : count_(0) {}
388 void OnMessage(int severity,
389 const char* file,
390 int line,
391 size_t message_start,
392 const std::string& str) override {
393 count_++;
394 if (count_ == 1) {
395 LOG(ERROR) << "message inside OnMessage()";
396 }
397 }
398 int Count() { return count_; }
399
400 private:
401 int count_;
402 };
403
404 TEST_F(LoggingTest, LogListenerReentrant) {
405 ChattyLogMessageListener log;
406
407 LOG(INFO) << "testing1";
408 EXPECT_EQ(2, log.Count());
409 }
410
296 // Test that defining an operator<< for a type in a namespace doesn't prevent 411 // Test that defining an operator<< for a type in a namespace doesn't prevent
297 // other code in that namespace from calling the operator<<(ostream, wstring) 412 // other code in that namespace from calling the operator<<(ostream, wstring)
298 // defined by logging.h. This can fail if operator<<(ostream, wstring) can't be 413 // defined by logging.h. This can fail if operator<<(ostream, wstring) can't be
299 // found by ADL, since defining another operator<< prevents name lookup from 414 // found by ADL, since defining another operator<< prevents name lookup from
300 // looking in the global namespace. 415 // looking in the global namespace.
301 namespace nested_test { 416 namespace nested_test {
302 class Streamable {}; 417 class Streamable {};
303 ALLOW_UNUSED_TYPE std::ostream& operator<<(std::ostream& out, 418 ALLOW_UNUSED_TYPE std::ostream& operator<<(std::ostream& out,
304 const Streamable&) { 419 const Streamable&) {
305 return out << "Streamable"; 420 return out << "Streamable";
306 } 421 }
307 TEST_F(LoggingTest, StreamingWstringFindsCorrectOperator) { 422 TEST_F(LoggingTest, StreamingWstringFindsCorrectOperator) {
308 std::wstring wstr = L"Hello World"; 423 std::wstring wstr = L"Hello World";
309 std::ostringstream ostr; 424 std::ostringstream ostr;
310 ostr << wstr; 425 ostr << wstr;
311 EXPECT_EQ("Hello World", ostr.str()); 426 EXPECT_EQ("Hello World", ostr.str());
312 } 427 }
313 } // namespace nested_test 428 } // namespace nested_test
314 429
315 } // namespace 430 } // namespace
316 431
317 } // namespace logging 432 } // namespace logging
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698