err.hpp 8.9 KB

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