1 /*
2     Copyright (c) 2009-2012 250bpm s.r.o.
3     Copyright (c) 2007-2009 iMatix Corporation
4     Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
5 
6     This file is part of Crossroads I/O project.
7 
8     Crossroads I/O is free software; you can redistribute it and/or modify it
9     under the terms of the GNU Lesser General Public License as published by
10     the Free Software Foundation; either version 3 of the License, or
11     (at your option) any later version.
12 
13     Crossroads is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU Lesser General Public License for more details.
17 
18     You should have received a copy of the GNU Lesser General Public License
19     along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #ifndef __XS_ERR_HPP_INCLUDED__
23 #define __XS_ERR_HPP_INCLUDED__
24 
25 //  Crossroads-specific error codes are defined in xs.h
26 #include "../include/xs/xs.h"
27 
28 #include <assert.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 
34 #include "platform.hpp"
35 #include "likely.hpp"
36 
37 #ifdef XS_HAVE_WINDOWS
38 #include "windows.hpp"
39 #else
40 #include <netdb.h>
41 #endif
42 
43 namespace xs
44 {
45     const char *errno_to_string (int errno_);
46     void xs_abort (const char *errmsg_);
47 }
48 
49 #ifdef XS_HAVE_WINDOWS
50 
51 namespace xs
52 {
53     const char *wsa_error ();
54     const char *wsa_error_no (int no_);
55     void win_error (char *buffer_, size_t buffer_size_);
56     void wsa_error_to_errno ();
57 }
58 
59 //  Provides convenient way to check WSA-style errors on Windows.
60 #define wsa_assert(x) \
61     do {\
62         if (unlikely (!(x))) {\
63             const char *errstr = xs::wsa_error ();\
64             if (errstr != NULL) {\
65                 fprintf (stderr, "Assertion failed: %s (%s:%d)\n", errstr, \
66                     __FILE__, __LINE__);\
67                 xs::xs_abort (errstr);\
68             }\
69         }\
70     } while (false)
71 
72 //  Provides convenient way to assert on WSA-style errors on Windows.
73 #define wsa_assert_no(no) \
74     do {\
75         const char *errstr = xs::wsa_error_no (no);\
76         if (errstr != NULL) {\
77             fprintf (stderr, "Assertion failed: %s (%s:%d)\n", errstr, \
78                 __FILE__, __LINE__);\
79             xs::xs_abort (errstr);\
80         }\
81     } while (false)
82 
83 // Provides convenient way to check GetLastError-style errors on Windows.
84 #define win_assert(x) \
85     do {\
86         if (unlikely (!(x))) {\
87             char errstr [256];\
88             xs::win_error (errstr, 256);\
89             fprintf (stderr, "Assertion failed: %s (%s:%d)\n", errstr, \
90                 __FILE__, __LINE__);\
91             xs::xs_abort (errstr);\
92         }\
93     } while (false)
94 
95 #endif
96 
97 //  This macro works in exactly the same way as the normal assert. It is used
98 //  in its stead because standard assert on Win32 in broken - it prints nothing
99 //  when used within the scope of JNI library.
100 #define xs_assert(x) \
101     do {\
102         if (unlikely (!(x))) {\
103             fprintf (stderr, "Assertion failed: %s (%s:%d)\n", #x, \
104                 __FILE__, __LINE__);\
105             xs::xs_abort (#x);\
106         }\
107     } while (false)
108 
109 //  Provides convenient way to check for errno-style errors.
110 #define errno_assert(x) \
111     do {\
112         if (unlikely (!(x))) {\
113             const char *errstr = strerror (errno);\
114             fprintf (stderr, "%s (%s:%d)\n", errstr, __FILE__, __LINE__);\
115             xs::xs_abort (errstr);\
116         }\
117     } while (false)
118 
119 //  Provides convenient way to check for POSIX errors.
120 #define posix_assert(x) \
121     do {\
122         if (unlikely (x)) {\
123             const char *errstr = strerror (x);\
124             fprintf (stderr, "%s (%s:%d)\n", errstr, __FILE__, __LINE__);\
125             xs::xs_abort (errstr);\
126         }\
127     } while (false)
128 
129 //  Provides convenient way to check for errors from getaddrinfo.
130 #define gai_assert(x) \
131     do {\
132         if (unlikely (x)) {\
133             const char *errstr = gai_strerror (x);\
134             fprintf (stderr, "%s (%s:%d)\n", errstr, __FILE__, __LINE__);\
135             xs::xs_abort (errstr);\
136         }\
137     } while (false)
138 
139 //  Provides convenient way to check whether memory allocation have succeeded.
140 #define alloc_assert(x) \
141     do {\
142         if (unlikely (!x)) {\
143             fprintf (stderr, "FATAL ERROR: OUT OF MEMORY (%s:%d)\n",\
144                 __FILE__, __LINE__);\
145             xs::xs_abort ("FATAL ERROR: OUT OF MEMORY");\
146         }\
147     } while (false)
148 
149 #endif
150 
151 
152