error.hpp
Go to the documentation of this file.
1 
5 /* Copyright (c) 2005-2011 Taneli Kalvas. All rights reserved.
6  *
7  * You can redistribute this software and/or modify it under the terms
8  * of the GNU General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this library (file "COPYING" included in the package);
19  * if not, write to the Free Software Foundation, Inc., 51 Franklin
20  * Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  * If you have questions about your rights to use or distribute this
23  * software, please contact Berkeley Lab's Technology Transfer
24  * Department at TTD@lbl.gov. Other questions, comments and bug
25  * reports should be sent directly to the author via email at
26  * taneli.kalvas@jyu.fi.
27  *
28  * NOTICE. This software was developed under partial funding from the
29  * U.S. Department of Energy. As such, the U.S. Government has been
30  * granted for itself and others acting on its behalf a paid-up,
31  * nonexclusive, irrevocable, worldwide license in the Software to
32  * reproduce, prepare derivative works, and perform publicly and
33  * display publicly. Beginning five (5) years after the date
34  * permission to assert copyright is obtained from the U.S. Department
35  * of Energy, and subject to any subsequent five (5) year renewals,
36  * the U.S. Government is granted for itself and others acting on its
37  * behalf a paid-up, nonexclusive, irrevocable, worldwide license in
38  * the Software to reproduce, prepare derivative works, distribute
39  * copies to the public, perform publicly and display publicly, and to
40  * permit others to do so.
41  */
42 
43 #ifndef ERROR_HPP
44 #define ERROR_HPP 1
45 
46 
47 #include "config.h"
48 #include <string>
49 #include <cstring>
50 #include <stdlib.h>
51 #include <stdint.h>
52 #include <stdlib.h>
53 #include <stdio.h>
54 #include <sstream>
55 #include <errno.h>
56 #include <signal.h>
57 
58 
61 template <class T>
62 inline std::string to_string( const T& t )
63 {
64  std::stringstream ss;
65  ss << t;
66  return( ss.str() );
67 }
68 
69 
72 #define ERROR_LOCATION ErrorLocation( __FILE__, __LINE__, __func__ )
73 
74 
83 
84  const char *_file;
85  int _line;
86  const char *_func;
87 
88 public:
89 
94  ErrorLocation();
95 
100  ErrorLocation( const char *file, int line, const char *func );
101 
104  std::string file( void );
105 
108  int line( void );
109 
112  std::string func( void );
113 };
114 
115 
122 
123  void *_traceaddress[25];
124  int _tracecount;
125 
126 public:
127 
132  ExceptionTracer();
133 
136  void print_trace( std::ostream &os );
137 };
138 
139 
142 class Error : public ExceptionTracer {
143 
144  ErrorLocation _loc;
145 
146 protected:
147 
148  std::string _error_str;
149 
150 public:
151 
154  Error();
155 
158  Error( const std::string &str );
159 
162  Error( const ErrorLocation &loc );
163 
167  Error( const ErrorLocation &loc, const std::string &str );
168 
171  std::string get_error_message( void );
172 
179  void print_error_message( std::ostream &os, bool traceprint = true );
180 };
181 
182 
185 class ErrorNoMem : public Error {
186 
187 public:
188 
193  ErrorNoMem( const ErrorLocation &loc );
194 
197  ErrorNoMem( const ErrorLocation &loc, const std::string &str );
198 };
199 
200 
203 class ErrorErrno : public Error {
204 
205  int _ierrno;
206 
207 public:
208 
212  ErrorErrno( const ErrorLocation &loc );
213 };
214 
215 
218 class ErrorUnimplemented : public Error {
219 
220 public:
221 
226  ErrorUnimplemented( const ErrorLocation &loc );
227 
230  ErrorUnimplemented( const ErrorLocation &loc, const std::string &str );
231 };
232 
233 
236 class ErrorDim : public Error {
237 
238 public:
239 
244  ErrorDim( const ErrorLocation &loc );
245 
248  ErrorDim( const ErrorLocation &loc, const std::string &str );
249 };
250 
251 
254 class ErrorRange : public Error {
255 
256 public:
257 
263  ErrorRange( const ErrorLocation &loc,
264  uint32_t i, uint32_t n,
265  uint32_t j, uint32_t m );
266 
271  ErrorRange( const ErrorLocation &loc, uint32_t i, uint32_t n );
272 };
273 
274 
278 
279 public:
280 
281 #if defined(_GNU_SOURCE) && defined(HAVE_SIGINFO_T)
282 
284  static void signal_handler_SIGSEGV( int signum, siginfo_t *info, void *ptr );
285 #endif
286 
287 #ifdef HAVE_SIGINFO_T
288 
290  static void signal_handler_SIGTERM( int signum, siginfo_t *info, void *ptr );
291 #else
292 
294  static void signal_handler_SIGTERM( int signum );
295 #endif
296 };
297 
298 
299 #endif
300 
ErrorErrno(const ErrorLocation &loc)
Constructor for errno based error with standard error message from errno database.
Error()
Default constructor for error class.
ErrorLocation()
Default constructor for error location.
Basic error class.
Definition: error.hpp:142
void print_trace(std::ostream &os)
Print the backtrace to os.
void print_error_message(std::ostream &os, bool traceprint=true)
Print a standard error message to os.
Error class for dimension mismatch errors.
Definition: error.hpp:236
ErrorDim(const ErrorLocation &loc)
Constructor for dimension mismatch error with standard error message.
std::string to_string(const T &t)
Function for converting a type to string.
Definition: error.hpp:62
std::string _error_str
Definition: error.hpp:148
ErrorNoMem(const ErrorLocation &loc)
Constructor for memory allocation error with standard error message.
static void signal_handler_SIGSEGV(int signum, siginfo_t *info, void *ptr)
Signal handler function for SIGSEGV.
Signal handler.
Definition: error.hpp:277
Error class for C-style errno errors.
Definition: error.hpp:203
std::string get_error_message(void)
Return error message.
int line(void)
Return line number of location.
Error class to use if requested feature is unimplemented.
Definition: error.hpp:218
ExceptionTracer()
Default constructor for exception tracer. Saves the backtrace of the program at this location for pri...
Error class for memory allocation errors.
Definition: error.hpp:185
ErrorRange(const ErrorLocation &loc, uint32_t i, uint32_t n, uint32_t j, uint32_t m)
Constructor for error message for two dimensional indexing error.
static void signal_handler_SIGTERM(int signum, siginfo_t *info, void *ptr)
Signal handler function for SIGTERM.
ErrorUnimplemented(const ErrorLocation &loc)
Constructor for unimplemented feature error with standard error message.
Error class for index range checking errors.
Definition: error.hpp:254
Exception backtrace.
Definition: error.hpp:121
Error location class.
Definition: error.hpp:82
std::string func(void)
Return function name of location.
std::string file(void)
Return file name of location.