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