Chromium Code Reviews| 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 <cassert> | 8 #include <cassert> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <cstring> | 10 #include <cstring> |
| 11 #include <sstream> | 11 #include <sstream> |
| 12 #include <type_traits> | |
|
dcheng
2015/11/13 03:30:23
Pretty sure this isn't allowed yet.
| |
| 12 | 13 |
| 13 #include "base/base_export.h" | 14 #include "base/base_export.h" |
| 14 #include "base/basictypes.h" | 15 #include "base/basictypes.h" |
| 15 #include "base/debug/debugger.h" | 16 #include "base/debug/debugger.h" |
| 16 #include "build/build_config.h" | 17 #include "build/build_config.h" |
| 17 | 18 |
| 18 // | 19 // |
| 19 // Optional message capabilities | 20 // Optional message capabilities |
| 20 // ----------------------------- | 21 // ----------------------------- |
| 21 // Assertion failed messages and fatal errors are displayed in a dialog box | 22 // Assertion failed messages and fatal errors are displayed in a dialog box |
| (...skipping 484 matching lines...) Expand 10 before | Expand all | Expand 10 after 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. | |
| 519 template <typename T> | |
| 520 using is_scoped_enum = | |
| 521 std::integral_constant<bool, | |
| 522 std::is_enum<T>::value && | |
| 523 !std::is_convertible<T, int>::value>; | |
| 524 } // namespace internal | |
| 525 | |
| 516 // Build the error message string. This is separate from the "Impl" | 526 // Build the error message string. This is separate from the "Impl" |
| 517 // function template because it is not performance critical and so can | 527 // function template because it is not performance critical and so can |
| 518 // be out of line, while the "Impl" code should be inline. Caller | 528 // be out of line, while the "Impl" code should be inline. Caller |
| 519 // takes ownership of the returned string. | 529 // takes ownership of the returned string. |
| 520 template<class t1, class t2> | 530 template <class T1, |
| 521 std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) { | 531 class T2, |
| 532 typename std::enable_if<!internal::is_scoped_enum<T1>::value && | |
| 533 !internal::is_scoped_enum<T2>::value>::type* = | |
| 534 nullptr> | |
| 535 std::string* MakeCheckOpString(const T1& v1, const T2& v2, const char* names) { | |
| 522 std::ostringstream ss; | 536 std::ostringstream ss; |
| 523 ss << names << " (" << v1 << " vs. " << v2 << ")"; | 537 ss << names << " (" << v1 << " vs. " << v2 << ")"; |
| 524 std::string* msg = new std::string(ss.str()); | 538 std::string* msg = new std::string(ss.str()); |
| 525 return msg; | 539 return msg; |
| 526 } | 540 } |
| 527 | 541 |
| 542 // Template specialization for scoped enums. Only allows comparisons between | |
| 543 // values from the same scoped enum. | |
| 544 template <class T, | |
| 545 typename std::enable_if<internal::is_scoped_enum<T>::value>::type* = | |
| 546 nullptr> | |
| 547 std::string* MakeCheckOpString(const T& v1, const T& v2, const char* names) { | |
| 548 using EnumIntegralType = typename std::underlying_type<T>::type; | |
| 549 std::ostringstream ss; | |
| 550 ss << names << " (" << static_cast<EnumIntegralType>(v1) << " vs. " | |
| 551 << static_cast<EnumIntegralType>(v2) << ")"; | |
| 552 std::string* msg = new std::string(ss.str()); | |
| 553 return msg; | |
| 554 } | |
| 555 | |
| 528 // Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated | 556 // Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated |
| 529 // in logging.cc. | 557 // in logging.cc. |
| 530 extern template BASE_EXPORT std::string* MakeCheckOpString<int, int>( | 558 extern template BASE_EXPORT std::string* MakeCheckOpString<int, int>( |
| 531 const int&, const int&, const char* names); | 559 const int&, const int&, const char* names); |
| 532 extern template BASE_EXPORT | 560 extern template BASE_EXPORT |
| 533 std::string* MakeCheckOpString<unsigned long, unsigned long>( | 561 std::string* MakeCheckOpString<unsigned long, unsigned long>( |
| 534 const unsigned long&, const unsigned long&, const char* names); | 562 const unsigned long&, const unsigned long&, const char* names); |
| 535 extern template BASE_EXPORT | 563 extern template BASE_EXPORT |
| 536 std::string* MakeCheckOpString<unsigned long, unsigned int>( | 564 std::string* MakeCheckOpString<unsigned long, unsigned int>( |
| 537 const unsigned long&, const unsigned int&, const char* names); | 565 const unsigned long&, const unsigned int&, const char* names); |
| (...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 962 #elif NOTIMPLEMENTED_POLICY == 5 | 990 #elif NOTIMPLEMENTED_POLICY == 5 |
| 963 #define NOTIMPLEMENTED() do {\ | 991 #define NOTIMPLEMENTED() do {\ |
| 964 static bool logged_once = false;\ | 992 static bool logged_once = false;\ |
| 965 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ | 993 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ |
| 966 logged_once = true;\ | 994 logged_once = true;\ |
| 967 } while(0);\ | 995 } while(0);\ |
| 968 EAT_STREAM_PARAMETERS | 996 EAT_STREAM_PARAMETERS |
| 969 #endif | 997 #endif |
| 970 | 998 |
| 971 #endif // BASE_LOGGING_H_ | 999 #endif // BASE_LOGGING_H_ |
| OLD | NEW |