error.C
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-2014 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 \*---------------------------------------------------------------------------*/
28 
29 #include "error.H"
30 #include "StringStream.H"
31 #include "fileName.H"
32 #include "dictionary.H"
33 #include "JobInfo.H"
34 #include "Pstream.H"
35 #include "foamVersion.H"
36 #include "OSspecific.H"
37 
38 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
39 
40 void Foam::error::warnAboutAge(const char* what, const int version)
41 {
42  if (version <= 0)
43  {
44  // No warning for 0 (unversioned) or -ve values (silent versioning)
45  }
46  else if (version < 1000)
47  {
48  // Warning for things that predate the YYMM versioning
49  // (eg, 240 for version 2.4)
50  std::cerr
51  << " This " << what << " is considered to be VERY old!\n"
52  << std::endl;
53  }
54  else if (version < foamVersion::api)
55  {
56  const int months =
57  (
58  // YYMM -> months
59  (12 * (foamVersion::api/100) + (foamVersion::api % 100))
60  - (12 * (version/100) + (version % 100))
61  );
62 
63  std::cerr
64  << " This " << what << " is deemed to be " << months
65  << " months old.\n"
66  << std::endl;
67  }
68  // No warning for (foamVersion::api < version).
69  // We use this to denote future expiry dates of transition features.
70 }
71 
72 
73 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
74 
75 Foam::error::error(const string& title)
76 :
77  std::exception(),
78  messageStream(title, messageStream::FATAL),
79  functionName_("unknown"),
80  sourceFileName_("unknown"),
81  sourceFileLineNumber_(0),
82  throwExceptions_(false),
83  messageStreamPtr_(new OStringStream())
84 {
85  if (!messageStreamPtr_->good())
86  {
87  Perr<< nl
88  << "error::error(const string& title) : cannot open error stream"
89  << endl;
90  exit(1);
91  }
92 }
93 
94 
96 :
97  std::exception(),
98  messageStream(errDict),
99  functionName_(errDict.get<string>("functionName")),
100  sourceFileName_(errDict.get<string>("sourceFileName")),
101  sourceFileLineNumber_(errDict.get<label>("sourceFileLineNumber")),
102  throwExceptions_(false),
103  messageStreamPtr_(new OStringStream())
104 {
105  if (!messageStreamPtr_->good())
106  {
107  Perr<< nl
108  << "error::error(const dictionary& errDict) : "
109  "cannot open error stream"
110  << endl;
111  exit(1);
112  }
113 }
114 
115 
117 :
118  std::exception(),
119  messageStream(err),
120  functionName_(err.functionName_),
121  sourceFileName_(err.sourceFileName_),
122  sourceFileLineNumber_(err.sourceFileLineNumber_),
123  throwExceptions_(err.throwExceptions_),
124  messageStreamPtr_(new OStringStream(*err.messageStreamPtr_))
125 {
126  //*messageStreamPtr_ << err.message();
127 }
128 
129 
130 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
131 
133 {
134  delete messageStreamPtr_;
135 }
136 
137 
138 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
139 
140 Foam::OSstream& Foam::error::operator()
141 (
142  const char* functionName,
143  const char* sourceFileName,
144  const int sourceFileLineNumber
145 )
146 {
147  functionName_ = functionName;
148  sourceFileName_ = sourceFileName;
149  sourceFileLineNumber_ = sourceFileLineNumber;
150 
151  return operator OSstream&();
152 }
153 
154 
155 Foam::OSstream& Foam::error::operator()
156 (
157  const string& functionName,
158  const char* sourceFileName,
159  const int sourceFileLineNumber
160 )
161 {
162  return operator()
163  (
164  functionName.c_str(),
165  sourceFileName,
166  sourceFileLineNumber
167  );
168 }
169 
170 
171 Foam::error::operator Foam::OSstream&()
172 {
173  if (!messageStreamPtr_->good())
174  {
175  Perr<< nl
176  << "error::operator OSstream&() : error stream has failed"
177  << endl;
178  abort();
179  }
180 
181  return *messageStreamPtr_;
182 }
183 
184 
185 Foam::error::operator Foam::dictionary() const
186 {
187  dictionary errDict;
188 
189  string oneLineMessage(message());
190  oneLineMessage.replaceAll("\n", " ");
191 
192  errDict.add("type", word("Foam::error"));
193  errDict.add("message", oneLineMessage);
194  errDict.add("function", functionName());
195  errDict.add("sourceFile", sourceFileName());
196  errDict.add("sourceFileLineNumber", sourceFileLineNumber());
197 
198  return errDict;
199 }
200 
201 
202 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
203 
205 {
206  return messageStreamPtr_->str();
207 }
208 
209 
210 void Foam::error::clear() const
211 {
212  return messageStreamPtr_->reset();
213 }
214 
215 
216 void Foam::error::exit(const int errNo)
217 {
218  if (!throwExceptions_ && JobInfo::constructed)
219  {
220  jobInfo.add("FatalError", operator dictionary());
221  jobInfo.exit();
222  }
223 
224  if (env("FOAM_ABORT"))
225  {
226  abort();
227  }
228  else if (throwExceptions_)
229  {
230  // Make a copy of the error to throw
231  error errorException(*this);
232 
233  // Reset the message buffer for the next error message
234  messageStreamPtr_->reset();
235 
236  throw errorException;
237  }
238  else if (Pstream::parRun())
239  {
240  Perr<< nl << *this << nl
241  << "\nFOAM parallel run exiting\n" << endl;
242  Pstream::exit(errNo);
243  }
244  else
245  {
246  Perr<< nl << *this << nl
247  << "\nFOAM exiting\n" << endl;
248  std::exit(errNo);
249  }
250 }
251 
252 
254 {
255  if (!throwExceptions_ && JobInfo::constructed)
256  {
257  jobInfo.add("FatalError", operator dictionary());
258  jobInfo.abort();
259  }
260 
261  if (env("FOAM_ABORT"))
262  {
263  Perr<< nl << *this << nl
264  << "\nFOAM aborting (FOAM_ABORT set)\n" << endl;
265  printStack(Perr);
266  std::abort();
267  }
268  else if (throwExceptions_)
269  {
270  // Make a copy of the error to throw
271  error errorException(*this);
272 
273  // Reset the message buffer for the next error message
274  messageStreamPtr_->reset();
275 
276  throw errorException;
277  }
278  else if (Pstream::parRun())
279  {
280  Perr<< nl << *this << nl
281  << "\nFOAM parallel run aborting\n" << endl;
282  printStack(Perr);
283  Pstream::abort();
284  }
285  else
286  {
287  Perr<< nl << *this << nl
288  << "\nFOAM aborting\n" << endl;
289  printStack(Perr);
290 
291  #ifdef _WIN32
292  std::exit(1); // Prefer exit() to avoid unnecessary warnings
293  #else
294  std::abort();
295  #endif
296  }
297 }
298 
299 
300 void Foam::error::write(Ostream& os, const bool includeTitle) const
301 {
302  os << nl;
303  if (includeTitle)
304  {
305  os << title().c_str() << nl;
306  }
307  os << message().c_str();
308 
309  if (error::level >= 2 && sourceFileLineNumber())
310  {
311  os << nl << nl
312  << " From function " << functionName().c_str() << nl
313  << " in file " << sourceFileName().c_str()
314  << " at line " << sourceFileLineNumber() << '.';
315  }
316 }
317 
318 
319 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
320 
322 {
323  err.write(os);
324 
325  return os;
326 }
327 
328 
329 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
330 // Global error definitions
331 
332 Foam::error Foam::FatalError("--> FOAM FATAL ERROR: ");
333 
334 
335 // ************************************************************************* //
Foam::JobInfo::exit
void exit()
End with "termination=exit".
Definition: JobInfo.C:172
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Foam::messageStream
Class to handle messaging in a simple, consistent stream-based manner.
Definition: messageStream.H:71
Foam::UPstream::exit
static void exit(int errnum=1)
Exit program.
Definition: UPstream.C:59
Foam::UPstream::parRun
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:414
Foam::messageStream::level
static int level
Controls the output verbosity of messageStream.
Definition: messageStream.H:108
StringStream.H
Input/output from string buffers.
Foam::UPstream::abort
static void abort()
Abort program.
Definition: UPstream.C:66
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:73
Foam::env
bool env(const std::string &envName)
True if environment variable of given name is defined.
Definition: MSwindows.C:363
Foam::foamVersion::api
const int api
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
error.H
fileName.H
Foam::OSstream
Generic output stream.
Definition: OSstream.H:54
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
Pstream.H
Foam::error::message
string message() const
Definition: error.C:204
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:137
Foam::error::exit
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: error.C:216
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::foamVersion::version
const std::string version
OpenFOAM version (name or stringified number) as a std::string.
Foam::jobInfo
JobInfo jobInfo
Definition: JobInfo.C:49
Foam::Perr
prefixOSstream Perr
An Ostream wrapper for parallel output to std::cerr.
Foam::error::~error
virtual ~error()
Destructor.
Definition: error.C:132
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::nl
constexpr char nl
Definition: Ostream.H:372
Foam::JobInfo::constructed
static bool constructed
Global value for constructed job info.
Definition: JobInfo.H:80
Foam::error::abort
void abort()
Abort : used to stop code for fatal errors.
Definition: error.C:253
Foam::OStringStream
Output to string buffer, using a OSstream.
Definition: StringStream.H:189
Foam::error::messageStreamPtr_
OStringStream * messageStreamPtr_
Definition: error.H:79
dictionary.H
Foam::error::clear
void clear() const
Clear any messages.
Definition: error.C:210
JobInfo.H
Foam::dictionary::add
entry * add(entry *entryPtr, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:703
Foam::error::write
void write(Ostream &os, const bool includeTitle=true) const
Print error message.
Definition: error.C:300
Foam::JobInfo::abort
void abort()
End with "termination=abort".
Definition: JobInfo.C:178
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:216
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
foamVersion.H