FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

IOerror.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2010 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software: you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include "error.H"
00027 #include <OpenFOAM/OStringStream.H>
00028 #include <OpenFOAM/fileName.H>
00029 #include <OpenFOAM/dictionary.H>
00030 #include <OpenFOAM/JobInfo.H>
00031 #include <OpenFOAM/Pstream.H>
00032 
00033 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00034 
00035 Foam::IOerror::IOerror(const string& title)
00036 :
00037     error(title),
00038     ioFileName_("unknown"),
00039     ioStartLineNumber_(-1),
00040     ioEndLineNumber_(-1)
00041 {}
00042 
00043 
00044 Foam::IOerror::IOerror(const dictionary& errDict)
00045 :
00046     error(errDict),
00047     ioFileName_(errDict.lookup("ioFileName")),
00048     ioStartLineNumber_(readLabel(errDict.lookup("ioStartLineNumber"))),
00049     ioEndLineNumber_(readLabel(errDict.lookup("ioEndLineNumber")))
00050 {}
00051 
00052 
00053 Foam::IOerror::~IOerror() throw()
00054 {}
00055 
00056 
00057 Foam::OSstream& Foam::IOerror::operator()
00058 (
00059     const char* functionName,
00060     const char* sourceFileName,
00061     const int sourceFileLineNumber,
00062     const string& ioFileName,
00063     const label ioStartLineNumber,
00064     const label ioEndLineNumber
00065 )
00066 {
00067     error::operator()(functionName, sourceFileName, sourceFileLineNumber);
00068     ioFileName_ = ioFileName;
00069     ioStartLineNumber_ = ioStartLineNumber;
00070     ioEndLineNumber_ = ioEndLineNumber;
00071 
00072     return operator OSstream&();
00073 }
00074 
00075 
00076 Foam::OSstream& Foam::IOerror::operator()
00077 (
00078     const char* functionName,
00079     const char* sourceFileName,
00080     const int sourceFileLineNumber,
00081     const IOstream& ioStream
00082 )
00083 {
00084     return operator()
00085     (
00086         functionName,
00087         sourceFileName,
00088         sourceFileLineNumber,
00089         ioStream.name(),
00090         ioStream.lineNumber(),
00091         -1
00092     );
00093 }
00094 
00095 
00096 Foam::OSstream& Foam::IOerror::operator()
00097 (
00098     const char* functionName,
00099     const char* sourceFileName,
00100     const int sourceFileLineNumber,
00101     const dictionary& dict
00102 )
00103 {
00104     return operator()
00105     (
00106         functionName,
00107         sourceFileName,
00108         sourceFileLineNumber,
00109         dict.name(),
00110         dict.startLineNumber(),
00111         dict.endLineNumber()
00112     );
00113 }
00114 
00115 
00116 Foam::IOerror::operator Foam::dictionary() const
00117 {
00118     dictionary errDict(error::operator dictionary());
00119 
00120     errDict.remove("type");
00121     errDict.add("type", word("Foam::IOerror"));
00122 
00123     errDict.add("ioFileName", ioFileName());
00124     errDict.add("ioStartLineNumber", ioStartLineNumber());
00125     errDict.add("ioEndLineNumber", ioEndLineNumber());
00126 
00127     return errDict;
00128 }
00129 
00130 
00131 void Foam::IOerror::exit(const int)
00132 {
00133     if (!throwExceptions_ && JobInfo::constructed)
00134     {
00135         jobInfo.add("FatalIOError", operator dictionary());
00136         jobInfo.exit();
00137     }
00138 
00139     if (abort_)
00140     {
00141         abort();
00142     }
00143 
00144     if (Pstream::parRun())
00145     {
00146         Perr<< endl << *this << endl
00147             << "\nFOAM parallel run exiting\n" << endl;
00148         Pstream::exit(1);
00149     }
00150     else
00151     {
00152         if (throwExceptions_)
00153         {
00154             // Make a copy of the error to throw
00155             IOerror errorException(*this);
00156 
00157             // Rewind the message buffer for the next error message
00158             messageStreamPtr_->rewind();
00159 
00160             throw errorException;
00161         }
00162         else
00163         {
00164             Perr<< endl << *this << endl
00165                 << "\nFOAM exiting\n" << endl;
00166             ::exit(1);
00167         }
00168     }
00169 }
00170 
00171 
00172 void Foam::IOerror::abort()
00173 {
00174     if (!throwExceptions_ && JobInfo::constructed)
00175     {
00176         jobInfo.add("FatalIOError", operator dictionary());
00177         jobInfo.abort();
00178     }
00179 
00180     if (abort_)
00181     {
00182         Perr<< endl << *this << endl
00183             << "\nFOAM aborting (FOAM_ABORT set)\n" << endl;
00184         printStack(Perr);
00185         ::abort();
00186     }
00187 
00188     if (Pstream::parRun())
00189     {
00190         Perr<< endl << *this << endl
00191             << "\nFOAM parallel run aborting\n" << endl;
00192         printStack(Perr);
00193         Pstream::abort();
00194     }
00195     else
00196     {
00197         if (throwExceptions_)
00198         {
00199             // Make a copy of the error to throw
00200             IOerror errorException(*this);
00201 
00202             // Rewind the message buffer for the next error message
00203             messageStreamPtr_->rewind();
00204 
00205             throw errorException;
00206         }
00207         else
00208         {
00209             Perr<< endl << *this << endl
00210                 << "\nFOAM aborting\n" << endl;
00211             printStack(Perr);
00212             ::abort();
00213         }
00214     }
00215 }
00216 
00217 
00218 Foam::Ostream& Foam::operator<<(Ostream& os, const IOerror& ioErr)
00219 {
00220     os  << endl
00221         << ioErr.title().c_str() << endl
00222         << ioErr.message().c_str() << endl << endl;
00223 
00224     os  << "file: " << ioErr.ioFileName().c_str();
00225 
00226     if (ioErr.ioStartLineNumber() >= 0 && ioErr.ioEndLineNumber() >= 0)
00227     {
00228         os  << " from line " << ioErr.ioStartLineNumber()
00229             << " to line " << ioErr.ioEndLineNumber() << '.';
00230     }
00231     else if (ioErr.ioStartLineNumber() >= 0)
00232     {
00233         os  << " at line " << ioErr.ioStartLineNumber() << '.';
00234     }
00235 
00236     if (IOerror::level >= 2 && ioErr.sourceFileLineNumber())
00237     {
00238         os  << endl << endl
00239             << "    From function " << ioErr.functionName().c_str() << endl
00240             << "    in file " << ioErr.sourceFileName().c_str()
00241             << " at line " << ioErr.sourceFileLineNumber() << '.';
00242     }
00243 
00244     return os;
00245 }
00246 
00247 
00248 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00249 // Global error definitions
00250 
00251 Foam::IOerror Foam::FatalIOError("--> FOAM FATAL IO ERROR: ");
00252 
00253 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines