logging.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2009 The RE2 Authors. All Rights Reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. #ifndef UTIL_LOGGING_H_
  5. #define UTIL_LOGGING_H_
  6. // Simplified version of Google's logging.
  7. #include <assert.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <ostream>
  11. #include <sstream>
  12. #include "absl/base/attributes.h"
  13. // Debug-only checking.
  14. #define DCHECK(condition) assert(condition)
  15. #define DCHECK_EQ(val1, val2) assert((val1) == (val2))
  16. #define DCHECK_NE(val1, val2) assert((val1) != (val2))
  17. #define DCHECK_LE(val1, val2) assert((val1) <= (val2))
  18. #define DCHECK_LT(val1, val2) assert((val1) < (val2))
  19. #define DCHECK_GE(val1, val2) assert((val1) >= (val2))
  20. #define DCHECK_GT(val1, val2) assert((val1) > (val2))
  21. // Always-on checking
  22. #define CHECK(x) if(x){}else LogMessageFatal(__FILE__, __LINE__).stream() << "Check failed: " #x
  23. #define CHECK_LT(x, y) CHECK((x) < (y))
  24. #define CHECK_GT(x, y) CHECK((x) > (y))
  25. #define CHECK_LE(x, y) CHECK((x) <= (y))
  26. #define CHECK_GE(x, y) CHECK((x) >= (y))
  27. #define CHECK_EQ(x, y) CHECK((x) == (y))
  28. #define CHECK_NE(x, y) CHECK((x) != (y))
  29. #define LOG_INFO LogMessage(__FILE__, __LINE__)
  30. #define LOG_WARNING LogMessage(__FILE__, __LINE__)
  31. #define LOG_ERROR LogMessage(__FILE__, __LINE__)
  32. #define LOG_FATAL LogMessageFatal(__FILE__, __LINE__)
  33. #define LOG_QFATAL LOG_FATAL
  34. // It seems that one of the Windows header files defines ERROR as 0.
  35. #ifdef _WIN32
  36. #define LOG_0 LOG_INFO
  37. #endif
  38. #ifdef NDEBUG
  39. #define LOG_DFATAL LOG_ERROR
  40. #else
  41. #define LOG_DFATAL LOG_FATAL
  42. #endif
  43. #define LOG(severity) LOG_ ## severity.stream()
  44. #define VLOG(x) if((x)>0){}else LOG_INFO.stream()
  45. class LogMessage {
  46. public:
  47. LogMessage(const char* file, int line)
  48. : flushed_(false) {
  49. stream() << file << ":" << line << ": ";
  50. }
  51. void Flush() {
  52. stream() << "\n";
  53. std::string s = str_.str();
  54. size_t n = s.size();
  55. if (fwrite(s.data(), 1, n, stderr) < n) {} // shut up gcc
  56. flushed_ = true;
  57. }
  58. ~LogMessage() {
  59. if (!flushed_) {
  60. Flush();
  61. }
  62. }
  63. std::ostream& stream() { return str_; }
  64. private:
  65. bool flushed_;
  66. std::ostringstream str_;
  67. LogMessage(const LogMessage&) = delete;
  68. LogMessage& operator=(const LogMessage&) = delete;
  69. };
  70. // Silence "destructor never returns" warning for ~LogMessageFatal().
  71. // Since this is a header file, push and then pop to limit the scope.
  72. #ifdef _MSC_VER
  73. #pragma warning(push)
  74. #pragma warning(disable: 4722)
  75. #endif
  76. class LogMessageFatal : public LogMessage {
  77. public:
  78. LogMessageFatal(const char* file, int line)
  79. : LogMessage(file, line) {}
  80. ABSL_ATTRIBUTE_NORETURN ~LogMessageFatal() {
  81. Flush();
  82. abort();
  83. }
  84. private:
  85. LogMessageFatal(const LogMessageFatal&) = delete;
  86. LogMessageFatal& operator=(const LogMessageFatal&) = delete;
  87. };
  88. #ifdef _MSC_VER
  89. #pragma warning(pop)
  90. #endif
  91. #endif // UTIL_LOGGING_H_