BUG: UListIO: byteSize overflowing on really big faceLists
[OpenFOAM-2.0.x.git] / src / OpenFOAM / db / error / IOerror.C
blobf68fd651f7b52b841f17837a5c8cbd65a78de9d9
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
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
19     for more details.
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 \*---------------------------------------------------------------------------*/
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 "JobInfo.H"
34 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
36 Foam::IOerror::IOerror(const string& title)
38     error(title),
39     ioFileName_("unknown"),
40     ioStartLineNumber_(-1),
41     ioEndLineNumber_(-1)
45 Foam::IOerror::IOerror(const dictionary& errDict)
47     error(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
85     return operator()
86     (
87         functionName,
88         sourceFileName,
89         sourceFileLineNumber,
90         ioStream.name(),
91         ioStream.lineNumber(),
92         -1
93     );
97 Foam::OSstream& Foam::IOerror::operator()
99     const char* functionName,
100     const char* sourceFileName,
101     const int sourceFileLineNumber,
102     const dictionary& dict
105     return operator()
106     (
107         functionName,
108         sourceFileName,
109         sourceFileLineNumber,
110         dict.name(),
111         dict.startLineNumber(),
112         dict.endLineNumber()
113     );
117 void Foam::IOerror::SafeFatalIOError
119     const char* functionName,
120     const char* sourceFileName,
121     const int sourceFileLineNumber,
122     const IOstream& ioStream,
123     const string& msg
126     if (JobInfo::constructed)
127     {
128         FatalIOErrorIn
129         (
130             "primitiveEntry::readEntry(const dictionary&, Istream&)",
131             ioStream
132         )   << msg << Foam::exit(FatalIOError);
133     }
134     else
135     {
136         std::cerr
137             << std::endl
138             << "--> FOAM FATAL IO ERROR:" << std::endl
139             << msg
140             << std::endl
141             << "file: " << ioStream.name()
142             << " at line " << ioStream.lineNumber() << '.'
143             << std::endl << std::endl
144             << "    From function " << functionName
145             << std::endl
146             << "    in file " << sourceFileName
147             << " at line " << sourceFileLineNumber << '.'
148             << std::endl;
149         ::exit(1);
150     }
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());
165     return errDict;
169 void Foam::IOerror::exit(const int)
171     if (!throwExceptions_ && JobInfo::constructed)
172     {
173         jobInfo.add("FatalIOError", operator dictionary());
174         jobInfo.exit();
175     }
177     if (abort_)
178     {
179         abort();
180     }
182     if (Pstream::parRun())
183     {
184         Perr<< endl << *this << endl
185             << "\nFOAM parallel run exiting\n" << endl;
186         Pstream::exit(1);
187     }
188     else
189     {
190         if (throwExceptions_)
191         {
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;
199         }
200         else
201         {
202             Perr<< endl << *this << endl
203                 << "\nFOAM exiting\n" << endl;
204             ::exit(1);
205         }
206     }
210 void Foam::IOerror::abort()
212     if (!throwExceptions_ && JobInfo::constructed)
213     {
214         jobInfo.add("FatalIOError", operator dictionary());
215         jobInfo.abort();
216     }
218     if (abort_)
219     {
220         Perr<< endl << *this << endl
221             << "\nFOAM aborting (FOAM_ABORT set)\n" << endl;
222         printStack(Perr);
223         ::abort();
224     }
226     if (Pstream::parRun())
227     {
228         Perr<< endl << *this << endl
229             << "\nFOAM parallel run aborting\n" << endl;
230         printStack(Perr);
231         Pstream::abort();
232     }
233     else
234     {
235         if (throwExceptions_)
236         {
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;
244         }
245         else
246         {
247             Perr<< endl << *this << endl
248                 << "\nFOAM aborting\n" << endl;
249             printStack(Perr);
250             ::abort();
251         }
252     }
256 Foam::Ostream& Foam::operator<<(Ostream& os, const IOerror& ioErr)
258     os  << endl
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)
265     {
266         os  << " from line " << ioErr.ioStartLineNumber()
267             << " to line " << ioErr.ioEndLineNumber() << '.';
268     }
269     else if (ioErr.ioStartLineNumber() >= 0)
270     {
271         os  << " at line " << ioErr.ioStartLineNumber() << '.';
272     }
274     if (IOerror::level >= 2 && ioErr.sourceFileLineNumber())
275     {
276         os  << endl << endl
277             << "    From function " << ioErr.functionName().c_str() << endl
278             << "    in file " << ioErr.sourceFileName().c_str()
279             << " at line " << ioErr.sourceFileLineNumber() << '.';
280     }
282     return os;
286 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
287 // Global error definitions
289 Foam::IOerror Foam::FatalIOError("--> FOAM FATAL IO ERROR: ");
291 // ************************************************************************* //