Coverage Report

Created: 2026-10-17 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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 <exception>
16
#include <iostream>
17
#include <sstream>
18
#include <stdexcept>
19
20
#include <cstring>
21
22
#include <pistache/winornix.h>
23
24
#include PST_STRERROR_R_HDR
25
26
#include PST_NETDB_HDR
27
#include PST_SOCKET_HDR
28
29
#include <sys/types.h>
30
31
#include <pistache/pist_check.h>
32
#include <pistache/pist_syslog.h>
33
34
#define TRY(...)                                               \
35
0
    do                                                         \
36
0
    {                                                          \
37
0
        PST_SOCK_STARTUP_CHECK;                                \
38
0
        auto ret = __VA_ARGS__;                                \
39
0
        if (ret < 0)                                           \
40
0
        {                                                      \
41
0
            const char* str = #__VA_ARGS__;                    \
42
0
            std::ostringstream oss;                            \
43
0
            oss << str << ": ";                                \
44
0
            if (errno == 0)                                    \
45
0
            {                                                  \
46
0
                oss << gai_strerror(static_cast<int>(ret));    \
47
0
            }                                                  \
48
0
            else                                               \
49
0
            {                                                  \
50
0
                PST_DECL_SE_ERR_P_EXTRA;                           \
51
0
                oss << PST_STRERROR_R_ERRNO; \
52
0
            }                                                  \
53
0
            PS_LOG_INFO_ARGS("TRY ret %d  errno %d  throw %s", \
54
0
                             ret, errno, oss.str().c_str());   \
55
0
            PS_LOGDBG_STACK_TRACE;                             \
56
0
            oss << " (" << __FILE__ << ":" << __LINE__ << ")"; \
57
0
            throw std::runtime_error(oss.str());               \
58
0
        }                                                      \
59
0
    } while (0)
60
61
#define TRY_RET(...)                                           \
62
0
    [&]() {                                                    \
63
0
        PST_SOCK_STARTUP_CHECK;                                \
64
0
        auto ret = __VA_ARGS__;                                \
65
0
        if (ret < 0)                                           \
66
0
        {                                                      \
67
0
            const char* str = #__VA_ARGS__;                    \
68
0
            std::ostringstream oss;                            \
69
0
            PST_DECL_SE_ERR_P_EXTRA;                               \
70
0
            oss << str << ": " << PST_STRERROR_R_ERRNO; \
71
0
            PS_LOG_INFO_ARGS("TRY ret %d  errno %d  throw %s", \
72
0
                             ret, errno, oss.str().c_str());   \
73
0
            PS_LOGDBG_STACK_TRACE;                             \
74
0
            oss << " (" << __FILE__ << ":" << __LINE__ << ")"; \
75
0
            throw std::runtime_error(oss.str());               \
76
0
        }                                                      \
77
0
        return ret;                                            \
78
0
    }();                                                       \
79
0
    (void)0
80
81
#define TRY_NULL_RET(...)                                      \
82
    [&]() {                                                    \
83
        PST_SOCK_STARTUP_CHECK;                                \
84
        auto ret = __VA_ARGS__;                                \
85
        if (ret == nullptr)                                       \
86
        {                                                      \
87
            const char* str = #__VA_ARGS__;                    \
88
            std::ostringstream oss;                            \
89
            PST_DECL_SE_ERR_P_EXTRA;                               \
90
            oss << str << ": " << PST_STRERROR_R_ERRNO; \
91
            PS_LOG_INFO_ARGS("TRY_NULL_RET throw errno %d %s", \
92
                             errno, oss.str().c_str());        \
93
            PS_LOGDBG_STACK_TRACE;                             \
94
            oss << " (" << __FILE__ << ":" << __LINE__ << ")"; \
95
            throw std::runtime_error(oss.str());               \
96
        }                                                      \
97
        return ret;                                            \
98
    }();                                                       \
99
    (void)0
100
101
struct PrintException
102
{
103
    void operator()(std::exception_ptr exc) const
104
0
    {
105
0
        try
106
0
        {
107
0
            std::rethrow_exception(exc);
108
0
        }
109
0
        catch (const std::exception& e)
110
0
        {
111
0
            std::cerr << "An exception occured: " << e.what() << std::endl;
112
0
        }
113
0
    }
114
};