libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
err.hpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3 
4  This file is part of libzmq, the ZeroMQ core engine in C++.
5 
6  libzmq is free software; you can redistribute it and/or modify it under
7  the terms of the GNU Lesser General Public License (LGPL) as published
8  by the Free Software Foundation; either version 3 of the License, or
9  (at your option) any later version.
10 
11  As a special exception, the Contributors give you permission to link
12  this library with independent modules to produce an executable,
13  regardless of the license terms of these independent modules, and to
14  copy and distribute the resulting executable under terms of your choice,
15  provided that you also meet, for each linked independent module, the
16  terms and conditions of the license of that module. An independent
17  module is a module which is not derived from or based on this library.
18  If you modify this library, you must extend this exception to your
19  version of the library.
20 
21  libzmq is distributed in the hope that it will be useful, but WITHOUT
22  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24  License for more details.
25 
26  You should have received a copy of the GNU Lesser General Public License
27  along with this program. If not, see <http://www.gnu.org/licenses/>.
28 */
29 
30 #ifndef __ZMQ_ERR_HPP_INCLUDED__
31 #define __ZMQ_ERR_HPP_INCLUDED__
32 
33 #include <assert.h>
34 #if defined _WIN32_WCE
35 #include "..\builds\msvc\errno.hpp"
36 #else
37 #include <errno.h>
38 #endif
39 #include <string.h>
40 #include <stdlib.h>
41 #include <stdio.h>
42 
43 #include "platform.hpp"
44 
45 #ifdef ZMQ_HAVE_WINDOWS
46 #include "windows.hpp"
47 #else
48 #include <netdb.h>
49 #endif
50 
51 #include "likely.hpp"
52 
53 // 0MQ-specific error codes are defined in zmq.h
54 #include "../include/zmq.h"
55 
56 // EPROTO is not used by OpenBSD and maybe other platforms.
57 #ifndef EPROTO
58 #define EPROTO 0
59 #endif
60 
61 namespace zmq
62 {
63  const char *errno_to_string (int errno_);
64  void zmq_abort (const char *errmsg_);
65  void print_backtrace (void);
66 }
67 
68 #ifdef ZMQ_HAVE_WINDOWS
69 
70 namespace zmq
71 {
72  const char *wsa_error ();
73  const char *wsa_error_no (int no_);
74  void win_error (char *buffer_, size_t buffer_size_);
75  int wsa_error_to_errno (int errcode);
76 }
77 
78 // Provides convenient way to check WSA-style errors on Windows.
79 #define wsa_assert(x) \
80  do {\
81  if (unlikely (!(x))) {\
82  const char *errstr = zmq::wsa_error ();\
83  if (errstr != NULL) {\
84  fprintf (stderr, "Assertion failed: %s (%s:%d)\n", errstr, \
85  __FILE__, __LINE__);\
86  zmq::zmq_abort (errstr);\
87  }\
88  }\
89  } while (false)
90 
91 // Provides convenient way to assert on WSA-style errors on Windows.
92 #define wsa_assert_no(no) \
93  do {\
94  const char *errstr = zmq::wsa_error_no (no);\
95  if (errstr != NULL) {\
96  fprintf (stderr, "Assertion failed: %s (%s:%d)\n", errstr, \
97  __FILE__, __LINE__);\
98  zmq::zmq_abort (errstr);\
99  }\
100  } while (false)
101 
102 // Provides convenient way to check GetLastError-style errors on Windows.
103 #define win_assert(x) \
104  do {\
105  if (unlikely (!(x))) {\
106  char errstr [256];\
107  zmq::win_error (errstr, 256);\
108  fprintf (stderr, "Assertion failed: %s (%s:%d)\n", errstr, \
109  __FILE__, __LINE__);\
110  zmq::zmq_abort (errstr);\
111  }\
112  } while (false)
113 
114 #endif
115 
116 // This macro works in exactly the same way as the normal assert. It is used
117 // in its stead because standard assert on Win32 in broken - it prints nothing
118 // when used within the scope of JNI library.
119 #define zmq_assert(x) \
120  do {\
121  if (unlikely (!(x))) {\
122  fprintf (stderr, "Assertion failed: %s (%s:%d)\n", #x, \
123  __FILE__, __LINE__);\
124  zmq::zmq_abort (#x);\
125  }\
126  } while (false)
127 
128 // Provides convenient way to check for errno-style errors.
129 #define errno_assert(x) \
130  do {\
131  if (unlikely (!(x))) {\
132  const char *errstr = strerror (errno);\
133  fprintf (stderr, "%s (%s:%d)\n", errstr, __FILE__, __LINE__);\
134  zmq::zmq_abort (errstr);\
135  }\
136  } while (false)
137 
138 // Provides convenient way to check for POSIX errors.
139 #define posix_assert(x) \
140  do {\
141  if (unlikely (x)) {\
142  const char *errstr = strerror (x);\
143  fprintf (stderr, "%s (%s:%d)\n", errstr, __FILE__, __LINE__);\
144  zmq::zmq_abort (errstr);\
145  }\
146  } while (false)
147 
148 // Provides convenient way to check for errors from getaddrinfo.
149 #define gai_assert(x) \
150  do {\
151  if (unlikely (x)) {\
152  const char *errstr = gai_strerror (x);\
153  fprintf (stderr, "%s (%s:%d)\n", errstr, __FILE__, __LINE__);\
154  zmq::zmq_abort (errstr);\
155  }\
156  } while (false)
157 
158 // Provides convenient way to check whether memory allocation have succeeded.
159 #define alloc_assert(x) \
160  do {\
161  if (unlikely (!x)) {\
162  fprintf (stderr, "FATAL ERROR: OUT OF MEMORY (%s:%d)\n",\
163  __FILE__, __LINE__);\
164  zmq::zmq_abort ("FATAL ERROR: OUT OF MEMORY");\
165  }\
166  } while (false)
167 
168 #endif
169 
170 
void zmq_abort(const char *errmsg_)
Definition: err.cpp:74
void print_backtrace(void)
Definition: err.cpp:441
const char * errno_to_string(int errno_)
Definition: err.cpp:33
Definition: address.hpp:35