/src/pistache/include/pistache/common.h
Line | Count | Source |
1 | | /* |
2 | | * SPDX-FileCopyrightText: 2015 Mathieu Stefani |
3 | | * |
4 | | * SPDX-License-Identifier: Apache-2.0 |
5 | | */ |
6 | | |
7 | | /* common.h |
8 | | Mathieu Stefani, 12 August 2015 |
9 | | |
10 | | A collection of macro / utilities / constants |
11 | | */ |
12 | | |
13 | | #pragma once |
14 | | |
15 | | #include <iostream> |
16 | | #include <sstream> |
17 | | #include <stdexcept> |
18 | | |
19 | | #include <cstring> |
20 | | |
21 | | #include <pistache/winornix.h> |
22 | | |
23 | | #include PST_STRERROR_R_HDR |
24 | | |
25 | | #include PST_NETDB_HDR |
26 | | #include PST_SOCKET_HDR |
27 | | |
28 | | #include <sys/types.h> |
29 | | |
30 | | #include <pistache/pist_check.h> |
31 | | #include <pistache/pist_syslog.h> |
32 | | |
33 | | #define TRY(...) \ |
34 | 0 | do \ |
35 | 0 | { \ |
36 | 0 | PST_SOCK_STARTUP_CHECK; \ |
37 | 0 | auto ret = __VA_ARGS__; \ |
38 | 0 | if (ret < 0) \ |
39 | 0 | { \ |
40 | 0 | const char* str = #__VA_ARGS__; \ |
41 | 0 | std::ostringstream oss; \ |
42 | 0 | oss << str << ": "; \ |
43 | 0 | if (errno == 0) \ |
44 | 0 | { \ |
45 | 0 | oss << gai_strerror(static_cast<int>(ret)); \ |
46 | 0 | } \ |
47 | 0 | else \ |
48 | 0 | { \ |
49 | 0 | PST_DECL_SE_ERR_P_EXTRA; \ |
50 | 0 | oss << PST_STRERROR_R_ERRNO; \ |
51 | 0 | } \ |
52 | 0 | PS_LOG_INFO_ARGS("TRY ret %d errno %d throw %s", \ |
53 | 0 | ret, errno, oss.str().c_str()); \ |
54 | 0 | PS_LOGDBG_STACK_TRACE; \ |
55 | 0 | oss << " (" << __FILE__ << ":" << __LINE__ << ")"; \ |
56 | 0 | throw std::runtime_error(oss.str()); \ |
57 | 0 | } \ |
58 | 0 | } while (0) |
59 | | |
60 | | #define TRY_RET(...) \ |
61 | 0 | [&]() { \ |
62 | 0 | PST_SOCK_STARTUP_CHECK; \ |
63 | 0 | auto ret = __VA_ARGS__; \ |
64 | 0 | if (ret < 0) \ |
65 | 0 | { \ |
66 | 0 | const char* str = #__VA_ARGS__; \ |
67 | 0 | std::ostringstream oss; \ |
68 | 0 | PST_DECL_SE_ERR_P_EXTRA; \ |
69 | 0 | oss << str << ": " << PST_STRERROR_R_ERRNO; \ |
70 | 0 | PS_LOG_INFO_ARGS("TRY ret %d errno %d throw %s", \ |
71 | 0 | ret, errno, oss.str().c_str()); \ |
72 | 0 | PS_LOGDBG_STACK_TRACE; \ |
73 | 0 | oss << " (" << __FILE__ << ":" << __LINE__ << ")"; \ |
74 | 0 | throw std::runtime_error(oss.str()); \ |
75 | 0 | } \ |
76 | 0 | return ret; \ |
77 | 0 | }(); \ |
78 | 0 | (void)0 |
79 | | |
80 | | #define TRY_NULL_RET(...) \ |
81 | | [&]() { \ |
82 | | PST_SOCK_STARTUP_CHECK; \ |
83 | | auto ret = __VA_ARGS__; \ |
84 | | if (ret == nullptr) \ |
85 | | { \ |
86 | | const char* str = #__VA_ARGS__; \ |
87 | | std::ostringstream oss; \ |
88 | | PST_DECL_SE_ERR_P_EXTRA; \ |
89 | | oss << str << ": " << PST_STRERROR_R_ERRNO; \ |
90 | | PS_LOG_INFO_ARGS("TRY_NULL_RET throw errno %d %s", \ |
91 | | errno, oss.str().c_str()); \ |
92 | | PS_LOGDBG_STACK_TRACE; \ |
93 | | oss << " (" << __FILE__ << ":" << __LINE__ << ")"; \ |
94 | | throw std::runtime_error(oss.str()); \ |
95 | | } \ |
96 | | return ret; \ |
97 | | }(); \ |
98 | | (void)0 |
99 | | |
100 | | struct PrintException |
101 | | { |
102 | | void operator()(std::exception_ptr exc) const |
103 | 0 | { |
104 | 0 | try |
105 | 0 | { |
106 | 0 | std::rethrow_exception(exc); |
107 | 0 | } |
108 | 0 | catch (const std::exception& e) |
109 | 0 | { |
110 | 0 | std::cerr << "An exception occured: " << e.what() << std::endl; |
111 | 0 | } |
112 | 0 | } |
113 | | }; |