error.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2021 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::error
26 
27 Description
28  Class to handle errors and exceptions in a simple, consistent stream-based
29  manner.
30 
31  The error class is globally instantiated with a title string. Errors,
32  messages and other data are piped to the messageStream class in the
33  standard manner. Manipulators are supplied for exit and abort which may
34  terminate the program or throw an exception depending on whether the
35  exception handling has been switched on (off by default).
36 
37 Usage
38  \code
39  error << "message1" << "message2" << FoamDataType << exit(errNo);
40  error << "message1" << "message2" << FoamDataType << abort();
41  \endcode
42 
43 SourceFiles
44  error.C
45 
46 \*---------------------------------------------------------------------------*/
47 
48 #ifndef error_H
49 #define error_H
50 
51 #include "messageStream.H"
52 
53 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
54 
55 namespace Foam
56 {
57 
58 // Forward declaration of friend functions and operators
59 class error;
60 Ostream& operator<<(Ostream&, const error&);
61 
62 
63 /*---------------------------------------------------------------------------*\
64  Class error Declaration
65 \*---------------------------------------------------------------------------*/
66 
67 class error
68 :
69  public std::exception,
70  public messageStream
71 {
72 
73 protected:
74 
75  // Protected data
76 
77  string functionName_;
80 
81  bool abort_;
82 
85 
86 public:
87 
88  // Constructors
89 
90  //- Construct from title string
91  error(const string& title);
92 
93  //- Construct from dictionary
94  error(const dictionary&);
95 
96  //- Copy constructor
97  error(const error&);
98 
99 
100  //- Destructor
101  virtual ~error() throw();
102 
103 
104  // Member Functions
105 
106  string message() const;
108  const string& functionName() const
109  {
110  return functionName_;
111  }
113  const string& sourceFileName() const
114  {
115  return sourceFileName_;
116  }
119  {
120  return sourceFileLineNumber_;
121  }
123  void throwExceptions()
124  {
125  throwExceptions_ = true;
126  }
128  void dontThrowExceptions()
129  {
130  throwExceptions_ = false;
131  }
132 
133  //- Convert to OSstream
134  // Prints basic message and returns OSstream for further info.
135  OSstream& operator()
136  (
137  const char* functionName,
138  const char* sourceFileName,
139  const int sourceFileLineNumber = 0
140  );
141 
142  //- Convert to OSstream
143  // Prints basic message and returns OSstream for further info.
144  OSstream& operator()
145  (
146  const string& functionName,
147  const char* sourceFileName,
148  const int sourceFileLineNumber = 0
149  );
150 
151  //- Convert to OSstream
152  // Prints basic message and returns OSstream for further info.
153  operator OSstream&();
154 
155  //- Explicitly convert to OSstream for << operations
157  {
158  return operator OSstream&();
159  }
160 
161  //- Create and return a dictionary
162  operator dictionary() const;
163 
164 
165  //- Helper function to print a stack (if OpenFOAM IO not yet
166  // initialised)
167  static void safePrintStack(std::ostream&);
168 
169  //- Helper function to print a stack
170  static void printStack(Ostream&);
171 
172  //- Exit : can be called for any error to exit program.
173  // Prints stack before exiting.
174  void exit(const int errNo = 1);
175 
176  //- Abort : used to stop code for fatal errors.
177  // Prints stack before exiting.
178  void abort();
179 
180 
181  // Ostream operator
182 
183  friend Ostream& operator<<(Ostream&, const error&);
184 };
185 
186 
187 // Forward declaration of friend functions and operators
188 class IOerror;
189 Ostream& operator<<(Ostream&, const IOerror&);
190 
191 
192 /*---------------------------------------------------------------------------*\
193  Class IOerror Declaration
194 \*---------------------------------------------------------------------------*/
195 
196 //- Report an I/O error
197 class IOerror
198 :
199  public error
200 {
201  // Private Data
202 
203  string ioFileName_;
204  label ioStartLineNumber_;
205  label ioEndLineNumber_;
206 
207 
208 public:
209 
210  // Constructors
211 
212  //- Construct from title string
213  IOerror(const string& title);
214 
215  //- Construct from dictionary
216  IOerror(const dictionary&);
217 
218 
219  //- Destructor
220  virtual ~IOerror() throw();
221 
222 
223  // Member Functions
225  const string& ioFileName() const
226  {
227  return ioFileName_;
228  }
230  label ioStartLineNumber() const
231  {
232  return ioStartLineNumber_;
233  }
235  label ioEndLineNumber() const
236  {
237  return ioEndLineNumber_;
238  }
239 
240  //- Convert to OSstream
241  // Prints basic message and returns OSstream for further info.
242  OSstream& operator()
243  (
244  const char* functionName,
245  const char* sourceFileName,
246  const int sourceFileLineNumber,
247  const string& ioFileName,
248  const label ioStartLineNumber = -1,
249  const label ioEndLineNumber = -1
250  );
251 
252  //- Convert to OSstream
253  // Prints basic message and returns OSstream for further info.
254  OSstream& operator()
255  (
256  const char* functionName,
257  const char* sourceFileName,
258  const int sourceFileLineNumber,
259  const IOstream&
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 dictionary&
270  );
271 
272  //- Print basic message and exit. Uses cerr if streams not constructed
273  // yet (at startup). Use in startup parsing instead of FatalError.
274  static void SafeFatalIOError
275  (
276  const char* functionName,
277  const char* sourceFileName,
278  const int sourceFileLineNumber,
279  const IOstream&,
280  const string& msg
281  );
282 
283  //- Create and return a dictionary
284  operator dictionary() const;
285 
286 
287  //- Exit : can be called for any error to exit program
288  void exit(const int errNo = 1);
289 
290  //- Abort : used to stop code for fatal errors
291  void abort();
292 
293 
294  // Ostream operator
295 
296  friend Ostream& operator<<(Ostream&, const IOerror&);
297 };
298 
299 
300 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
301 // Global error declarations: defined in error.C
302 
303 extern error FatalError;
304 extern IOerror FatalIOError;
305 
306 // Template argument dependent "false" for static_assert in templated functions
307 template <class... T>
308 constexpr bool False = false;
309 
310 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
311 
312 } // End namespace Foam
313 
314 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
315 // Convenience macros to add the file name and line number to the function name
316 
317 //- Report an error message using Foam::FatalError
318 // for functionName in file __FILE__ at line __LINE__
319 #define FatalErrorIn(functionName) \
320  ::Foam::FatalError((functionName), __FILE__, __LINE__)
321 
322 //- Report an error message using Foam::FatalError
323 // for FUNCTION_NAME in file __FILE__ at line __LINE__
324 #define FatalErrorInFunction FatalErrorIn(FUNCTION_NAME)
325 
326 
327 //- Report an error message using Foam::FatalIOError
328 // for functionName in file __FILE__ at line __LINE__
329 // for a particular IOstream
330 #define FatalIOErrorIn(functionName, ios) \
331  ::Foam::FatalIOError((functionName), __FILE__, __LINE__, (ios))
332 
333 //- Report an error message using Foam::FatalIOError
334 // for FUNCTION_NAME in file __FILE__ at line __LINE__
335 // for a particular IOstream
336 #define FatalIOErrorInFunction(ios) FatalIOErrorIn(FUNCTION_NAME, ios)
337 
338 
339 //- Report an error message using Foam::FatalIOError
340 // (or cerr if FatalIOError not yet constructed)
341 // for functionName in file __FILE__ at line __LINE__
342 // for a particular IOstream
343 #define SafeFatalIOErrorIn(functionName, ios, msg) \
344  ::Foam::IOerror::SafeFatalIOError \
345  ((functionName), __FILE__, __LINE__, (ios), (msg))
346 
347 //- Report an error message using Foam::FatalIOError
348 // (or cerr if FatalIOError not yet constructed)
349 // for functionName in file __FILE__ at line __LINE__
350 // for a particular IOstream
351 #define SafeFatalIOErrorInFunction(ios, msg) \
352  SafeFatalIOErrorIn(FUNCTION_NAME, ios, msg)
353 
354 
355 //- Issue a FatalErrorIn for a function not currently implemented.
356 // The functionName is printed and then abort is called.
357 //
358 // This macro can be particularly useful when methods must be defined to
359 // complete the interface of a derived class even if they should never be
360 // called for this derived class.
361 #define notImplemented(functionName) \
362  FatalErrorIn(functionName) \
363  << "Not implemented" << ::Foam::abort(FatalError);
364 
365 //- Issue a FatalErrorIn for a function not currently implemented.
366 // The FUNCTION_NAME is printed and then abort is called.
367 //
368 // This macro can be particularly useful when methods must be defined to
369 // complete the interface of a derived class even if they should never be
370 // called for this derived class.
371 #define NotImplemented notImplemented(FUNCTION_NAME)
372 
373 
374 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
375 
376 #include "errorManip.H"
377 
378 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
379 
380 #endif
381 
382 // ************************************************************************* //
static void printStack(Ostream &)
Helper function to print a stack.
Generic output stream.
Definition: OSstream.H:51
virtual ~error()
Destructor.
Definition: error.C:94
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:59
error FatalError
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
bool abort_
Definition: error.H:80
const string & title() const
Return the title of this error type.
static void safePrintStack(std::ostream &)
Helper function to print a stack (if OpenFOAM IO not yet.
Definition: printStack.C:192
string sourceFileName_
Definition: error.H:77
Class to handle messaging in a simple, consistent stream-based manner.
Definition: messageStream.H:68
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: error.C:168
error(const string &title)
Construct from title string.
Definition: error.C:36
Class to handle errors and exceptions in a simple, consistent stream-based manner.
Definition: error.H:66
label sourceFileLineNumber_
Definition: error.H:78
bool throwExceptions_
Definition: error.H:82
void throwExceptions()
Definition: error.H:122
A functionName is a word starting with &#39;#&#39;.
Definition: functionName.H:57
string message() const
Definition: error.C:162
void abort()
Abort : used to stop code for fatal errors.
Definition: error.C:209
const string & functionName() const
Definition: error.H:107
Report an I/O error.
Definition: error.H:196
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
OSstream & operator()()
Explicitly convert to OSstream for << operations.
Definition: error.H:155
string functionName_
Definition: error.H:76
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
friend Ostream & operator<<(Ostream &, const error &)
An IOstream is an abstract base class for all input/output systems; be they streams, files, token lists etc.
Definition: IOstream.H:71
const string & sourceFileName() const
Definition: error.H:112
Ostream & operator<<(Ostream &, const ensightPart &)
OStringStream * messageStreamPtr_
Definition: error.H:83
Output to memory buffer stream.
Definition: OStringStream.H:49
constexpr bool False
Definition: error.H:307
Namespace for OpenFOAM.
void dontThrowExceptions()
Definition: error.H:127
IOerror FatalIOError
label sourceFileLineNumber() const
Definition: error.H:117