error.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2015 OpenFOAM Foundation
9  Copyright (C) 2015-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::error
29 
30 Description
31  Class to handle errors and exceptions in a simple, consistent stream-based
32  manner.
33 
34  The error class is globally instantiated with a title string. Errors,
35  messages and other data are piped to the messageStream class in the
36  standard manner. Manipulators are supplied for exit and abort that may
37  terminate the program or throw an exception depending on whether the
38  exception handling has been switched on (off by default).
39 
40 Usage
41  \code
42  error << "message1" << "message2" << FoamDataType << exit(errNo);
43  error << "message1" << "message2" << FoamDataType << abort();
44  \endcode
45 
46 SourceFiles
47  error.C
48 
49 \*---------------------------------------------------------------------------*/
50 
51 #ifndef error_H
52 #define error_H
53 
54 #include "messageStream.H"
55 
56 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
57 
58 namespace Foam
59 {
60 
61 /*---------------------------------------------------------------------------*\
62  Class error Declaration
63 \*---------------------------------------------------------------------------*/
64 
65 class error
66 :
67  public std::exception,
68  public messageStream
69 {
70  // Private Member Functions
71 
72  //- Common code for exit or abort
73  void exitOrAbort(const int errNo, const bool isAbort);
74 
75 
76 protected:
77 
78  // Protected Data
79 
80  string functionName_;
85 
86 
87 public:
88 
89  // Constructors
90 
91  //- Construct from title string
92  explicit error(const string& title);
93 
94  //- Construct from dictionary
95  explicit error(const dictionary& errDict);
96 
97  //- Copy construct
98  error(const error& err);
99 
100 
101  //- Destructor
102  virtual ~error() noexcept;
103 
104 
105  // Static Functions
106 
107  //- Emit warning on stderr about something being old.
108  // \param what description for the warning
109  // \param version is the old version (YYMM) for determining the
110  // age in months compared to the current OpenFOAM version
111  // as conveyed by the \c foamVersion::api value.
112  static void warnAboutAge(const char* what, const int version);
113 
114 
115  // Member Functions
116 
117  //- The accumulated error message
118  string message() const;
119 
120  //- Clear any messages
121  void clear() const;
122 
123  const string& functionName() const
124  {
125  return functionName_;
126  }
127 
128  const string& sourceFileName() const
129  {
130  return sourceFileName_;
131  }
132 
133  label sourceFileLineNumber() const
134  {
135  return sourceFileLineNumber_;
136  }
137 
138  //- Return the current exception throwing (on or off)
139  bool throwing() const
140  {
141  return throwExceptions_;
142  }
143 
144  //- Activate/deactivate exception throwing
145  // \return the previous throwing state
146  inline bool throwExceptions(bool doThrow)
147  {
148  const bool prev = throwExceptions_;
149  throwExceptions_ = doThrow;
150  return prev;
151  }
152 
153  //- Activate exception throwing
154  // \return the previous throwing state
155  inline bool throwExceptions()
156  {
157  return throwExceptions(true);
158  }
159 
160  //- Deactivate exception throwing
161  // \return the previous throwing state
162  inline bool dontThrowExceptions()
163  {
164  return throwExceptions(false);
165  }
166 
167  //- Define basic print message
168  // \return OSstream for further info.
169  OSstream& operator()
170  (
171  const string& functionName
172  );
173 
174  //- Define basic print message
175  // \return OSstream for further info.
176  OSstream& operator()
177  (
178  const char* functionName,
179  const char* sourceFileName,
180  const int sourceFileLineNumber = 0
181  );
182 
183  //- Define basic print message
184  // \return OSstream for further info.
185  OSstream& operator()
186  (
187  const string& functionName,
188  const char* sourceFileName,
189  const int sourceFileLineNumber = 0
190  );
191 
192  //- Explicit convert to OSstream for << operations
194  {
195  return operator OSstream&();
196  }
197 
198  //- Convert to OSstream
199  operator OSstream&();
200 
201  //- Create and return a dictionary representation of the error
202  operator dictionary() const;
203 
204 
205  //- Helper function to print a stack,
206  //- used when OpenFOAM IO not yet initialised.
207  static void safePrintStack(std::ostream& os);
208 
209  //- Helper function to print a stack
210  static void printStack(Ostream& os);
211 
212  //- Exit : can be called for any error to exit program.
213  // Redirects to abort() when FOAM_ABORT is on.
214  void exit(const int errNo = 1);
215 
216  //- Abort : used to stop code for fatal errors.
217  // Prints stack before exiting.
218  void abort();
219 
220  //- Print error message
221  void write(Ostream& os, const bool includeTitle = true) const;
222 };
223 
224 
225 /*---------------------------------------------------------------------------*\
226  Class IOerror Declaration
227 \*---------------------------------------------------------------------------*/
228 
229 //- Report an I/O error
230 class IOerror
231 :
232  public error
233 {
234  // Private Data
235 
236  string ioFileName_;
237  label ioStartLineNumber_;
238  label ioEndLineNumber_;
239 
240 
241  // Private Member Functions
242 
243  //- Common code for exit or abort
244  void exitOrAbort(const int errNo, const bool isAbort);
245 
246 
247 public:
248 
249  // Constructors
250 
251  //- Construct from title string
252  explicit IOerror(const string& title);
253 
254  //- Construct from dictionary
255  explicit IOerror(const dictionary& errDict);
256 
257 
258  //- Destructor
259  virtual ~IOerror() noexcept;
260 
261 
262  // Member Functions
263 
264  const string& ioFileName() const
265  {
266  return ioFileName_;
267  }
268 
269  label ioStartLineNumber() const
270  {
271  return ioStartLineNumber_;
272  }
273 
274  label ioEndLineNumber() const
275  {
276  return ioEndLineNumber_;
277  }
278 
279  //- Convert to OSstream
280  // Prints basic message and returns OSstream for further info.
281  OSstream& operator()
282  (
283  const char* functionName,
284  const char* sourceFileName,
285  const int sourceFileLineNumber,
286  const string& ioFileName,
287  const label ioStartLineNumber = -1,
288  const label ioEndLineNumber = -1
289  );
290 
291  //- Convert to OSstream
292  // Prints basic message and returns OSstream for further info.
293  OSstream& operator()
294  (
295  const char* functionName,
296  const char* sourceFileName,
297  const int sourceFileLineNumber,
298  const IOstream& ioStream
299  );
300 
301  //- Convert to OSstream
302  // Prints basic message and returns OSstream for further info.
303  OSstream& operator()
304  (
305  const char* functionName,
306  const char* sourceFileName,
307  const int sourceFileLineNumber,
308  const dictionary& dict
309  );
310 
311  //- Print basic message and exit.
312  // Uses cerr if streams not yet constructed (at startup).
313  // Use in startup parsing instead of FatalError.
314  static void SafeFatalIOError
315  (
316  const char* functionName,
317  const char* sourceFileName,
318  const int sourceFileLineNumber,
319  const IOstream& ioStream,
320  const string& msg
321  );
322 
323  //- Create and return a dictionary representation of the error
324  operator dictionary() const;
325 
326 
327  //- Exit : can be called for any error to exit program
328  void exit(const int errNo = 1);
329 
330  //- Abort : used to stop code for fatal errors
331  void abort();
332 
333  //- Print error message
334  void write(Ostream& os, const bool includeTitle = true) const;
335 };
336 
337 
338 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
339 
340 //- Ostream operator
341 Ostream& operator<<(Ostream& os, const error& err);
342 
343 //- Ostream operator
344 Ostream& operator<<(Ostream& os, const IOerror& err);
345 
346 
347 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
348 // Global error declarations: defined in error.C
349 
350 //- Error stream (uses stdout - output on all processes),
351 //- with additional 'FOAM FATAL ERROR' header text and stack trace.
352 extern error FatalError;
353 
354 //- Error stream (uses stdout - output on all processes),
355 //- with additional 'FOAM FATAL IO ERROR' header text and stack trace.
356 extern IOerror FatalIOError;
357 
358 
359 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
360 
361 } // End namespace Foam
362 
363 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
364 // Convenience macros to add the file name and line number to the function name
365 
366 //- Report an error message using Foam::FatalError
367 // for functionName in file __FILE__ at line __LINE__
368 #define FatalErrorIn(functionName) \
369  ::Foam::FatalError((functionName), __FILE__, __LINE__)
370 
371 //- Report an error message using Foam::FatalError
372 // for FUNCTION_NAME in file __FILE__ at line __LINE__
373 #define FatalErrorInFunction FatalErrorIn(FUNCTION_NAME)
374 
375 //- Report an error message using Foam::FatalError
376 // about unknown lookup type in table
377 #define FatalErrorInLookup(lookupTag, lookupName, lookupTable) \
378  ::Foam::FatalError(FUNCTION_NAME, __FILE__, __LINE__) \
379  << "Unknown " << (lookupTag) << " type " << (lookupName) \
380  << "\n\nValid " << (lookupTag) << " types :\n" \
381  << ((lookupTable).sortedToc()) << '\n'
382 
383 
384 //- Report an error message using Foam::FatalIOError
385 // for functionName in file __FILE__ at line __LINE__
386 // for a particular IOstream
387 #define FatalIOErrorIn(functionName, ios) \
388  ::Foam::FatalIOError((functionName), __FILE__, __LINE__, (ios))
389 
390 //- Report an error message using Foam::FatalIOError
391 // for FUNCTION_NAME in file __FILE__ at line __LINE__
392 // for a particular IOstream
393 #define FatalIOErrorInFunction(ios) FatalIOErrorIn(FUNCTION_NAME, ios)
394 
395 
396 //- Report an error message using Foam::FatalIOError
397 // about unknown lookup type in table
398 #define FatalIOErrorInLookup(ios, lookupTag, lookupName, lookupTable) \
399  ::Foam::FatalIOError(FUNCTION_NAME, __FILE__, __LINE__, (ios)) \
400  << "Unknown " << (lookupTag) << " type " << (lookupName) \
401  << "\n\nValid " << (lookupTag) << " types :\n" \
402  << ((lookupTable).sortedToc()) << '\n'
403 
404 
405 //- Report an error message using Foam::FatalIOError
406 // (or cerr if FatalIOError not yet constructed)
407 // for functionName in file __FILE__ at line __LINE__
408 // for a particular IOstream
409 #define SafeFatalIOErrorIn(functionName, ios, msg) \
410  ::Foam::IOerror::SafeFatalIOError \
411  ((functionName), __FILE__, __LINE__, (ios), (msg))
412 
413 //- Report an error message using Foam::FatalIOError
414 // (or cerr if FatalIOError not yet constructed)
415 // for functionName in file __FILE__ at line __LINE__
416 // for a particular IOstream
417 #define SafeFatalIOErrorInFunction(ios, msg) \
418  SafeFatalIOErrorIn(FUNCTION_NAME, ios, msg)
419 
420 
421 //- Issue a FatalErrorIn for a function not currently implemented.
422 // The functionName is printed and then abort is called.
423 //
424 // This macro can be particularly useful when methods must be defined to
425 // complete the interface of a derived class even if they should never be
426 // called for this derived class.
427 #define notImplemented(functionName) \
428  FatalErrorIn(functionName) \
429  << "Not implemented" << ::Foam::abort(FatalError);
430 
431 //- Issue a FatalErrorIn for a function not currently implemented.
432 // The FUNCTION_NAME is printed and then abort is called.
433 //
434 // This macro can be particularly useful when methods must be defined to
435 // complete the interface of a derived class even if they should never be
436 // called for this derived class.
437 #define NotImplemented notImplemented(FUNCTION_NAME)
438 
439 
440 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
441 
442 #include "errorManip.H"
443 
444 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
445 
446 #endif
447 
448 // ************************************************************************* //
Foam::IOerror::ioEndLineNumber
label ioEndLineNumber() const
Definition: error.H:273
Foam::error::sourceFileName
const string & sourceFileName() const
Definition: error.H:127
Foam::error::printStack
static void printStack(Ostream &os)
Helper function to print a stack.
Definition: dummyPrintStack.C:36
Foam::error::dontThrowExceptions
bool dontThrowExceptions()
Deactivate exception throwing.
Definition: error.H:161
Foam::IOerror::abort
void abort()
Abort : used to stop code for fatal errors.
Definition: IOerror.C:253
Foam::error::throwExceptions
bool throwExceptions()
Activate exception throwing.
Definition: error.H:154
Foam::messageStream
Class to handle messaging in a simple, consistent stream-based manner.
Definition: messageStream.H:71
Foam::error::sourceFileName_
string sourceFileName_
Definition: error.H:80
Foam::IOerror::IOerror
IOerror(const string &title)
Construct from title string.
Definition: IOerror.C:38
Foam::IOstream
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:75
Foam::IOerror::ioStartLineNumber
label ioStartLineNumber() const
Definition: error.H:268
Foam::error::functionName
const string & functionName() const
Definition: error.H:122
SafeFatalIOErrorIn
#define SafeFatalIOErrorIn(functionName, ios, msg)
Report an error message using Foam::FatalIOError.
Definition: error.H:408
Foam::FatalIOError
IOerror FatalIOError
Foam::error::throwing
bool throwing() const
Return the current exception throwing (on or off)
Definition: error.H:138
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
NotImplemented
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:436
Foam::IOerror::SafeFatalIOError
static void SafeFatalIOError(const char *functionName, const char *sourceFileName, const int sourceFileLineNumber, const IOstream &ioStream, const string &msg)
Print basic message and exit.
Definition: IOerror.C:124
Foam::IOerror::ioFileName
const string & ioFileName() const
Definition: error.H:263
FatalIOErrorInLookup
#define FatalIOErrorInLookup(ios, lookupTag, lookupName, lookupTable)
Report an error message using Foam::FatalIOError.
Definition: error.H:397
Foam::error::error
error(const string &title)
Construct from title string.
Definition: error.C:75
Foam::error::throwExceptions_
bool throwExceptions_
Definition: error.H:82
notImplemented
#define notImplemented(functionName)
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:426
FatalIOErrorIn
#define FatalIOErrorIn(functionName, ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:386
Foam::OSstream
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:54
Foam::IOerror::~IOerror
virtual ~IOerror() noexcept
Destructor.
Definition: IOerror.C:58
dict
dictionary dict
Definition: searchingEngine.H:14
SafeFatalIOErrorInFunction
#define SafeFatalIOErrorInFunction(ios, msg)
Report an error message using Foam::FatalIOError.
Definition: error.H:416
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
FatalErrorInLookup
#define FatalErrorInLookup(lookupTag, lookupName, lookupTable)
Report an error message using Foam::FatalError.
Definition: error.H:376
Foam::error::message
string message() const
The accumulated error message.
Definition: error.C:214
Foam::error::sourceFileLineNumber_
label sourceFileLineNumber_
Definition: error.H:81
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::error::exit
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: error.C:298
Foam::foamVersion::version
const std::string version
OpenFOAM version (name or stringified number) as a std::string.
messageStream.H
Foam::error::operator()
OSstream & operator()()
Explicit convert to OSstream for << operations.
Definition: error.H:192
Foam::error::~error
virtual ~error() noexcept
Destructor.
Definition: error.C:129
Foam::error::functionName_
string functionName_
Definition: error.H:79
Foam::error::sourceFileLineNumber
label sourceFileLineNumber() const
Definition: error.H:132
Foam::IOerror::exit
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:247
Foam::error::warnAboutAge
static void warnAboutAge(const char *what, const int version)
Emit warning on stderr about something being old.
Definition: error.C:40
Foam::error::abort
void abort()
Abort : used to stop code for fatal errors.
Definition: error.C:304
Foam::IOerror::write
void write(Ostream &os, const bool includeTitle=true) const
Print error message.
Definition: IOerror.C:259
Foam::OStringStream
Output to string buffer, using a OSstream.
Definition: StringStream.H:196
Foam::error::messageStreamPtr_
OStringStream * messageStreamPtr_
Definition: error.H:83
Foam::error::clear
void clear() const
Clear any messages.
Definition: error.C:220
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:265
Foam::messageStream::title
const string & title() const
The title of this error type.
Definition: messageStream.H:127
errorManip.H
Foam::IOerror
Report an I/O error.
Definition: error.H:229
FatalErrorIn
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:367
Foam::error::write
void write(Ostream &os, const bool includeTitle=true) const
Print error message.
Definition: error.C:310
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::error
Class to handle errors and exceptions in a simple, consistent stream-based manner.
Definition: error.H:64
Foam::error::safePrintStack
static void safePrintStack(std::ostream &os)
Definition: dummyPrintStack.C:32