err.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. Copyright (c) 2012 Martin Sustrik All rights reserved.
  3. Copyright 2016 Garrett D'Amore <garrett@damore.org>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"),
  6. to deal in the Software without restriction, including without limitation
  7. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. and/or sell copies of the Software, and to permit persons to whom
  9. the Software is furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included
  11. in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  17. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  18. IN THE SOFTWARE.
  19. */
  20. #ifndef NN_ERR_INCLUDED
  21. #define NN_ERR_INCLUDED
  22. #include <errno.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. /* Include nn.h header to define nanomsg-specific error codes. */
  26. #include "../nn.h"
  27. #include "fast.h"
  28. #if defined _MSC_VER
  29. #define NN_NORETURN __declspec(noreturn)
  30. #elif defined __GNUC__
  31. #define NN_NORETURN __attribute__ ((noreturn))
  32. #else
  33. #define NN_NORETURN
  34. #endif
  35. /* Same as system assert(). However, under Win32 assert has some deficiencies.
  36. Thus this macro. */
  37. #define nn_assert(x) \
  38. do {\
  39. if (nn_slow (!(x))) {\
  40. nn_backtrace_print (); \
  41. fprintf (stderr, "Assertion failed: %s (%s:%d)\n", #x, \
  42. __FILE__, __LINE__);\
  43. fflush (stderr);\
  44. nn_err_abort ();\
  45. }\
  46. } while (0)
  47. #define nn_assert_state(obj, state_name) \
  48. do {\
  49. if (nn_slow ((obj)->state != state_name)) {\
  50. nn_backtrace_print (); \
  51. fprintf (stderr, \
  52. "Assertion failed: %d == %s (%s:%d)\n", \
  53. (obj)->state, #state_name, \
  54. __FILE__, __LINE__);\
  55. fflush (stderr);\
  56. nn_err_abort ();\
  57. }\
  58. } while (0)
  59. /* Checks whether memory allocation was successful. */
  60. #define alloc_assert(x) \
  61. do {\
  62. if (nn_slow (!x)) {\
  63. nn_backtrace_print (); \
  64. fprintf (stderr, "Out of memory (%s:%d)\n",\
  65. __FILE__, __LINE__);\
  66. fflush (stderr);\
  67. nn_err_abort ();\
  68. }\
  69. } while (0)
  70. /* Check the condition. If false prints out the errno. */
  71. #define errno_assert(x) \
  72. do {\
  73. if (nn_slow (!(x))) {\
  74. nn_backtrace_print (); \
  75. fprintf (stderr, "%s [%d] (%s:%d)\n", nn_err_strerror (errno),\
  76. (int) errno, __FILE__, __LINE__);\
  77. fflush (stderr);\
  78. nn_err_abort ();\
  79. }\
  80. } while (0)
  81. /* Checks whether supplied errno number is an error. */
  82. #define errnum_assert(cond, err) \
  83. do {\
  84. if (nn_slow (!(cond))) {\
  85. nn_backtrace_print (); \
  86. fprintf (stderr, "%s [%d] (%s:%d)\n", nn_err_strerror (err),\
  87. (int) (err), __FILE__, __LINE__);\
  88. fflush (stderr);\
  89. nn_err_abort ();\
  90. }\
  91. } while (0)
  92. /* Checks the condition. If false prints out the GetLastError info. */
  93. #define win_assert(x) \
  94. do {\
  95. if (nn_slow (!(x))) {\
  96. char errstr [256];\
  97. DWORD errnum = WSAGetLastError ();\
  98. nn_backtrace_print (); \
  99. nn_win_error ((int) errnum, errstr, 256);\
  100. fprintf (stderr, "%s [%d] (%s:%d)\n",\
  101. errstr, (int) errnum, __FILE__, __LINE__);\
  102. fflush (stderr);\
  103. nn_err_abort ();\
  104. }\
  105. } while (0)
  106. /* Checks the condition. If false prints out the WSAGetLastError info. */
  107. #define wsa_assert(x) \
  108. do {\
  109. if (nn_slow (!(x))) {\
  110. char errstr [256];\
  111. DWORD errnum = WSAGetLastError ();\
  112. nn_backtrace_print (); \
  113. nn_win_error (errnum, errstr, 256);\
  114. fprintf (stderr, "%s [%d] (%s:%d)\n",\
  115. errstr, (int) errnum, __FILE__, __LINE__);\
  116. fflush (stderr);\
  117. nn_err_abort ();\
  118. }\
  119. } while (0)
  120. /* Assertion-like macros for easier fsm debugging. */
  121. #define nn_fsm_error(message, state, src, type) \
  122. do {\
  123. nn_backtrace_print(); \
  124. fprintf (stderr, "%s: state=%d source=%d action=%d (%s:%d)\n", \
  125. message, state, src, type, __FILE__, __LINE__);\
  126. fflush (stderr);\
  127. nn_err_abort ();\
  128. } while (0)
  129. #define nn_fsm_bad_action(state, src, type) nn_fsm_error(\
  130. "Unexpected action", state, src, type)
  131. #define nn_fsm_bad_state(state, src, type) nn_fsm_error(\
  132. "Unexpected state", state, src, type)
  133. #define nn_fsm_bad_source(state, src, type) nn_fsm_error(\
  134. "Unexpected source", state, src, type)
  135. /* Compile-time assert. */
  136. #define CT_ASSERT_HELPER2(prefix, line) prefix##line
  137. #define CT_ASSERT_HELPER1(prefix, line) CT_ASSERT_HELPER2(prefix, line)
  138. #if defined __COUNTER__
  139. #define CT_ASSERT(x) \
  140. typedef int CT_ASSERT_HELPER1(ct_assert_,__COUNTER__) [(x) ? 1 : -1]
  141. #else
  142. #define CT_ASSERT(x) \
  143. typedef int CT_ASSERT_HELPER1(ct_assert_,__LINE__) [(x) ? 1 : -1]
  144. #endif
  145. NN_NORETURN void nn_err_abort (void);
  146. int nn_err_errno (void);
  147. const char *nn_err_strerror (int errnum);
  148. void nn_backtrace_print (void);
  149. #ifdef NN_HAVE_WINDOWS
  150. int nn_err_wsa_to_posix (int wsaerr);
  151. void nn_win_error (int err, char *buf, size_t bufsize);
  152. #endif
  153. #endif