Kudu C++ client API
stubs.h
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 #ifndef KUDU_CLIENT_STUBS_H
18 #define KUDU_CLIENT_STUBS_H
19 
20 #include <stdlib.h> // for exit()
21 
22 #include <iostream>
23 
24 //
25 // GCC can be told that a certain branch is not likely to be taken (for
26 // instance, a CHECK failure), and use that information in static analysis.
27 // Giving it this information can help it optimize for the common case in
28 // the absence of better information (ie. -fprofile-arcs).
29 //
30 #ifndef PREDICT_FALSE
31 #if defined(__GNUC__)
32 #define PREDICT_FALSE(x) (__builtin_expect(x, 0))
33 #else
34 #define PREDICT_FALSE(x) x
35 #endif
36 #endif
37 #ifndef PREDICT_TRUE
38 #if defined(__GNUC__)
39 #define PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
40 #else
41 #define PREDICT_TRUE(x) x
42 #endif
43 #endif
44 
45 // Annotate a function indicating the caller must examine the return value.
46 // Use like:
47 // int foo() WARN_UNUSED_RESULT;
48 // To explicitly ignore a result, see |ignore_result()| in <base/basictypes.h>.
49 #ifndef WARN_UNUSED_RESULT
50 #if defined(__GNUC__)
51 #define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
52 #else
53 #define WARN_UNUSED_RESULT
54 #endif
55 #endif
56 
57 #if (defined(__GNUC__) || defined(__APPLE__)) && !defined(SWIG)
58 #undef ATTRIBUTE_UNUSED
59 #define ATTRIBUTE_UNUSED __attribute__ ((unused))
60 #else
61 #ifndef ATTRIBUTE_UNUSED
62 #define ATTRIBUTE_UNUSED
63 #endif
64 #endif
65 
66 // For deprecated functions or variables, generate a warning at usage sites.
67 // Verified to work as early as GCC 3.1.1 and clang 3.2 (so we'll assume any
68 // clang is new enough).
69 #ifndef ATTRIBUTE_DEPRECATED
70 #if defined(__clang__) || \
71  (defined(COMPILER_GCC) && \
72  (__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >= 30200)
73 #define ATTRIBUTE_DEPRECATED(msg) __attribute__ ((deprecated (msg) ))
74 #else
75 #define ATTRIBUTE_DEPRECATED(msg)
76 #endif
77 #endif // #ifndef ATTRIBUTE_DEPRECATED
78 
79 #ifndef COMPILE_ASSERT
80 // The COMPILE_ASSERT macro can be used to verify that a compile time
81 // expression is true. For example, you could use it to verify the
82 // size of a static array:
83 //
84 // COMPILE_ASSERT(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES,
85 // content_type_names_incorrect_size);
86 //
87 // or to make sure a struct is smaller than a certain size:
88 //
89 // COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
90 //
91 // The second argument to the macro is the name of the variable. If
92 // the expression is false, most compilers will issue a warning/error
93 // containing the name of the variable.
94 
96 template <bool>
97 struct StubsCompileAssert {
98 };
100 
101 #define COMPILE_ASSERT(expr, msg) \
102  typedef StubsCompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] ATTRIBUTE_UNUSED // NOLINT(*)
103 #endif
104 
105 // Annotate a virtual method indicating it must be overriding a virtual
106 // method in the parent class.
107 // Use like:
108 // virtual void foo() OVERRIDE;
109 #ifndef OVERRIDE
110 #if defined(COMPILER_MSVC)
111 #define OVERRIDE override
112 #elif defined(__clang__)
113 #define OVERRIDE override
114 #elif defined(COMPILER_GCC) && __cplusplus >= 201103 && \
115  (__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >= 40700
116 // GCC 4.7 supports explicit virtual overrides when C++11 support is enabled.
117 #define OVERRIDE override
118 #else
119 #define OVERRIDE
120 #endif
121 #endif
122 
123 #ifndef DISALLOW_COPY_AND_ASSIGN
124 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
125  TypeName(const TypeName&); \
126  void operator=(const TypeName&)
127 #endif
128 
129 #ifndef FRIEND_TEST
130 #define FRIEND_TEST(test_case_name, test_name) \
131  friend class test_case_name##_##test_name##_Test
132 #endif
133 
134 // Stubbed versions of macros defined in glog/logging.h, intended for
135 // environments where glog headers aren't available.
136 //
137 // Add more as needed.
138 
139 #define KUDU_DCHECK(condition) while (false) kudu::internal_logging::NullLog()
140 #define KUDU_DCHECK_EQ(val1, val2) while (false) kudu::internal_logging::NullLog()
141 #define KUDU_DCHECK_NE(val1, val2) while (false) kudu::internal_logging::NullLog()
142 #define KUDU_DCHECK_LE(val1, val2) while (false) kudu::internal_logging::NullLog()
143 #define KUDU_DCHECK_LT(val1, val2) while (false) kudu::internal_logging::NullLog()
144 #define KUDU_DCHECK_GE(val1, val2) while (false) kudu::internal_logging::NullLog()
145 #define KUDU_DCHECK_GT(val1, val2) while (false) kudu::internal_logging::NullLog()
146 #define KUDU_DCHECK_NOTNULL(val) (val)
147 #define KUDU_DCHECK_STREQ(str1, str2) while (false) kudu::internal_logging::NullLog()
148 #define KUDU_DCHECK_STRCASEEQ(str1, str2) while (false) kudu::internal_logging::NullLog()
149 #define KUDU_DCHECK_STRNE(str1, str2) while (false) kudu::internal_logging::NullLog()
150 #define KUDU_DCHECK_STRCASENE(str1, str2) while (false) kudu::internal_logging::NullLog()
151 
152 // Log levels. LOG ignores them, so their values are abitrary.
153 
154 #define KUDU_INFO 0
155 #define KUDU_WARNING 1
156 #define KUDU_ERROR 2
157 #define KUDU_FATAL 3
158 
159 #ifdef NDEBUG
160 #define KUDU_DFATAL KUDU_WARNING
161 #else
162 #define KUDU_DFATAL KUDU_FATAL
163 #endif // NDEBUG
164 
165 #define KUDU_LOG_INTERNAL(level) kudu::internal_logging::CerrLog(level)
166 #define KUDU_LOG(level) KUDU_LOG_INTERNAL(KUDU_##level)
167 
168 #define KUDU_CHECK(condition) \
169  (condition) ? 0 : KUDU_LOG(FATAL) << "Check failed: " #condition " "
170 
171 namespace kudu {
172 
173 namespace internal_logging {
174 
179 class NullLog {
180  public:
186  template<class T>
187  NullLog& operator<<(const T& t) {
188  return *this;
189  }
190 };
191 
193 class CerrLog {
194  public:
199  CerrLog(int severity) // NOLINT(runtime/explicit)
200  : severity_(severity),
201  has_logged_(false) {
202  }
203 
204  ~CerrLog() {
205  if (has_logged_) {
206  std::cerr << std::endl;
207  }
208  if (severity_ == KUDU_FATAL) {
209  exit(1);
210  }
211  }
212 
218  template<class T>
219  CerrLog& operator<<(const T& t) {
220  has_logged_ = true;
221  std::cerr << t;
222  return *this;
223  }
224 
225  private:
226  const int severity_;
227  bool has_logged_;
228 };
229 
230 } // namespace internal_logging
231 } // namespace kudu
232 
233 #endif
A helper for the nil log sink.
Definition: stubs.h:179
Definition: callbacks.h:28
CerrLog(int severity)
Definition: stubs.h:199
A helper for stderr log sink.
Definition: stubs.h:193
CerrLog & operator<<(const T &t)
Definition: stubs.h:219
NullLog & operator<<(const T &t)
Definition: stubs.h:187