File: exception.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 239,888 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 95
file content (429 lines) | stat: -rw-r--r-- 11,410 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//

#include <BALL/COMMON/exception.h>
#include <BALL/COMMON/logStream.h>
#include <BALL/DATATYPE/string.h>

#include <iostream>
#include <typeinfo>
#include <exception>
#include <cstdio>
#include <cstdlib>	// for getenv in terminate()
#include <sys/types.h>
#include <csignal> // for SIGSEGV and kill

#ifdef BALL_HAS_UNISTD_H
#	include <unistd.h> // fot getpid
#endif
#ifdef BALL_HAS_PROCESS_H
#	include <process.h>
#endif

#define BALL_CORE_DUMP_ENVNAME "BALL_DUMP_CORE"

#define DEF_EXCEPTION(a,b) \
	a :: a (const char* file, int line) \
		: GeneralException(file, line, #a, b)\
	{\
	}\
	

using std::string;
using std::endl;

namespace BALL 
{

	namespace Exception 
	{

			GeneralException::GeneralException() 
				:	file_("?"),
					line_(-1),
					name_("GeneralException"),
					message_("unspecified error")
			{
				globalHandler.set(file_, line_, String(name_), String(message_));
			}

			GeneralException::GeneralException(const char* file, int line, const String& name, const String& message) 
				:	file_(file),
					line_(line),
					name_(name),
					message_(message)
			{
				globalHandler.set(file_, line_, name_, message_);
			}

			GeneralException::GeneralException(const char* file, int line) 
				:	file_(file),
					line_(line),
					name_("GeneralException"),
					message_("unknown error")
			{
				globalHandler.set(file_, line_, name_, message_);
			}

			GeneralException::GeneralException(const GeneralException& exception)
				:	std::exception(exception),
					file_(exception.file_),
					line_(exception.line_),
					name_(exception.name_),
					message_(exception.message_)
			{
			}

			GeneralException::~GeneralException() throw()
			{
			}
		
			const char* GeneralException::getName() const
			{
				return name_.c_str();
			}

			const char* GeneralException::getMessage() const
			{
				return message_.c_str();
			}

			void GeneralException::setMessage(const std::string& message)
			{
				message_ = message;
			}

			const char* GeneralException::getFile() const
			{
				return file_;
			}
			
			int GeneralException::getLine() const
			{
				return line_;
			}

			IndexUnderflow::IndexUnderflow(const char* file, int line, Index index, Size size)
				: GeneralException(file, line, "IndexUnderflow", ""),
					size_(size),
					index_(index)
			{
				message_ = "the given index was too small: ";
				char buf[40];

				sprintf(buf, "%ld", (long)index);
				message_ += buf;
				message_ += " (size = ";

				sprintf(buf, "%ld", (long)size);
				message_ += buf;
				message_ += ")";

				globalHandler.setMessage(message_);
			}

			IndexOverflow::IndexOverflow(const char* file, int line, Index index, Size size)
				:	GeneralException(file, line, "IndexOverflow", "an index was too large"),
					size_(size),
					index_(index)
			{
				message_ = "the given index was too large: ";
				char buf[40];

				sprintf(buf, "%ld", (long)index);
				message_ += buf;
				message_ += " (size = ";

				sprintf(buf, "%ld", (long)size);
				message_ += buf;
				message_ += ")";

				globalHandler.setMessage(message_);
			}

			OutOfMemory::OutOfMemory(const char* file, int line, Size size)
				:	GeneralException(file, line, "OutOfMemory", "a memory allocation failed"),
					size_(size)
			{
				message_ = "unable to allocate enough memory (size = ";
				char buf[40];

				sprintf(buf, "%ld", (long)size_);
				message_ += buf;
				message_ += " bytes) ";

				globalHandler.setMessage(message_);
			}

			OutOfMemory::~OutOfMemory() throw()
			{
			}

			SizeUnderflow::SizeUnderflow(const char* file, int line, Size size)
				:	GeneralException(file, line, "SizeUnderflow", ""),
					size_(size)
			{
				message_ = "the given size was too small: ";
				char buf[40];
				sprintf(buf, "%ld", (long)size);
				
				message_ += buf;
				globalHandler.setMessage(message_);
			}

			InvalidArgument::InvalidArgument(const char* file, int line, const String& arg)
				: GeneralException(file, line, "InvalidArgument",
				                   "An invalid argument has been passed: ")
			{
				message_ += arg;
			}

			InvalidSize::InvalidSize(const char* file, int line, Size size)
				:	GeneralException(file, line, "InvalidSize", ""),
					size_(size)
			{
				message_ = "the given size was not expected: ";
				char buf[40];
				sprintf(buf, "%ld", (long)size);
				
				message_ += buf;
				globalHandler.setMessage(message_);
			}

			IllegalPosition::IllegalPosition(const char* file, int line, float x, float y, float z)
				:	GeneralException(file, line, "IllegalPosition:", "")
			{
				char buf1[40];
				sprintf(buf1, "%f", x);
				char buf2[40];
				sprintf(buf2, "%f", y);
				char buf3[40];
				sprintf(buf3, "%f", z);

				message_ += "(";
				message_ += buf1;
				message_ += ",";
				message_ += buf2;
				message_ += ",";
				message_ += buf3;
				message_ += ")";
				globalHandler.setMessage(message_);
			}

			ParseError::ParseError(const char* file, int line, 
					const String& expression, const String& message)
				: GeneralException(file, line, "Parse Error", "")
			{

				message_ += message;
				message_ += " in ";
				message_ += expression;
				globalHandler.setMessage(message_);
			}

			Precondition::Precondition(const char* file, int line, const char* condition)
				: GeneralException(file, line, "Precondition failed", "")
			{
				message_ += std::string(condition);
				globalHandler.setMessage(message_);
			}

			Postcondition::Postcondition(const char* file, int line, const char* condition)
				: GeneralException(file, line, "Postcondition failed", "")
			{
				message_ += std::string(condition);
				globalHandler.setMessage(message_);
			}

			FileNotFound::FileNotFound(const char* file, int line, const String& filename)
				:	GeneralException(file, line, "FileNotFound", ""),
					filename_(filename)
			{
				message_ = "the file " + filename + " could not be found";
				globalHandler.setMessage(message_);
			}

			FileNotFound::~FileNotFound() throw()
			{
			}

			String FileNotFound::getFilename() const
			{
				return filename_;
			}

			InvalidFormat::InvalidFormat(const char* file, int line, const String& s)
				:	GeneralException(file, line, "InvalidFormat", ""),
					format_(s)
			{
				message_ = "problem converting '";
				message_.append(s + "' to a number.");

				globalHandler.setMessage(message_);
			}

			InvalidFormat::~InvalidFormat() throw()
			{
			}
		
			InvalidOption::InvalidOption(const char* file, int line, String option)
				: GeneralException(file, line, "Invalid option: ", option)
			{
			}

			TooManyErrors::TooManyErrors(const char* file, int line)
				: GeneralException(file, line, "Too many errors", "")
			{
			}

			TooManyBonds::TooManyBonds(const char* file, int line, const String& error)
				: GeneralException(file, line, 
						String("Unable to create additional bond between ") + error, "")
			{
			}


			InvalidRange::InvalidRange(const char* file, int line, float value)
				: GeneralException(file, line, "The argument was out of range: ", String(value))
			{
			}

			CUDAError::CUDAError(const char *file, int line, const BALL::String &error)
				: GeneralException(file, line, String("CUDA error: ") + error, "")
			{
			}
			
			NoBufferAvailable::NoBufferAvailable(const char* file, int line, const String& reason)
				: GeneralException(file, line, "NoBufferAvailable", String("Unavailable because: ") + reason)
			{
			}

			NotInitialized::NotInitialized(const char* file, int line, const String& reason)
				: GeneralException(file, line, "NotInitialized", reason)
			{
			}

			DEF_EXCEPTION(OutOfRange, "the range of the operation was invalid")

			DEF_EXCEPTION(DivisionByZero, "a division by zero was requested")

			DEF_EXCEPTION(NullPointer, "a null pointer was specified")

			DEF_EXCEPTION(InvalidIterator, "the iterator is invalid - probably it is not bound to a container")

			DEF_EXCEPTION(IncompatibleIterators, "the iterator could not be assigned because it is bound to a different container")

			DEF_EXCEPTION(NotImplemented, "this method has not been implemented yet. Feel free to complain about it!")

			DEF_EXCEPTION(IllegalSelfOperation, "cannot perform operation on the same object")

			DEF_EXCEPTION(IllegalTreeOperation, "an illegal tree operation was requested")

			DEF_EXCEPTION(BufferOverflow, "the maximum buffersize has been reached")

			DEF_EXCEPTION(OutOfGrid, "a point was outside a grid")	

			DEF_EXCEPTION(FormatUnsupported, "given framebuffer format is not supported")

		
			GlobalExceptionHandler::GlobalExceptionHandler()
			{
				std::set_terminate(terminate);
				std::set_unexpected(terminate);
				std::set_new_handler(newHandler);
			}

			void GlobalExceptionHandler::newHandler()
			{
				throw Exception::OutOfMemory(__FILE__, __LINE__);
			}
				
			void GlobalExceptionHandler::terminate()
			{
				// add cerr to the log stream
				// and write all available information on
				// the exception to the log stream (potentially with an assigned file!)
				// and cerr

				Log.insert(std::cerr);
				Log.error() << endl;
				Log.error() << "---------------------------------------------------" << endl;
				Log.error() << "FATAL: uncaught exception!" << endl;
				Log.error() << "---------------------------------------------------" << endl;
				if ((line_ != -1) && (name_ != "unknown"))
				{
					Log.error() << "last entry in the exception handler: " << endl;
					Log.error() << "exception of type " << name_.c_str() << " occurred in line "
											<< line_ << " of " << file_.c_str() << endl;
					Log.error() << "error message: " << message_.c_str() << endl;
				}
				Log.error() << "---------------------------------------------------" << endl;

				// if the environment variable declared in BALL_CORE_DUMP_ENVNAME
				// is set, provoke a core dump (this is helpful to get s stack traceback)
				if (getenv(BALL_CORE_DUMP_ENVNAME) != 0)
				{
					#ifdef BALL_HAS_KILL
						Log.error() << "dumping core file.... (to avoid this, unset " << BALL_CORE_DUMP_ENVNAME 
									<< " in your environment)" << endl;
						// provoke a core dump 
						kill(getpid(), SIGSEGV);
					#endif
				}

				// otherwise exit cleanly
				exit(1);
			}
				
			void GlobalExceptionHandler::set
				(const String& file, int line,
				 const String& name, const String& message)
			{
				name_ = name;
				line_ = line;
				message_ = message;
				file_ = file;
			}
			
			void GlobalExceptionHandler::setName(const String& name)
			{
				name_ = name;
			}
			
			void GlobalExceptionHandler::setMessage(const String& message)
			{
				message_ = message;
			}
			
			void GlobalExceptionHandler::setFile(const String& file)
			{
				file_ = file;
			}
			
			void GlobalExceptionHandler::setLine(int line) 
			{
				line_ = line;
			}

			std::string	GlobalExceptionHandler::name_			= "unknown exception";
			int					GlobalExceptionHandler::line_			= -1;
			std::string	GlobalExceptionHandler::message_	= " - ";
			std::string	GlobalExceptionHandler::file_			= "unknown";



			// create a global instance of the exception handler
			BALL_EXPORT GlobalExceptionHandler globalHandler;

	} // namespace Exception

	std::ostream& operator << (std::ostream& os, const Exception::GeneralException& e)
	{
		os << e.getName() << " @ " << e.getFile() << ":" << e.getLine() << ": " << e.getMessage();

		return os;
	}


} // namespace BALL