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-2019 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 
71 protected:
72 
73  // Protected data
74 
75  string functionName_;
78 
81 
82 
83 public:
84 
85  // Constructors
86 
87  //- Construct from title string
88  explicit error(const string& title);
89 
90  //- Construct from dictionary
91  explicit error(const dictionary& errDict);
92 
93  //- Construct as copy
94  error(const error& err);
95 
96 
97  //- Destructor
98  virtual ~error() throw();
99 
100 
101  // Static Member Functions
102 
103  //- Emit warning on stderr about something being old.
104  // \param what description for the warning
105  // \param version is the old version (YYMM) for determining the
106  // age in months compared to the current OpenFOAM version
107  // as conveyed by the \c foamVersion::api value.
108  static void warnAboutAge(const char* what, const int version);
109 
110 
111  // Member Functions
112 
113  string message() const;
114 
115  //- Clear any messages
116  void clear() const;
117 
118  inline const string& functionName() const
119  {
120  return functionName_;
121  }
122 
123  inline const string& sourceFileName() const
124  {
125  return sourceFileName_;
126  }
127 
128  inline label sourceFileLineNumber() const
129  {
130  return sourceFileLineNumber_;
131  }
132 
133  //- Return the current exception throwing (on or off)
134  inline bool throwing() const
135  {
136  return throwExceptions_;
137  }
138 
139  //- Activate/deactivate exception throwing
140  // \return the previous throwing state
141  inline bool throwExceptions(bool doThrow)
142  {
143  const bool prev = throwExceptions_;
144  throwExceptions_ = doThrow;
145  return prev;
146  }
147 
148  //- Activate exception throwing
149  // \return the previous throwing state
150  inline bool throwExceptions()
151  {
152  return throwExceptions(true);
153  }
154 
155  //- Deactivate exception throwing
156  // \return the previous throwing state
157  inline bool dontThrowExceptions()
158  {
159  return throwExceptions(false);
160  }
161 
162  //- Convert to OSstream
163  // Prints basic message and returns OSstream for further info.
164  OSstream& operator()
165  (
166  const char* functionName,
167  const char* sourceFileName,
168  const int sourceFileLineNumber = 0
169  );
170 
171  //- Convert to OSstream
172  // Prints basic message and returns OSstream for further info.
173  OSstream& operator()
174  (
175  const string& functionName,
176  const char* sourceFileName,
177  const int sourceFileLineNumber = 0
178  );
179 
180  //- Convert to OSstream
181  // Prints basic message and returns OSstream for further info.
182  operator OSstream&();
183 
184  //- Explicitly convert to OSstream for << operations
186  {
187  return operator OSstream&();
188  }
189 
190  //- Create and return a dictionary representation of the error
191  operator dictionary() const;
192 
193 
194  //- Helper function to print a stack,
195  //- used when OpenFOAM IO not yet initialised.
196  static void safePrintStack(std::ostream& os);
197 
198  //- Helper function to print a stack
199  static void printStack(Ostream& os);
200 
201  //- Exit : can be called for any error to exit program.
202  // Prints stack before exiting, when FOAM_ABORT is on.
203  void exit(const int errNo = 1);
204 
205  //- Abort : used to stop code for fatal errors.
206  // Prints stack before exiting.
207  void abort();
208 
209  //- Print error message
210  void write(Ostream& os, const bool includeTitle = true) const;
211 };
212 
213 
214 /*---------------------------------------------------------------------------*\
215  Class IOerror Declaration
216 \*---------------------------------------------------------------------------*/
217 
218 //- Report an I/O error
219 class IOerror
220 :
221  public error
222 {
223  // Private data
224 
225  string ioFileName_;
226  label ioStartLineNumber_;
227  label ioEndLineNumber_;
228 
229 
230 public:
231 
232  // Constructors
233 
234  //- Construct from title string
235  IOerror(const string& title);
236 
237  //- Construct from dictionary
238  IOerror(const dictionary& errDict);
239 
240 
241  //- Destructor
242  virtual ~IOerror() throw();
243 
244 
245  // Member functions
246 
247  inline const string& ioFileName() const
248  {
249  return ioFileName_;
250  }
251 
252  inline label ioStartLineNumber() const
253  {
254  return ioStartLineNumber_;
255  }
256 
257  inline label ioEndLineNumber() const
258  {
259  return ioEndLineNumber_;
260  }
261 
262  //- Convert to OSstream
263  // Prints basic message and returns OSstream for further info.
264  OSstream& operator()
265  (
266  const char* functionName,
267  const char* sourceFileName,
268  const int sourceFileLineNumber,
269  const string& ioFileName,
270  const label ioStartLineNumber = -1,
271  const label ioEndLineNumber = -1
272  );
273 
274  //- Convert to OSstream
275  // Prints basic message and returns OSstream for further info.
276  OSstream& operator()
277  (
278  const char* functionName,
279  const char* sourceFileName,
280  const int sourceFileLineNumber,
281  const IOstream& ioStream
282  );
283 
284  //- Convert to OSstream
285  // Prints basic message and returns OSstream for further info.
286  OSstream& operator()
287  (
288  const char* functionName,
289  const char* sourceFileName,
290  const int sourceFileLineNumber,
291  const dictionary& dict
292  );
293 
294  //- Print basic message and exit.
295  // Uses cerr if streams not yet constructed (at startup).
296  // Use in startup parsing instead of FatalError.
297  static void SafeFatalIOError
298  (
299  const char* functionName,
300  const char* sourceFileName,
301  const int sourceFileLineNumber,
302  const IOstream& ioStream,
303  const string& msg
304  );
305 
306  //- Create and return a dictionary representation of the error
307  operator dictionary() const;
308 
309 
310  //- Exit : can be called for any error to exit program
311  void exit(const int errNo = 1);
312 
313  //- Abort : used to stop code for fatal errors
314  void abort();
315 
316  //- Print error message
317  void write(Ostream& os, const bool includeTitle = true) const;
318 };
319 
320 
321 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
322 
323 //- Ostream operator
324 Ostream& operator<<(Ostream& os, const error& err);
325 
326 //- Ostream operator
327 Ostream& operator<<(Ostream& os, const IOerror& err);
328 
329 
330 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
331 // Global error declarations: defined in error.C
332 
333 //- Error stream (uses stdout - output on all processes),
334 //- with additional 'FOAM FATAL ERROR' header text and stack trace.
335 extern error FatalError;
336 
337 //- Error stream (uses stdout - output on all processes),
338 //- with additional 'FOAM FATAL IO ERROR' header text and stack trace.
339 extern IOerror FatalIOError;
340 
341 
342 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
343 
344 } // End namespace Foam
345 
346 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
347 // Convenience macros to add the file name and line number to the function name
348 
349 //- Report an error message using Foam::FatalError
350 // for functionName in file __FILE__ at line __LINE__
351 #define FatalErrorIn(functionName) \
352  ::Foam::FatalError((functionName), __FILE__, __LINE__)
353 
354 //- Report an error message using Foam::FatalError
355 // for FUNCTION_NAME in file __FILE__ at line __LINE__
356 #define FatalErrorInFunction FatalErrorIn(FUNCTION_NAME)
357 
358 //- Report an error message using Foam::FatalError
359 // about unknown lookup type in table
360 #define FatalErrorInLookup(lookupTag, lookupName, lookupTable) \
361  ::Foam::FatalError(FUNCTION_NAME, __FILE__, __LINE__) \
362  << "Unknown " << (lookupTag) << " type " << (lookupName) \
363  << "\n\nValid " << (lookupTag) << " types :\n" \
364  << ((lookupTable).sortedToc()) << '\n'
365 
366 
367 //- Report an error message using Foam::FatalIOError
368 // for functionName in file __FILE__ at line __LINE__
369 // for a particular IOstream
370 #define FatalIOErrorIn(functionName, ios) \
371  ::Foam::FatalIOError((functionName), __FILE__, __LINE__, (ios))
372 
373 //- Report an error message using Foam::FatalIOError
374 // for FUNCTION_NAME in file __FILE__ at line __LINE__
375 // for a particular IOstream
376 #define FatalIOErrorInFunction(ios) FatalIOErrorIn(FUNCTION_NAME, ios)
377 
378 
379 //- Report an error message using Foam::FatalIOError
380 // about unknown lookup type in table
381 #define FatalIOErrorInLookup(ios, lookupTag, lookupName, lookupTable) \
382  ::Foam::FatalIOError(FUNCTION_NAME, __FILE__, __LINE__, (ios)) \
383  << "Unknown " << (lookupTag) << " type " << (lookupName) \
384  << "\n\nValid " << (lookupTag) << " types :\n" \
385  << ((lookupTable).sortedToc()) << '\n'
386 
387 
388 //- Report an error message using Foam::FatalIOError
389 // (or cerr if FatalIOError not yet constructed)
390 // for functionName in file __FILE__ at line __LINE__
391 // for a particular IOstream
392 #define SafeFatalIOErrorIn(functionName, ios, msg) \
393  ::Foam::IOerror::SafeFatalIOError \
394  ((functionName), __FILE__, __LINE__, (ios), (msg))
395 
396 //- Report an error message using Foam::FatalIOError
397 // (or cerr if FatalIOError not yet constructed)
398 // for functionName in file __FILE__ at line __LINE__
399 // for a particular IOstream
400 #define SafeFatalIOErrorInFunction(ios, msg) \
401  SafeFatalIOErrorIn(FUNCTION_NAME, ios, msg)
402 
403 
404 //- Issue a FatalErrorIn for a function not currently implemented.
405 // The functionName is printed and then abort is called.
406 //
407 // This macro can be particularly useful when methods must be defined to
408 // complete the interface of a derived class even if they should never be
409 // called for this derived class.
410 #define notImplemented(functionName) \
411  FatalErrorIn(functionName) \
412  << "Not implemented" << ::Foam::abort(FatalError);
413 
414 //- Issue a FatalErrorIn for a function not currently implemented.
415 // The FUNCTION_NAME is printed and then abort is called.
416 //
417 // This macro can be particularly useful when methods must be defined to
418 // complete the interface of a derived class even if they should never be
419 // called for this derived class.
420 #define NotImplemented notImplemented(FUNCTION_NAME)
421 
422 
423 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
424 
425 #include "errorManip.H"
426 
427 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
428 
429 #endif
430 
431 // ************************************************************************* //
Foam::IOerror::ioEndLineNumber
label ioEndLineNumber() const
Definition: error.H:256
Foam::error::sourceFileName
const string & sourceFileName() const
Definition: error.H:122
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:156
Foam::IOerror::abort
void abort()
Abort : used to stop code for fatal errors.
Definition: IOerror.C:212
Foam::error::throwExceptions
bool throwExceptions()
Activate exception throwing.
Definition: error.H:149
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:75
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:251
Foam::error::functionName
const string & functionName() const
Definition: error.H:117
SafeFatalIOErrorIn
#define SafeFatalIOErrorIn(functionName, ios, msg)
Report an error message using Foam::FatalIOError.
Definition: error.H:391
Foam::FatalIOError
IOerror FatalIOError
Foam::error::throwing
bool throwing() const
Return the current exception throwing (on or off)
Definition: error.H:133
NotImplemented
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:419
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:246
FatalIOErrorInLookup
#define FatalIOErrorInLookup(ios, lookupTag, lookupName, lookupTable)
Report an error message using Foam::FatalIOError.
Definition: error.H:380
Foam::error::error
error(const string &title)
Construct from title string.
Definition: error.C:75
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::error::throwExceptions_
bool throwExceptions_
Definition: error.H:78
Foam::IOerror::~IOerror
virtual ~IOerror()
Destructor.
Definition: IOerror.C:58
notImplemented
#define notImplemented(functionName)
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:409
FatalIOErrorIn
#define FatalIOErrorIn(functionName, ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:369
Foam::OSstream
Generic output stream.
Definition: OSstream.H:54
dict
dictionary dict
Definition: searchingEngine.H:14
SafeFatalIOErrorInFunction
#define SafeFatalIOErrorInFunction(ios, msg)
Report an error message using Foam::FatalIOError.
Definition: error.H:399
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:359
Foam::error::message
string message() const
Definition: error.C:204
Foam::error::sourceFileLineNumber_
label sourceFileLineNumber_
Definition: error.H:76
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:216
Foam::foamVersion::version
const std::string version
OpenFOAM version (name or stringified number) as a std::string.
messageStream.H
Foam::error::operator()
OSstream & operator()()
Explicitly convert to OSstream for << operations.
Definition: error.H:184
Foam::error::functionName_
string functionName_
Definition: error.H:74
Foam::error::sourceFileLineNumber
label sourceFileLineNumber() const
Definition: error.H:127
Foam::error::~error
virtual ~error()
Destructor.
Definition: error.C:132
Foam::IOerror::exit
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:175
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:253
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:189
Foam::error::messageStreamPtr_
OStringStream * messageStreamPtr_
Definition: error.H:79
Foam::error::clear
void clear() const
Clear any messages.
Definition: error.C:210
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:261
Foam::messageStream::title
const string & title() const
Return the title of this error type.
Definition: messageStream.H:129
errorManip.H
Foam::IOerror
Report an I/O error.
Definition: error.H:218
FatalErrorIn
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:350
Foam::error::write
void write(Ostream &os, const bool includeTitle=true) const
Print error message.
Definition: error.C:300
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::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &)
Definition: boundaryPatch.C:102
Foam::error::safePrintStack
static void safePrintStack(std::ostream &os)
Definition: dummyPrintStack.C:32