OLD | NEW |
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/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/logging.h" | 6 #include "base/logging.h" |
7 | 7 |
8 #include "testing/gmock/include/gmock/gmock.h" | 8 #include "testing/gmock/include/gmock/gmock.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
11 namespace logging { | 11 namespace logging { |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 using ::testing::Return; | 15 using ::testing::Return; |
16 | 16 |
17 // Needs to be global since log assert handlers can't maintain state. | 17 // Needs to be global since log assert handlers can't maintain state. |
18 int log_sink_call_count = 0; | 18 int log_sink_call_count = 0; |
19 | 19 |
20 #if !LOGGING_IS_OFFICIAL_BUILD | 20 #if !defined(OFFICIAL_BUILD) || defined(DCHECK_ALWAYS_ON) || !defined(NDEBUG) |
21 void LogSink(const std::string& str) { | 21 void LogSink(const std::string& str) { |
22 ++log_sink_call_count; | 22 ++log_sink_call_count; |
23 } | 23 } |
24 #endif // !LOGGING_IS_OFFICIAL_BUILD | 24 #endif |
25 | 25 |
26 // Class to make sure any manipulations we do to the min log level are | 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). | 27 // contained (i.e., do not affect other unit tests). |
28 class LogStateSaver { | 28 class LogStateSaver { |
29 public: | 29 public: |
30 LogStateSaver() : old_min_log_level_(GetMinLogLevel()) {} | 30 LogStateSaver() : old_min_log_level_(GetMinLogLevel()) {} |
31 | 31 |
32 ~LogStateSaver() { | 32 ~LogStateSaver() { |
33 SetMinLogLevel(old_min_log_level_); | 33 SetMinLogLevel(old_min_log_level_); |
34 SetLogAssertHandler(NULL); | 34 SetLogAssertHandler(NULL); |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 | 162 |
163 DLOG(INFO) << mock_log_source.Log(); | 163 DLOG(INFO) << mock_log_source.Log(); |
164 DLOG_IF(INFO, true) << mock_log_source.Log(); | 164 DLOG_IF(INFO, true) << mock_log_source.Log(); |
165 DPLOG(INFO) << mock_log_source.Log(); | 165 DPLOG(INFO) << mock_log_source.Log(); |
166 DPLOG_IF(INFO, true) << mock_log_source.Log(); | 166 DPLOG_IF(INFO, true) << mock_log_source.Log(); |
167 DVLOG(1) << mock_log_source.Log(); | 167 DVLOG(1) << mock_log_source.Log(); |
168 DVLOG_IF(1, true) << mock_log_source.Log(); | 168 DVLOG_IF(1, true) << mock_log_source.Log(); |
169 } | 169 } |
170 | 170 |
171 // Official builds have CHECKs directly call BreakDebugger. | 171 // Official builds have CHECKs directly call BreakDebugger. |
172 #if !LOGGING_IS_OFFICIAL_BUILD | 172 #if !defined(OFFICIAL_BUILD) |
173 | 173 |
174 TEST_F(LoggingTest, CheckStreamsAreLazy) { | 174 TEST_F(LoggingTest, CheckStreamsAreLazy) { |
175 MockLogSource mock_log_source, uncalled_mock_log_source; | 175 MockLogSource mock_log_source, uncalled_mock_log_source; |
176 EXPECT_CALL(mock_log_source, Log()).Times(8). | 176 EXPECT_CALL(mock_log_source, Log()).Times(8). |
177 WillRepeatedly(Return("check message")); | 177 WillRepeatedly(Return("check message")); |
178 EXPECT_CALL(uncalled_mock_log_source, Log()).Times(0); | 178 EXPECT_CALL(uncalled_mock_log_source, Log()).Times(0); |
179 | 179 |
180 SetLogAssertHandler(&LogSink); | 180 SetLogAssertHandler(&LogSink); |
181 | 181 |
182 CHECK(mock_log_source.Log()) << uncalled_mock_log_source.Log(); | 182 CHECK(mock_log_source.Log()) << uncalled_mock_log_source.Log(); |
(...skipping 14 matching lines...) Expand all Loading... |
197 // in release mode. | 197 // in release mode. |
198 DLOG_IF(INFO, debug_only_variable) << "test"; | 198 DLOG_IF(INFO, debug_only_variable) << "test"; |
199 DLOG_ASSERT(debug_only_variable) << "test"; | 199 DLOG_ASSERT(debug_only_variable) << "test"; |
200 DPLOG_IF(INFO, debug_only_variable) << "test"; | 200 DPLOG_IF(INFO, debug_only_variable) << "test"; |
201 DVLOG_IF(1, debug_only_variable) << "test"; | 201 DVLOG_IF(1, debug_only_variable) << "test"; |
202 } | 202 } |
203 | 203 |
204 TEST_F(LoggingTest, DcheckStreamsAreLazy) { | 204 TEST_F(LoggingTest, DcheckStreamsAreLazy) { |
205 MockLogSource mock_log_source; | 205 MockLogSource mock_log_source; |
206 EXPECT_CALL(mock_log_source, Log()).Times(0); | 206 EXPECT_CALL(mock_log_source, Log()).Times(0); |
207 #if !LOGGING_IS_OFFICIAL_BUILD && defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) | 207 if (DCHECK_IS_ON()) { |
208 // Unofficial release build without dcheck enabled. | 208 DCHECK(true) << mock_log_source.Log(); |
209 set_dcheck_state(DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS); | 209 DCHECK_EQ(0, 0) << mock_log_source.Log(); |
210 DCHECK(mock_log_source.Log()) << mock_log_source.Log(); | 210 } else { |
211 DPCHECK(mock_log_source.Log()) << mock_log_source.Log(); | 211 DCHECK(mock_log_source.Log()) << mock_log_source.Log(); |
212 DCHECK_EQ(0, 0) << mock_log_source.Log(); | 212 DPCHECK(mock_log_source.Log()) << mock_log_source.Log(); |
213 DCHECK_EQ(mock_log_source.Log(), static_cast<const char*>(NULL)) | 213 DCHECK_EQ(0, 0) << mock_log_source.Log(); |
214 << mock_log_source.Log(); | 214 DCHECK_EQ(mock_log_source.Log(), static_cast<const char*>(NULL)) |
215 #endif | 215 << mock_log_source.Log(); |
| 216 } |
216 } | 217 } |
217 | 218 |
218 TEST_F(LoggingTest, Dcheck) { | 219 TEST_F(LoggingTest, Dcheck) { |
219 #if LOGGING_IS_OFFICIAL_BUILD | 220 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
220 // Official build. | 221 // Release build. |
221 EXPECT_FALSE(DCHECK_IS_ON()); | 222 EXPECT_FALSE(DCHECK_IS_ON()); |
222 EXPECT_FALSE(DLOG_IS_ON(DCHECK)); | 223 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) | 224 #elif defined(NDEBUG) && defined(DCHECK_ALWAYS_ON) |
230 // Unofficial release build with real DCHECKS. | 225 // Release build with real DCHECKS. |
231 set_dcheck_state(ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS); | |
232 SetLogAssertHandler(&LogSink); | 226 SetLogAssertHandler(&LogSink); |
233 EXPECT_TRUE(DCHECK_IS_ON()); | 227 EXPECT_TRUE(DCHECK_IS_ON()); |
234 EXPECT_FALSE(DLOG_IS_ON(DCHECK)); | 228 EXPECT_FALSE(DLOG_IS_ON(DCHECK)); |
235 #else | 229 #else |
236 // Unofficial debug build. | 230 // Debug build. |
237 SetLogAssertHandler(&LogSink); | 231 SetLogAssertHandler(&LogSink); |
238 EXPECT_TRUE(DCHECK_IS_ON()); | 232 EXPECT_TRUE(DCHECK_IS_ON()); |
239 EXPECT_TRUE(DLOG_IS_ON(DCHECK)); | 233 EXPECT_TRUE(DLOG_IS_ON(DCHECK)); |
240 #endif // defined(LOGGING_IS_OFFICIAL_BUILD) | 234 #endif |
241 | 235 |
242 EXPECT_EQ(0, log_sink_call_count); | 236 EXPECT_EQ(0, log_sink_call_count); |
243 DCHECK(false); | 237 DCHECK(false); |
244 EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, log_sink_call_count); | 238 EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, log_sink_call_count); |
245 DPCHECK(false); | 239 DPCHECK(false); |
246 EXPECT_EQ(DCHECK_IS_ON() ? 2 : 0, log_sink_call_count); | 240 EXPECT_EQ(DCHECK_IS_ON() ? 2 : 0, log_sink_call_count); |
247 DCHECK_EQ(0, 1); | 241 DCHECK_EQ(0, 1); |
248 EXPECT_EQ(DCHECK_IS_ON() ? 3 : 0, log_sink_call_count); | 242 EXPECT_EQ(DCHECK_IS_ON() ? 3 : 0, log_sink_call_count); |
249 } | 243 } |
250 | 244 |
251 TEST_F(LoggingTest, DcheckReleaseBehavior) { | 245 TEST_F(LoggingTest, DcheckReleaseBehavior) { |
252 int some_variable = 1; | 246 int some_variable = 1; |
253 // These should still reference |some_variable| so we don't get | 247 // These should still reference |some_variable| so we don't get |
254 // unused variable warnings. | 248 // unused variable warnings. |
255 DCHECK(some_variable) << "test"; | 249 DCHECK(some_variable) << "test"; |
256 DPCHECK(some_variable) << "test"; | 250 DPCHECK(some_variable) << "test"; |
257 DCHECK_EQ(some_variable, 1) << "test"; | 251 DCHECK_EQ(some_variable, 1) << "test"; |
258 } | 252 } |
259 | 253 |
260 } // namespace | 254 } // namespace |
261 | 255 |
262 } // namespace logging | 256 } // namespace logging |
OLD | NEW |