OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <cassert> | 10 #include <cassert> |
11 #include <cstring> | 11 #include <cstring> |
12 #include <sstream> | 12 #include <sstream> |
13 #include <string> | 13 #include <string> |
| 14 #include <type_traits> |
14 | 15 |
15 #include "base/base_export.h" | 16 #include "base/base_export.h" |
16 #include "base/debug/debugger.h" | 17 #include "base/debug/debugger.h" |
17 #include "base/macros.h" | 18 #include "base/macros.h" |
18 #include "build/build_config.h" | 19 #include "build/build_config.h" |
19 | 20 |
20 // | 21 // |
21 // Optional message capabilities | 22 // Optional message capabilities |
22 // ----------------------------- | 23 // ----------------------------- |
23 // Assertion failed messages and fatal errors are displayed in a dialog box | 24 // Assertion failed messages and fatal errors are displayed in a dialog box |
(...skipping 482 matching lines...) Loading... |
506 switch (0) case 0: default: \ | 507 switch (0) case 0: default: \ |
507 if (logging::CheckOpResult true_if_passed = \ | 508 if (logging::CheckOpResult true_if_passed = \ |
508 logging::Check##name##Impl((val1), (val2), \ | 509 logging::Check##name##Impl((val1), (val2), \ |
509 #val1 " " #op " " #val2)) \ | 510 #val1 " " #op " " #val2)) \ |
510 ; \ | 511 ; \ |
511 else \ | 512 else \ |
512 logging::LogMessage(__FILE__, __LINE__, true_if_passed.message()).stream() | 513 logging::LogMessage(__FILE__, __LINE__, true_if_passed.message()).stream() |
513 | 514 |
514 #endif | 515 #endif |
515 | 516 |
| 517 namespace internal { |
| 518 // Type trait helper to detect scoped enums. Unscoped enums are always |
| 519 // implicitly convertible to ints, per C++11 4.7.1: |
| 520 // A prvalue of an unscoped enumeration type can be converted to a prvalue of |
| 521 // an integer type. |
| 522 // while a scoped enum requires an explicit conversion per C++11 5.2.9.9. |
| 523 // |
| 524 // Note that these helper traits don't use std::integral_constant, because |
| 525 // less-than and greater-than operators don't work well inside a template |
| 526 // parameter list. |
| 527 template <typename T> |
| 528 using is_scoped_enum = |
| 529 std::integral_constant<bool, |
| 530 std::is_enum<T>::value && |
| 531 !std::is_convertible<T, int>::value>; |
| 532 |
| 533 template <typename T, bool = is_scoped_enum<T>::value> |
| 534 struct is_signed_scoped_enum { |
| 535 static const bool value = static_cast<T>(-1) < static_cast<T>(0); |
| 536 }; |
| 537 |
| 538 template <typename T> |
| 539 struct is_signed_scoped_enum<T, false> : std::false_type {}; |
| 540 |
| 541 template <typename T, bool = is_scoped_enum<T>::value> |
| 542 struct is_unsigned_scoped_enum { |
| 543 static const bool value = static_cast<T>(-1) > static_cast<T>(0); |
| 544 }; |
| 545 |
| 546 template <typename T> |
| 547 struct is_unsigned_scoped_enum<T, false> : public std::false_type {}; |
| 548 |
| 549 } // namespace internal |
| 550 |
| 551 // Default value logger just uses operator<<. |
| 552 template <class T, |
| 553 typename std::enable_if<!internal::is_scoped_enum<T>::value>::type* = |
| 554 nullptr> |
| 555 void MakeCheckOpValueString(const T& v, std::ostream* os) { |
| 556 *os << v; |
| 557 } |
| 558 |
| 559 // TODO(dcheng): Once all platforms support the std::underlying_type trait, use |
| 560 // it here. For now, there is no universal library support, so cheat and convert |
| 561 // it to the max-width integral type. |
| 562 template <class T, |
| 563 typename std::enable_if< |
| 564 internal::is_signed_scoped_enum<T>::value>::type* = nullptr> |
| 565 void MakeCheckOpValueString(const T& v, std::ostream* os) { |
| 566 *os << static_cast<intmax_t>(v); |
| 567 } |
| 568 |
| 569 template <class T, |
| 570 typename std::enable_if< |
| 571 internal::is_unsigned_scoped_enum<T>::value>::type* = nullptr> |
| 572 void MakeCheckOpValueString(const T& v, std::ostream* os) { |
| 573 *os << static_cast<uintmax_t>(v); |
| 574 } |
| 575 |
516 // Build the error message string. This is separate from the "Impl" | 576 // Build the error message string. This is separate from the "Impl" |
517 // function template because it is not performance critical and so can | 577 // function template because it is not performance critical and so can |
518 // be out of line, while the "Impl" code should be inline. Caller | 578 // be out of line, while the "Impl" code should be inline. Caller |
519 // takes ownership of the returned string. | 579 // takes ownership of the returned string. |
520 template<class t1, class t2> | 580 template <class T1, class T2> |
521 std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) { | 581 std::string* MakeCheckOpString(const T1& v1, const T2& v2, const char* names) { |
522 std::ostringstream ss; | 582 std::ostringstream ss; |
523 ss << names << " (" << v1 << " vs. " << v2 << ")"; | 583 ss << names << " ("; |
| 584 MakeCheckOpValueString(v1, &ss); |
| 585 ss << " vs. "; |
| 586 MakeCheckOpValueString(v2, &ss); |
| 587 ss << ")"; |
524 std::string* msg = new std::string(ss.str()); | 588 std::string* msg = new std::string(ss.str()); |
525 return msg; | 589 return msg; |
526 } | 590 } |
527 | 591 |
528 // Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated | 592 // Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated |
529 // in logging.cc. | 593 // in logging.cc. |
530 extern template BASE_EXPORT std::string* MakeCheckOpString<int, int>( | 594 extern template BASE_EXPORT std::string* MakeCheckOpString<int, int>( |
531 const int&, const int&, const char* names); | 595 const int&, const int&, const char* names); |
532 extern template BASE_EXPORT | 596 extern template BASE_EXPORT |
533 std::string* MakeCheckOpString<unsigned long, unsigned long>( | 597 std::string* MakeCheckOpString<unsigned long, unsigned long>( |
(...skipping 422 matching lines...) Loading... |
956 #elif NOTIMPLEMENTED_POLICY == 5 | 1020 #elif NOTIMPLEMENTED_POLICY == 5 |
957 #define NOTIMPLEMENTED() do {\ | 1021 #define NOTIMPLEMENTED() do {\ |
958 static bool logged_once = false;\ | 1022 static bool logged_once = false;\ |
959 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ | 1023 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ |
960 logged_once = true;\ | 1024 logged_once = true;\ |
961 } while(0);\ | 1025 } while(0);\ |
962 EAT_STREAM_PARAMETERS | 1026 EAT_STREAM_PARAMETERS |
963 #endif | 1027 #endif |
964 | 1028 |
965 #endif // BASE_LOGGING_H_ | 1029 #endif // BASE_LOGGING_H_ |
OLD | NEW |