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

Side by Side Diff: base/logging_unittest.cc

Issue 4262001: Fixed bug where CHECKs don't fire if min_log_level > FATAL (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed ipc_tests Created 10 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 | Annotate | Revision Log
« no previous file with comments | « base/logging.cc ('k') | ipc/ipc_sync_channel_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 VLOG_IF(0, true) << mock_log_source.Log(); 73 VLOG_IF(0, true) << mock_log_source.Log();
74 74
75 DLOG(INFO) << mock_log_source.Log(); 75 DLOG(INFO) << mock_log_source.Log();
76 DLOG_IF(INFO, true) << mock_log_source.Log(); 76 DLOG_IF(INFO, true) << mock_log_source.Log();
77 DPLOG(INFO) << mock_log_source.Log(); 77 DPLOG(INFO) << mock_log_source.Log();
78 DPLOG_IF(INFO, true) << mock_log_source.Log(); 78 DPLOG_IF(INFO, true) << mock_log_source.Log();
79 DVLOG(0) << mock_log_source.Log(); 79 DVLOG(0) << mock_log_source.Log();
80 DVLOG_IF(0, true) << mock_log_source.Log(); 80 DVLOG_IF(0, true) << mock_log_source.Log();
81 } 81 }
82 82
83 TEST_F(LoggingTest, LogIsOn) {
84 #if defined(NDEBUG)
85 const bool kDfatalIsFatal = false;
86 #else // defined(NDEBUG)
87 const bool kDfatalIsFatal = true;
88 #endif // defined(NDEBUG)
89
90 SetMinLogLevel(LOG_INFO);
91 EXPECT_TRUE(LOG_IS_ON(INFO));
92 EXPECT_TRUE(LOG_IS_ON(WARNING));
93 EXPECT_TRUE(LOG_IS_ON(ERROR));
94 EXPECT_TRUE(LOG_IS_ON(ERROR_REPORT));
95 EXPECT_TRUE(LOG_IS_ON(FATAL));
96 EXPECT_TRUE(LOG_IS_ON(DFATAL));
97
98 SetMinLogLevel(LOG_WARNING);
99 EXPECT_FALSE(LOG_IS_ON(INFO));
100 EXPECT_TRUE(LOG_IS_ON(WARNING));
101 EXPECT_TRUE(LOG_IS_ON(ERROR));
102 EXPECT_TRUE(LOG_IS_ON(ERROR_REPORT));
103 EXPECT_TRUE(LOG_IS_ON(FATAL));
104 EXPECT_TRUE(LOG_IS_ON(DFATAL));
105
106 SetMinLogLevel(LOG_ERROR);
107 EXPECT_FALSE(LOG_IS_ON(INFO));
108 EXPECT_FALSE(LOG_IS_ON(WARNING));
109 EXPECT_TRUE(LOG_IS_ON(ERROR));
110 EXPECT_TRUE(LOG_IS_ON(ERROR_REPORT));
111 EXPECT_TRUE(LOG_IS_ON(FATAL));
112 EXPECT_TRUE(LOG_IS_ON(DFATAL));
113
114 SetMinLogLevel(LOG_ERROR_REPORT);
115 EXPECT_FALSE(LOG_IS_ON(INFO));
116 EXPECT_FALSE(LOG_IS_ON(WARNING));
117 EXPECT_FALSE(LOG_IS_ON(ERROR));
118 EXPECT_TRUE(LOG_IS_ON(ERROR_REPORT));
119 EXPECT_TRUE(LOG_IS_ON(FATAL));
120 EXPECT_EQ(kDfatalIsFatal, LOG_IS_ON(DFATAL));
121
122 // LOG_IS_ON(ERROR_REPORT) should always be true.
123 SetMinLogLevel(LOG_FATAL);
124 EXPECT_FALSE(LOG_IS_ON(INFO));
125 EXPECT_FALSE(LOG_IS_ON(WARNING));
126 EXPECT_FALSE(LOG_IS_ON(ERROR));
127 EXPECT_TRUE(LOG_IS_ON(ERROR_REPORT));
128 EXPECT_TRUE(LOG_IS_ON(FATAL));
129 EXPECT_EQ(kDfatalIsFatal, LOG_IS_ON(DFATAL));
130
131 // So should LOG_IS_ON(FATAL).
132 SetMinLogLevel(LOG_FATAL + 1);
133 EXPECT_FALSE(LOG_IS_ON(INFO));
134 EXPECT_FALSE(LOG_IS_ON(WARNING));
135 EXPECT_FALSE(LOG_IS_ON(ERROR));
136 EXPECT_TRUE(LOG_IS_ON(ERROR_REPORT));
137 EXPECT_TRUE(LOG_IS_ON(FATAL));
138 EXPECT_EQ(kDfatalIsFatal, LOG_IS_ON(DFATAL));
139 }
140
83 TEST_F(LoggingTest, LoggingIsLazy) { 141 TEST_F(LoggingTest, LoggingIsLazy) {
84 MockLogSource mock_log_source; 142 MockLogSource mock_log_source;
85 EXPECT_CALL(mock_log_source, Log()).Times(0); 143 EXPECT_CALL(mock_log_source, Log()).Times(0);
86 144
87 SetMinLogLevel(LOG_WARNING); 145 SetMinLogLevel(LOG_WARNING);
88 146
89 EXPECT_FALSE(LOG_IS_ON(INFO)); 147 EXPECT_FALSE(LOG_IS_ON(INFO));
90 EXPECT_FALSE(DLOG_IS_ON(INFO)); 148 EXPECT_FALSE(DLOG_IS_ON(INFO));
91 EXPECT_FALSE(VLOG_IS_ON(1)); 149 EXPECT_FALSE(VLOG_IS_ON(1));
92 150
93 LOG(INFO) << mock_log_source.Log(); 151 LOG(INFO) << mock_log_source.Log();
94 LOG_IF(INFO, false) << mock_log_source.Log(); 152 LOG_IF(INFO, false) << mock_log_source.Log();
95 PLOG(INFO) << mock_log_source.Log(); 153 PLOG(INFO) << mock_log_source.Log();
96 PLOG_IF(INFO, false) << mock_log_source.Log(); 154 PLOG_IF(INFO, false) << mock_log_source.Log();
97 VLOG(1) << mock_log_source.Log(); 155 VLOG(1) << mock_log_source.Log();
98 VLOG_IF(1, true) << mock_log_source.Log(); 156 VLOG_IF(1, true) << mock_log_source.Log();
99 157
100 DLOG(INFO) << mock_log_source.Log(); 158 DLOG(INFO) << mock_log_source.Log();
101 DLOG_IF(INFO, true) << mock_log_source.Log(); 159 DLOG_IF(INFO, true) << mock_log_source.Log();
102 DPLOG(INFO) << mock_log_source.Log(); 160 DPLOG(INFO) << mock_log_source.Log();
103 DPLOG_IF(INFO, true) << mock_log_source.Log(); 161 DPLOG_IF(INFO, true) << mock_log_source.Log();
104 DVLOG(1) << mock_log_source.Log(); 162 DVLOG(1) << mock_log_source.Log();
105 DVLOG_IF(1, true) << mock_log_source.Log(); 163 DVLOG_IF(1, true) << mock_log_source.Log();
106 } 164 }
107 165
108 TEST_F(LoggingTest, ChecksAreNotLazy) { 166 TEST_F(LoggingTest, CheckStreamsAreLazy) {
109 MockLogSource mock_log_source, uncalled_mock_log_source; 167 MockLogSource mock_log_source, uncalled_mock_log_source;
110 EXPECT_CALL(mock_log_source, Log()).Times(8). 168 EXPECT_CALL(mock_log_source, Log()).Times(8).
111 WillRepeatedly(Return("check message")); 169 WillRepeatedly(Return("check message"));
112 EXPECT_CALL(uncalled_mock_log_source, Log()).Times(0); 170 EXPECT_CALL(uncalled_mock_log_source, Log()).Times(0);
113 171
114 SetMinLogLevel(LOG_FATAL + 1); 172 SetLogAssertHandler(&LogSink);
115 EXPECT_FALSE(LOG_IS_ON(FATAL));
116 173
117 CHECK(mock_log_source.Log()) << uncalled_mock_log_source.Log(); 174 CHECK(mock_log_source.Log()) << uncalled_mock_log_source.Log();
118 PCHECK(!mock_log_source.Log()) << mock_log_source.Log(); 175 PCHECK(!mock_log_source.Log()) << mock_log_source.Log();
119 CHECK_EQ(mock_log_source.Log(), mock_log_source.Log()) 176 CHECK_EQ(mock_log_source.Log(), mock_log_source.Log())
120 << uncalled_mock_log_source.Log(); 177 << uncalled_mock_log_source.Log();
121 CHECK_NE(mock_log_source.Log(), mock_log_source.Log()) 178 CHECK_NE(mock_log_source.Log(), mock_log_source.Log())
122 << mock_log_source.Log(); 179 << mock_log_source.Log();
123 } 180 }
124 181
125 TEST_F(LoggingTest, DebugLoggingReleaseBehavior) { 182 TEST_F(LoggingTest, DebugLoggingReleaseBehavior) {
126 #if !defined(NDEBUG) 183 #if !defined(NDEBUG)
127 int debug_only_variable = 1; 184 int debug_only_variable = 1;
128 #endif 185 #endif
129 // These should avoid emitting references to |debug_only_variable| 186 // These should avoid emitting references to |debug_only_variable|
130 // in release mode. 187 // in release mode.
131 DLOG_IF(INFO, debug_only_variable) << "test"; 188 DLOG_IF(INFO, debug_only_variable) << "test";
132 DLOG_ASSERT(debug_only_variable) << "test"; 189 DLOG_ASSERT(debug_only_variable) << "test";
133 DPLOG_IF(INFO, debug_only_variable) << "test"; 190 DPLOG_IF(INFO, debug_only_variable) << "test";
134 DVLOG_IF(1, debug_only_variable) << "test"; 191 DVLOG_IF(1, debug_only_variable) << "test";
135 } 192 }
136 193
137 TEST_F(LoggingTest, DchecksAreLazy) { 194 TEST_F(LoggingTest, DcheckStreamsAreLazy) {
138 MockLogSource mock_log_source; 195 MockLogSource mock_log_source;
139 EXPECT_CALL(mock_log_source, Log()).Times(0); 196 EXPECT_CALL(mock_log_source, Log()).Times(0);
140 197
141 #if !defined(LOGGING_IS_OFFICIAL_BUILD) && defined(NDEBUG) 198 #if !defined(LOGGING_IS_OFFICIAL_BUILD) && defined(NDEBUG)
142 // Unofficial release build. 199 // Unofficial release build.
143 logging::g_enable_dcheck = false; 200 logging::g_enable_dcheck = false;
144 #else // !defined(LOGGING_IS_OFFICIAL_BUILD) && defined(NDEBUG)
145 SetMinLogLevel(LOG_FATAL + 1);
146 EXPECT_FALSE(LOG_IS_ON(FATAL));
147 #endif // !defined(LOGGING_IS_OFFICIAL_BUILD) && defined(NDEBUG)
148 DCHECK(mock_log_source.Log()) << mock_log_source.Log(); 201 DCHECK(mock_log_source.Log()) << mock_log_source.Log();
149 DPCHECK(mock_log_source.Log()) << mock_log_source.Log(); 202 DPCHECK(mock_log_source.Log()) << mock_log_source.Log();
150 DCHECK_EQ(0, 0) << mock_log_source.Log(); 203 DCHECK_EQ(0, 0) << mock_log_source.Log();
151 DCHECK_EQ(mock_log_source.Log(), static_cast<const char*>(NULL)) 204 DCHECK_EQ(mock_log_source.Log(), static_cast<const char*>(NULL))
152 << mock_log_source.Log(); 205 << mock_log_source.Log();
206 #endif // !defined(LOGGING_IS_OFFICIAL_BUILD) && defined(NDEBUG)
153 } 207 }
154 208
155 TEST_F(LoggingTest, Dcheck) { 209 TEST_F(LoggingTest, Dcheck) {
156 #if defined(LOGGING_IS_OFFICIAL_BUILD) 210 #if defined(LOGGING_IS_OFFICIAL_BUILD)
157 // Official build. 211 // Official build.
158 EXPECT_FALSE(DCHECK_IS_ON()); 212 EXPECT_FALSE(DCHECK_IS_ON());
159 EXPECT_FALSE(DLOG_IS_ON(DCHECK)); 213 EXPECT_FALSE(DLOG_IS_ON(DCHECK));
160 #elif defined(NDEBUG) 214 #elif defined(NDEBUG)
161 // Unofficial release build. 215 // Unofficial release build.
162 logging::g_enable_dcheck = true; 216 logging::g_enable_dcheck = true;
(...skipping 21 matching lines...) Expand all
184 // These should still reference |some_variable| so we don't get 238 // These should still reference |some_variable| so we don't get
185 // unused variable warnings. 239 // unused variable warnings.
186 DCHECK(some_variable) << "test"; 240 DCHECK(some_variable) << "test";
187 DPCHECK(some_variable) << "test"; 241 DPCHECK(some_variable) << "test";
188 DCHECK_EQ(some_variable, 1) << "test"; 242 DCHECK_EQ(some_variable, 1) << "test";
189 } 243 }
190 244
191 } // namespace 245 } // namespace
192 246
193 } // namespace logging 247 } // namespace logging
OLDNEW
« no previous file with comments | « base/logging.cc ('k') | ipc/ipc_sync_channel_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698