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

Side by Side Diff: base/logging.h

Issue 21216: Change the behavior of --enable-dcheck from crashing (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 10 months 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 | « no previous file | base/logging.cc » ('j') | base/logging.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 #ifndef BASE_LOGGING_H_ 5 #ifndef BASE_LOGGING_H_
6 #define BASE_LOGGING_H_ 6 #define BASE_LOGGING_H_
7 7
8 #include <string> 8 #include <string>
9 #include <cstring> 9 #include <cstring>
10 #include <sstream> 10 #include <sstream>
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 // We also have 70 // We also have
71 // 71 //
72 // LOG_ASSERT(assertion); 72 // LOG_ASSERT(assertion);
73 // DLOG_ASSERT(assertion); 73 // DLOG_ASSERT(assertion);
74 // 74 //
75 // which is syntactic sugar for {,D}LOG_IF(FATAL, assert fails) << assertion; 75 // which is syntactic sugar for {,D}LOG_IF(FATAL, assert fails) << assertion;
76 // 76 //
77 // We also override the standard 'assert' to use 'DLOG_ASSERT'. 77 // We also override the standard 'assert' to use 'DLOG_ASSERT'.
78 // 78 //
79 // The supported severity levels for macros that allow you to specify one 79 // The supported severity levels for macros that allow you to specify one
80 // are (in increasing order of severity) INFO, WARNING, ERROR, and FATAL. 80 // are (in increasing order of severity) INFO, WARNING, ERROR, ERROR_REPORT,
81 // 81 // and FATAL.
82 // There is also the special severity of DFATAL, which logs FATAL in
83 // debug mode, ERROR in normal mode.
84 // 82 //
85 // Very important: logging a message at the FATAL severity level causes 83 // Very important: logging a message at the FATAL severity level causes
86 // the program to terminate (after the message is logged). 84 // the program to terminate (after the message is logged).
85 //
86 // Note the special severity of ERROR_REPORT only available/relevant in normal
wtc 2009/02/19 01:32:04 Nit: add "is" before "available/relevant".
87 // mode, which displays error dialog without terminating the program. There is
wtc 2009/02/19 01:32:04 Nit: add "an" before "error dialog".
88 // no error dialog for severity ERROR or below in normal mode.
89 //
90 // There is also the special severity of DFATAL, which logs FATAL in
91 // debug mode, ERROR_REPORT in normal mode.
87 92
88 namespace logging { 93 namespace logging {
89 94
90 // Where to record logging output? A flat file and/or system debug log via 95 // Where to record logging output? A flat file and/or system debug log via
91 // OutputDebugString. Defaults on Windows to LOG_ONLY_TO_FILE, and on 96 // OutputDebugString. Defaults on Windows to LOG_ONLY_TO_FILE, and on
92 // POSIX to LOG_ONLY_TO_SYSTEM_DEBUG_LOG (aka stderr). 97 // POSIX to LOG_ONLY_TO_SYSTEM_DEBUG_LOG (aka stderr).
93 enum LoggingDestination { LOG_NONE, 98 enum LoggingDestination { LOG_NONE,
94 LOG_ONLY_TO_FILE, 99 LOG_ONLY_TO_FILE,
95 LOG_ONLY_TO_SYSTEM_DEBUG_LOG, 100 LOG_ONLY_TO_SYSTEM_DEBUG_LOG,
96 LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG }; 101 LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG };
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 void SetLogFilterPrefix(const char* filter); 149 void SetLogFilterPrefix(const char* filter);
145 150
146 // Sets the common items you want to be prepended to each log message. 151 // Sets the common items you want to be prepended to each log message.
147 // process and thread IDs default to off, the timestamp defaults to on. 152 // process and thread IDs default to off, the timestamp defaults to on.
148 // If this function is not called, logging defaults to writing the timestamp 153 // If this function is not called, logging defaults to writing the timestamp
149 // only. 154 // only.
150 void SetLogItems(bool enable_process_id, bool enable_thread_id, 155 void SetLogItems(bool enable_process_id, bool enable_thread_id,
151 bool enable_timestamp, bool enable_tickcount); 156 bool enable_timestamp, bool enable_tickcount);
152 157
153 // Sets the Log Assert Handler that will be used to notify of check failures. 158 // Sets the Log Assert Handler that will be used to notify of check failures.
154 // The default handler shows a dialog box, however clients can use this 159 // The default handler shows a dialog box and then terminate the process,
wtc 2009/02/19 01:32:04 Nit: "terminate" => "terminates"
155 // function to override with their own handling (e.g. a silent one for Unit 160 // however clients can use this function to override with their own handling
156 // Tests) 161 // (e.g. a silent one for Unit Tests)
157 typedef void (*LogAssertHandlerFunction)(const std::string& str); 162 typedef void (*LogAssertHandlerFunction)(const std::string& str);
158 void SetLogAssertHandler(LogAssertHandlerFunction handler); 163 void SetLogAssertHandler(LogAssertHandlerFunction handler);
164 // Sets the Log Report Handler that will be used to notify of check failures
wtc 2009/02/19 01:32:04 Nit: add a blank line above this line.
165 // in non-debug mode. The default handler shows a dialog box and continues
166 // the execution, however clients can use this function to override with their
167 // own handling.
168 typedef void (*LogReportHandlerFunction)(const std::string& str);
169 void SetLogReportHandler(LogReportHandlerFunction handler);
159 170
160 typedef int LogSeverity; 171 typedef int LogSeverity;
161 const LogSeverity LOG_INFO = 0; 172 const LogSeverity LOG_INFO = 0;
162 const LogSeverity LOG_WARNING = 1; 173 const LogSeverity LOG_WARNING = 1;
163 const LogSeverity LOG_ERROR = 2; 174 const LogSeverity LOG_ERROR = 2;
164 const LogSeverity LOG_FATAL = 3; 175 const LogSeverity LOG_ERROR_REPORT = 3;
165 const LogSeverity LOG_NUM_SEVERITIES = 4; 176 const LogSeverity LOG_FATAL = 4;
177 const LogSeverity LOG_NUM_SEVERITIES = 5;
166 178
167 // LOG_DFATAL_LEVEL is LOG_FATAL in debug mode, ERROR in normal mode 179 // LOG_DFATAL_LEVEL is LOG_FATAL in debug mode, ERROR_REPORT in normal mode
168 #ifdef NDEBUG 180 #ifdef NDEBUG
169 const LogSeverity LOG_DFATAL_LEVEL = LOG_ERROR; 181 const LogSeverity LOG_DFATAL_LEVEL = LOG_ERROR_REPORT;
170 #else 182 #else
171 const LogSeverity LOG_DFATAL_LEVEL = LOG_FATAL; 183 const LogSeverity LOG_DFATAL_LEVEL = LOG_FATAL;
172 #endif 184 #endif
173 185
174 // A few definitions of macros that don't generate much code. These are used 186 // A few definitions of macros that don't generate much code. These are used
175 // by LOG() and LOG_IF, etc. Since these are used all over our code, it's 187 // by LOG() and LOG_IF, etc. Since these are used all over our code, it's
176 // better to have compact code for these operations. 188 // better to have compact code for these operations.
177 #define COMPACT_GOOGLE_LOG_INFO \ 189 #define COMPACT_GOOGLE_LOG_INFO \
178 logging::LogMessage(__FILE__, __LINE__) 190 logging::LogMessage(__FILE__, __LINE__)
179 #define COMPACT_GOOGLE_LOG_WARNING \ 191 #define COMPACT_GOOGLE_LOG_WARNING \
180 logging::LogMessage(__FILE__, __LINE__, logging::LOG_WARNING) 192 logging::LogMessage(__FILE__, __LINE__, logging::LOG_WARNING)
181 #define COMPACT_GOOGLE_LOG_ERROR \ 193 #define COMPACT_GOOGLE_LOG_ERROR \
182 logging::LogMessage(__FILE__, __LINE__, logging::LOG_ERROR) 194 logging::LogMessage(__FILE__, __LINE__, logging::LOG_ERROR)
195 #define COMPACT_GOOGLE_LOG_ERROR_REPORT \
196 logging::LogMessage(__FILE__, __LINE__, logging::LOG_ERROR_REPORT)
183 #define COMPACT_GOOGLE_LOG_FATAL \ 197 #define COMPACT_GOOGLE_LOG_FATAL \
184 logging::LogMessage(__FILE__, __LINE__, logging::LOG_FATAL) 198 logging::LogMessage(__FILE__, __LINE__, logging::LOG_FATAL)
185 #define COMPACT_GOOGLE_LOG_DFATAL \ 199 #define COMPACT_GOOGLE_LOG_DFATAL \
186 logging::LogMessage(__FILE__, __LINE__, logging::LOG_DFATAL_LEVEL) 200 logging::LogMessage(__FILE__, __LINE__, logging::LOG_DFATAL_LEVEL)
187 201
188 // wingdi.h defines ERROR to be 0. When we call LOG(ERROR), it gets 202 // wingdi.h defines ERROR to be 0. When we call LOG(ERROR), it gets
189 // substituted with 0, and it expands to COMPACT_GOOGLE_LOG_0. To allow us 203 // substituted with 0, and it expands to COMPACT_GOOGLE_LOG_0. To allow us
190 // to keep using this syntax, we define this macro to do the same thing 204 // to keep using this syntax, we define this macro to do the same thing
191 // as COMPACT_GOOGLE_LOG_ERROR, and also define ERROR the same way that 205 // as COMPACT_GOOGLE_LOG_ERROR, and also define ERROR the same way that
192 // the Windows SDK does for consistency. 206 // the Windows SDK does for consistency.
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
388 // non-debug mode. The DCHECK and friends macros use this so that 402 // non-debug mode. The DCHECK and friends macros use this so that
389 // the expanded expression DCHECK(foo) << "asdf" is still syntactically 403 // the expanded expression DCHECK(foo) << "asdf" is still syntactically
390 // valid, even though the expression will get optimized away. 404 // valid, even though the expression will get optimized away.
391 #define NDEBUG_EAT_STREAM_PARAMETERS \ 405 #define NDEBUG_EAT_STREAM_PARAMETERS \
392 logging::LogMessage(__FILE__, __LINE__).stream() 406 logging::LogMessage(__FILE__, __LINE__).stream()
393 407
394 // Set to true in InitLogging when we want to enable the dchecks in release. 408 // Set to true in InitLogging when we want to enable the dchecks in release.
395 extern bool g_enable_dcheck; 409 extern bool g_enable_dcheck;
396 #define DCHECK(condition) \ 410 #define DCHECK(condition) \
397 !logging::g_enable_dcheck ? void (0) : \ 411 !logging::g_enable_dcheck ? void (0) : \
398 LOG_IF(FATAL, !(condition)) << "Check failed: " #condition ". " 412 LOG_IF(ERROR_REPORT, !(condition)) << "Check failed: " #condition ". "
399 413
400 // Helper macro for binary operators. 414 // Helper macro for binary operators.
401 // Don't use this macro directly in your code, use DCHECK_EQ et al below. 415 // Don't use this macro directly in your code, use DCHECK_EQ et al below.
402 #define DCHECK_OP(name, op, val1, val2) \ 416 #define DCHECK_OP(name, op, val1, val2) \
403 if (logging::g_enable_dcheck) \ 417 if (logging::g_enable_dcheck) \
404 if (logging::CheckOpString _result = \ 418 if (logging::CheckOpString _result = \
405 logging::Check##name##Impl((val1), (val2), #val1 " " #op " " #val2)) \ 419 logging::Check##name##Impl((val1), (val2), #val1 " " #op " " #val2)) \
406 logging::LogMessage(__FILE__, __LINE__, _result).stream() 420 logging::LogMessage(__FILE__, __LINE__, logging::LOG_ERROR_REPORT, \
421 _result).stream()
407 422
408 #define DCHECK_STREQ(str1, str2) \ 423 #define DCHECK_STREQ(str1, str2) \
409 while (false) NDEBUG_EAT_STREAM_PARAMETERS 424 while (false) NDEBUG_EAT_STREAM_PARAMETERS
410 425
411 #define DCHECK_STRCASEEQ(str1, str2) \ 426 #define DCHECK_STRCASEEQ(str1, str2) \
412 while (false) NDEBUG_EAT_STREAM_PARAMETERS 427 while (false) NDEBUG_EAT_STREAM_PARAMETERS
413 428
414 #define DCHECK_STRNE(str1, str2) \ 429 #define DCHECK_STRNE(str1, str2) \
415 while (false) NDEBUG_EAT_STREAM_PARAMETERS 430 while (false) NDEBUG_EAT_STREAM_PARAMETERS
416 431
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
501 // are: ctr = 0 516 // are: ctr = 0
502 // 517 //
503 // Using this constructor instead of the more complex constructor above 518 // Using this constructor instead of the more complex constructor above
504 // saves a couple of bytes per call site. 519 // saves a couple of bytes per call site.
505 LogMessage(const char* file, int line, LogSeverity severity); 520 LogMessage(const char* file, int line, LogSeverity severity);
506 521
507 // A special constructor used for check failures. 522 // A special constructor used for check failures.
508 // Implied severity = LOG_FATAL 523 // Implied severity = LOG_FATAL
509 LogMessage(const char* file, int line, const CheckOpString& result); 524 LogMessage(const char* file, int line, const CheckOpString& result);
510 525
526 // A special constructor used for check failures, with the option to
527 // specify severity.
528 LogMessage(const char* file, int line, LogSeverity severity,
529 const CheckOpString& result);
530
511 ~LogMessage(); 531 ~LogMessage();
512 532
513 std::ostream& stream() { return stream_; } 533 std::ostream& stream() { return stream_; }
514 534
515 private: 535 private:
516 void Init(const char* file, int line); 536 void Init(const char* file, int line);
517 537
518 LogSeverity severity_; 538 LogSeverity severity_;
519 std::ostringstream stream_; 539 std::ostringstream stream_;
520 size_t message_start_; // Offset of the start of the message (past prefix 540 size_t message_start_; // Offset of the start of the message (past prefix
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
595 #elif NOTIMPLEMENTED_POLICY == 4 615 #elif NOTIMPLEMENTED_POLICY == 4
596 #define NOTIMPLEMENTED() LOG(ERROR) << NOTIMPLEMENTED_MSG 616 #define NOTIMPLEMENTED() LOG(ERROR) << NOTIMPLEMENTED_MSG
597 #elif NOTIMPLEMENTED_POLICY == 5 617 #elif NOTIMPLEMENTED_POLICY == 5
598 #define NOTIMPLEMENTED() do {\ 618 #define NOTIMPLEMENTED() do {\
599 static int count = 0;\ 619 static int count = 0;\
600 LOG_IF(ERROR, 0 == count++) << NOTIMPLEMENTED_MSG;\ 620 LOG_IF(ERROR, 0 == count++) << NOTIMPLEMENTED_MSG;\
601 } while(0) 621 } while(0)
602 #endif 622 #endif
603 623
604 #endif // BASE_LOGGING_H_ 624 #endif // BASE_LOGGING_H_
OLDNEW
« no previous file with comments | « no previous file | base/logging.cc » ('j') | base/logging.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698