check.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Copyright 2022 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // -----------------------------------------------------------------------------
  16. // File: log/check.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. // This header declares a family of `CHECK` macros.
  20. //
  21. // `CHECK` macros terminate the program with a fatal error if the specified
  22. // condition is not true.
  23. //
  24. // Except for those whose names begin with `DCHECK`, these macros are not
  25. // controlled by `NDEBUG` (cf. `assert`), so the check will be executed
  26. // regardless of compilation mode. `CHECK` and friends are thus useful for
  27. // confirming invariants in situations where continuing to run would be worse
  28. // than terminating, e.g., due to risk of data corruption or security
  29. // compromise. It is also more robust and portable to deliberately terminate
  30. // at a particular place with a useful message and backtrace than to assume some
  31. // ultimately unspecified and unreliable crashing behavior (such as a
  32. // "segmentation fault").
  33. #ifndef Y_ABSL_LOG_CHECK_H_
  34. #define Y_ABSL_LOG_CHECK_H_
  35. #include "y_absl/log/internal/check_impl.h"
  36. #include "y_absl/log/internal/check_op.h" // IWYU pragma: export
  37. #include "y_absl/log/internal/conditions.h" // IWYU pragma: export
  38. #include "y_absl/log/internal/log_message.h" // IWYU pragma: export
  39. #include "y_absl/log/internal/strip.h" // IWYU pragma: export
  40. // CHECK()
  41. //
  42. // `CHECK` terminates the program with a fatal error if `condition` is not true.
  43. //
  44. // The message may include additional information such as stack traces, when
  45. // available.
  46. //
  47. // Example:
  48. //
  49. // CHECK(!cheese.empty()) << "Out of Cheese";
  50. //
  51. // Might produce a message like:
  52. //
  53. // Check failed: !cheese.empty() Out of Cheese
  54. #define CHECK(condition) Y_ABSL_LOG_INTERNAL_CHECK_IMPL((condition), #condition)
  55. // QCHECK()
  56. //
  57. // `QCHECK` behaves like `CHECK` but does not print a full stack trace and does
  58. // not run registered error handlers (as `QFATAL`). It is useful when the
  59. // problem is definitely unrelated to program flow, e.g. when validating user
  60. // input.
  61. #define QCHECK(condition) Y_ABSL_LOG_INTERNAL_QCHECK_IMPL((condition), #condition)
  62. // PCHECK()
  63. //
  64. // `PCHECK` behaves like `CHECK` but appends a description of the current state
  65. // of `errno` to the failure message.
  66. //
  67. // Example:
  68. //
  69. // int fd = open("/var/empty/missing", O_RDONLY);
  70. // PCHECK(fd != -1) << "posix is difficult";
  71. //
  72. // Might produce a message like:
  73. //
  74. // Check failed: fd != -1 posix is difficult: No such file or directory [2]
  75. #define PCHECK(condition) Y_ABSL_LOG_INTERNAL_PCHECK_IMPL((condition), #condition)
  76. // DCHECK()
  77. //
  78. // `DCHECK` behaves like `CHECK` in debug mode and does nothing otherwise (as
  79. // `DLOG`). Unlike with `CHECK` (but as with `assert`), it is not safe to rely
  80. // on evaluation of `condition`: when `NDEBUG` is enabled, DCHECK does not
  81. // evaluate the condition.
  82. #define DCHECK(condition) Y_ABSL_LOG_INTERNAL_DCHECK_IMPL((condition), #condition)
  83. // `CHECK_EQ` and friends are syntactic sugar for `CHECK(x == y)` that
  84. // automatically output the expression being tested and the evaluated values on
  85. // either side.
  86. //
  87. // Example:
  88. //
  89. // int x = 3, y = 5;
  90. // CHECK_EQ(2 * x, y) << "oops!";
  91. //
  92. // Might produce a message like:
  93. //
  94. // Check failed: 2 * x == y (6 vs. 5) oops!
  95. //
  96. // The values must implement the appropriate comparison operator as well as
  97. // `operator<<(std::ostream&, ...)`. Care is taken to ensure that each
  98. // argument is evaluated exactly once, and that anything which is legal to pass
  99. // as a function argument is legal here. In particular, the arguments may be
  100. // temporary expressions which will end up being destroyed at the end of the
  101. // statement,
  102. //
  103. // Example:
  104. //
  105. // CHECK_EQ(TString("abc")[1], 'b');
  106. //
  107. // WARNING: Passing `NULL` as an argument to `CHECK_EQ` and similar macros does
  108. // not compile. Use `nullptr` instead.
  109. #define CHECK_EQ(val1, val2) \
  110. Y_ABSL_LOG_INTERNAL_CHECK_EQ_IMPL((val1), #val1, (val2), #val2)
  111. #define CHECK_NE(val1, val2) \
  112. Y_ABSL_LOG_INTERNAL_CHECK_NE_IMPL((val1), #val1, (val2), #val2)
  113. #define CHECK_LE(val1, val2) \
  114. Y_ABSL_LOG_INTERNAL_CHECK_LE_IMPL((val1), #val1, (val2), #val2)
  115. #define CHECK_LT(val1, val2) \
  116. Y_ABSL_LOG_INTERNAL_CHECK_LT_IMPL((val1), #val1, (val2), #val2)
  117. #define CHECK_GE(val1, val2) \
  118. Y_ABSL_LOG_INTERNAL_CHECK_GE_IMPL((val1), #val1, (val2), #val2)
  119. #define CHECK_GT(val1, val2) \
  120. Y_ABSL_LOG_INTERNAL_CHECK_GT_IMPL((val1), #val1, (val2), #val2)
  121. #define QCHECK_EQ(val1, val2) \
  122. Y_ABSL_LOG_INTERNAL_QCHECK_EQ_IMPL((val1), #val1, (val2), #val2)
  123. #define QCHECK_NE(val1, val2) \
  124. Y_ABSL_LOG_INTERNAL_QCHECK_NE_IMPL((val1), #val1, (val2), #val2)
  125. #define QCHECK_LE(val1, val2) \
  126. Y_ABSL_LOG_INTERNAL_QCHECK_LE_IMPL((val1), #val1, (val2), #val2)
  127. #define QCHECK_LT(val1, val2) \
  128. Y_ABSL_LOG_INTERNAL_QCHECK_LT_IMPL((val1), #val1, (val2), #val2)
  129. #define QCHECK_GE(val1, val2) \
  130. Y_ABSL_LOG_INTERNAL_QCHECK_GE_IMPL((val1), #val1, (val2), #val2)
  131. #define QCHECK_GT(val1, val2) \
  132. Y_ABSL_LOG_INTERNAL_QCHECK_GT_IMPL((val1), #val1, (val2), #val2)
  133. #define DCHECK_EQ(val1, val2) \
  134. Y_ABSL_LOG_INTERNAL_DCHECK_EQ_IMPL((val1), #val1, (val2), #val2)
  135. #define DCHECK_NE(val1, val2) \
  136. Y_ABSL_LOG_INTERNAL_DCHECK_NE_IMPL((val1), #val1, (val2), #val2)
  137. #define DCHECK_LE(val1, val2) \
  138. Y_ABSL_LOG_INTERNAL_DCHECK_LE_IMPL((val1), #val1, (val2), #val2)
  139. #define DCHECK_LT(val1, val2) \
  140. Y_ABSL_LOG_INTERNAL_DCHECK_LT_IMPL((val1), #val1, (val2), #val2)
  141. #define DCHECK_GE(val1, val2) \
  142. Y_ABSL_LOG_INTERNAL_DCHECK_GE_IMPL((val1), #val1, (val2), #val2)
  143. #define DCHECK_GT(val1, val2) \
  144. Y_ABSL_LOG_INTERNAL_DCHECK_GT_IMPL((val1), #val1, (val2), #val2)
  145. // `CHECK_OK` and friends validate that the provided `y_absl::Status` or
  146. // `y_absl::StatusOr<T>` is OK. If it isn't, they print a failure message that
  147. // includes the actual status and terminate the program.
  148. //
  149. // As with all `DCHECK` variants, `DCHECK_OK` has no effect (not even
  150. // evaluating its argument) if `NDEBUG` is enabled.
  151. //
  152. // Example:
  153. //
  154. // CHECK_OK(FunctionReturnsStatus(x, y, z)) << "oops!";
  155. //
  156. // Might produce a message like:
  157. //
  158. // Check failed: FunctionReturnsStatus(x, y, z) is OK (ABORTED: timeout) oops!
  159. #define CHECK_OK(status) Y_ABSL_LOG_INTERNAL_CHECK_OK_IMPL((status), #status)
  160. #define QCHECK_OK(status) Y_ABSL_LOG_INTERNAL_QCHECK_OK_IMPL((status), #status)
  161. #define DCHECK_OK(status) Y_ABSL_LOG_INTERNAL_DCHECK_OK_IMPL((status), #status)
  162. // `CHECK_STREQ` and friends provide `CHECK_EQ` functionality for C strings,
  163. // i.e., null-terminated char arrays. The `CASE` versions are case-insensitive.
  164. //
  165. // Example:
  166. //
  167. // CHECK_STREQ(argv[0], "./skynet");
  168. //
  169. // Note that both arguments may be temporary strings which are destroyed by the
  170. // compiler at the end of the current full expression.
  171. //
  172. // Example:
  173. //
  174. // CHECK_STREQ(Foo().c_str(), Bar().c_str());
  175. #define CHECK_STREQ(s1, s2) \
  176. Y_ABSL_LOG_INTERNAL_CHECK_STREQ_IMPL((s1), #s1, (s2), #s2)
  177. #define CHECK_STRNE(s1, s2) \
  178. Y_ABSL_LOG_INTERNAL_CHECK_STRNE_IMPL((s1), #s1, (s2), #s2)
  179. #define CHECK_STRCASEEQ(s1, s2) \
  180. Y_ABSL_LOG_INTERNAL_CHECK_STRCASEEQ_IMPL((s1), #s1, (s2), #s2)
  181. #define CHECK_STRCASENE(s1, s2) \
  182. Y_ABSL_LOG_INTERNAL_CHECK_STRCASENE_IMPL((s1), #s1, (s2), #s2)
  183. #define QCHECK_STREQ(s1, s2) \
  184. Y_ABSL_LOG_INTERNAL_QCHECK_STREQ_IMPL((s1), #s1, (s2), #s2)
  185. #define QCHECK_STRNE(s1, s2) \
  186. Y_ABSL_LOG_INTERNAL_QCHECK_STRNE_IMPL((s1), #s1, (s2), #s2)
  187. #define QCHECK_STRCASEEQ(s1, s2) \
  188. Y_ABSL_LOG_INTERNAL_QCHECK_STRCASEEQ_IMPL((s1), #s1, (s2), #s2)
  189. #define QCHECK_STRCASENE(s1, s2) \
  190. Y_ABSL_LOG_INTERNAL_QCHECK_STRCASENE_IMPL((s1), #s1, (s2), #s2)
  191. #define DCHECK_STREQ(s1, s2) \
  192. Y_ABSL_LOG_INTERNAL_DCHECK_STREQ_IMPL((s1), #s1, (s2), #s2)
  193. #define DCHECK_STRNE(s1, s2) \
  194. Y_ABSL_LOG_INTERNAL_DCHECK_STRNE_IMPL((s1), #s1, (s2), #s2)
  195. #define DCHECK_STRCASEEQ(s1, s2) \
  196. Y_ABSL_LOG_INTERNAL_DCHECK_STRCASEEQ_IMPL((s1), #s1, (s2), #s2)
  197. #define DCHECK_STRCASENE(s1, s2) \
  198. Y_ABSL_LOG_INTERNAL_DCHECK_STRCASENE_IMPL((s1), #s1, (s2), #s2)
  199. #endif // Y_ABSL_LOG_CHECK_H_