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 {
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 (!throwExceptions_ && jobInfo::constructed)
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
152  messageStream_.rewind();
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 {
168  if (!throwExceptions_ && jobInfo::constructed)
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
197  messageStream_.rewind();
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 // ************************************************************************* //
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:330
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 keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:160
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:1169
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:166
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:251
errorManip< error > abort(error &err)
Definition: errorManip.H:131
error FatalError
Ostream & operator<<(Ostream &, const ensightPart &)