062ebd9c8347c8a8b3dc3f4d82b37b478d3819c4
[m6w6/libmemcached] / src / libmemcached / assert.hpp
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #pragma once
17
18 #ifdef __cplusplus
19 # include <cassert>
20 #else
21 # include <assert.h>
22 #endif // __cplusplus
23
24 #ifdef NDEBUG
25 # define assert_msg(__expr, __mesg) \
26 (void) (__expr); \
27 (void) (__mesg);
28 # define assert_vmsg(__expr, __mesg, ...) \
29 (void) (__expr); \
30 (void) (__mesg);
31 #else
32
33 # ifdef _WIN32
34 # include <malloc.h>
35 # elif HAVE_ALLOCA_H
36 # include <alloca.h>
37 # endif
38
39 # ifdef __cplusplus
40 # include <cstdarg>
41 # include <cstdio>
42 # else
43 # include <stdarg.h>
44 # include <stdio.h>
45 # endif
46
47 # include "libmemcached/backtrace.hpp"
48
49 # define assert_msg(__expr, __mesg) \
50 do { \
51 if (not(__expr)) { \
52 fprintf(stderr, "\n%s:%d Assertion \"%s\" failed for function \"%s\" likely for %s\n", \
53 __FILE__, __LINE__, #__expr, __func__, (#__mesg)); \
54 custom_backtrace(); \
55 abort(); \
56 } \
57 } while (0)
58
59 # define assert_vmsg(__expr, __mesg, ...) \
60 do { \
61 if (not(__expr)) { \
62 size_t ask = snprintf(0, 0, (__mesg), __VA_ARGS__); \
63 ask++; \
64 char *_error_message = (char *) alloca(sizeof(char) * ask); \
65 size_t _error_message_size = snprintf(_error_message, ask, (__mesg), __VA_ARGS__); \
66 fprintf(stderr, "\n%s:%d Assertion '%s' failed for function '%s' [ %.*s ]\n", __FILE__, \
67 __LINE__, #__expr, __func__, int(_error_message_size), _error_message); \
68 custom_backtrace(); \
69 abort(); \
70 } \
71 } while (0)
72
73 #endif // NDEBUG