81a8ccdd83d6c09df6aa3eb247ac816ca0e1bc0e
[folly.git] / folly / SafeAssert.h
1 /*
2  * Copyright 2014 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef FOLLY_SAFEASSERT_H_
18 #define FOLLY_SAFEASSERT_H_
19
20 #include <folly/Portability.h>
21 #include <folly/Preprocessor.h>
22
23 /**
24  * Verify that the expression is true. If not, prints an error message
25  * (containing msg) to stderr and abort()s. Just like CHECK(), but only
26  * logs to stderr and only does async-signal-safe calls.
27  */
28 #define FOLLY_SAFE_CHECK(expr, msg) \
29   ((expr) ? static_cast<void>(0) : \
30    ::folly::detail::assertionFailure( \
31        FB_STRINGIZE(expr), (msg), __FILE__, __LINE__, __PRETTY_FUNCTION__))
32
33 /**
34  * In debug mode, verify that the expression is true. Otherwise, do nothing
35  * (do not even evaluate expr). Just like assert() or DCHECK(), but only
36  * logs to stderr and only does async-signal-safe calls.
37  */
38 #ifdef NDEBUG
39 #define FOLLY_SAFE_DCHECK(expr, msg) (static_cast<void>(0))
40 #else
41 #define FOLLY_SAFE_DCHECK FOLLY_SAFE_CHECK
42 #endif
43
44 namespace folly { namespace detail {
45
46 FOLLY_NORETURN void assertionFailure(const char* expr, const char* msg,
47                       const char* file, unsigned int line,
48                       const char* function);
49
50 }}  // namespace folly
51
52 #endif /* FOLLY_SAFEASSERT_H_ */
53