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 #include "absl/log/internal/check_op.h"
16
17 #include <string.h>
18
19 #include <ostream>
20
21 #include "absl/strings/string_view.h"
22
23 #ifdef _MSC_VER
24 #define strcasecmp _stricmp
25 #else
26 #include <strings.h> // for strcasecmp, but msvc does not have this header
27 #endif
28
29 #include <sstream>
30 #include <string>
31
32 #include "absl/base/config.h"
33 #include "absl/strings/str_cat.h"
34
35 namespace absl {
36 ABSL_NAMESPACE_BEGIN
37 namespace log_internal {
38
39 #define ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(x) \
40 template std::string* MakeCheckOpString(x, x, const char*)
41 ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(bool);
42 ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(int64_t);
43 ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(uint64_t);
44 ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(float);
45 ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(double);
46 ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(char);
47 ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(unsigned char);
48 ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(const std::string&);
49 ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(const absl::string_view&);
50 ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(const char*);
51 ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(const signed char*);
52 ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(const unsigned char*);
53 ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING(const void*);
54 #undef ABSL_LOGGING_INTERNAL_DEFINE_MAKE_CHECK_OP_STRING
55
CheckOpMessageBuilder(const char * exprtext)56 CheckOpMessageBuilder::CheckOpMessageBuilder(const char* exprtext) {
57 stream_ << exprtext << " (";
58 }
59
ForVar2()60 std::ostream& CheckOpMessageBuilder::ForVar2() {
61 stream_ << " vs. ";
62 return stream_;
63 }
64
NewString()65 std::string* CheckOpMessageBuilder::NewString() {
66 stream_ << ")";
67 return new std::string(stream_.str());
68 }
69
MakeCheckOpValueString(std::ostream & os,const char v)70 void MakeCheckOpValueString(std::ostream& os, const char v) {
71 if (v >= 32 && v <= 126) {
72 os << "'" << v << "'";
73 } else {
74 os << "char value " << int{v};
75 }
76 }
77
MakeCheckOpValueString(std::ostream & os,const signed char v)78 void MakeCheckOpValueString(std::ostream& os, const signed char v) {
79 if (v >= 32 && v <= 126) {
80 os << "'" << v << "'";
81 } else {
82 os << "signed char value " << int{v};
83 }
84 }
85
MakeCheckOpValueString(std::ostream & os,const unsigned char v)86 void MakeCheckOpValueString(std::ostream& os, const unsigned char v) {
87 if (v >= 32 && v <= 126) {
88 os << "'" << v << "'";
89 } else {
90 os << "unsigned char value " << int{v};
91 }
92 }
93
MakeCheckOpValueString(std::ostream & os,const void * p)94 void MakeCheckOpValueString(std::ostream& os, const void* p) {
95 if (p == nullptr) {
96 os << "(null)";
97 } else {
98 os << p;
99 }
100 }
101
102 // Helper functions for string comparisons.
103 #define DEFINE_CHECK_STROP_IMPL(name, func, expected) \
104 std::string* Check##func##expected##Impl(const char* s1, const char* s2, \
105 const char* exprtext) { \
106 bool equal = s1 == s2 || (s1 && s2 && !func(s1, s2)); \
107 if (equal == expected) { \
108 return nullptr; \
109 } else { \
110 return new std::string( \
111 absl::StrCat(exprtext, " (", s1, " vs. ", s2, ")")); \
112 } \
113 }
114 DEFINE_CHECK_STROP_IMPL(CHECK_STREQ, strcmp, true)
115 DEFINE_CHECK_STROP_IMPL(CHECK_STRNE, strcmp, false)
116 DEFINE_CHECK_STROP_IMPL(CHECK_STRCASEEQ, strcasecmp, true)
117 DEFINE_CHECK_STROP_IMPL(CHECK_STRCASENE, strcasecmp, false)
118 #undef DEFINE_CHECK_STROP_IMPL
119
120 namespace detect_specialization {
121
StringifySink(std::ostream & os)122 StringifySink::StringifySink(std::ostream& os) : os_(os) {}
123
Append(absl::string_view text)124 void StringifySink::Append(absl::string_view text) { os_ << text; }
125
Append(size_t length,char ch)126 void StringifySink::Append(size_t length, char ch) {
127 for (size_t i = 0; i < length; ++i) os_.put(ch);
128 }
129
AbslFormatFlush(StringifySink * sink,absl::string_view text)130 void AbslFormatFlush(StringifySink* sink, absl::string_view text) {
131 sink->Append(text);
132 }
133
134 } // namespace detect_specialization
135
136 } // namespace log_internal
137 ABSL_NAMESPACE_END
138 } // namespace absl
139