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-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 \*---------------------------------------------------------------------------*/
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 {
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(),
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 {
128  {
129  jobInfo_.add("FatalError", operator dictionary());
130  jobInfo_.exit();
131  }
132 
133  if (abort_)
134  {
135  abort();
136  }
137 
138  if (Pstream::parRun())
139  {
140  Perr<< endl << *this << endl
141  << "\nFOAM parallel run exiting\n" << endl;
142  Pstream::exit(errNo);
143  }
144  else
145  {
146  if (throwExceptions_)
147  {
148  // Make a copy of the error to throw
149  error errorException(*this);
150 
151  // Rewind the message buffer for the next error message
153 
154  throw errorException;
155  }
156  else
157  {
158  Perr<< endl << *this << endl
159  << "\nFOAM exiting\n" << endl;
160  ::exit(errNo);
161  }
162  }
163 }
164 
165 
167 {
169  {
170  jobInfo_.add("FatalError", operator dictionary());
171  jobInfo_.abort();
172  }
173 
174  if (abort_)
175  {
176  Perr<< endl << *this << endl
177  << "\nFOAM aborting (FOAM_ABORT set)\n" << endl;
178  printStack(Perr);
179  ::abort();
180  }
181 
182  if (Pstream::parRun())
183  {
184  Perr<< endl << *this << endl
185  << "\nFOAM parallel run aborting\n" << endl;
186  printStack(Perr);
187  Pstream::abort();
188  }
189  else
190  {
191  if (throwExceptions_)
192  {
193  // Make a copy of the error to throw
194  error errorException(*this);
195 
196  // Rewind the message buffer for the next error message
198 
199  throw errorException;
200  }
201  else
202  {
203  Perr<< endl << *this << endl
204  << "\nFOAM aborting\n" << endl;
205  printStack(Perr);
206  ::abort();
207  }
208  }
209 }
210 
211 
213 {
214  os << endl
215  << fErr.title().c_str() << endl
216  << fErr.message().c_str();
217 
218  if (error::level >= 2 && fErr.sourceFileLineNumber())
219  {
220  os << endl << endl
221  << " From function " << fErr.functionName().c_str() << endl
222  << " in file " << fErr.sourceFileName().c_str()
223  << " at line " << fErr.sourceFileLineNumber() << '.';
224  }
225 
226  return os;
227 }
228 
229 
230 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
231 // Global error definitions
232 
233 Foam::error Foam::FatalError("--> FOAM FATAL ERROR: ");
234 
235 // ************************************************************************* //
static void printStack(Ostream &)
Helper function to print a stack.
Generic output stream.
Definition: OSstream.H:51
string & replaceAll(const string &oldStr, const string &newStr, size_type start=0)
In this string replace all occurrences of sub-string oldStr.
Definition: string.C:93
static bool constructed
Definition: jobInfo.H:72
void exit()
Definition: jobInfo.C:198
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:81
const string & title() const
Return the title of this error type.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
void rewind()
Rewind the OStringStream.
string sourceFileName_
Definition: error.H:78
Class to handle messaging in a simple, consistent stream-based manner.
Definition: messageStream.H:68
jobInfo jobInfo_
Definition: jobInfo.C:44
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:330
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: error.C:125
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:1153
error(const string &title)
Construct from title string.
Definition: error.C:36
OStringStream messageStream_
Definition: error.H:85
Class to handle errors and exceptions in a simple, consistent stream-based manner.
Definition: error.H:67
OSstream & operator()()
Explicitly convert to OSstream for << operations.
Definition: error.C:88
label sourceFileLineNumber_
Definition: error.H:79
bool throwExceptions_
Definition: error.H:83
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
A class for handling words, derived from string.
Definition: word.H:59
string message() const
Definition: error.C:119
void abort()
Abort : used to stop code for fatal errors.
Definition: error.C:166
const string & functionName() const
Definition: error.H:100
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
static void exit(int errnum=1)
Exit program.
Definition: UPstream.C:46
prefixOSstream Perr(cerr, "Perr")
Definition: IOstreams.H:54
string functionName_
Definition: error.H:77
static void abort()
Abort program.
Definition: UPstream.C:52
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:399
const string & sourceFileName() const
Definition: error.H:105
Ostream & operator<<(Ostream &, const ensightPart &)
string str() const
Return the string.
bool env(const word &)
Return true if environment variable of given name is defined.
Definition: POSIX.C:91
A class for handling character strings derived from std::string.
Definition: string.H:76
void abort()
Definition: jobInfo.C:204
label sourceFileLineNumber() const
Definition: error.H:110