1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
4 \\ / O peration | Version: 3.2
5 \\ / A nd | Web: http://www.foam-extend.org
6 \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation, either version 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
27 #include "OStringStream.H"
29 #include "dictionary.H"
33 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
35 Foam::IOerror::IOerror(const string& title)
38 ioFileName_("unknown"),
39 ioStartLineNumber_(-1),
44 Foam::IOerror::IOerror(const dictionary& errDict)
47 ioFileName_(errDict.lookup("ioFileName")),
48 ioStartLineNumber_(readLabel(errDict.lookup("ioStartLineNumber"))),
49 ioEndLineNumber_(readLabel(errDict.lookup("ioEndLineNumber")))
53 Foam::IOerror::~IOerror() throw()
57 Foam::OSstream& Foam::IOerror::operator()
59 const char* functionName,
60 const char* sourceFileName,
61 const int sourceFileLineNumber,
62 const string& ioFileName,
63 const label ioStartLineNumber,
64 const label ioEndLineNumber
67 error::operator()(functionName, sourceFileName, sourceFileLineNumber);
68 ioFileName_ = ioFileName;
69 ioStartLineNumber_ = ioStartLineNumber;
70 ioEndLineNumber_ = ioEndLineNumber;
72 return operator OSstream&();
76 Foam::OSstream& Foam::IOerror::operator()
78 const char* functionName,
79 const char* sourceFileName,
80 const int sourceFileLineNumber,
81 const IOstream& ioStream
90 ioStream.lineNumber(),
96 Foam::OSstream& Foam::IOerror::operator()
98 const char* functionName,
99 const char* sourceFileName,
100 const int sourceFileLineNumber,
101 const dictionary& dict
108 sourceFileLineNumber,
110 dict.startLineNumber(),
116 Foam::IOerror::operator Foam::dictionary() const
118 dictionary errDict(error::operator dictionary());
120 errDict.remove("type");
121 errDict.add("type", word("Foam::IOerror"));
123 errDict.add("ioFileName", ioFileName());
124 errDict.add("ioStartLineNumber", ioStartLineNumber());
125 errDict.add("ioEndLineNumber", ioEndLineNumber());
131 void Foam::IOerror::exit(const int)
133 if (!throwExceptions_ && JobInfo::constructed)
135 jobInfo.add("FatalIOError", operator dictionary());
141 Perr<< endl << *this << endl
142 << "\nFOAM aborting (FOAM_ABORT set)\n" << endl;
147 if (Pstream::parRun())
149 Perr<< endl << *this << endl
150 << "\nFOAM parallel run exiting\n" << endl;
155 if (throwExceptions_)
157 // Make a copy of the error to throw
158 IOerror errorException(*this);
160 // Rewind the message buffer for the next error message
161 messageStreamPtr_->rewind();
163 throw errorException;
167 Perr<< endl << *this << endl
168 << "\nFOAM exiting\n" << endl;
175 void Foam::IOerror::abort()
177 if (!throwExceptions_ && JobInfo::constructed)
179 jobInfo.add("FatalIOError", operator dictionary());
185 Perr<< endl << *this << endl
186 << "\nFOAM aborting (FOAM_ABORT set)\n" << endl;
191 if (Pstream::parRun())
193 Perr<< endl << *this << endl
194 << "\nFOAM parallel run aborting\n" << endl;
200 if (throwExceptions_)
202 // Make a copy of the error to throw
203 IOerror errorException(*this);
205 // Rewind the message buffer for the next error message
206 messageStreamPtr_->rewind();
208 throw errorException;
212 Perr<< endl << *this << endl
213 << "\nFOAM aborting\n" << endl;
221 Foam::Ostream& Foam::operator<<(Ostream& os, const IOerror& ioErr)
224 << ioErr.title().c_str() << endl
225 << ioErr.message().c_str() << endl << endl;
227 os << "file: " << ioErr.ioFileName().c_str();
229 if (ioErr.ioStartLineNumber() >= 0 && ioErr.ioEndLineNumber() >= 0)
231 os << " from line " << ioErr.ioStartLineNumber()
232 << " to line " << ioErr.ioEndLineNumber() << '.';
234 else if (ioErr.ioStartLineNumber() >= 0)
236 os << " at line " << ioErr.ioStartLineNumber() << '.';
239 if (IOerror::level >= 2 && ioErr.sourceFileLineNumber())
242 << " From function " << ioErr.functionName().c_str() << endl
243 << " in file " << ioErr.sourceFileName().c_str()
244 << " at line " << ioErr.sourceFileLineNumber() << '.';
251 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
252 // Global error definitions
254 Foam::IOerror Foam::FatalIOError("--> FOAM FATAL IO ERROR: ");
256 // ************************************************************************* //