1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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.
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
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/>.
24 \*---------------------------------------------------------------------------*/
27 #include "OStringStream.H"
29 #include "dictionary.H"
34 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
36 Foam::IOerror::IOerror(const string& title)
39 ioFileName_("unknown"),
40 ioStartLineNumber_(-1),
45 Foam::IOerror::IOerror(const dictionary& errDict)
48 ioFileName_(errDict.lookup("ioFileName")),
49 ioStartLineNumber_(readLabel(errDict.lookup("ioStartLineNumber"))),
50 ioEndLineNumber_(readLabel(errDict.lookup("ioEndLineNumber")))
54 Foam::IOerror::~IOerror() throw()
58 Foam::OSstream& Foam::IOerror::operator()
60 const char* functionName,
61 const char* sourceFileName,
62 const int sourceFileLineNumber,
63 const string& ioFileName,
64 const label ioStartLineNumber,
65 const label ioEndLineNumber
68 error::operator()(functionName, sourceFileName, sourceFileLineNumber);
69 ioFileName_ = ioFileName;
70 ioStartLineNumber_ = ioStartLineNumber;
71 ioEndLineNumber_ = ioEndLineNumber;
73 return operator OSstream&();
77 Foam::OSstream& Foam::IOerror::operator()
79 const char* functionName,
80 const char* sourceFileName,
81 const int sourceFileLineNumber,
82 const IOstream& ioStream
91 ioStream.lineNumber(),
97 Foam::OSstream& Foam::IOerror::operator()
99 const char* functionName,
100 const char* sourceFileName,
101 const int sourceFileLineNumber,
102 const dictionary& dict
109 sourceFileLineNumber,
111 dict.startLineNumber(),
117 void Foam::IOerror::SafeFatalIOError
119 const char* functionName,
120 const char* sourceFileName,
121 const int sourceFileLineNumber,
122 const IOstream& ioStream,
126 if (JobInfo::constructed)
130 "primitiveEntry::readEntry(const dictionary&, Istream&)",
132 ) << msg << Foam::exit(FatalIOError);
138 << "--> FOAM FATAL IO ERROR:" << std::endl
141 << "file: " << ioStream.name()
142 << " at line " << ioStream.lineNumber() << '.'
143 << std::endl << std::endl
144 << " From function " << functionName
146 << " in file " << sourceFileName
147 << " at line " << sourceFileLineNumber << '.'
154 Foam::IOerror::operator Foam::dictionary() const
156 dictionary errDict(error::operator dictionary());
158 errDict.remove("type");
159 errDict.add("type", word("Foam::IOerror"));
161 errDict.add("ioFileName", ioFileName());
162 errDict.add("ioStartLineNumber", ioStartLineNumber());
163 errDict.add("ioEndLineNumber", ioEndLineNumber());
169 void Foam::IOerror::exit(const int)
171 if (!throwExceptions_ && JobInfo::constructed)
173 jobInfo.add("FatalIOError", operator dictionary());
182 if (Pstream::parRun())
184 Perr<< endl << *this << endl
185 << "\nFOAM parallel run exiting\n" << endl;
190 if (throwExceptions_)
192 // Make a copy of the error to throw
193 IOerror errorException(*this);
195 // Rewind the message buffer for the next error message
196 messageStreamPtr_->rewind();
198 throw errorException;
202 Perr<< endl << *this << endl
203 << "\nFOAM exiting\n" << endl;
210 void Foam::IOerror::abort()
212 if (!throwExceptions_ && JobInfo::constructed)
214 jobInfo.add("FatalIOError", operator dictionary());
220 Perr<< endl << *this << endl
221 << "\nFOAM aborting (FOAM_ABORT set)\n" << endl;
226 if (Pstream::parRun())
228 Perr<< endl << *this << endl
229 << "\nFOAM parallel run aborting\n" << endl;
235 if (throwExceptions_)
237 // Make a copy of the error to throw
238 IOerror errorException(*this);
240 // Rewind the message buffer for the next error message
241 messageStreamPtr_->rewind();
243 throw errorException;
247 Perr<< endl << *this << endl
248 << "\nFOAM aborting\n" << endl;
256 Foam::Ostream& Foam::operator<<(Ostream& os, const IOerror& ioErr)
259 << ioErr.title().c_str() << endl
260 << ioErr.message().c_str() << endl << endl;
262 os << "file: " << ioErr.ioFileName().c_str();
264 if (ioErr.ioStartLineNumber() >= 0 && ioErr.ioEndLineNumber() >= 0)
266 os << " from line " << ioErr.ioStartLineNumber()
267 << " to line " << ioErr.ioEndLineNumber() << '.';
269 else if (ioErr.ioStartLineNumber() >= 0)
271 os << " at line " << ioErr.ioStartLineNumber() << '.';
274 if (IOerror::level >= 2 && ioErr.sourceFileLineNumber())
277 << " From function " << ioErr.functionName().c_str() << endl
278 << " in file " << ioErr.sourceFileName().c_str()
279 << " at line " << ioErr.sourceFileLineNumber() << '.';
286 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
287 // Global error definitions
289 Foam::IOerror Foam::FatalIOError("--> FOAM FATAL IO ERROR: ");
291 // ************************************************************************* //