mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
mxError.hpp
Go to the documentation of this file.
1 /** \file mxError.hpp
2  * \author Jared R. Males (jaredmales@gmail.com)
3  * \brief Declares and defines the mxlib error reporting system.
4  * \ingroup error_handling_files
5  *
6 */
7 
8 //***********************************************************************//
9 // Copyright 2015, 2016, 2017 Jared R. Males (jaredmales@gmail.com)
10 //
11 // This file is part of mxlib.
12 //
13 // mxlib is free software: you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation, either version 3 of the License, or
16 // (at your option) any later version.
17 //
18 // mxlib is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with mxlib. If not, see <http://www.gnu.org/licenses/>.
25 //***********************************************************************//
26 
27 #ifndef __mxError__
28 #define __mxError__
29 
30 #include <cerrno>
31 #include <cstring>
32 #include <sstream>
33 
34 /** \addtogroup error_macros
35  *
36  * mxlib components use these macros to report errors to the user, which by default are wrappers for mx::error_report and mx::errno_report.
37  * This behavior can be changed with preprocessor defines.
38  *
39  * To completely suppress all mxlib error reporting, include the following before includng any mxlib headers
40  * \code
41  * #define MX_NO_ERROR_REPORTS
42  * \endcode
43  */
44 
45  /** \ingroup error_handling
46  * @{
47  */
48 
49 /** \def MX_NO_ERROR_REPORTS
50  * \brief If this is defined, then no errors are reported.
51  * \ingroup error_macros
52  */
53 #ifdef MX_NO_ERROR_REPORTS
54 
55 //Just defining these as empty macros, though this is currently untested.
56 /** \todo test MX_NO_ERROR_REPORTS */
57 
58 #define mxError(esrc,ecode,expl)
59 #define mxPError(esrc,ecode,expl)
60 
61 #else
62 
63 
64 #ifndef mxError
65 
66 #include <iostream>
67 
68 /** \def mxError
69  * \brief This reports an mxlib specific error
70  *
71  * Can be predefined to change the error reporting of mxlib.
72  *
73  * \param[in] esrc is intended to identify the component (i.e. the class name)
74  * \param[in] ecode is an errno erro code
75  * \param[in] expl [optional] if more information can be provided, use this to inform the user.
76  *
77  * \ingroup error_macros
78  */
79 #define mxError(esrc,ecode,expl) std::cerr << "\n" << mx::error_report(esrc, ecode, __FILE__, __LINE__, expl) << "\n";
80 
81 #endif //mxError
82 
83 #ifndef mxPError
84 
85 /** \def mxPError
86  * \brief This reports a standard library error, taking after perror.
87  *
88  * Can be pre-defined to change the error reporting of mxlib.
89  *
90  * \param[in] esrc is intended to identify the component (i.e. the class name)
91  * \param[in] ecode is an errno erro code
92  * \param[in] expl [optional] if more information can be provided, use this to inform the user.
93  *
94  * \addtogroup error_macros
95  */
96 #define mxPError(esrc,ecode,expl) std::cerr << "\n" << mx::errno_report(esrc, ecode, __FILE__, __LINE__, expl) << "\n";
97 
98 
99 #endif //mxPError
100 
101 #endif //MX_NO_ERROR_REPORTS
102 
103 /// @}
104 
105 
106 namespace mx
107 {
108 /** \defgroup mxe_errors mxlib Error Codes
109  * \ingroup error_handling
110  */
111 
112 /** \def MXE_INVALIDARG
113  * \brief An argument was invalid.
114  * \ingroup mxe_errors
115  */
116 #define MXE_INVALIDARG 25
117 #define MXE_INVALIDARG_NAME "MXE_INVALIDARG"
118 #define MXE_INVALIDARG_MSG "An argument was invalid."
119 
120 /** \def MXE_INVALIDCONFIG
121  * \brief A config setting was invalid.
122  * \ingroup mxe_errors
123  */
124 #define MXE_INVALIDCONFIG 27
125 #define MXE_INVALIDCONFIG_NAME "MXE_INVALIDCONFIG"
126 #define MXE_INVALIDCONFIG_MSG "A config setting was invalid."
127 
128 /** \def MXE_NOTIMPL
129  * \brief A component or technique is not implemented.
130  * \ingroup mxe_errors
131  */
132 #define MXE_NOTIMPL 30
133 #define MXE_NOTIMPL_NAME "MXE_NOTIMPL"
134 #define MXE_NOTIMPL_MSG "A component or technique is not implemented."
135 
136 /** \def MXE_PARAMNOTSET
137  * \brief A parameter was not set
138  * \ingroup mxe_errors
139  */
140 #define MXE_PARAMNOTSET 35
141 #define MXE_PARAMNOTSET_NAME "MXE_PARAMNOTSET"
142 #define MXE_PARAMNOTSET_MSG "A parameter was not set."
143 
144 /** \def MXE_ENVNOTSET
145  * \brief An environment variable is not set
146  * \ingroup mxe_errors
147  */
148 #define MXE_ENVNOTSET 36
149 #define MXE_ENVNOTSET_NAME "MXE_ENVNOTSET"
150 #define MXE_ENVNOTSET_MSG "An environment variable is not set."
151 
152 /** \def MXE_NOTFOUND
153  * \brief An item was not found
154  * \ingroup mxe_errors
155  */
156 #define MXE_NOTFOUND 40
157 #define MXE_NOTFOUND_NAME "MXE_NOTFOUND"
158 #define MXE_NOTFOUND_MSG "An item was not found."
159 
160 /** \def MXE_SIZEERR
161  * \brief A size was invalid or calculated incorrectly
162  * \ingroup mxe_errors
163  */
164 #define MXE_SIZEERR 55
165 #define MXE_SIZEERR_NAME "MXE_SIZEERR"
166 #define MXE_SIZEERR_MSG "A size was invalid or calculated incorrectly."
167 
168 /** \def MXE_ALLOCERR
169  * \brief An error occurred during memory allocation.
170  * \ingroup mxe_errors
171  */
172 #define MXE_ALLOCERR 60
173 #define MXE_ALLOCERR_NAME "MXE_ALLOCERR"
174 #define MXE_ALLOCERR_MSG "An error occurred during memory allocation."
175 
176 /** \def MXE_FREEERR
177  * \brief An error occurred during memory de-allocation.
178  * \ingroup mxe_errors
179  */
180 #define MXE_FREEERR 65
181 #define MXE_FREEERR_NAME "MXE_FREEERR"
182 #define MXE_FREEERR_MSG "An error occurred during memory de-allocation."
183 
184 
185 /** \def MXE_PARSEERR
186  * \brief A parsing error occurred.
187  * \ingroup mxe_errors
188  */
189 #define MXE_PARSEERR 75
190 #define MXE_PARSEERR_NAME "MXE_PARSEERR"
191 #define MXE_PARSEERR_MSG "A parsing error occurred."
192 
193 
194 /** \def MXE_FILEOERR
195  * \brief An error occurred while opening a file.
196  * \ingroup mxe_errors
197  */
198 #define MXE_FILEOERR 1034
199 #define MXE_FILEOERR_NAME "MXE_FILEOERR"
200 #define MXE_FILEOERR_MSG "An error occurred while opening a file."
201 
202 /** \def MXE_FILEWERR
203  * \brief An error occurred while writing to a file.
204  * \ingroup mxe_errors
205  */
206 #define MXE_FILEWERR 1044
207 #define MXE_FILEWERR_NAME "MXE_FILEWERR"
208 #define MXE_FILEWERR_MSG "An error occurred while writing to a file."
209 
210 /** \def MXE_FILERERR
211  * \brief An error occurred while reading from a file.
212  * \ingroup mxe_errors
213  */
214 #define MXE_FILERERR 1049
215 #define MXE_FILERERR_NAME "MXE_FILERERR"
216 #define MXE_FILERERR_MSG "An error occurred while reading from a file."
217 
218 /** \def MXE_FILECERR
219  * \brief An error occurred while closing a file.
220  * \ingroup mxe_errors
221  */
222 #define MXE_FILECERR 1054
223 #define MXE_FILECERR_NAME "MXE_FILECERR"
224 #define MXE_FILECERR_MSG "An error occurred while closing a file."
225 
226 /** \def MXE_FILENOTFOUND
227  * \brief The file was not found.
228  * \ingroup mxe_errors
229  */
230 #define MXE_FILENOTFOUND 1059
231 #define MXE_FILENOTFOUND_NAME "MXE_FILENOTFOUND"
232 #define MXE_FILENOTFOUND_MSG "The file was not found."
233 
234 /** \def MXE_PROCERR
235  * \brief An error occrred while starting a process.
236  * \ingroup mxe_errors
237  */
238 #define MXE_PROCERR 2001
239 #define MXE_PROCERR_NAME "MXE_PROCERR"
240 #define MXE_PROCERR_MSG "An error occured while starting a process."
241 
242 /** \def MXE_TIMEOUT
243  * \brief A timeout occurred.
244  * \ingroup mxe_errors
245  */
246 #define MXE_TIMEOUT 2322
247 #define MXE_TIMEOUT_NAME "MXE_TIMEOUT"
248 #define MXE_TIMEOUT_MSG "A timeout occurred."
249 
250 /** \def MXE_LIBERR
251  * \brief An error was returned by a library.
252  * \ingroup mxe_errors
253  */
254 #define MXE_LIBERR 4000
255 #define MXE_LIBERR_NAME "MXE_LIBERR"
256 #define MXE_LIBERR_MSG "An error was returned by a library."
257 
258 
259 /** \def MXE_GNUPLOTERR
260  * \brief An error was returned by gnuplot.
261  * \ingroup mxe_errors
262  */
263 #define MXE_GNUPLOTERR 4567
264 #define MXE_GNUPLOTERR_NAME "MXE_GNUPLOTERR"
265 #define MXE_GNUPLOTERR_MSG "An error was returned by gnuplot."
266 
267 /** \def MXE_LAPACKERR
268  * \brief An error was returned by Lapack.
269  * \ingroup mxe_errors
270  */
271 #define MXE_LAPACKERR 6890
272 #define MXE_LAPACKERR_NAME "MXE_LAPACKERR"
273 #define MXE_LAPACKERR_MSG "An error was returned by Lapack."
274 
275 
276 ///Return the name for an mxlib error code
277 /**
278  *
279  * \returns the name of the macro corresponding to the error code.
280  *
281  * \ingroup error_handling
282  */
283 std::string MXE_CodeToName( int ec /**< [in] the error code */ );
284 
285 ///Return the description for an mxlib error code
286 /**
287  *
288  * \returns the description for and error code.
289  *
290  * \ingroup error_handling
291  */
292 std::string MXE_CodeToDescription( int ec /**< [in] the error code */ );
293 
294 ///Return the macro name and a message for a standard errno code
295 /**
296  *
297  * \returns the name of the macro corresponding to the code.
298  *
299  * \ingroup error_handling
300  */
301 std::string errno_CodeToName( int ec /**< [in] the error code */ );
302 
303 ///Construct a rich error report given an mxlib error code
304 /**
305  *
306  * \return the formatted error report.
307  *
308  * \ingroup error_handling
309  */
310 std::string error_report( const std::string & source, ///< [in] is intended to identify the mxlib component (i.e. the class name)
311  const int & code, ///< [in] is an MXE_* error code
312  const std::string & file, ///< [in] should be passed the __FILE__ macro
313  const int & line, ///< [in] should be passed the __LINE__ macro
314  const std::string & expl = "" ///< [in] [optional] if more information can be provided, use this to inform the user.
315  );
316 
317 
318 ///Construct a rich error report given a standard errno error code
319 /**
320  * \return the formatted error report.
321  *
322  * \ingroup error_handling
323  */
324 std::string errno_report( const std::string & source, ///< [in] intended to identify the component (i.e. the class name)
325  int ec, ///< [in] an errno erro code
326  const std::string & file, ///< [in] file should be passed the __FILE__ macro
327  const int & line, ///< [in] line should be passed the __LINE__ macro
328  const std::string & expl = "" ///< [in] [optional] if more information can be provided, use this to inform the user.
329  );
330 
331 
332 
333 
334 
335 
336 } //namespace mx
337 
338 #endif //__mxError__
std::string error_report(const std::string &source, const int &code, const std::string &file, const int &line, const std::string &expl="")
Construct a rich error report given an mxlib error code.
Definition: mxError.cpp:458
std::string MXE_CodeToName(int ec)
Return the name for an mxlib error code.
Definition: mxError.cpp:32
std::string errno_CodeToName(int ec)
Return the macro name and a message for a standard errno code.
Definition: mxError.cpp:132
std::string MXE_CodeToDescription(int ec)
Return the description for an mxlib error code.
Definition: mxError.cpp:82
std::string errno_report(const std::string &source, int ec, const std::string &file, const int &line, const std::string &expl="")
Construct a rich error report given a standard errno error code.
Definition: mxError.cpp:486
The mxlib c++ namespace.
Definition: mxError.hpp:107