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 | Copyright (C) 2011-2015 OpenFOAM Foundation
6  \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd.
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 
83 
84 
85 public:
86 
87  // Constructors
88 
89  //- Construct from title string
90  error(const string& title);
91 
92  //- Construct from dictionary
93  error(const dictionary&);
94 
95  //- Construct as copy
96  error(const error&);
97 
98 
99  //- Destructor
100  virtual ~error() throw();
101 
102 
103  // Member functions
104 
105  string message() const;
106 
107  const string& functionName() const
108  {
109  return functionName_;
110  }
111 
112  const string& sourceFileName() const
113  {
114  return sourceFileName_;
115  }
116 
118  {
119  return sourceFileLineNumber_;
120  }
121 
122  void throwExceptions()
123  {
124  throwExceptions_ = true;
125  }
126 
127  void dontThrowExceptions()
128  {
129  throwExceptions_ = false;
130  }
131 
132  //- Convert to OSstream
133  // Prints basic message and returns OSstream for further info.
134  OSstream& operator()
135  (
136  const char* functionName,
137  const char* sourceFileName,
138  const int sourceFileLineNumber = 0
139  );
140 
141  //- Convert to OSstream
142  // Prints basic message and returns OSstream for further info.
143  OSstream& operator()
144  (
145  const string& functionName,
146  const char* sourceFileName,
147  const int sourceFileLineNumber = 0
148  );
149 
150  //- Convert to OSstream
151  // Prints basic message and returns OSstream for further info.
152  operator OSstream&();
153 
154  //- Explicitly convert to OSstream for << operations
156  {
157  return operator OSstream&();
158  }
159 
160  //- Create and return a dictionary
161  operator dictionary() const;
162 
163 
164  //- Helper function to print a stack (if OpenFOAM IO not yet
165  // initialised)
166  static void safePrintStack(std::ostream&);
167 
168  //- Helper function to print a stack
169  static void printStack(Ostream&);
170 
171  //- Exit : can be called for any error to exit program.
172  // Prints stack before exiting.
173  void exit(const int errNo = 1);
174 
175  //- Abort : used to stop code for fatal errors.
176  // Prints stack before exiting.
177  void abort();
178 
179 
180  // Ostream operator
181 
182  friend Ostream& operator<<(Ostream&, const error&);
183 };
184 
185 
186 // Forward declaration of friend functions and operators
187 class IOerror;
188 Ostream& operator<<(Ostream&, const IOerror&);
189 
190 
191 /*---------------------------------------------------------------------------*\
192  Class IOerror Declaration
193 \*---------------------------------------------------------------------------*/
194 
195 //- Report an I/O error
196 class IOerror
197 :
198  public error
199 {
200  // Private data
201 
202  string ioFileName_;
205 
206 
207 public:
208 
209  // Constructors
210 
211  //- Construct from title string
212  IOerror(const string& title);
213 
214  //- Construct from dictionary
215  IOerror(const dictionary&);
216 
217 
218  //- Destructor
219  virtual ~IOerror() throw();
220 
221 
222  // Member functions
223 
224  const string& ioFileName() const
225  {
226  return ioFileName_;
227  }
228 
229  label ioStartLineNumber() const
230  {
231  return ioStartLineNumber_;
232  }
233 
234  label ioEndLineNumber() const
235  {
236  return ioEndLineNumber_;
237  }
238 
239  //- Convert to OSstream
240  // Prints basic message and returns OSstream for further info.
241  OSstream& operator()
242  (
243  const char* functionName,
244  const char* sourceFileName,
245  const int sourceFileLineNumber,
246  const string& ioFileName,
247  const label ioStartLineNumber = -1,
248  const label ioEndLineNumber = -1
249  );
250 
251  //- Convert to OSstream
252  // Prints basic message and returns OSstream for further info.
253  OSstream& operator()
254  (
255  const char* functionName,
256  const char* sourceFileName,
257  const int sourceFileLineNumber,
258  const IOstream&
259  );
260 
261  //- Convert to OSstream
262  // Prints basic message and returns OSstream for further info.
263  OSstream& operator()
264  (
265  const char* functionName,
266  const char* sourceFileName,
267  const int sourceFileLineNumber,
268  const dictionary&
269  );
270 
271  //- Print basic message and exit. Uses cerr if streams not constructed
272  // yet (at startup). Use in startup parsing instead of FatalError.
273  static void SafeFatalIOError
274  (
275  const char* functionName,
276  const char* sourceFileName,
277  const int sourceFileLineNumber,
278  const IOstream&,
279  const string& msg
280  );
281 
282  //- Create and return a dictionary
283  operator dictionary() const;
284 
285 
286  //- Exit : can be called for any error to exit program
287  void exit(const int errNo = 1);
288 
289  //- Abort : used to stop code for fatal errors
290  void abort();
291 
292 
293  // Ostream operator
294 
295  friend Ostream& operator<<(Ostream&, const IOerror&);
296 };
297 
298 
299 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
300 // Global error declarations: defined in error.C
301 
302 extern error FatalError;
303 extern IOerror FatalIOError;
304 
305 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
306 
307 } // End namespace Foam
308 
309 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
310 // Convenience macros to add the file name and line number to the function name
311 
312 //- Report an error message using Foam::FatalError
313 // for functionName in file __FILE__ at line __LINE__
314 #define FatalErrorIn(functionName) \
315  ::Foam::FatalError((functionName), __FILE__, __LINE__)
316 
317 //- Report an error message using Foam::FatalError
318 // for FUNCTION_NAME in file __FILE__ at line __LINE__
319 #define FatalErrorInFunction FatalErrorIn(FUNCTION_NAME)
320 
321 
322 //- Report an error message using Foam::FatalIOError
323 // for functionName in file __FILE__ at line __LINE__
324 // for a particular IOstream
325 #define FatalIOErrorIn(functionName, ios) \
326  ::Foam::FatalIOError((functionName), __FILE__, __LINE__, (ios))
327 
328 //- Report an error message using Foam::FatalIOError
329 // for FUNCTION_NAME in file __FILE__ at line __LINE__
330 // for a particular IOstream
331 #define FatalIOErrorInFunction(ios) FatalIOErrorIn(FUNCTION_NAME, ios)
332 
333 
334 //- Report an error message using Foam::FatalIOError
335 // (or cerr if FatalIOError not yet constructed)
336 // for functionName in file __FILE__ at line __LINE__
337 // for a particular IOstream
338 #define SafeFatalIOErrorIn(functionName, ios, msg) \
339  ::Foam::IOerror::SafeFatalIOError \
340  ((functionName), __FILE__, __LINE__, (ios), (msg))
341 
342 //- Report an error message using Foam::FatalIOError
343 // (or cerr if FatalIOError not yet constructed)
344 // for functionName in file __FILE__ at line __LINE__
345 // for a particular IOstream
346 #define SafeFatalIOErrorInFunction(ios, msg) \
347  SafeFatalIOErrorIn(FUNCTION_NAME, ios, msg)
348 
349 
350 //- Issue a FatalErrorIn for a function not currently implemented.
351 // The functionName is printed and then abort is called.
352 //
353 // This macro can be particularly useful when methods must be defined to
354 // complete the interface of a derived class even if they should never be
355 // called for this derived class.
356 #define notImplemented(functionName) \
357  FatalErrorIn(functionName) \
358  << "Not implemented" << ::Foam::abort(FatalError);
359 
360 //- Issue a FatalErrorIn for a function not currently implemented.
361 // The FUNCTION_NAME is printed and then abort is called.
362 //
363 // This macro can be particularly useful when methods must be defined to
364 // complete the interface of a derived class even if they should never be
365 // called for this derived class.
366 #define NotImplemented notImplemented(FUNCTION_NAME)
367 
368 
369 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
370 
371 #include "errorManip.H"
372 
373 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
374 
375 #endif
376 
377 // ************************************************************************* //
Foam::error::dontThrowExceptions
void dontThrowExceptions()
Definition: error.H:126
Foam::IOerror::ioEndLineNumber
label ioEndLineNumber() const
Definition: error.H:233
Foam::error::sourceFileName
const string & sourceFileName() const
Definition: error.H:111
Foam::IOerror::operator<<
friend Ostream & operator<<(Ostream &, const IOerror &)
Foam::IOerror::abort
void abort()
Abort : used to stop code for fatal errors.
Definition: IOerror.C:209
Foam::messageStream
Class to handle messaging in a simple, consistent stream-based manner.
Definition: messageStream.H:68
Foam::error::sourceFileName_
string sourceFileName_
Definition: error.H:77
Foam::IOerror::IOerror
IOerror(const string &title)
Construct from title string.
Definition: IOerror.C:36
Foam::IOstream
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:71
Foam::IOerror::ioStartLineNumber
label ioStartLineNumber() const
Definition: error.H:228
Foam::error::functionName
const string & functionName() const
Definition: error.H:106
SafeFatalIOErrorIn
#define SafeFatalIOErrorIn(functionName, ios, msg)
Report an error message using Foam::FatalIOError.
Definition: error.H:337
Foam::IOerror::ioFileName_
string ioFileName_
Definition: error.H:201
Foam::FatalIOError
IOerror FatalIOError
NotImplemented
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:365
Foam::IOerror::ioFileName
const string & ioFileName() const
Definition: error.H:223
Foam::error::printStack
static void printStack(Ostream &)
Helper function to print a stack.
Definition: dummyPrintStack.C:30
Foam::error::error
error(const string &title)
Construct from title string.
Definition: error.C:36
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:59
Foam::error::throwExceptions_
bool throwExceptions_
Definition: error.H:80
Foam::IOerror::~IOerror
virtual ~IOerror()
Destructor.
Definition: IOerror.C:54
notImplemented
#define notImplemented(functionName)
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:355
FatalIOErrorIn
#define FatalIOErrorIn(functionName, ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:324
Foam::IOerror::SafeFatalIOError
static void SafeFatalIOError(const char *functionName, const char *sourceFileName, const int sourceFileLineNumber, const IOstream &, const string &msg)
Print basic message and exit. Uses cerr if streams not constructed.
Definition: IOerror.C:118
Foam::OSstream
Generic output stream.
Definition: OSstream.H:51
Foam::operator<<
Ostream & operator<<(Ostream &, const edgeMesh &)
Definition: edgeMeshIO.C:130
SafeFatalIOErrorInFunction
#define SafeFatalIOErrorInFunction(ios, msg)
Report an error message using Foam::FatalIOError.
Definition: error.H:345
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Foam::error::message
string message() const
Definition: error.C:159
Foam::error::safePrintStack
static void safePrintStack(std::ostream &)
Helper function to print a stack (if OpenFOAM IO not yet.
Definition: printStack.C:192
Foam::error::sourceFileLineNumber_
label sourceFileLineNumber_
Definition: error.H:78
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::error::exit
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: error.C:165
Foam::error::throwExceptions
void throwExceptions()
Definition: error.H:121
messageStream.H
Foam::error::operator()
OSstream & operator()()
Explicitly convert to OSstream for << operations.
Definition: error.H:154
Foam::error::functionName_
string functionName_
Definition: error.H:76
Foam::error::sourceFileLineNumber
label sourceFileLineNumber() const
Definition: error.H:116
Foam::error::~error
virtual ~error()
Destructor.
Definition: error.C:91
Foam::IOerror::exit
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:168
Foam::error::abort
void abort()
Abort : used to stop code for fatal errors.
Definition: error.C:206
Foam::OStringStream
Output to memory buffer stream.
Definition: OStringStream.H:49
Foam::error::messageStreamPtr_
OStringStream * messageStreamPtr_
Definition: error.H:81
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:226
Foam::messageStream::title
const string & title() const
Return the title of this error type.
Definition: messageStream.H:118
errorManip.H
Foam::IOerror
Report an I/O error.
Definition: error.H:195
FatalErrorIn
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:313
Foam::error::operator<<
friend Ostream & operator<<(Ostream &, const error &)
Foam::IOerror::ioStartLineNumber_
label ioStartLineNumber_
Definition: error.H:202
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::error
Class to handle errors and exceptions in a simple, consistent stream-based manner.
Definition: error.H:66
Foam::IOerror::ioEndLineNumber_
label ioEndLineNumber_
Definition: error.H:203