error.C
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-2025 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 \*---------------------------------------------------------------------------*/
25 
26 #include "error.H"
27 #include "OStringStream.H"
28 #include "fileName.H"
29 #include "dictionary.H"
30 #include "jobInfo.H"
31 #include "Pstream.H"
32 #include "OSspecific.H"
33 
34 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
35 
36 Foam::error::error(const string& title)
37 :
38  std::exception(),
39  messageStream(title, messageStream::FATAL),
40  functionName_("unknown"),
41  sourceFileName_("unknown"),
42  sourceFileLineNumber_(0),
43  abort_(env("FOAM_ABORT")),
44  throwExceptions_(false),
45  messageStream_()
46 {
47  if (!messageStream_.good())
48  {
49  Perr<< endl
50  << "error::error(const string& title) : cannot open error stream"
51  << endl;
52  exit(1);
53  }
54 }
55 
56 
57 Foam::OSstream& Foam::error::operator()
58 (
59  const char* functionName,
60  const char* sourceFileName,
61  const int sourceFileLineNumber
62 )
63 {
64  functionName_ = functionName;
65  sourceFileName_ = sourceFileName;
66  sourceFileLineNumber_ = sourceFileLineNumber;
67 
68  return operator OSstream&();
69 }
70 
71 
72 Foam::OSstream& Foam::error::operator()
73 (
74  const string& functionName,
75  const char* sourceFileName,
76  const int sourceFileLineNumber
77 )
78 {
79  return operator()
80  (
81  functionName.c_str(),
82  sourceFileName,
83  sourceFileLineNumber
84  );
85 }
86 
87 
89 {
90  if (!messageStream_.good())
91  {
92  Perr<< endl
93  << "error::operator OSstream&() : error stream has failed"
94  << endl;
95  abort();
96  }
97 
98  return messageStream_;
99 }
100 
101 
102 Foam::error::operator Foam::dictionary() const
103 {
104  dictionary errDict;
105 
106  string oneLineMessage(message());
107  oneLineMessage.replaceAll('\n', ' ');
108 
109  errDict.add("type", word("Foam::error"));
110  errDict.add("message", oneLineMessage);
111  errDict.add("function", functionName());
112  errDict.add("sourceFile", sourceFileName());
113  errDict.add("sourceFileLineNumber", sourceFileLineNumber());
114 
115  return errDict;
116 }
117 
118 
120 {
121  return messageStream_.str();
122 }
123 
124 
125 void Foam::error::exit(const int errNo)
126 {
127  if (error::level <= 0)
128  {
129  if (Pstream::parRun())
130  {
131  Pstream::exit(errNo);
132  }
133  else
134  {
135  ::exit(errNo);
136  }
137  }
138 
139  if (!throwExceptions_ && jobInfo::constructed)
140  {
141  jobInfo_.add("FatalError", operator dictionary());
142  jobInfo_.exit();
143  }
144 
145  if (abort_)
146  {
147  abort();
148  }
149 
150  if (Pstream::parRun())
151  {
152  Perr<< endl << *this << endl
153  << "\nFOAM parallel run exiting\n" << endl;
154  Pstream::exit(errNo);
155  }
156  else
157  {
158  if (throwExceptions_)
159  {
160  // Make a copy of the error to throw
161  error errorException(*this);
162 
163  // Rewind the message buffer for the next error message
164  messageStream_.rewind();
165 
166  throw errorException;
167  }
168  else
169  {
170  Perr<< endl << *this << endl
171  << "\nFOAM exiting\n" << endl;
172  ::exit(errNo);
173  }
174  }
175 }
176 
177 
179 {
180  if (!throwExceptions_ && jobInfo::constructed)
181  {
182  jobInfo_.add("FatalError", operator dictionary());
183  jobInfo_.abort();
184  }
185 
186  if (abort_)
187  {
188  Perr<< endl << *this << endl
189  << "\nFOAM aborting (FOAM_ABORT set)\n" << endl;
190  printStack(Perr);
191  ::abort();
192  }
193 
194  if (Pstream::parRun())
195  {
196  Perr<< endl << *this << endl
197  << "\nFOAM parallel run aborting\n" << endl;
198  printStack(Perr);
199  Pstream::abort();
200  }
201  else
202  {
203  if (throwExceptions_)
204  {
205  // Make a copy of the error to throw
206  error errorException(*this);
207 
208  // Rewind the message buffer for the next error message
209  messageStream_.rewind();
210 
211  throw errorException;
212  }
213  else
214  {
215  Perr<< endl << *this << endl
216  << "\nFOAM aborting\n" << endl;
217  printStack(Perr);
218  ::abort();
219  }
220  }
221 }
222 
223 
225 {
226  os << endl
227  << fErr.title().c_str() << endl
228  << fErr.message().c_str();
229 
230  if (error::level >= 2 && fErr.sourceFileLineNumber())
231  {
232  os << endl << endl
233  << " From function " << fErr.functionName().c_str() << endl
234  << " in file " << fErr.sourceFileName().c_str()
235  << " at line " << fErr.sourceFileLineNumber() << '.';
236  }
237 
238  return os;
239 }
240 
241 
242 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
243 // Global error definitions
244 
245 Foam::error Foam::FatalError("--> FOAM FATAL ERROR: ");
246 
247 // ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
Generic output stream.
Definition: OSstream.H:54
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
static void abort()
Abort program.
Definition: UPstream.C:52
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:399
static void exit(int errnum=1)
Exit program.
Definition: UPstream.C:46
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:1020
Class to handle errors and exceptions in a simple, consistent stream-based manner.
Definition: error.H:71
const string & sourceFileName() const
Definition: error.H:105
OStringStream messageStream_
Definition: error.H:85
const string & functionName() const
Definition: error.H:100
string message() const
Definition: error.C:119
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: error.C:125
error(const string &title)
Construct from title string.
Definition: error.C:36
OSstream & operator()()
Explicitly convert to OSstream for << operations.
Definition: error.C:88
void abort()
Abort : used to stop code for fatal errors.
Definition: error.C:178
label sourceFileLineNumber() const
Definition: error.H:110
A functionName is a word starting with '#'.
Definition: functionName.H:60
void exit()
Definition: jobInfo.C:198
static bool constructed
Definition: jobInfo.H:72
void abort()
Definition: jobInfo.C:204
Class to handle messaging in a simple, consistent stream-based manner.
Definition: messageStream.H:69
const string & title() const
Return the title of this error type.
A class for handling character strings derived from std::string.
Definition: string.H:79
A class for handling words, derived from string.
Definition: word.H:62
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
prefixOSstream Perr(cerr, "Perr")
Definition: IOstreams.H:54
bool env(const word &)
Return true if environment variable of given name is defined.
Definition: POSIX.C:91
jobInfo jobInfo_
Definition: jobInfo.C:44
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:258
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)
error FatalError