logging.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #pragma once
  2. // Copyright (c) 2005, Google Inc.
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // ---
  31. // This file contains #include information about logging-related stuff.
  32. // Pretty much everybody needs to #include this file so that they can
  33. // log various happenings.
  34. //
  35. #ifndef _LOGGING_H_
  36. #define _LOGGING_H_
  37. #include <string.h>
  38. #include <stdio.h>
  39. #include <unistd.h>
  40. #define WRITE_TO_STDERR(buf, len) write(STDERR_FILENO, buf, len)
  41. #define CHECK(condition) \
  42. do { \
  43. if (!(condition)) { \
  44. WRITE_TO_STDERR("Check failed: " #condition "\n", \
  45. sizeof("Check failed: " #condition "\n")-1); \
  46. abort(); \
  47. } \
  48. } while (0)
  49. // This takes a message to print. The name is historical.
  50. #define RAW_CHECK(condition, message) \
  51. do { \
  52. if (!(condition)) { \
  53. WRITE_TO_STDERR("Check failed: " #condition ": " message "\n", \
  54. sizeof("Check failed: " #condition ": " message "\n")-1);\
  55. abort(); \
  56. } \
  57. } while (0)
  58. // This is like RAW_CHECK, but only in debug-mode
  59. #ifdef NDEBUG
  60. enum { DEBUG_MODE = 0 };
  61. #define RAW_DCHECK(condition, message)
  62. #else
  63. enum { DEBUG_MODE = 1 };
  64. #define RAW_DCHECK(condition, message) RAW_CHECK(condition, message)
  65. #endif
  66. // This prints errno as well. Note we use write instead of printf/puts to
  67. // avoid the risk we'll call malloc().
  68. #define PCHECK(condition) \
  69. do { \
  70. if (!(condition)) { \
  71. const int err_no = errno; \
  72. WRITE_TO_STDERR("Check failed: " #condition ": ", \
  73. sizeof("Check failed: " #condition ": ")-1); \
  74. WRITE_TO_STDERR(strerror(err_no), strlen(strerror(err_no))); \
  75. WRITE_TO_STDERR("\n", sizeof("\n")-1); \
  76. abort(); \
  77. } \
  78. } while (0)
  79. // Helper macro for binary operators; prints the two values on error
  80. // Don't use this macro directly in your code, use CHECK_EQ et al below
  81. // WARNING: These don't compile correctly if one of the arguments is a pointer
  82. // and the other is NULL. To work around this, simply static_cast NULL to the
  83. // type of the desired pointer.
  84. // TODO(jandrews): Also print the values in case of failure. Requires some
  85. // sort of type-sensitive ToString() function.
  86. #define CHECK_OP(op, val1, val2) \
  87. do { \
  88. if (!((val1) op (val2))) { \
  89. fprintf(stderr, "Check failed: %s %s %s\n", #val1, #op, #val2); \
  90. abort(); \
  91. } \
  92. } while (0)
  93. #define CHECK_EQ(val1, val2) CHECK_OP(==, val1, val2)
  94. #define CHECK_NE(val1, val2) CHECK_OP(!=, val1, val2)
  95. #define CHECK_LE(val1, val2) CHECK_OP(<=, val1, val2)
  96. #define CHECK_LT(val1, val2) CHECK_OP(< , val1, val2)
  97. #define CHECK_GE(val1, val2) CHECK_OP(>=, val1, val2)
  98. #define CHECK_GT(val1, val2) CHECK_OP(> , val1, val2)
  99. // Synonyms for CHECK_* that are used in some unittests.
  100. #define EXPECT_EQ(val1, val2) CHECK_EQ(val1, val2)
  101. #define EXPECT_NE(val1, val2) CHECK_NE(val1, val2)
  102. #define EXPECT_LE(val1, val2) CHECK_LE(val1, val2)
  103. #define EXPECT_LT(val1, val2) CHECK_LT(val1, val2)
  104. #define EXPECT_GE(val1, val2) CHECK_GE(val1, val2)
  105. #define EXPECT_GT(val1, val2) CHECK_GT(val1, val2)
  106. #define ASSERT_EQ(val1, val2) EXPECT_EQ(val1, val2)
  107. #define ASSERT_NE(val1, val2) EXPECT_NE(val1, val2)
  108. #define ASSERT_LE(val1, val2) EXPECT_LE(val1, val2)
  109. #define ASSERT_LT(val1, val2) EXPECT_LT(val1, val2)
  110. #define ASSERT_GE(val1, val2) EXPECT_GE(val1, val2)
  111. #define ASSERT_GT(val1, val2) EXPECT_GT(val1, val2)
  112. // As are these variants.
  113. #define EXPECT_TRUE(cond) CHECK(cond)
  114. #define EXPECT_FALSE(cond) CHECK(!(cond))
  115. #define EXPECT_STREQ(a, b) CHECK(strcmp(a, b) == 0)
  116. #define ASSERT_TRUE(cond) EXPECT_TRUE(cond)
  117. #define ASSERT_FALSE(cond) EXPECT_FALSE(cond)
  118. #define ASSERT_STREQ(a, b) EXPECT_STREQ(a, b)
  119. // Used for (libc) functions that return -1 and set errno
  120. #define CHECK_ERR(invocation) PCHECK((invocation) != -1)
  121. // A few more checks that only happen in debug mode
  122. #ifdef NDEBUG
  123. #define DCHECK_EQ(val1, val2)
  124. #define DCHECK_NE(val1, val2)
  125. #define DCHECK_LE(val1, val2)
  126. #define DCHECK_LT(val1, val2)
  127. #define DCHECK_GE(val1, val2)
  128. #define DCHECK_GT(val1, val2)
  129. #else
  130. #define DCHECK_EQ(val1, val2) CHECK_EQ(val1, val2)
  131. #define DCHECK_NE(val1, val2) CHECK_NE(val1, val2)
  132. #define DCHECK_LE(val1, val2) CHECK_LE(val1, val2)
  133. #define DCHECK_LT(val1, val2) CHECK_LT(val1, val2)
  134. #define DCHECK_GE(val1, val2) CHECK_GE(val1, val2)
  135. #define DCHECK_GT(val1, val2) CHECK_GT(val1, val2)
  136. #endif
  137. #endif // _LOGGING_H_