Quota: Remove ifdef macros used for callback cleanup
[chromium-blink-merge.git] / base / logging_unittest.cc
blob4996abc2240661939217ad04a9ce6d8d35aa1931
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
3 // found in the LICENSE file.
5 #include "base/basictypes.h"
6 #include "base/logging.h"
8 #include "testing/gmock/include/gmock/gmock.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace logging {
13 namespace {
15 using ::testing::Return;
17 // Needs to be global since log assert handlers can't maintain state.
18 int log_sink_call_count = 0;
20 #if !LOGGING_IS_OFFICIAL_BUILD
21 void LogSink(const std::string& str) {
22 ++log_sink_call_count;
24 #endif // !LOGGING_IS_OFFICIAL_BUILD
26 // Class to make sure any manipulations we do to the min log level are
27 // contained (i.e., do not affect other unit tests).
28 class LogStateSaver {
29 public:
30 LogStateSaver() : old_min_log_level_(GetMinLogLevel()) {}
32 ~LogStateSaver() {
33 SetMinLogLevel(old_min_log_level_);
34 SetLogAssertHandler(NULL);
35 SetLogReportHandler(NULL);
36 log_sink_call_count = 0;
39 private:
40 int old_min_log_level_;
42 DISALLOW_COPY_AND_ASSIGN(LogStateSaver);
45 class LoggingTest : public testing::Test {
46 private:
47 LogStateSaver log_state_saver_;
50 class MockLogSource {
51 public:
52 MOCK_METHOD0(Log, const char*());
55 TEST_F(LoggingTest, BasicLogging) {
56 MockLogSource mock_log_source;
57 const int kExpectedDebugOrReleaseCalls = 6;
58 const int kExpectedDebugCalls = 6;
59 const int kExpectedCalls =
60 kExpectedDebugOrReleaseCalls + (DEBUG_MODE ? kExpectedDebugCalls : 0);
61 EXPECT_CALL(mock_log_source, Log()).Times(kExpectedCalls).
62 WillRepeatedly(Return("log message"));
64 SetMinLogLevel(LOG_INFO);
66 EXPECT_TRUE(LOG_IS_ON(INFO));
67 // As of g++-4.5, the first argument to EXPECT_EQ cannot be a
68 // constant expression.
69 const bool kIsDebugMode = (DEBUG_MODE != 0);
70 EXPECT_TRUE(kIsDebugMode == DLOG_IS_ON(INFO));
71 EXPECT_TRUE(VLOG_IS_ON(0));
73 LOG(INFO) << mock_log_source.Log();
74 LOG_IF(INFO, true) << mock_log_source.Log();
75 PLOG(INFO) << mock_log_source.Log();
76 PLOG_IF(INFO, true) << mock_log_source.Log();
77 VLOG(0) << mock_log_source.Log();
78 VLOG_IF(0, true) << mock_log_source.Log();
80 DLOG(INFO) << mock_log_source.Log();
81 DLOG_IF(INFO, true) << mock_log_source.Log();
82 DPLOG(INFO) << mock_log_source.Log();
83 DPLOG_IF(INFO, true) << mock_log_source.Log();
84 DVLOG(0) << mock_log_source.Log();
85 DVLOG_IF(0, true) << mock_log_source.Log();
88 TEST_F(LoggingTest, LogIsOn) {
89 #if defined(NDEBUG)
90 const bool kDfatalIsFatal = false;
91 #else // defined(NDEBUG)
92 const bool kDfatalIsFatal = true;
93 #endif // defined(NDEBUG)
95 SetMinLogLevel(LOG_INFO);
96 EXPECT_TRUE(LOG_IS_ON(INFO));
97 EXPECT_TRUE(LOG_IS_ON(WARNING));
98 EXPECT_TRUE(LOG_IS_ON(ERROR));
99 EXPECT_TRUE(LOG_IS_ON(ERROR_REPORT));
100 EXPECT_TRUE(LOG_IS_ON(FATAL));
101 EXPECT_TRUE(LOG_IS_ON(DFATAL));
103 SetMinLogLevel(LOG_WARNING);
104 EXPECT_FALSE(LOG_IS_ON(INFO));
105 EXPECT_TRUE(LOG_IS_ON(WARNING));
106 EXPECT_TRUE(LOG_IS_ON(ERROR));
107 EXPECT_TRUE(LOG_IS_ON(ERROR_REPORT));
108 EXPECT_TRUE(LOG_IS_ON(FATAL));
109 EXPECT_TRUE(LOG_IS_ON(DFATAL));
111 SetMinLogLevel(LOG_ERROR);
112 EXPECT_FALSE(LOG_IS_ON(INFO));
113 EXPECT_FALSE(LOG_IS_ON(WARNING));
114 EXPECT_TRUE(LOG_IS_ON(ERROR));
115 EXPECT_TRUE(LOG_IS_ON(ERROR_REPORT));
116 EXPECT_TRUE(LOG_IS_ON(FATAL));
117 EXPECT_TRUE(LOG_IS_ON(DFATAL));
119 SetMinLogLevel(LOG_ERROR_REPORT);
120 EXPECT_FALSE(LOG_IS_ON(INFO));
121 EXPECT_FALSE(LOG_IS_ON(WARNING));
122 EXPECT_FALSE(LOG_IS_ON(ERROR));
123 EXPECT_TRUE(LOG_IS_ON(ERROR_REPORT));
124 EXPECT_TRUE(LOG_IS_ON(FATAL));
125 EXPECT_TRUE(kDfatalIsFatal == LOG_IS_ON(DFATAL));
127 // LOG_IS_ON(ERROR_REPORT) should always be true.
128 SetMinLogLevel(LOG_FATAL);
129 EXPECT_FALSE(LOG_IS_ON(INFO));
130 EXPECT_FALSE(LOG_IS_ON(WARNING));
131 EXPECT_FALSE(LOG_IS_ON(ERROR));
132 EXPECT_TRUE(LOG_IS_ON(ERROR_REPORT));
133 EXPECT_TRUE(LOG_IS_ON(FATAL));
134 EXPECT_TRUE(kDfatalIsFatal == LOG_IS_ON(DFATAL));
136 // So should LOG_IS_ON(FATAL).
137 SetMinLogLevel(LOG_FATAL + 1);
138 EXPECT_FALSE(LOG_IS_ON(INFO));
139 EXPECT_FALSE(LOG_IS_ON(WARNING));
140 EXPECT_FALSE(LOG_IS_ON(ERROR));
141 EXPECT_TRUE(LOG_IS_ON(ERROR_REPORT));
142 EXPECT_TRUE(LOG_IS_ON(FATAL));
143 EXPECT_TRUE(kDfatalIsFatal == LOG_IS_ON(DFATAL));
146 TEST_F(LoggingTest, LoggingIsLazy) {
147 MockLogSource mock_log_source;
148 EXPECT_CALL(mock_log_source, Log()).Times(0);
150 SetMinLogLevel(LOG_WARNING);
152 EXPECT_FALSE(LOG_IS_ON(INFO));
153 EXPECT_FALSE(DLOG_IS_ON(INFO));
154 EXPECT_FALSE(VLOG_IS_ON(1));
156 LOG(INFO) << mock_log_source.Log();
157 LOG_IF(INFO, false) << mock_log_source.Log();
158 PLOG(INFO) << mock_log_source.Log();
159 PLOG_IF(INFO, false) << mock_log_source.Log();
160 VLOG(1) << mock_log_source.Log();
161 VLOG_IF(1, true) << mock_log_source.Log();
163 DLOG(INFO) << mock_log_source.Log();
164 DLOG_IF(INFO, true) << mock_log_source.Log();
165 DPLOG(INFO) << mock_log_source.Log();
166 DPLOG_IF(INFO, true) << mock_log_source.Log();
167 DVLOG(1) << mock_log_source.Log();
168 DVLOG_IF(1, true) << mock_log_source.Log();
171 // Official builds have CHECKs directly call BreakDebugger.
172 #if !LOGGING_IS_OFFICIAL_BUILD
174 TEST_F(LoggingTest, CheckStreamsAreLazy) {
175 MockLogSource mock_log_source, uncalled_mock_log_source;
176 EXPECT_CALL(mock_log_source, Log()).Times(8).
177 WillRepeatedly(Return("check message"));
178 EXPECT_CALL(uncalled_mock_log_source, Log()).Times(0);
180 SetLogAssertHandler(&LogSink);
182 CHECK(mock_log_source.Log()) << uncalled_mock_log_source.Log();
183 PCHECK(!mock_log_source.Log()) << mock_log_source.Log();
184 CHECK_EQ(mock_log_source.Log(), mock_log_source.Log())
185 << uncalled_mock_log_source.Log();
186 CHECK_NE(mock_log_source.Log(), mock_log_source.Log())
187 << mock_log_source.Log();
190 #endif
192 TEST_F(LoggingTest, DebugLoggingReleaseBehavior) {
193 #if !defined(NDEBUG)
194 int debug_only_variable = 1;
195 #endif
196 // These should avoid emitting references to |debug_only_variable|
197 // in release mode.
198 DLOG_IF(INFO, debug_only_variable) << "test";
199 DLOG_ASSERT(debug_only_variable) << "test";
200 DPLOG_IF(INFO, debug_only_variable) << "test";
201 DVLOG_IF(1, debug_only_variable) << "test";
204 TEST_F(LoggingTest, DcheckStreamsAreLazy) {
205 MockLogSource mock_log_source;
206 EXPECT_CALL(mock_log_source, Log()).Times(0);
207 #if !LOGGING_IS_OFFICIAL_BUILD && defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
208 // Unofficial release build without dcheck enabled.
209 set_dcheck_state(DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS);
210 DCHECK(mock_log_source.Log()) << mock_log_source.Log();
211 DPCHECK(mock_log_source.Log()) << mock_log_source.Log();
212 DCHECK_EQ(0, 0) << mock_log_source.Log();
213 DCHECK_EQ(mock_log_source.Log(), static_cast<const char*>(NULL))
214 << mock_log_source.Log();
215 #endif
218 TEST_F(LoggingTest, Dcheck) {
219 #if LOGGING_IS_OFFICIAL_BUILD
220 // Official build.
221 EXPECT_FALSE(DCHECK_IS_ON());
222 EXPECT_FALSE(DLOG_IS_ON(DCHECK));
223 #elif defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
224 // Unofficial release build.
225 set_dcheck_state(ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS);
226 SetLogReportHandler(&LogSink);
227 EXPECT_TRUE(DCHECK_IS_ON());
228 EXPECT_FALSE(DLOG_IS_ON(DCHECK));
229 #elif defined(NDEBUG) && defined(DCHECK_ALWAYS_ON)
230 // Unofficial release build with real DCHECKS.
231 set_dcheck_state(ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS);
232 SetLogAssertHandler(&LogSink);
233 EXPECT_TRUE(DCHECK_IS_ON());
234 EXPECT_FALSE(DLOG_IS_ON(DCHECK));
235 #else
236 // Unofficial debug build.
237 SetLogAssertHandler(&LogSink);
238 EXPECT_TRUE(DCHECK_IS_ON());
239 EXPECT_TRUE(DLOG_IS_ON(DCHECK));
240 #endif // defined(LOGGING_IS_OFFICIAL_BUILD)
242 EXPECT_EQ(0, log_sink_call_count);
243 DCHECK(false);
244 EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, log_sink_call_count);
245 DPCHECK(false);
246 EXPECT_EQ(DCHECK_IS_ON() ? 2 : 0, log_sink_call_count);
247 DCHECK_EQ(0, 1);
248 EXPECT_EQ(DCHECK_IS_ON() ? 3 : 0, log_sink_call_count);
251 TEST_F(LoggingTest, DcheckReleaseBehavior) {
252 int some_variable = 1;
253 // These should still reference |some_variable| so we don't get
254 // unused variable warnings.
255 DCHECK(some_variable) << "test";
256 DPCHECK(some_variable) << "test";
257 DCHECK_EQ(some_variable, 1) << "test";
260 } // namespace
262 } // namespace logging