Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ce91604eebfae8b4bc27bbfe1dbf4b436be93327
[simgrid.git] / include / xbt / ex.h
1 /* Copyright (c) 2005-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /*  Copyright (c) 2002-2004 Ralf S. Engelschall <rse@engelschall.com>       */
5 /*  Copyright (c) 2002-2004 The OSSP Project <http://www.ossp.org/>         */
6 /*  Copyright (c) 2002-2004 Cable & Wireless <http://www.cw.com/>           */
7 /*  All rights reserved.                                                    */
8
9 /* This code is inspirated from the OSSP version (as retrieved back in 2004)*/
10 /* It was heavily modified to fit the SimGrid framework.                    */
11
12 /* The OSSP version has the following copyright notice:
13 **  OSSP ex - Exception Handling
14 **  Copyright (c) 2002-2004 Ralf S. Engelschall <rse@engelschall.com>
15 **  Copyright (c) 2002-2004 The OSSP Project <http://www.ossp.org/>
16 **  Copyright (c) 2002-2004 Cable & Wireless <http://www.cw.com/>
17 **
18 **  This file is part of OSSP ex, an exception handling library
19 **  which can be found at http://www.ossp.org/pkg/lib/ex/.
20 **
21 **  Permission to use, copy, modify, and distribute this software for
22 **  any purpose with or without fee is hereby granted, provided that
23 **  the above copyright notice and this permission notice appear in all
24 **  copies.
25 **
26 **  THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESSED OR IMPLIED
27 **  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28 **  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 **  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
30 **  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 **  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 **  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
33 **  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
34 **  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35 **  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
36 **  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 **  SUCH DAMAGE.
38  */
39
40 /* The extensions made for the SimGrid project can either be distributed    */
41 /* under the same license, or under the LGPL v2.1                           */
42
43 #ifndef __XBT_EX_H__
44 #define __XBT_EX_H__
45
46 #include <stdlib.h>
47
48 #ifdef __cplusplus
49 #include <stdexcept>
50 #include <xbt/exception.hpp>
51 #endif
52
53 #include "xbt/base.h"
54 #include "xbt/sysdep.h"
55 #include "xbt/misc.h"
56 #include "xbt/virtu.h"
57
58 /*-*-* Emergency debuging: define this when the exceptions get crazy *-*-*/
59 #undef __EX_MAYDAY
60 #ifdef __EX_MAYDAY
61 # include <stdio.h>
62 #include <errno.h>
63 #  define MAYDAY_SAVE(m)    printf("%d %s:%d save %p\n",                \
64                                    xbt_getpid(), __FILE__, __LINE__,    \
65                                    (m)->jb                              \
66                                   ),
67 #  define MAYDAY_RESTORE(m) printf("%d %s:%d restore %p\n",             \
68                                    xbt_getpid(), __FILE__, __LINE__,    \
69                                    (m)->jb                              \
70                                   ),
71 #  define MAYDAY_CATCH(e)   printf("%d %s:%d Catched '%s'\n",           \
72                                    xbt_getpid(), __FILE__, __LINE__,    \
73                                    (e).msg                              \
74           ),
75 #else
76 #  define MAYDAY_SAVE(m)
77 #  define MAYDAY_RESTORE(m)
78 #  define MAYDAY_CATCH(e)
79 #endif
80 /*-*-* end of debugging stuff *-*-*/
81
82 /** @brief different kind of errors */
83 typedef enum {
84   unknown_error = 0,            /**< unknown error */
85   arg_error,                    /**< Invalid argument */
86   bound_error,                  /**< Out of bounds argument */
87   mismatch_error,               /**< The provided ID does not match */
88   not_found_error,              /**< The searched element was not found */
89   system_error,                 /**< a syscall did fail */
90   network_error,                /**< error while sending/receiving data */
91   timeout_error,                /**< not quick enough, dude */
92   cancel_error,                 /**< an action was canceled */
93   thread_error,                 /**< error while [un]locking */
94   host_error,                   /**< host failed */
95   tracing_error,                /**< error during the simulation tracing */
96   io_error,                     /**< disk or file error */
97   vm_error                      /**< vm  error */
98 } xbt_errcat_t;
99
100 #ifdef __cplusplus
101 XBT_PUBLIC_CLASS xbt_ex :
102   public std::runtime_error,
103   public simgrid::xbt::WithContextException {
104 public:
105   xbt_ex() : std::runtime_error("") {}
106   xbt_ex(const char* message) : std::runtime_error(message) {}
107   ~xbt_ex() override;
108
109   xbt_errcat_t category;        /**< category like HTTP (what went wrong) */
110   int value;                    /**< like errno (why did it went wrong) */
111   const char *file;             /**< Thrown point */
112   int line;                     /**< Thrown point */
113   const char *func;             /**< Thrown point */
114 };
115 #endif
116
117 SG_BEGIN_DECL()
118
119 XBT_PUBLIC(const char *) xbt_ex_catname(xbt_errcat_t cat);
120
121 typedef struct xbt_ex xbt_ex_t;
122
123 XBT_PUBLIC(void) xbt_throw(char* message, xbt_errcat_t errcat, int value, const char* file, int line, const char* func) XBT_ATTRIB_NORETURN;
124
125 /** @brief Builds and throws an exception
126     @hideinitializer */
127 #define THROW(c, v)             { xbt_throw(NULL, (xbt_errcat_t) c, v, __FILE__, __LINE__, __func__); }
128
129 /** @brief Builds and throws an exception with a printf-like formatted message
130     @hideinitializer */
131 #define THROWF(c, v, ...)       xbt_throw(bprintf(__VA_ARGS__), (xbt_errcat_t) c, v, __FILE__, __LINE__, __func__)
132
133 #define THROW_IMPOSSIBLE \
134   THROWF(unknown_error, 0, "The Impossible Did Happen (yet again)")
135 #define THROW_UNIMPLEMENTED \
136   THROWF(unknown_error, 0, "Function %s unimplemented",__func__)
137 #define THROW_DEADCODE \
138   THROWF(unknown_error, 0, "Function %s was supposed to be DEADCODE, but it's not",__func__)
139
140 #define DIE_IMPOSSIBLE xbt_die("The Impossible Did Happen (yet again)")
141
142 /** @brief The display made by an exception that is not catched */
143 XBT_PUBLIC(void) xbt_ex_display(xbt_ex_t * e);
144
145 SG_END_DECL()
146
147 /** @} */
148 #endif                          /* __XBT_EX_H__ */