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"
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 !defined(OFFICIAL_BUILD) || defined(DCHECK_ALWAYS_ON) || !defined(NDEBUG)
21 void LogSink(const std::string
& str
) {
22 ++log_sink_call_count
;
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).
30 LogStateSaver() : old_min_log_level_(GetMinLogLevel()) {}
33 SetMinLogLevel(old_min_log_level_
);
34 SetLogAssertHandler(NULL
);
35 SetLogReportHandler(NULL
);
36 log_sink_call_count
= 0;
40 int old_min_log_level_
;
42 DISALLOW_COPY_AND_ASSIGN(LogStateSaver
);
45 class LoggingTest
: public testing::Test
{
47 LogStateSaver log_state_saver_
;
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
) {
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 !defined(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();
192 TEST_F(LoggingTest
, DebugLoggingReleaseBehavior
) {
194 int debug_only_variable
= 1;
196 // These should avoid emitting references to |debug_only_variable|
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);
208 DCHECK(true) << mock_log_source
.Log();
209 DCHECK_EQ(0, 0) << mock_log_source
.Log();
211 DCHECK(mock_log_source
.Log()) << mock_log_source
.Log();
212 DPCHECK(mock_log_source
.Log()) << mock_log_source
.Log();
213 DCHECK_EQ(0, 0) << mock_log_source
.Log();
214 DCHECK_EQ(mock_log_source
.Log(), static_cast<const char*>(NULL
))
215 << mock_log_source
.Log();
219 TEST_F(LoggingTest
, Dcheck
) {
220 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
222 EXPECT_FALSE(DCHECK_IS_ON
);
223 EXPECT_FALSE(DLOG_IS_ON(DCHECK
));
224 #elif defined(NDEBUG) && defined(DCHECK_ALWAYS_ON)
225 // Release build with real DCHECKS.
226 SetLogAssertHandler(&LogSink
);
227 EXPECT_TRUE(DCHECK_IS_ON
);
228 EXPECT_FALSE(DLOG_IS_ON(DCHECK
));
231 SetLogAssertHandler(&LogSink
);
232 EXPECT_TRUE(DCHECK_IS_ON
);
233 EXPECT_TRUE(DLOG_IS_ON(DCHECK
));
236 EXPECT_EQ(0, log_sink_call_count
);
238 EXPECT_EQ(DCHECK_IS_ON
? 1 : 0, log_sink_call_count
);
240 EXPECT_EQ(DCHECK_IS_ON
? 2 : 0, log_sink_call_count
);
242 EXPECT_EQ(DCHECK_IS_ON
? 3 : 0, log_sink_call_count
);
245 TEST_F(LoggingTest
, DcheckReleaseBehavior
) {
246 int some_variable
= 1;
247 // These should still reference |some_variable| so we don't get
248 // unused variable warnings.
249 DCHECK(some_variable
) << "test";
250 DPCHECK(some_variable
) << "test";
251 DCHECK_EQ(some_variable
, 1) << "test";
256 } // namespace logging