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-2024 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 
36 :
37  ioFileName_("unknown"),
38  ioStartLineNumber_(-1),
39  ioEndLineNumber_(-1),
40  ioGlobal_(false)
41 {}
42 
43 
45 (
46  const string& ioFileName,
47  const label ioStartLineNumber,
48  const label ioEndLineNumber,
49  const bool ioGlobal
50 )
51 :
52  ioFileName_(ioFileName),
53  ioStartLineNumber_(ioStartLineNumber),
54  ioEndLineNumber_(ioEndLineNumber),
55  ioGlobal_(ioGlobal)
56 {}
57 
58 
60 :
61  ioFileName_(ios.name()),
62  ioStartLineNumber_(ios.lineNumber()),
63  ioEndLineNumber_(-1),
64  ioGlobal_(false)
65 {}
66 
67 
69 :
70  ioFileName_(dict.name()),
71  ioStartLineNumber_(dict.startLineNumber()),
72  ioEndLineNumber_(dict.endLineNumber()),
73  ioGlobal_(dict.global())
74 {}
75 
76 
77 Foam::IOerror::IOerror(const string& title)
78 :
79  error(title),
81 {}
82 
83 
84 Foam::OSstream& Foam::IOerror::operator()
85 (
86  const char* functionName,
87  const char* sourceFileName,
88  const int sourceFileLineNumber,
89  const IOerrorLocation& location
90 )
91 {
92  error::operator()(functionName, sourceFileName, sourceFileLineNumber);
93 
94  IOerrorLocation::operator=(location);
95 
96  return operator OSstream&();
97 }
98 
99 
101 (
102  const char* functionName,
103  const char* sourceFileName,
104  const int sourceFileLineNumber,
105  const IOstream& ioStream,
106  const string& msg
107 )
108 {
110  {
112  (
113  functionName,
114  sourceFileName,
115  sourceFileLineNumber,
116  ioStream
117  ) << msg << Foam::exit(FatalIOError);
118  }
119  else
120  {
121  std::cerr
122  << std::endl
123  << "--> FOAM FATAL IO ERROR:" << std::endl
124  << msg
125  << std::endl
126  << "file: " << ioStream.name()
127  << " at line " << ioStream.lineNumber() << '.'
128  << std::endl << std::endl
129  << " From function " << functionName
130  << std::endl
131  << " in file " << sourceFileName
132  << " at line " << sourceFileLineNumber << '.'
133  << std::endl;
134  ::exit(1);
135  }
136 }
137 
138 
139 Foam::IOerror::operator Foam::dictionary() const
140 {
141  dictionary errDict(error::operator dictionary());
142 
143  errDict.remove("type");
144  errDict.add("type", word("Foam::IOerror"));
145 
146  errDict.add("ioFileName", ioFileName());
147  errDict.add("ioStartLineNumber", ioStartLineNumber());
148  errDict.add("ioEndLineNumber", ioEndLineNumber());
149 
150  return errDict;
151 }
152 
153 
154 void Foam::IOerror::exit(const int)
155 {
156  if (!throwExceptions_ && jobInfo::constructed)
157  {
158  jobInfo_.add("FatalIOError", operator dictionary());
159  jobInfo_.exit();
160  }
161 
162  if (abort_)
163  {
164  abort();
165  }
166 
167  if (Pstream::parRun())
168  {
169  if (ioGlobal())
170  {
171  if (Pstream::master())
172  {
173  Serr<< endl << *this << endl
174  << "\nFOAM parallel run exiting\n" << endl;
175  }
176  }
177  else
178  {
179  Perr<< endl << *this << endl
180  << "\nFOAM parallel run exiting\n" << endl;
181  }
182 
183  Pstream::exit(1);
184  }
185  else
186  {
187  if (throwExceptions_)
188  {
189  // Make a copy of the error to throw
190  IOerror errorException(*this);
191 
192  // Rewind the message buffer for the next error message
193  messageStream_.rewind();
194 
195  throw errorException;
196  }
197  else
198  {
199  Perr<< endl << *this << endl
200  << "\nFOAM exiting\n" << endl;
201  ::exit(1);
202  }
203  }
204 }
205 
206 
208 {
209  if (!throwExceptions_ && jobInfo::constructed)
210  {
211  jobInfo_.add("FatalIOError", operator dictionary());
212  jobInfo_.abort();
213  }
214 
215  if (abort_)
216  {
217  Perr<< endl << *this << endl
218  << "\nFOAM aborting (FOAM_ABORT set)\n" << endl;
219  printStack(Perr);
220  ::abort();
221  }
222 
223  if (Pstream::parRun())
224  {
225  if (ioGlobal())
226  {
227  if (Pstream::master())
228  {
229  Serr<< endl << *this << endl
230  << "\nFOAM parallel run aborting\n" << endl;
231  printStack(Perr);
232  }
233  }
234  else
235  {
236  Perr<< endl << *this << endl
237  << "\nFOAM parallel run aborting\n" << endl;
238  printStack(Perr);
239  }
240 
241  Pstream::abort();
242  }
243  else
244  {
245  if (throwExceptions_)
246  {
247  // Make a copy of the error to throw
248  IOerror errorException(*this);
249 
250  // Rewind the message buffer for the next error message
251  messageStream_.rewind();
252 
253  throw errorException;
254  }
255  else
256  {
257  Perr<< endl << *this << endl
258  << "\nFOAM aborting\n" << endl;
259  printStack(Perr);
260  ::abort();
261  }
262  }
263 }
264 
265 
267 {
268  if (!os.bad())
269  {
270  os << endl
271  << ioErr.title().c_str() << endl
272  << ioErr.message().c_str() << endl << endl;
273 
274  os << "file: " << ioErr.ioFileName().c_str();
275 
276  if (ioErr.ioStartLineNumber() >= 0 && ioErr.ioEndLineNumber() >= 0)
277  {
278  os << " from line " << ioErr.ioStartLineNumber()
279  << " to line " << ioErr.ioEndLineNumber() << '.';
280  }
281  else if (ioErr.ioStartLineNumber() >= 0)
282  {
283  os << " at line " << ioErr.ioStartLineNumber() << '.';
284  }
285 
286  if (IOerror::level >= 2 && ioErr.sourceFileLineNumber())
287  {
288  os << endl << endl
289  << " From function " << ioErr.functionName().c_str() << endl
290  << " in file " << ioErr.sourceFileName().c_str()
291  << " at line " << ioErr.sourceFileLineNumber() << '.';
292  }
293  }
294 
295  return os;
296 }
297 
298 
299 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
300 // Global error definitions
301 
302 Foam::IOerror Foam::FatalIOError("--> FOAM FATAL IO ERROR: ");
303 
304 // ************************************************************************* //
const string & ioFileName() const
Access the file name.
Definition: error.H:229
IOerrorLocation()
Construct null.
Definition: IOerror.C:35
label ioEndLineNumber() const
Access the end line number.
Definition: error.H:241
label ioStartLineNumber() const
Access the start line number.
Definition: error.H:235
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:101
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:154
IOerror(const string &title)
Construct from title string.
Definition: IOerror.C:77
void abort()
Abort : used to stop code for fatal errors.
Definition: IOerror.C:207
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:72
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:435
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:294
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:348
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 bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:423
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:162
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:1014
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
const string & functionName() const
Definition: error.H:100
string message() const
Definition: error.C:119
OSstream & operator()()
Explicitly convert to OSstream for << operations.
Definition: error.C:88
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
const string & title() const
Return the title of this error type.
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
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
jobInfo jobInfo_
Definition: jobInfo.C:44
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
errorManip< error > abort(error &err)
Definition: errorManip.H:131
IOerror FatalIOError
OSstream Serr(cerr, "Serr")
Definition: IOstreams.H:52
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)
dictionary dict