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

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 Created 4 years, 1 month 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 MockLogMessageListener : logging::LogMessageListener {
299 public:
300 MockLogMessageListener(std::string& buffer) : buffer_(buffer) {}
301 void OnMessage(int severity,
302 const char* file,
303 int line,
304 size_t message_start,
305 const std::string& str) override {
306 buffer_.append(str);
307 }
308
309 private:
310 std::string& buffer_;
311 };
312
313 TEST_F(LoggingTest, LogListenerBasic) {
314 std::string buffer;
315 MockLogMessageListener log(buffer);
316
317 LOG(INFO) << "testing1";
318 LOG(WARNING) << "testing2";
319
320 EXPECT_NE(std::string::npos, buffer.find("testing1"));
321 EXPECT_NE(std::string::npos, buffer.find("testing2"));
322 }
323
324 TEST_F(LoggingTest, LogListenerLifespan) {
325 size_t count = LogMessageListenerCountForTesting();
326 std::string buf1, buf2;
327 MockLogMessageListener* log1 = nullptr;
328 {
329 MockLogMessageListener log2(buf2);
330 EXPECT_EQ(count + 1, LogMessageListenerCountForTesting());
331
332 LOG(WARNING) << "testing1";
333 log1 = new MockLogMessageListener(buf1);
334 EXPECT_EQ(count + 2, LogMessageListenerCountForTesting());
335 LOG(WARNING) << "testing2";
336 }
337 EXPECT_EQ(count + 1, LogMessageListenerCountForTesting());
338 LOG(WARNING) << "testing3";
339 delete log1;
340 EXPECT_EQ(count, LogMessageListenerCountForTesting());
341
342 EXPECT_EQ(std::string::npos, buf1.find("testing1"));
343 EXPECT_NE(std::string::npos, buf1.find("testing2"));
344 EXPECT_NE(std::string::npos, buf1.find("testing3"));
345 EXPECT_NE(std::string::npos, buf2.find("testing1"));
346 EXPECT_NE(std::string::npos, buf2.find("testing2"));
347 EXPECT_EQ(std::string::npos, buf2.find("testing3"));
348 }
349
350 class ChattyLogMessageListener : logging::LogMessageListener {
351 public:
352 ChattyLogMessageListener() : count_(0) {}
353 void OnMessage(int severity,
354 const char* file,
355 int line,
356 size_t message_start,
357 const std::string& str) override {
358 count_++;
359 if (count_ == 1) {
360 LOG(ERROR) << "message inside OnMessage()";
361 }
362 }
363 int Count() { return count_; }
364
365 private:
366 int count_;
367 };
368
369 TEST_F(LoggingTest, LogListenerReentrant) {
370 ChattyLogMessageListener log;
371
372 LOG(INFO) << "testing1";
373 EXPECT_EQ(2, log.Count());
374 }
375
296 // Test that defining an operator<< for a type in a namespace doesn't prevent 376 // 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) 377 // 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 378 // 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 379 // found by ADL, since defining another operator<< prevents name lookup from
300 // looking in the global namespace. 380 // looking in the global namespace.
301 namespace nested_test { 381 namespace nested_test {
302 class Streamable {}; 382 class Streamable {};
303 ALLOW_UNUSED_TYPE std::ostream& operator<<(std::ostream& out, 383 ALLOW_UNUSED_TYPE std::ostream& operator<<(std::ostream& out,
304 const Streamable&) { 384 const Streamable&) {
305 return out << "Streamable"; 385 return out << "Streamable";
306 } 386 }
307 TEST_F(LoggingTest, StreamingWstringFindsCorrectOperator) { 387 TEST_F(LoggingTest, StreamingWstringFindsCorrectOperator) {
308 std::wstring wstr = L"Hello World"; 388 std::wstring wstr = L"Hello World";
309 std::ostringstream ostr; 389 std::ostringstream ostr;
310 ostr << wstr; 390 ostr << wstr;
311 EXPECT_EQ("Hello World", ostr.str()); 391 EXPECT_EQ("Hello World", ostr.str());
312 } 392 }
313 } // namespace nested_test 393 } // namespace nested_test
314 394
315 } // namespace 395 } // namespace
316 396
317 } // namespace logging 397 } // namespace logging
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698