1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd.
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"
32 #include "OSspecific.H"
34 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
36 Foam::error::error(const string& title)
39 messageStream(title, messageStream::FATAL),
40 functionName_("unknown"),
41 sourceFileName_("unknown"),
42 sourceFileLineNumber_(0),
43 abort_(env("FOAM_ABORT")),
44 throwExceptions_(false),
45 messageStreamPtr_(new OStringStream())
47 if (!messageStreamPtr_->good())
50 << "error::error(const string& title) : cannot open error stream"
57 Foam::error::error(const dictionary& errDict)
60 messageStream(errDict),
61 functionName_(errDict.lookup("functionName")),
62 sourceFileName_(errDict.lookup("sourceFileName")),
63 sourceFileLineNumber_(readLabel(errDict.lookup("sourceFileLineNumber"))),
64 abort_(env("FOAM_ABORT")),
65 throwExceptions_(false),
66 messageStreamPtr_(new OStringStream())
68 if (!messageStreamPtr_->good())
71 << "error::error(const dictionary& errDict) : "
72 "cannot open error stream"
79 Foam::error::error(const error& err)
83 functionName_(err.functionName_),
84 sourceFileName_(err.sourceFileName_),
85 sourceFileLineNumber_(err.sourceFileLineNumber_),
87 throwExceptions_(err.throwExceptions_),
88 messageStreamPtr_(new OStringStream(*err.messageStreamPtr_))
90 //*messageStreamPtr_ << err.message();
94 Foam::error::~error() throw()
96 delete messageStreamPtr_;
100 Foam::OSstream& Foam::error::operator()
102 const char* functionName,
103 const char* sourceFileName,
104 const int sourceFileLineNumber
107 functionName_ = functionName;
108 sourceFileName_ = sourceFileName;
109 sourceFileLineNumber_ = sourceFileLineNumber;
111 return operator OSstream&();
115 Foam::OSstream& Foam::error::operator()
117 const string& functionName,
118 const char* sourceFileName,
119 const int sourceFileLineNumber
124 functionName.c_str(),
131 Foam::error::operator OSstream&()
133 if (!messageStreamPtr_->good())
136 << "error::operator OSstream&() : error stream has failed"
141 return *messageStreamPtr_;
145 Foam::error::operator dictionary() const
149 string oneLineMessage(message());
150 oneLineMessage.replaceAll('\n', ' ');
152 errDict.add("type", word("Foam::error"));
153 errDict.add("message", oneLineMessage);
154 errDict.add("function", functionName());
155 errDict.add("sourceFile", sourceFileName());
156 errDict.add("sourceFileLineNumber", sourceFileLineNumber());
162 Foam::string Foam::error::message() const
164 return messageStreamPtr_->str();
168 void Foam::error::exit(const int errNo)
170 if (!throwExceptions_ && JobInfo::constructed)
172 jobInfo.add("FatalError", operator dictionary());
181 if (Pstream::parRun())
183 Perr<< endl << *this << endl
184 << "\nFOAM parallel run exiting\n" << endl;
185 Pstream::exit(errNo);
189 if (throwExceptions_)
191 // Make a copy of the error to throw
192 error errorException(*this);
194 // Rewind the message buffer for the next error message
195 messageStreamPtr_->rewind();
197 throw errorException;
201 Perr<< endl << *this << endl
202 << "\nFOAM exiting\n" << endl;
209 void Foam::error::abort()
211 if (!throwExceptions_ && JobInfo::constructed)
213 jobInfo.add("FatalError", operator dictionary());
219 Perr<< endl << *this << endl
220 << "\nFOAM aborting (FOAM_ABORT set)\n" << endl;
225 if (Pstream::parRun())
227 Perr<< endl << *this << endl
228 << "\nFOAM parallel run aborting\n" << endl;
234 if (throwExceptions_)
236 // Make a copy of the error to throw
237 error errorException(*this);
239 // Rewind the message buffer for the next error message
240 messageStreamPtr_->rewind();
242 throw errorException;
246 Perr<< endl << *this << endl
247 << "\nFOAM aborting\n" << endl;
255 Foam::Ostream& Foam::operator<<(Ostream& os, const error& fErr)
258 << fErr.title().c_str() << endl
259 << fErr.message().c_str();
261 if (error::level >= 2 && fErr.sourceFileLineNumber())
264 << " From function " << fErr.functionName().c_str() << endl
265 << " in file " << fErr.sourceFileName().c_str()
266 << " at line " << fErr.sourceFileLineNumber() << '.';
273 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
274 // Global error definitions
276 Foam::error Foam::FatalError("--> FOAM FATAL ERROR: ");
278 // ************************************************************************* //