IOerror.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-2019 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 
33 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
34 
35 Foam::IOerror::IOerror(const string& title)
36 :
37  error(title),
38  ioFileName_("unknown"),
39  ioStartLineNumber_(-1),
40  ioEndLineNumber_(-1)
41 {}
42 
43 
45 :
46  error(errDict),
47  ioFileName_(errDict.lookup("ioFileName")),
48  ioStartLineNumber_(errDict.lookup<label>("ioStartLineNumber")),
49  ioEndLineNumber_(errDict.lookup<label>("ioEndLineNumber"))
50 {}
51 
52 
54 {}
55 
56 
57 Foam::OSstream& Foam::IOerror::operator()
58 (
59  const char* functionName,
60  const char* sourceFileName,
61  const int sourceFileLineNumber,
62  const string& ioFileName,
65 )
66 {
68  ioFileName_ = ioFileName;
69  ioStartLineNumber_ = ioStartLineNumber;
70  ioEndLineNumber_ = ioEndLineNumber;
71 
72  return operator OSstream&();
73 }
74 
75 
76 Foam::OSstream& Foam::IOerror::operator()
77 (
78  const char* functionName,
79  const char* sourceFileName,
80  const int sourceFileLineNumber,
81  const IOstream& ioStream
82 )
83 {
84  return operator()
85  (
89  ioStream.name(),
90  ioStream.lineNumber(),
91  -1
92  );
93 }
94 
95 
96 Foam::OSstream& Foam::IOerror::operator()
97 (
98  const char* functionName,
99  const char* sourceFileName,
100  const int sourceFileLineNumber,
101  const dictionary& dict
102 )
103 {
104  return operator()
105  (
106  functionName,
109  dict.name(),
110  dict.startLineNumber(),
111  dict.endLineNumber()
112  );
113 }
114 
115 
117 (
118  const char* functionName,
119  const char* sourceFileName,
120  const int sourceFileLineNumber,
121  const IOstream& ioStream,
122  const string& msg
123 )
124 {
126  {
128  (
129  functionName,
130  sourceFileName,
131  sourceFileLineNumber,
132  ioStream
133  ) << msg << Foam::exit(FatalIOError);
134  }
135  else
136  {
137  std::cerr
138  << std::endl
139  << "--> FOAM FATAL IO ERROR:" << std::endl
140  << msg
141  << std::endl
142  << "file: " << ioStream.name()
143  << " at line " << ioStream.lineNumber() << '.'
144  << std::endl << std::endl
145  << " From function " << functionName
146  << std::endl
147  << " in file " << sourceFileName
148  << " at line " << sourceFileLineNumber << '.'
149  << std::endl;
150  ::exit(1);
151  }
152 }
153 
154 
155 Foam::IOerror::operator Foam::dictionary() const
156 {
157  dictionary errDict(error::operator dictionary());
158 
159  errDict.remove("type");
160  errDict.add("type", word("Foam::IOerror"));
161 
162  errDict.add("ioFileName", ioFileName());
163  errDict.add("ioStartLineNumber", ioStartLineNumber());
164  errDict.add("ioEndLineNumber", ioEndLineNumber());
165 
166  return errDict;
167 }
168 
169 
170 void Foam::IOerror::exit(const int)
171 {
173  {
174  jobInfo_.add("FatalIOError", operator dictionary());
175  jobInfo_.exit();
176  }
177 
178  if (abort_)
179  {
180  abort();
181  }
182 
183  if (Pstream::parRun())
184  {
185  Perr<< endl << *this << endl
186  << "\nFOAM parallel run exiting\n" << endl;
187  Pstream::exit(1);
188  }
189  else
190  {
191  if (throwExceptions_)
192  {
193  // Make a copy of the error to throw
194  IOerror 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 exiting\n" << endl;
205  ::exit(1);
206  }
207  }
208 }
209 
210 
212 {
214  {
215  jobInfo_.add("FatalIOError", operator dictionary());
216  jobInfo_.abort();
217  }
218 
219  if (abort_)
220  {
221  Perr<< endl << *this << endl
222  << "\nFOAM aborting (FOAM_ABORT set)\n" << endl;
223  printStack(Perr);
224  ::abort();
225  }
226 
227  if (Pstream::parRun())
228  {
229  Perr<< endl << *this << endl
230  << "\nFOAM parallel run aborting\n" << endl;
231  printStack(Perr);
232  Pstream::abort();
233  }
234  else
235  {
236  if (throwExceptions_)
237  {
238  // Make a copy of the error to throw
239  IOerror errorException(*this);
240 
241  // Rewind the message buffer for the next error message
243 
244  throw errorException;
245  }
246  else
247  {
248  Perr<< endl << *this << endl
249  << "\nFOAM aborting\n" << endl;
250  printStack(Perr);
251  ::abort();
252  }
253  }
254 }
255 
256 
258 {
259  if (!os.bad())
260  {
261  os << endl
262  << ioErr.title().c_str() << endl
263  << ioErr.message().c_str() << endl << endl;
264 
265  os << "file: " << ioErr.ioFileName().c_str();
266 
267  if (ioErr.ioStartLineNumber() >= 0 && ioErr.ioEndLineNumber() >= 0)
268  {
269  os << " from line " << ioErr.ioStartLineNumber()
270  << " to line " << ioErr.ioEndLineNumber() << '.';
271  }
272  else if (ioErr.ioStartLineNumber() >= 0)
273  {
274  os << " at line " << ioErr.ioStartLineNumber() << '.';
275  }
276 
277  if (IOerror::level >= 2 && ioErr.sourceFileLineNumber())
278  {
279  os << endl << endl
280  << " From function " << ioErr.functionName().c_str() << endl
281  << " in file " << ioErr.sourceFileName().c_str()
282  << " at line " << ioErr.sourceFileLineNumber() << '.';
283  }
284  }
285 
286  return os;
287 }
288 
289 
290 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
291 // Global error definitions
292 
293 Foam::IOerror Foam::FatalIOError("--> FOAM FATAL IO ERROR: ");
294 
295 // ************************************************************************* //
static void printStack(Ostream &)
Helper function to print a stack.
virtual ~IOerror()
Destructor.
Definition: IOerror.C:53
Generic output stream.
Definition: OSstream.H:51
dictionary dict
IOerror(const string &title)
Construct from title string.
Definition: IOerror.C:35
bool remove(const word &)
Remove an entry specified by keyword.
Definition: dictionary.C:1296
static bool constructed
Definition: jobInfo.H:72
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
void exit()
Definition: jobInfo.C:198
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
void abort()
Abort : used to stop code for fatal errors.
Definition: IOerror.C:211
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:348
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:80
const string & title() const
Return the title of this error type.
label ioStartLineNumber() const
Definition: error.H:229
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
void rewind()
Rewind the OStringStream.
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:435
label ioEndLineNumber() const
Definition: error.H:234
jobInfo jobInfo_
Definition: jobInfo.C:44
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:1133
Class to handle errors and exceptions in a simple, consistent stream-based manner.
Definition: error.H:66
bool throwExceptions_
Definition: error.H:82
stressControl lookup("compactNormalStress") >> compactNormalStress
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:117
A functionName is a word starting with &#39;#&#39;.
Definition: functionName.H:57
A class for handling words, derived from string.
Definition: word.H:59
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:294
string message() const
Definition: error.C:162
const string & functionName() const
Definition: error.H:107
Report an I/O error.
Definition: error.H:196
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
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:170
OSstream & operator()()
Explicitly convert to OSstream for << operations.
Definition: error.H:155
prefixOSstream Perr(cerr, "Perr")
Definition: IOstreams.H:54
const string & ioFileName() const
Definition: error.H:224
static void abort()
Abort program.
Definition: UPstream.C:52
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:399
An IOstream is an abstract base class for all input/output systems; be they streams, files, token lists etc.
Definition: IOstream.H:71
const string & sourceFileName() const
Definition: error.H:112
Ostream & operator<<(Ostream &, const ensightPart &)
OStringStream * messageStreamPtr_
Definition: error.H:83
void abort()
Definition: jobInfo.C:204
IOerror FatalIOError
label sourceFileLineNumber() const
Definition: error.H:117