libzmq master
The Intelligent Transport Layer

err.hpp

Go to the documentation of this file.
00001 /*
00002     Copyright (c) 2009-2011 250bpm s.r.o.
00003     Copyright (c) 2007-2009 iMatix Corporation
00004     Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
00005 
00006     This file is part of 0MQ.
00007 
00008     0MQ is free software; you can redistribute it and/or modify it under
00009     the terms of the GNU Lesser General Public License as published by
00010     the Free Software Foundation; either version 3 of the License, or
00011     (at your option) any later version.
00012 
00013     0MQ is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU Lesser General Public License for more details.
00017 
00018     You should have received a copy of the GNU Lesser General Public License
00019     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00020 */
00021 
00022 #ifndef __ZMQ_ERR_HPP_INCLUDED__
00023 #define __ZMQ_ERR_HPP_INCLUDED__
00024 
00025 //  0MQ-specific error codes are defined in zmq.h
00026 #include "../include/zmq.h"
00027 
00028 #include <assert.h>
00029 #include <errno.h>
00030 #include <string.h>
00031 #include <stdlib.h>
00032 #include <stdio.h>
00033 
00034 #include "platform.hpp"
00035 #include "likely.hpp"
00036 
00037 #ifdef ZMQ_HAVE_WINDOWS
00038 #include "windows.hpp"
00039 #else
00040 #include <netdb.h>
00041 #endif
00042 
00043 namespace zmq
00044 {
00045     const char *errno_to_string (int errno_);
00046     void zmq_abort (const char *errmsg_);
00047 }
00048 
00049 #ifdef ZMQ_HAVE_WINDOWS
00050 
00051 namespace zmq
00052 {
00053     const char *wsa_error ();
00054     const char *wsa_error_no (int no_);
00055     void win_error (char *buffer_, size_t buffer_size_);
00056     void wsa_error_to_errno ();
00057 }
00058 
00059 //  Provides convenient way to check WSA-style errors on Windows.
00060 #define wsa_assert(x) \
00061     do {\
00062         if (unlikely (!(x))) {\
00063             const char *errstr = zmq::wsa_error ();\
00064             if (errstr != NULL) {\
00065                 fprintf (stderr, "Assertion failed: %s (%s:%d)\n", errstr, \
00066                     __FILE__, __LINE__);\
00067                 zmq::zmq_abort (errstr);\
00068             }\
00069         }\
00070     } while (false)
00071 
00072 //  Provides convenient way to assert on WSA-style errors on Windows.
00073 #define wsa_assert_no(no) \
00074     do {\
00075         const char *errstr = zmq::wsa_error_no (no);\
00076         if (errstr != NULL) {\
00077             fprintf (stderr, "Assertion failed: %s (%s:%d)\n", errstr, \
00078                 __FILE__, __LINE__);\
00079             zmq::zmq_abort (errstr);\
00080         }\
00081     } while (false)
00082 
00083 // Provides convenient way to check GetLastError-style errors on Windows.
00084 #define win_assert(x) \
00085     do {\
00086         if (unlikely (!(x))) {\
00087             char errstr [256];\
00088             zmq::win_error (errstr, 256);\
00089             fprintf (stderr, "Assertion failed: %s (%s:%d)\n", errstr, \
00090                 __FILE__, __LINE__);\
00091             zmq::zmq_abort (errstr);\
00092         }\
00093     } while (false)
00094 
00095 #endif
00096 
00097 //  This macro works in exactly the same way as the normal assert. It is used
00098 //  in its stead because standard assert on Win32 in broken - it prints nothing
00099 //  when used within the scope of JNI library.
00100 #define zmq_assert(x) \
00101     do {\
00102         if (unlikely (!(x))) {\
00103             fprintf (stderr, "Assertion failed: %s (%s:%d)\n", #x, \
00104                 __FILE__, __LINE__);\
00105             zmq::zmq_abort (#x);\
00106         }\
00107     } while (false) 
00108 
00109 //  Provides convenient way to check for errno-style errors.
00110 #define errno_assert(x) \
00111     do {\
00112         if (unlikely (!(x))) {\
00113             const char *errstr = strerror (errno);\
00114             fprintf (stderr, "%s (%s:%d)\n", errstr, __FILE__, __LINE__);\
00115             zmq::zmq_abort (errstr);\
00116         }\
00117     } while (false)
00118 
00119 //  Provides convenient way to check for POSIX errors.
00120 #define posix_assert(x) \
00121     do {\
00122         if (unlikely (x)) {\
00123             const char *errstr = strerror (x);\
00124             fprintf (stderr, "%s (%s:%d)\n", errstr, __FILE__, __LINE__);\
00125             zmq::zmq_abort (errstr);\
00126         }\
00127     } while (false)
00128 
00129 //  Provides convenient way to check for errors from getaddrinfo.
00130 #define gai_assert(x) \
00131     do {\
00132         if (unlikely (x)) {\
00133             const char *errstr = gai_strerror (x);\
00134             fprintf (stderr, "%s (%s:%d)\n", errstr, __FILE__, __LINE__);\
00135             zmq::zmq_abort (errstr);\
00136         }\
00137     } while (false)
00138 
00139 //  Provides convenient way to check whether memory allocation have succeeded.
00140 #define alloc_assert(x) \
00141     do {\
00142         if (unlikely (!x)) {\
00143             fprintf (stderr, "FATAL ERROR: OUT OF MEMORY (%s:%d)\n",\
00144                 __FILE__, __LINE__);\
00145             zmq::zmq_abort ("FATAL ERROR: OUT OF MEMORY");\
00146         }\
00147     } while (false)
00148 
00149 #endif
00150 
00151 
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines