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 void LogSink(const std::string
& str
) {
21 ++log_sink_call_count
;
24 // Class to make sure any manipulations we do to the min log level are
25 // contained (i.e., do not affect other unit tests).
28 LogStateSaver() : old_min_log_level_(GetMinLogLevel()) {}
31 SetMinLogLevel(old_min_log_level_
);
32 SetLogAssertHandler(NULL
);
33 SetLogReportHandler(NULL
);
34 log_sink_call_count
= 0;
38 int old_min_log_level_
;
40 DISALLOW_COPY_AND_ASSIGN(LogStateSaver
);
43 class LoggingTest
: public testing::Test
{
45 LogStateSaver log_state_saver_
;
50 MOCK_METHOD0(Log
, const char*());
53 TEST_F(LoggingTest
, BasicLogging
) {
54 MockLogSource mock_log_source
;
55 const int kExpectedDebugOrReleaseCalls
= 6;
56 const int kExpectedDebugCalls
= 6;
57 const int kExpectedCalls
=
58 kExpectedDebugOrReleaseCalls
+ (DEBUG_MODE
? kExpectedDebugCalls
: 0);
59 EXPECT_CALL(mock_log_source
, Log()).Times(kExpectedCalls
).
60 WillRepeatedly(Return("log message"));
62 SetMinLogLevel(LOG_INFO
);
64 EXPECT_TRUE(LOG_IS_ON(INFO
));
65 // As of g++-4.5, the first argument to EXPECT_EQ cannot be a
66 // constant expression.
67 const bool kIsDebugMode
= (DEBUG_MODE
!= 0);
68 EXPECT_TRUE(kIsDebugMode
== DLOG_IS_ON(INFO
));
69 EXPECT_TRUE(VLOG_IS_ON(0));
71 LOG(INFO
) << mock_log_source
.Log();
72 LOG_IF(INFO
, true) << mock_log_source
.Log();
73 PLOG(INFO
) << mock_log_source
.Log();
74 PLOG_IF(INFO
, true) << mock_log_source
.Log();
75 VLOG(0) << mock_log_source
.Log();
76 VLOG_IF(0, true) << mock_log_source
.Log();
78 DLOG(INFO
) << mock_log_source
.Log();
79 DLOG_IF(INFO
, true) << mock_log_source
.Log();
80 DPLOG(INFO
) << mock_log_source
.Log();
81 DPLOG_IF(INFO
, true) << mock_log_source
.Log();
82 DVLOG(0) << mock_log_source
.Log();
83 DVLOG_IF(0, true) << mock_log_source
.Log();
86 TEST_F(LoggingTest
, LogIsOn
) {
88 const bool kDfatalIsFatal
= false;
89 #else // defined(NDEBUG)
90 const bool kDfatalIsFatal
= true;
91 #endif // defined(NDEBUG)
93 SetMinLogLevel(LOG_INFO
);
94 EXPECT_TRUE(LOG_IS_ON(INFO
));
95 EXPECT_TRUE(LOG_IS_ON(WARNING
));
96 EXPECT_TRUE(LOG_IS_ON(ERROR
));
97 EXPECT_TRUE(LOG_IS_ON(ERROR_REPORT
));
98 EXPECT_TRUE(LOG_IS_ON(FATAL
));
99 EXPECT_TRUE(LOG_IS_ON(DFATAL
));
101 SetMinLogLevel(LOG_WARNING
);
102 EXPECT_FALSE(LOG_IS_ON(INFO
));
103 EXPECT_TRUE(LOG_IS_ON(WARNING
));
104 EXPECT_TRUE(LOG_IS_ON(ERROR
));
105 EXPECT_TRUE(LOG_IS_ON(ERROR_REPORT
));
106 EXPECT_TRUE(LOG_IS_ON(FATAL
));
107 EXPECT_TRUE(LOG_IS_ON(DFATAL
));
109 SetMinLogLevel(LOG_ERROR
);
110 EXPECT_FALSE(LOG_IS_ON(INFO
));
111 EXPECT_FALSE(LOG_IS_ON(WARNING
));
112 EXPECT_TRUE(LOG_IS_ON(ERROR
));
113 EXPECT_TRUE(LOG_IS_ON(ERROR_REPORT
));
114 EXPECT_TRUE(LOG_IS_ON(FATAL
));
115 EXPECT_TRUE(LOG_IS_ON(DFATAL
));
117 SetMinLogLevel(LOG_ERROR_REPORT
);
118 EXPECT_FALSE(LOG_IS_ON(INFO
));
119 EXPECT_FALSE(LOG_IS_ON(WARNING
));
120 EXPECT_FALSE(LOG_IS_ON(ERROR
));
121 EXPECT_TRUE(LOG_IS_ON(ERROR_REPORT
));
122 EXPECT_TRUE(LOG_IS_ON(FATAL
));
123 EXPECT_TRUE(kDfatalIsFatal
== LOG_IS_ON(DFATAL
));
125 // LOG_IS_ON(ERROR_REPORT) should always be true.
126 SetMinLogLevel(LOG_FATAL
);
127 EXPECT_FALSE(LOG_IS_ON(INFO
));
128 EXPECT_FALSE(LOG_IS_ON(WARNING
));
129 EXPECT_FALSE(LOG_IS_ON(ERROR
));
130 EXPECT_TRUE(LOG_IS_ON(ERROR_REPORT
));
131 EXPECT_TRUE(LOG_IS_ON(FATAL
));
132 EXPECT_TRUE(kDfatalIsFatal
== LOG_IS_ON(DFATAL
));
134 // So should LOG_IS_ON(FATAL).
135 SetMinLogLevel(LOG_FATAL
+ 1);
136 EXPECT_FALSE(LOG_IS_ON(INFO
));
137 EXPECT_FALSE(LOG_IS_ON(WARNING
));
138 EXPECT_FALSE(LOG_IS_ON(ERROR
));
139 EXPECT_TRUE(LOG_IS_ON(ERROR_REPORT
));
140 EXPECT_TRUE(LOG_IS_ON(FATAL
));
141 EXPECT_TRUE(kDfatalIsFatal
== LOG_IS_ON(DFATAL
));
144 TEST_F(LoggingTest
, LoggingIsLazy
) {
145 MockLogSource mock_log_source
;
146 EXPECT_CALL(mock_log_source
, Log()).Times(0);
148 SetMinLogLevel(LOG_WARNING
);
150 EXPECT_FALSE(LOG_IS_ON(INFO
));
151 EXPECT_FALSE(DLOG_IS_ON(INFO
));
152 EXPECT_FALSE(VLOG_IS_ON(1));
154 LOG(INFO
) << mock_log_source
.Log();
155 LOG_IF(INFO
, false) << mock_log_source
.Log();
156 PLOG(INFO
) << mock_log_source
.Log();
157 PLOG_IF(INFO
, false) << mock_log_source
.Log();
158 VLOG(1) << mock_log_source
.Log();
159 VLOG_IF(1, true) << mock_log_source
.Log();
161 DLOG(INFO
) << mock_log_source
.Log();
162 DLOG_IF(INFO
, true) << mock_log_source
.Log();
163 DPLOG(INFO
) << mock_log_source
.Log();
164 DPLOG_IF(INFO
, true) << mock_log_source
.Log();
165 DVLOG(1) << mock_log_source
.Log();
166 DVLOG_IF(1, true) << mock_log_source
.Log();
169 // Official builds have CHECKs directly call BreakDebugger.
170 #if !defined(LOGGING_IS_OFFICIAL_BUILD)
172 TEST_F(LoggingTest
, CheckStreamsAreLazy
) {
173 MockLogSource mock_log_source
, uncalled_mock_log_source
;
174 EXPECT_CALL(mock_log_source
, Log()).Times(8).
175 WillRepeatedly(Return("check message"));
176 EXPECT_CALL(uncalled_mock_log_source
, Log()).Times(0);
178 SetLogAssertHandler(&LogSink
);
180 CHECK(mock_log_source
.Log()) << uncalled_mock_log_source
.Log();
181 PCHECK(!mock_log_source
.Log()) << mock_log_source
.Log();
182 CHECK_EQ(mock_log_source
.Log(), mock_log_source
.Log())
183 << uncalled_mock_log_source
.Log();
184 CHECK_NE(mock_log_source
.Log(), mock_log_source
.Log())
185 << mock_log_source
.Log();
190 TEST_F(LoggingTest
, DebugLoggingReleaseBehavior
) {
192 int debug_only_variable
= 1;
194 // These should avoid emitting references to |debug_only_variable|
196 DLOG_IF(INFO
, debug_only_variable
) << "test";
197 DLOG_ASSERT(debug_only_variable
) << "test";
198 DPLOG_IF(INFO
, debug_only_variable
) << "test";
199 DVLOG_IF(1, debug_only_variable
) << "test";
202 TEST_F(LoggingTest
, DcheckStreamsAreLazy
) {
203 MockLogSource mock_log_source
;
204 EXPECT_CALL(mock_log_source
, Log()).Times(0);
205 #if !defined(LOGGING_IS_OFFICIAL_BUILD) && defined(NDEBUG) && \
206 !defined(DCHECK_ALWAYS_ON)
207 // Unofficial release build without dcheck enabled.
208 set_dcheck_state(DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS
);
209 DCHECK(mock_log_source
.Log()) << mock_log_source
.Log();
210 DPCHECK(mock_log_source
.Log()) << mock_log_source
.Log();
211 DCHECK_EQ(0, 0) << mock_log_source
.Log();
212 DCHECK_EQ(mock_log_source
.Log(), static_cast<const char*>(NULL
))
213 << mock_log_source
.Log();
217 TEST_F(LoggingTest
, Dcheck
) {
218 #if LOGGING_IS_OFFICIAL_BUILD
220 EXPECT_FALSE(DCHECK_IS_ON());
221 EXPECT_FALSE(DLOG_IS_ON(DCHECK
));
222 #elif defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
223 // Unofficial release build.
224 set_dcheck_state(ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS
);
225 SetLogReportHandler(&LogSink
);
226 EXPECT_TRUE(DCHECK_IS_ON());
227 EXPECT_FALSE(DLOG_IS_ON(DCHECK
));
228 #elif defined(NDEBUG) && defined(DCHECK_ALWAYS_ON)
229 // Unofficial release build with real DCHECKS.
230 set_dcheck_state(ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS
);
231 SetLogAssertHandler(&LogSink
);
232 EXPECT_TRUE(DCHECK_IS_ON());
233 EXPECT_FALSE(DLOG_IS_ON(DCHECK
));
235 // Unofficial debug build.
236 SetLogAssertHandler(&LogSink
);
237 EXPECT_TRUE(DCHECK_IS_ON());
238 EXPECT_TRUE(DLOG_IS_ON(DCHECK
));
239 #endif // defined(LOGGING_IS_OFFICIAL_BUILD)
241 EXPECT_EQ(0, log_sink_call_count
);
243 EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, log_sink_call_count
);
245 EXPECT_EQ(DCHECK_IS_ON() ? 2 : 0, log_sink_call_count
);
247 EXPECT_EQ(DCHECK_IS_ON() ? 3 : 0, log_sink_call_count
);
250 TEST_F(LoggingTest
, DcheckReleaseBehavior
) {
251 int some_variable
= 1;
252 // These should still reference |some_variable| so we don't get
253 // unused variable warnings.
254 DCHECK(some_variable
) << "test";
255 DPCHECK(some_variable
) << "test";
256 DCHECK_EQ(some_variable
, 1) << "test";
261 } // namespace logging