WIP
[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 # endif
36
37 # ifdef __cplusplus
38 # include <cstdarg>
39 # include <cstdio>
40 # else
41 # include <stdarg.h>
42 # include <stdio.h>
43 # endif
44
45 # include "libmemcached/backtrace.hpp"
46
47 # define assert_msg(__expr, __mesg) \
48 do { \
49 if (not(__expr)) { \
50 fprintf(stderr, "\n%s:%d Assertion \"%s\" failed for function \"%s\" likely for %s\n", \
51 __FILE__, __LINE__, #__expr, __func__, (#__mesg)); \
52 custom_backtrace(); \
53 abort(); \
54 } \
55 } while (0)
56
57 # define assert_vmsg(__expr, __mesg, ...) \
58 do { \
59 if (not(__expr)) { \
60 size_t ask = snprintf(0, 0, (__mesg), __VA_ARGS__); \
61 ask++; \
62 char *_error_message = (char *) alloca(sizeof(char) * ask); \
63 size_t _error_message_size = snprintf(_error_message, ask, (__mesg), __VA_ARGS__); \
64 fprintf(stderr, "\n%s:%d Assertion '%s' failed for function '%s' [ %.*s ]\n", __FILE__, \
65 __LINE__, #__expr, __func__, int(_error_message_size), _error_message); \
66 custom_backtrace(); \
67 abort(); \
68 } \
69 } while (0)
70
71 #endif // NDEBUG