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

Side by Side Diff: base/logging.h

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 | « no previous file | base/logging.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 #ifndef BASE_LOGGING_H_ 5 #ifndef BASE_LOGGING_H_
6 #define BASE_LOGGING_H_ 6 #define BASE_LOGGING_H_
7 #pragma once 7 #pragma once
8 8
9 #include <string> 9 #include <string>
10 #include <cstring> 10 #include <cstring>
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 // to keep using this syntax, we define this macro to do the same thing 309 // to keep using this syntax, we define this macro to do the same thing
310 // as COMPACT_GOOGLE_LOG_ERROR, and also define ERROR the same way that 310 // as COMPACT_GOOGLE_LOG_ERROR, and also define ERROR the same way that
311 // the Windows SDK does for consistency. 311 // the Windows SDK does for consistency.
312 #define ERROR 0 312 #define ERROR 0
313 #define COMPACT_GOOGLE_LOG_EX_0(ClassName, ...) \ 313 #define COMPACT_GOOGLE_LOG_EX_0(ClassName, ...) \
314 COMPACT_GOOGLE_LOG_EX_ERROR(ClassName , ##__VA_ARGS__) 314 COMPACT_GOOGLE_LOG_EX_ERROR(ClassName , ##__VA_ARGS__)
315 #define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR 315 #define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR
316 // Needed for LOG_IS_ON(ERROR). 316 // Needed for LOG_IS_ON(ERROR).
317 const LogSeverity LOG_0 = LOG_ERROR; 317 const LogSeverity LOG_0 = LOG_ERROR;
318 318
319 // As special cases, we can assume that LOG_IS_ON(ERROR_REPORT) and
320 // LOG_IS_ON(FATAL) always hold. Also, LOG_IS_ON(DFATAL) always holds
321 // in debug mode. In particular, CHECK()s will always fire if they
322 // fail.
319 #define LOG_IS_ON(severity) \ 323 #define LOG_IS_ON(severity) \
320 ((::logging::LOG_ ## severity) >= ::logging::GetMinLogLevel()) 324 ((::logging::LOG_ ## severity) >= ::logging::GetMinLogLevel())
321 325
322 // We can't do any caching tricks with VLOG_IS_ON() like the 326 // We can't do any caching tricks with VLOG_IS_ON() like the
323 // google-glog version since it requires GCC extensions. This means 327 // google-glog version since it requires GCC extensions. This means
324 // that using the v-logging functions in conjunction with --vmodule 328 // that using the v-logging functions in conjunction with --vmodule
325 // may be slow. 329 // may be slow.
326 #define VLOG_IS_ON(verboselevel) \ 330 #define VLOG_IS_ON(verboselevel) \
327 ((verboselevel) <= ::logging::GetVlogLevel(__FILE__)) 331 ((verboselevel) <= ::logging::GetVlogLevel(__FILE__))
328 332
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 395
392 #define PLOG_IF(severity, condition) \ 396 #define PLOG_IF(severity, condition) \
393 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition)) 397 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition))
394 398
395 // CHECK dies with a fatal error if condition is not true. It is *not* 399 // CHECK dies with a fatal error if condition is not true. It is *not*
396 // controlled by NDEBUG, so the check will be executed regardless of 400 // controlled by NDEBUG, so the check will be executed regardless of
397 // compilation mode. 401 // compilation mode.
398 // 402 //
399 // We make sure CHECK et al. always evaluates their arguments, as 403 // We make sure CHECK et al. always evaluates their arguments, as
400 // doing CHECK(FunctionWithSideEffect()) is a common idiom. 404 // doing CHECK(FunctionWithSideEffect()) is a common idiom.
401 //
402 // TODO(akalin): Fix the problem where if the min log level is >
403 // FATAL, CHECK() et al. won't terminate the program.
404 #define CHECK(condition) \ 405 #define CHECK(condition) \
405 LAZY_STREAM(LOG_STREAM(FATAL), !(condition)) \ 406 LAZY_STREAM(LOG_STREAM(FATAL), !(condition)) \
406 << "Check failed: " #condition ". " 407 << "Check failed: " #condition ". "
407 408
408 #define PCHECK(condition) \ 409 #define PCHECK(condition) \
409 LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \ 410 LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \
410 << "Check failed: " #condition ". " 411 << "Check failed: " #condition ". "
411 412
412 // A container for a string pointer which can be evaluated to a bool - 413 // A container for a string pointer which can be evaluated to a bool -
413 // true iff the pointer is NULL. 414 // true iff the pointer is NULL.
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
570 LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity)) 571 LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity))
571 572
572 #define DVLOG(verboselevel) DLOG_IF(INFO, VLOG_IS_ON(verboselevel)) 573 #define DVLOG(verboselevel) DLOG_IF(INFO, VLOG_IS_ON(verboselevel))
573 574
574 // Definitions for DCHECK et al. 575 // Definitions for DCHECK et al.
575 576
576 #if ENABLE_DCHECK 577 #if ENABLE_DCHECK
577 578
578 #if defined(NDEBUG) 579 #if defined(NDEBUG)
579 580
580 #define DCHECK_SEVERITY ERROR_REPORT 581 #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
582 COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(ClassName , ##__VA_ARGS__)
583 #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_ERROR_REPORT
581 const LogSeverity LOG_DCHECK = LOG_ERROR_REPORT; 584 const LogSeverity LOG_DCHECK = LOG_ERROR_REPORT;
582 // This is set to true in InitLogging when we want to enable the 585 // This is set to true in InitLogging when we want to enable the
583 // DCHECKs in release. 586 // DCHECKs in release.
584 extern bool g_enable_dcheck; 587 extern bool g_enable_dcheck;
585 #define DCHECK_IS_ON() (::logging::g_enable_dcheck && LOG_IS_ON(DCHECK)) 588 #define DCHECK_IS_ON() (::logging::g_enable_dcheck && LOG_IS_ON(DCHECK))
586 589
587 #else // defined(NDEBUG) 590 #else // defined(NDEBUG)
588 591
589 // On a regular debug build, we want to have DCHECKs enabled. 592 // On a regular debug build, we want to have DCHECKs enabled.
590 #define DCHECK_SEVERITY FATAL 593 #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
594 COMPACT_GOOGLE_LOG_EX_FATAL(ClassName , ##__VA_ARGS__)
595 #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_FATAL
591 const LogSeverity LOG_DCHECK = LOG_FATAL; 596 const LogSeverity LOG_DCHECK = LOG_FATAL;
592 // TODO(akalin): We don't define this as 'true' since if the log level 597 #define DCHECK_IS_ON() true
593 // is above FATAL, the DCHECK won't go through anyway. Make it so
594 // that DCHECKs work regardless of the logging level, then set this to
595 // 'true'.
596 #define DCHECK_IS_ON() LOG_IS_ON(DCHECK)
597 598
598 #endif // defined(NDEBUG) 599 #endif // defined(NDEBUG)
599 600
600 #else // ENABLE_DCHECK 601 #else // ENABLE_DCHECK
601 602
602 #define DCHECK_SEVERITY FATAL 603 // These are just dummy values since DCHECK_IS_ON() is always false in
603 const LogSeverity LOG_DCHECK = LOG_FATAL; 604 // this case.
605 #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
606 COMPACT_GOOGLE_LOG_EX_INFO(ClassName , ##__VA_ARGS__)
607 #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_INFO
608 const LogSeverity LOG_DCHECK = LOG_INFO;
604 #define DCHECK_IS_ON() false 609 #define DCHECK_IS_ON() false
605 610
606 #endif // ENABLE_DCHECK 611 #endif // ENABLE_DCHECK
607 #undef ENABLE_DCHECK 612 #undef ENABLE_DCHECK
608 613
609 // Unlike CHECK et al., DCHECK et al. *does* evaluate their arguments 614 // DCHECK et al. make sure to reference |condition| regardless of
610 // lazily.
611
612 // DCHECK et al. also make sure to reference |condition| regardless of
613 // whether DCHECKs are enabled; this is so that we don't get unused 615 // whether DCHECKs are enabled; this is so that we don't get unused
614 // variable warnings if the only use of a variable is in a DCHECK. 616 // variable warnings if the only use of a variable is in a DCHECK.
615 // This behavior is different from DLOG_IF et al. 617 // This behavior is different from DLOG_IF et al.
616 618
617 #define DCHECK(condition) \ 619 #define DCHECK(condition) \
618 !DCHECK_IS_ON() ? (void) 0 : \ 620 LAZY_STREAM(LOG_STREAM(DCHECK), DCHECK_IS_ON() && !(condition)) \
619 LOG_IF(DCHECK_SEVERITY, !(condition)) \
620 << "Check failed: " #condition ". " 621 << "Check failed: " #condition ". "
621 622
622 #define DPCHECK(condition) \ 623 #define DPCHECK(condition) \
623 !DCHECK_IS_ON() ? (void) 0 : \ 624 LAZY_STREAM(PLOG_STREAM(DCHECK), DCHECK_IS_ON() && !(condition)) \
624 PLOG_IF(DCHECK_SEVERITY, !(condition)) \
625 << "Check failed: " #condition ". " 625 << "Check failed: " #condition ". "
626 626
627 // Helper macro for binary operators. 627 // Helper macro for binary operators.
628 // Don't use this macro directly in your code, use DCHECK_EQ et al below. 628 // Don't use this macro directly in your code, use DCHECK_EQ et al below.
629 #define DCHECK_OP(name, op, val1, val2) \ 629 #define DCHECK_OP(name, op, val1, val2) \
630 if (DCHECK_IS_ON()) \ 630 if (DCHECK_IS_ON()) \
631 if (logging::CheckOpString _result = \ 631 if (logging::CheckOpString _result = \
632 logging::Check##name##Impl((val1), (val2), \ 632 logging::Check##name##Impl((val1), (val2), \
633 #val1 " " #op " " #val2)) \ 633 #val1 " " #op " " #val2)) \
634 logging::LogMessage( \ 634 logging::LogMessage( \
635 __FILE__, __LINE__, ::logging::LOG_DCHECK, \ 635 __FILE__, __LINE__, ::logging::LOG_DCHECK, \
636 _result).stream() 636 _result).stream()
637 637
638 // Equality/Inequality checks - compare two values, and log a LOG_FATAL message 638 // Equality/Inequality checks - compare two values, and log a
639 // including the two values when the result is not as expected. The values 639 // LOG_DCHECK message including the two values when the result is not
640 // must have operator<<(ostream, ...) defined. 640 // as expected. The values must have operator<<(ostream, ...)
641 // defined.
641 // 642 //
642 // You may append to the error message like so: 643 // You may append to the error message like so:
643 // DCHECK_NE(1, 2) << ": The world must be ending!"; 644 // DCHECK_NE(1, 2) << ": The world must be ending!";
644 // 645 //
645 // We are very careful to ensure that each argument is evaluated exactly 646 // We are very careful to ensure that each argument is evaluated exactly
646 // once, and that anything which is legal to pass as a function argument is 647 // once, and that anything which is legal to pass as a function argument is
647 // legal here. In particular, the arguments may be temporary expressions 648 // legal here. In particular, the arguments may be temporary expressions
648 // which will end up being destroyed at the end of the apparent statement, 649 // which will end up being destroyed at the end of the apparent statement,
649 // for example: 650 // for example:
650 // DCHECK_EQ(string("abc")[1], 'b'); 651 // DCHECK_EQ(string("abc")[1], 'b');
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after
881 #elif NOTIMPLEMENTED_POLICY == 4 882 #elif NOTIMPLEMENTED_POLICY == 4
882 #define NOTIMPLEMENTED() LOG(ERROR) << NOTIMPLEMENTED_MSG 883 #define NOTIMPLEMENTED() LOG(ERROR) << NOTIMPLEMENTED_MSG
883 #elif NOTIMPLEMENTED_POLICY == 5 884 #elif NOTIMPLEMENTED_POLICY == 5
884 #define NOTIMPLEMENTED() do {\ 885 #define NOTIMPLEMENTED() do {\
885 static int count = 0;\ 886 static int count = 0;\
886 LOG_IF(ERROR, 0 == count++) << NOTIMPLEMENTED_MSG;\ 887 LOG_IF(ERROR, 0 == count++) << NOTIMPLEMENTED_MSG;\
887 } while(0) 888 } while(0)
888 #endif 889 #endif
889 890
890 #endif // BASE_LOGGING_H_ 891 #endif // BASE_LOGGING_H_
OLDNEW
« no previous file with comments | « no previous file | base/logging.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698