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

Side by Side Diff: base/logging.h

Issue 4199005: Fixed subtle difference in behavior between DCHECK and DCHECK_EQ et al. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed wtc's comments 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_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 #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 513 matching lines...) Expand 10 before | Expand all | Expand 10 after
524 #else 524 #else
525 // Otherwise, we're a debug build so enable DLOGs and DCHECKs. 525 // Otherwise, we're a debug build so enable DLOGs and DCHECKs.
526 #define ENABLE_DLOG 1 526 #define ENABLE_DLOG 1
527 #define ENABLE_DCHECK 1 527 #define ENABLE_DCHECK 1
528 #endif 528 #endif
529 529
530 // Definitions for DLOG et al. 530 // Definitions for DLOG et al.
531 531
532 #if ENABLE_DLOG 532 #if ENABLE_DLOG
533 533
534 #define DLOG_IS_ON(severity) LOG_IS_ON(severity)
534 #define DLOG_IF(severity, condition) LOG_IF(severity, condition) 535 #define DLOG_IF(severity, condition) LOG_IF(severity, condition)
535 #define DLOG_ASSERT(condition) LOG_ASSERT(condition) 536 #define DLOG_ASSERT(condition) LOG_ASSERT(condition)
536 #define DPLOG_IF(severity, condition) PLOG_IF(severity, condition) 537 #define DPLOG_IF(severity, condition) PLOG_IF(severity, condition)
537 #define DVLOG_IF(verboselevel, condition) VLOG_IF(verboselevel, condition) 538 #define DVLOG_IF(verboselevel, condition) VLOG_IF(verboselevel, condition)
538 539
539 #else // ENABLE_DLOG 540 #else // ENABLE_DLOG
540 541
541 // If ENABLE_DLOG is off, we want to avoid emitting any references to 542 // If ENABLE_DLOG is off, we want to avoid emitting any references to
542 // |condition| (which may reference a variable defined only if NDEBUG 543 // |condition| (which may reference a variable defined only if NDEBUG
543 // is not defined). Contrast this with DCHECK et al., which has 544 // is not defined). Contrast this with DCHECK et al., which has
544 // different behavior. 545 // different behavior.
545 546
546 #define DLOG_EAT_STREAM_PARAMETERS \ 547 #define DLOG_EAT_STREAM_PARAMETERS \
547 true ? (void) 0 : ::logging::LogMessageVoidify() & LOG_STREAM(FATAL) 548 true ? (void) 0 : ::logging::LogMessageVoidify() & LOG_STREAM(FATAL)
548 549
550 #define DLOG_IS_ON(severity) false
549 #define DLOG_IF(severity, condition) DLOG_EAT_STREAM_PARAMETERS 551 #define DLOG_IF(severity, condition) DLOG_EAT_STREAM_PARAMETERS
550 #define DLOG_ASSERT(condition) DLOG_EAT_STREAM_PARAMETERS 552 #define DLOG_ASSERT(condition) DLOG_EAT_STREAM_PARAMETERS
551 #define DPLOG_IF(severity, condition) DLOG_EAT_STREAM_PARAMETERS 553 #define DPLOG_IF(severity, condition) DLOG_EAT_STREAM_PARAMETERS
552 #define DVLOG_IF(verboselevel, condition) DLOG_EAT_STREAM_PARAMETERS 554 #define DVLOG_IF(verboselevel, condition) DLOG_EAT_STREAM_PARAMETERS
553 555
554 #endif // ENABLE_DLOG 556 #endif // ENABLE_DLOG
555 557
556 // DEBUG_MODE is for uses like 558 // DEBUG_MODE is for uses like
557 // if (DEBUG_MODE) foo.CheckThatFoo(); 559 // if (DEBUG_MODE) foo.CheckThatFoo();
558 // instead of 560 // instead of
559 // #ifndef NDEBUG 561 // #ifndef NDEBUG
560 // foo.CheckThatFoo(); 562 // foo.CheckThatFoo();
561 // #endif 563 // #endif
562 // 564 //
563 // We tie its state to ENABLE_DLOG. 565 // We tie its state to ENABLE_DLOG.
564 enum { DEBUG_MODE = ENABLE_DLOG }; 566 enum { DEBUG_MODE = ENABLE_DLOG };
565 567
566 #undef ENABLE_DLOG 568 #undef ENABLE_DLOG
567 569
568 #define DLOG_IS_ON(severity) (::logging::DEBUG_MODE && LOG_IS_ON(severity))
569
570 #define DLOG(severity) \ 570 #define DLOG(severity) \
571 LAZY_STREAM(LOG_STREAM(severity), DLOG_IS_ON(severity)) 571 LAZY_STREAM(LOG_STREAM(severity), DLOG_IS_ON(severity))
572 572
573 #if defined(OS_WIN) 573 #if defined(OS_WIN)
574 #define DLOG_GETLASTERROR(severity) \ 574 #define DLOG_GETLASTERROR(severity) \
575 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity), DLOG_IS_ON(severity)) 575 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity), DLOG_IS_ON(severity))
576 #define DLOG_GETLASTERROR_MODULE(severity, module) \ 576 #define DLOG_GETLASTERROR_MODULE(severity, module) \
577 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity, module), \ 577 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity, module), \
578 DLOG_IS_ON(severity)) 578 DLOG_IS_ON(severity))
579 #elif defined(OS_POSIX) 579 #elif defined(OS_POSIX)
580 #define DLOG_ERRNO(severity) \ 580 #define DLOG_ERRNO(severity) \
581 LAZY_STREAM(LOG_ERRNO_STREAM(severity), DLOG_IS_ON(severity)) 581 LAZY_STREAM(LOG_ERRNO_STREAM(severity), DLOG_IS_ON(severity))
582 #endif 582 #endif
583 583
584 #define DPLOG(severity) \ 584 #define DPLOG(severity) \
585 LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity)) 585 LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity))
586 586
587 #define DVLOG(verboselevel) DLOG_IF(INFO, VLOG_IS_ON(verboselevel)) 587 #define DVLOG(verboselevel) DLOG_IF(INFO, VLOG_IS_ON(verboselevel))
588 588
589 // Definitions for DCHECK et al. 589 // Definitions for DCHECK et al.
590 590
591 #if ENABLE_DCHECK 591 #if ENABLE_DCHECK
592 592
593 #if defined(NDEBUG) 593 #if defined(NDEBUG)
594 594
595 // Set to true in InitLogging when we want to enable the dchecks in release.
596 extern bool g_enable_dcheck;
597 #define DCHECK_IS_ON() (::logging::g_enable_dcheck)
598 #define DCHECK_SEVERITY ERROR_REPORT 595 #define DCHECK_SEVERITY ERROR_REPORT
599 const LogSeverity LOG_DCHECK = LOG_ERROR_REPORT; 596 const LogSeverity LOG_DCHECK = LOG_ERROR_REPORT;
597 // This is set to true in InitLogging when we want to enable the
598 // DCHECKs in release.
599 extern bool g_enable_dcheck;
600 #define DCHECK_IS_ON() (::logging::g_enable_dcheck && LOG_IS_ON(DCHECK))
600 601
601 #else // defined(NDEBUG) 602 #else // defined(NDEBUG)
602 603
603 // On a regular debug build, we want to have DCHECKS enabled. 604 // On a regular debug build, we want to have DCHECKs enabled.
604 #define DCHECK_IS_ON() (true)
605 #define DCHECK_SEVERITY FATAL 605 #define DCHECK_SEVERITY FATAL
606 const LogSeverity LOG_DCHECK = LOG_FATAL; 606 const LogSeverity LOG_DCHECK = LOG_FATAL;
607 // TODO(akalin): We don't define this as 'true' since if the log level
608 // is above FATAL, the DCHECK won't go through anyway. Make it so
609 // that DCHECKs work regardless of the logging level, then set this to
610 // 'true'.
611 #define DCHECK_IS_ON() LOG_IS_ON(DCHECK)
607 612
608 #endif // defined(NDEBUG) 613 #endif // defined(NDEBUG)
609 614
610 #else // ENABLE_DCHECK 615 #else // ENABLE_DCHECK
611 616
612 #define DCHECK_IS_ON() (false)
613 #define DCHECK_SEVERITY FATAL 617 #define DCHECK_SEVERITY FATAL
614 const LogSeverity LOG_DCHECK = LOG_FATAL; 618 const LogSeverity LOG_DCHECK = LOG_FATAL;
619 #define DCHECK_IS_ON() false
615 620
616 #endif // ENABLE_DCHECK 621 #endif // ENABLE_DCHECK
622 #undef ENABLE_DCHECK
617 623
618 // Unlike CHECK et al., DCHECK et al. *does* evaluate their arguments 624 // Unlike CHECK et al., DCHECK et al. *does* evaluate their arguments
619 // lazily. 625 // lazily.
620 626
621 // DCHECK et al. also make sure to reference |condition| regardless of 627 // DCHECK et al. also make sure to reference |condition| regardless of
622 // whether DCHECKs are enabled; this is so that we don't get unused 628 // whether DCHECKs are enabled; this is so that we don't get unused
623 // variable warnings if the only use of a variable is in a DCHECK. 629 // variable warnings if the only use of a variable is in a DCHECK.
624 // This behavior is different from DLOG_IF et al. 630 // This behavior is different from DLOG_IF et al.
625 631
626 #define DCHECK(condition) \ 632 #define DCHECK(condition) \
627 !DCHECK_IS_ON() ? (void) 0 : \ 633 !DCHECK_IS_ON() ? (void) 0 : \
628 LOG_IF(DCHECK_SEVERITY, !(condition)) \ 634 LOG_IF(DCHECK_SEVERITY, !(condition)) \
629 << "Check failed: " #condition ". " 635 << "Check failed: " #condition ". "
630 636
631 #define DPCHECK(condition) \ 637 #define DPCHECK(condition) \
632 !DCHECK_IS_ON() ? (void) 0 : \ 638 !DCHECK_IS_ON() ? (void) 0 : \
633 PLOG_IF(DCHECK_SEVERITY, !(condition)) \ 639 PLOG_IF(DCHECK_SEVERITY, !(condition)) \
634 << "Check failed: " #condition ". " 640 << "Check failed: " #condition ". "
635 641
636 // Helper macro for binary operators. 642 // Helper macro for binary operators.
637 // Don't use this macro directly in your code, use DCHECK_EQ et al below. 643 // Don't use this macro directly in your code, use DCHECK_EQ et al below.
638 #define DCHECK_OP(name, op, val1, val2) \ 644 #define DCHECK_OP(name, op, val1, val2) \
639 if (DLOG_IS_ON(DCHECK_SEVERITY)) \ 645 if (DCHECK_IS_ON()) \
640 if (logging::CheckOpString _result = \ 646 if (logging::CheckOpString _result = \
641 logging::Check##name##Impl((val1), (val2), \ 647 logging::Check##name##Impl((val1), (val2), \
642 #val1 " " #op " " #val2)) \ 648 #val1 " " #op " " #val2)) \
643 logging::LogMessage( \ 649 logging::LogMessage( \
644 __FILE__, __LINE__, ::logging::LOG_DCHECK, \ 650 __FILE__, __LINE__, ::logging::LOG_DCHECK, \
645 _result).stream() 651 _result).stream()
646 652
647 // Equality/Inequality checks - compare two values, and log a LOG_FATAL message 653 // Equality/Inequality checks - compare two values, and log a LOG_FATAL message
648 // including the two values when the result is not as expected. The values 654 // including the two values when the result is not as expected. The values
649 // must have operator<<(ostream, ...) defined. 655 // must have operator<<(ostream, ...) defined.
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
933 #elif NOTIMPLEMENTED_POLICY == 4 939 #elif NOTIMPLEMENTED_POLICY == 4
934 #define NOTIMPLEMENTED() LOG(ERROR) << NOTIMPLEMENTED_MSG 940 #define NOTIMPLEMENTED() LOG(ERROR) << NOTIMPLEMENTED_MSG
935 #elif NOTIMPLEMENTED_POLICY == 5 941 #elif NOTIMPLEMENTED_POLICY == 5
936 #define NOTIMPLEMENTED() do {\ 942 #define NOTIMPLEMENTED() do {\
937 static int count = 0;\ 943 static int count = 0;\
938 LOG_IF(ERROR, 0 == count++) << NOTIMPLEMENTED_MSG;\ 944 LOG_IF(ERROR, 0 == count++) << NOTIMPLEMENTED_MSG;\
939 } while(0) 945 } while(0)
940 #endif 946 #endif
941 947
942 #endif // BASE_LOGGING_H_ 948 #endif // BASE_LOGGING_H_
OLDNEW
« no previous file with comments | « no previous file | base/logging_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698