File indexing completed on 2025-12-16 09:40:52
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 #ifndef ABSL_LOG_INTERNAL_CHECK_OP_H_
0023 #define ABSL_LOG_INTERNAL_CHECK_OP_H_
0024
0025 #include <stdint.h>
0026
0027 #include <cstddef>
0028 #include <ostream>
0029 #include <sstream>
0030 #include <string>
0031 #include <type_traits>
0032 #include <utility>
0033
0034 #include "absl/base/attributes.h"
0035 #include "absl/base/config.h"
0036 #include "absl/base/optimization.h"
0037 #include "absl/log/internal/nullguard.h"
0038 #include "absl/log/internal/nullstream.h"
0039 #include "absl/log/internal/strip.h"
0040 #include "absl/strings/has_absl_stringify.h"
0041 #include "absl/strings/string_view.h"
0042
0043
0044
0045 #ifdef ABSL_MIN_LOG_LEVEL
0046 #define ABSL_LOG_INTERNAL_STRIP_STRING_LITERAL(literal) \
0047 (::absl::LogSeverity::kFatal >= \
0048 static_cast<::absl::LogSeverity>(ABSL_MIN_LOG_LEVEL) \
0049 ? (literal) \
0050 : "")
0051 #else
0052 #define ABSL_LOG_INTERNAL_STRIP_STRING_LITERAL(literal) (literal)
0053 #endif
0054
0055 #ifdef NDEBUG
0056
0057
0058
0059 #define ABSL_LOG_INTERNAL_DCHECK_NOP(x, y) \
0060 while (false && ((void)(x), (void)(y), 0)) \
0061 ::absl::log_internal::NullStream().InternalStream()
0062 #endif
0063
0064 #define ABSL_LOG_INTERNAL_CHECK_OP(name, op, val1, val1_text, val2, val2_text) \
0065 while (::std::string* absl_log_internal_check_op_result \
0066 ABSL_LOG_INTERNAL_ATTRIBUTE_UNUSED_IF_STRIP_LOG = \
0067 ::absl::log_internal::name##Impl( \
0068 ::absl::log_internal::GetReferenceableValue(val1), \
0069 ::absl::log_internal::GetReferenceableValue(val2), \
0070 ABSL_LOG_INTERNAL_STRIP_STRING_LITERAL( \
0071 val1_text " " #op " " val2_text))) \
0072 ABSL_LOG_INTERNAL_CONDITION_FATAL(STATELESS, true) \
0073 ABSL_LOG_INTERNAL_CHECK(*absl_log_internal_check_op_result).InternalStream()
0074 #define ABSL_LOG_INTERNAL_QCHECK_OP(name, op, val1, val1_text, val2, \
0075 val2_text) \
0076 while (::std::string* absl_log_internal_qcheck_op_result = \
0077 ::absl::log_internal::name##Impl( \
0078 ::absl::log_internal::GetReferenceableValue(val1), \
0079 ::absl::log_internal::GetReferenceableValue(val2), \
0080 ABSL_LOG_INTERNAL_STRIP_STRING_LITERAL( \
0081 val1_text " " #op " " val2_text))) \
0082 ABSL_LOG_INTERNAL_CONDITION_QFATAL(STATELESS, true) \
0083 ABSL_LOG_INTERNAL_QCHECK(*absl_log_internal_qcheck_op_result).InternalStream()
0084 #define ABSL_LOG_INTERNAL_CHECK_STROP(func, op, expected, s1, s1_text, s2, \
0085 s2_text) \
0086 while (::std::string* absl_log_internal_check_strop_result = \
0087 ::absl::log_internal::Check##func##expected##Impl( \
0088 (s1), (s2), \
0089 ABSL_LOG_INTERNAL_STRIP_STRING_LITERAL(s1_text " " #op \
0090 " " s2_text))) \
0091 ABSL_LOG_INTERNAL_CONDITION_FATAL(STATELESS, true) \
0092 ABSL_LOG_INTERNAL_CHECK(*absl_log_internal_check_strop_result) \
0093 .InternalStream()
0094 #define ABSL_LOG_INTERNAL_QCHECK_STROP(func, op, expected, s1, s1_text, s2, \
0095 s2_text) \
0096 while (::std::string* absl_log_internal_qcheck_strop_result = \
0097 ::absl::log_internal::Check##func##expected##Impl( \
0098 (s1), (s2), \
0099 ABSL_LOG_INTERNAL_STRIP_STRING_LITERAL(s1_text " " #op \
0100 " " s2_text))) \
0101 ABSL_LOG_INTERNAL_CONDITION_QFATAL(STATELESS, true) \
0102 ABSL_LOG_INTERNAL_QCHECK(*absl_log_internal_qcheck_strop_result) \
0103 .InternalStream()
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128 #define ABSL_LOG_INTERNAL_CHECK_OK(val, val_text) \
0129 for (::std::pair<const ::absl::Status*, ::std::string*> \
0130 absl_log_internal_check_ok_goo; \
0131 absl_log_internal_check_ok_goo.first = \
0132 ::absl::log_internal::AsStatus(val), \
0133 absl_log_internal_check_ok_goo.second = \
0134 ABSL_PREDICT_TRUE(absl_log_internal_check_ok_goo.first->ok()) \
0135 ? nullptr \
0136 : ::absl::status_internal::MakeCheckFailString( \
0137 absl_log_internal_check_ok_goo.first, \
0138 ABSL_LOG_INTERNAL_STRIP_STRING_LITERAL(val_text \
0139 " is OK")), \
0140 !ABSL_PREDICT_TRUE(absl_log_internal_check_ok_goo.first->ok());) \
0141 ABSL_LOG_INTERNAL_CONDITION_FATAL(STATELESS, true) \
0142 ABSL_LOG_INTERNAL_CHECK(*absl_log_internal_check_ok_goo.second) \
0143 .InternalStream()
0144 #define ABSL_LOG_INTERNAL_QCHECK_OK(val, val_text) \
0145 for (::std::pair<const ::absl::Status*, ::std::string*> \
0146 absl_log_internal_qcheck_ok_goo; \
0147 absl_log_internal_qcheck_ok_goo.first = \
0148 ::absl::log_internal::AsStatus(val), \
0149 absl_log_internal_qcheck_ok_goo.second = \
0150 ABSL_PREDICT_TRUE(absl_log_internal_qcheck_ok_goo.first->ok()) \
0151 ? nullptr \
0152 : ::absl::status_internal::MakeCheckFailString( \
0153 absl_log_internal_qcheck_ok_goo.first, \
0154 ABSL_LOG_INTERNAL_STRIP_STRING_LITERAL(val_text \
0155 " is OK")), \
0156 !ABSL_PREDICT_TRUE(absl_log_internal_qcheck_ok_goo.first->ok());) \
0157 ABSL_LOG_INTERNAL_CONDITION_QFATAL(STATELESS, true) \
0158 ABSL_LOG_INTERNAL_QCHECK(*absl_log_internal_qcheck_ok_goo.second) \
0159 .InternalStream()
0160
0161 namespace absl {
0162 ABSL_NAMESPACE_BEGIN
0163
0164 class Status;
0165 template <typename T>
0166 class StatusOr;
0167
0168 namespace status_internal {
0169 ABSL_ATTRIBUTE_PURE_FUNCTION std::string* MakeCheckFailString(
0170 const absl::Status* status, const char* prefix);
0171 }
0172
0173 namespace log_internal {
0174
0175
0176
0177
0178 inline const absl::Status* AsStatus(const absl::Status& s) { return &s; }
0179 template <typename T>
0180 const absl::Status* AsStatus(const absl::StatusOr<T>& s) {
0181 return &s.status();
0182 }
0183
0184
0185
0186 class CheckOpMessageBuilder final {
0187 public:
0188
0189 explicit CheckOpMessageBuilder(const char* exprtext);
0190 ~CheckOpMessageBuilder() = default;
0191
0192 std::ostream& ForVar1() { return stream_; }
0193
0194 std::ostream& ForVar2();
0195
0196 std::string* NewString();
0197
0198 private:
0199 std::ostringstream stream_;
0200 };
0201
0202
0203
0204 template <typename T>
0205 inline void MakeCheckOpValueString(std::ostream& os, const T& v) {
0206 os << log_internal::NullGuard<T>::Guard(v);
0207 }
0208
0209
0210 void MakeCheckOpValueString(std::ostream& os, char v);
0211 void MakeCheckOpValueString(std::ostream& os, signed char v);
0212 void MakeCheckOpValueString(std::ostream& os, unsigned char v);
0213 void MakeCheckOpValueString(std::ostream& os, const void* p);
0214
0215 namespace detect_specialization {
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243 int64_t operator<<(std::ostream&, short value);
0244 int64_t operator<<(std::ostream&, unsigned short value);
0245 int64_t operator<<(std::ostream&, int value);
0246 int64_t operator<<(std::ostream&, unsigned int value);
0247 int64_t operator<<(std::ostream&, long value);
0248 uint64_t operator<<(std::ostream&, unsigned long value);
0249 int64_t operator<<(std::ostream&, long long value);
0250 uint64_t operator<<(std::ostream&, unsigned long long value);
0251 float operator<<(std::ostream&, float value);
0252 double operator<<(std::ostream&, double value);
0253 long double operator<<(std::ostream&, long double value);
0254 bool operator<<(std::ostream&, bool value);
0255 const void* operator<<(std::ostream&, const void* value);
0256 const void* operator<<(std::ostream&, std::nullptr_t);
0257
0258
0259
0260
0261
0262
0263 template <typename Traits>
0264 char operator<<(std::basic_ostream<char, Traits>&, char);
0265 template <typename Traits>
0266 signed char operator<<(std::basic_ostream<char, Traits>&, signed char);
0267 template <typename Traits>
0268 unsigned char operator<<(std::basic_ostream<char, Traits>&, unsigned char);
0269 template <typename Traits>
0270 const char* operator<<(std::basic_ostream<char, Traits>&, const char*);
0271 template <typename Traits>
0272 const signed char* operator<<(std::basic_ostream<char, Traits>&,
0273 const signed char*);
0274 template <typename Traits>
0275 const unsigned char* operator<<(std::basic_ostream<char, Traits>&,
0276 const unsigned char*);
0277
0278
0279
0280
0281 template <typename T, typename = decltype(std::declval<std::ostream&>()
0282 << std::declval<const T&>())>
0283 const T& Detect(int);
0284
0285
0286
0287
0288
0289 template <typename T>
0290 decltype(detect_specialization::operator<<(std::declval<std::ostream&>(),
0291 std::declval<const T&>()))
0292 Detect(char);
0293
0294
0295 class StringifySink {
0296 public:
0297 explicit StringifySink(std::ostream& os ABSL_ATTRIBUTE_LIFETIME_BOUND);
0298
0299 void Append(absl::string_view text);
0300 void Append(size_t length, char ch);
0301 friend void AbslFormatFlush(StringifySink* sink, absl::string_view text);
0302
0303 private:
0304 std::ostream& os_;
0305 };
0306
0307
0308 template <typename T>
0309 class StringifyToStreamWrapper {
0310 public:
0311 explicit StringifyToStreamWrapper(const T& v ABSL_ATTRIBUTE_LIFETIME_BOUND)
0312 : v_(v) {}
0313
0314 friend std::ostream& operator<<(std::ostream& os,
0315 const StringifyToStreamWrapper& wrapper) {
0316 StringifySink sink(os);
0317 AbslStringify(sink, wrapper.v_);
0318 return os;
0319 }
0320
0321 private:
0322 const T& v_;
0323 };
0324
0325
0326
0327
0328 template <typename T>
0329 std::enable_if_t<HasAbslStringify<T>::value,
0330 StringifyToStreamWrapper<T>>
0331 Detect(...);
0332 }
0333
0334 template <typename T>
0335 using CheckOpStreamType = decltype(detect_specialization::Detect<T>(0));
0336
0337
0338 template <typename T1, typename T2>
0339 ABSL_ATTRIBUTE_RETURNS_NONNULL std::string* MakeCheckOpString(
0340 T1 v1, T2 v2, const char* exprtext) ABSL_ATTRIBUTE_NOINLINE;
0341
0342 template <typename T1, typename T2>
0343 std::string* MakeCheckOpString(T1 v1, T2 v2, const char* exprtext) {
0344 CheckOpMessageBuilder comb(exprtext);
0345 MakeCheckOpValueString(comb.ForVar1(), v1);
0346 MakeCheckOpValueString(comb.ForVar2(), v2);
0347 return comb.NewString();
0348 }
0349
0350
0351
0352 #define ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING_EXTERN(x) \
0353 extern template std::string* MakeCheckOpString(x, x, const char*)
0354 ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING_EXTERN(bool);
0355 ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING_EXTERN(int64_t);
0356 ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING_EXTERN(uint64_t);
0357 ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING_EXTERN(float);
0358 ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING_EXTERN(double);
0359 ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING_EXTERN(char);
0360 ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING_EXTERN(unsigned char);
0361 ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING_EXTERN(const std::string&);
0362 ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING_EXTERN(const absl::string_view&);
0363 ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING_EXTERN(const char*);
0364 ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING_EXTERN(const signed char*);
0365 ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING_EXTERN(const unsigned char*);
0366 ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING_EXTERN(const void*);
0367 #undef ABSL_LOG_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING_EXTERN
0368
0369
0370
0371
0372 #ifdef ABSL_MIN_LOG_LEVEL
0373 #define ABSL_LOG_INTERNAL_CHECK_OP_IMPL_RESULT(U1, U2, v1, v2, exprtext) \
0374 ((::absl::LogSeverity::kFatal >= \
0375 static_cast<::absl::LogSeverity>(ABSL_MIN_LOG_LEVEL)) \
0376 ? MakeCheckOpString<U1, U2>(v1, v2, exprtext) \
0377 : new std::string())
0378 #else
0379 #define ABSL_LOG_INTERNAL_CHECK_OP_IMPL_RESULT(U1, U2, v1, v2, exprtext) \
0380 MakeCheckOpString<U1, U2>(v1, v2, exprtext)
0381 #endif
0382
0383
0384
0385
0386
0387 #define ABSL_LOG_INTERNAL_CHECK_OP_IMPL(name, op) \
0388 template <typename T1, typename T2> \
0389 inline constexpr ::std::string* name##Impl(const T1& v1, const T2& v2, \
0390 const char* exprtext) { \
0391 using U1 = CheckOpStreamType<T1>; \
0392 using U2 = CheckOpStreamType<T2>; \
0393 return ABSL_PREDICT_TRUE(v1 op v2) \
0394 ? nullptr \
0395 : ABSL_LOG_INTERNAL_CHECK_OP_IMPL_RESULT(U1, U2, U1(v1), \
0396 U2(v2), exprtext); \
0397 } \
0398 inline constexpr ::std::string* name##Impl(int v1, int v2, \
0399 const char* exprtext) { \
0400 return name##Impl<int, int>(v1, v2, exprtext); \
0401 }
0402
0403 ABSL_LOG_INTERNAL_CHECK_OP_IMPL(Check_EQ, ==)
0404 ABSL_LOG_INTERNAL_CHECK_OP_IMPL(Check_NE, !=)
0405 ABSL_LOG_INTERNAL_CHECK_OP_IMPL(Check_LE, <=)
0406 ABSL_LOG_INTERNAL_CHECK_OP_IMPL(Check_LT, <)
0407 ABSL_LOG_INTERNAL_CHECK_OP_IMPL(Check_GE, >=)
0408 ABSL_LOG_INTERNAL_CHECK_OP_IMPL(Check_GT, >)
0409 #undef ABSL_LOG_INTERNAL_CHECK_OP_IMPL_RESULT
0410 #undef ABSL_LOG_INTERNAL_CHECK_OP_IMPL
0411
0412 std::string* CheckstrcmptrueImpl(const char* s1, const char* s2,
0413 const char* exprtext);
0414 std::string* CheckstrcmpfalseImpl(const char* s1, const char* s2,
0415 const char* exprtext);
0416 std::string* CheckstrcasecmptrueImpl(const char* s1, const char* s2,
0417 const char* exprtext);
0418 std::string* CheckstrcasecmpfalseImpl(const char* s1, const char* s2,
0419 const char* exprtext);
0420
0421
0422
0423
0424
0425
0426
0427 template <typename T>
0428 inline constexpr const T& GetReferenceableValue(const T& t) {
0429 return t;
0430 }
0431 inline constexpr char GetReferenceableValue(char t) { return t; }
0432 inline constexpr unsigned char GetReferenceableValue(unsigned char t) {
0433 return t;
0434 }
0435 inline constexpr signed char GetReferenceableValue(signed char t) { return t; }
0436 inline constexpr short GetReferenceableValue(short t) { return t; }
0437 inline constexpr unsigned short GetReferenceableValue(
0438 unsigned short t) {
0439 return t;
0440 }
0441 inline constexpr int GetReferenceableValue(int t) { return t; }
0442 inline constexpr unsigned int GetReferenceableValue(unsigned int t) {
0443 return t;
0444 }
0445 inline constexpr long GetReferenceableValue(long t) { return t; }
0446 inline constexpr unsigned long GetReferenceableValue(
0447 unsigned long t) {
0448 return t;
0449 }
0450 inline constexpr long long GetReferenceableValue(long long t) {
0451 return t;
0452 }
0453 inline constexpr unsigned long long GetReferenceableValue(
0454 unsigned long long t) {
0455 return t;
0456 }
0457
0458 }
0459 ABSL_NAMESPACE_END
0460 }
0461
0462 #endif