error.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "error.H"
00028 #include "OStringStream.H"
00029 #include "fileName.H"
00030 #include "dictionary.H"
00031 #include "JobInfo.H"
00032 #include "Pstream.H"
00033 #include "OSspecific.H"
00034
00035
00036
00037 Foam::error::error(const string& title)
00038 :
00039 std::exception(),
00040 messageStream(title, messageStream::FATAL),
00041 functionName_("unknown"),
00042 sourceFileName_("unknown"),
00043 sourceFileLineNumber_(0),
00044 abort_(env("FOAM_ABORT")),
00045 throwExceptions_(false),
00046 messageStreamPtr_(new OStringStream())
00047 {
00048 if (!messageStreamPtr_->good())
00049 {
00050 Perr<< endl
00051 << "error::error(const string& title) : cannot open error stream"
00052 << endl;
00053 exit(1);
00054 }
00055 }
00056
00057
00058 Foam::error::error(const dictionary& errDict)
00059 :
00060 std::exception(),
00061 messageStream(errDict),
00062 functionName_(errDict.lookup("functionName")),
00063 sourceFileName_(errDict.lookup("sourceFileName")),
00064 sourceFileLineNumber_(readLabel(errDict.lookup("sourceFileLineNumber"))),
00065 abort_(env("FOAM_ABORT")),
00066 throwExceptions_(false),
00067 messageStreamPtr_(new OStringStream())
00068 {
00069 if (!messageStreamPtr_->good())
00070 {
00071 Perr<< endl
00072 << "error::error(const dictionary& errDict) : "
00073 "cannot open error stream"
00074 << endl;
00075 exit(1);
00076 }
00077 }
00078
00079
00080 Foam::error::error(const error& err)
00081 :
00082 std::exception(),
00083 messageStream(err),
00084 functionName_(err.functionName_),
00085 sourceFileName_(err.sourceFileName_),
00086 sourceFileLineNumber_(err.sourceFileLineNumber_),
00087 abort_(err.abort_),
00088 throwExceptions_(err.throwExceptions_),
00089 messageStreamPtr_(new OStringStream(*err.messageStreamPtr_))
00090 {
00091
00092 }
00093
00094
00095 Foam::error::~error() throw()
00096 {
00097 delete messageStreamPtr_;
00098 }
00099
00100
00101 Foam::OSstream& Foam::error::operator()
00102 (
00103 const char* functionName,
00104 const char* sourceFileName,
00105 const int sourceFileLineNumber
00106 )
00107 {
00108 messageStreamPtr_->rewind();
00109 functionName_ = functionName;
00110 sourceFileName_ = sourceFileName;
00111 sourceFileLineNumber_ = sourceFileLineNumber;
00112
00113 return operator OSstream&();
00114 }
00115
00116
00117 Foam::OSstream& Foam::error::operator()
00118 (
00119 const string& functionName,
00120 const char* sourceFileName,
00121 const int sourceFileLineNumber
00122 )
00123 {
00124 return operator()
00125 (
00126 functionName.c_str(),
00127 sourceFileName,
00128 sourceFileLineNumber
00129 );
00130 }
00131
00132
00133 Foam::error::operator OSstream&()
00134 {
00135 if (!messageStreamPtr_->good())
00136 {
00137 Perr<< endl
00138 << "error::operator OSstream&() : error stream has failed"
00139 << endl;
00140 printStack(Perr);
00141 abort();
00142 }
00143
00144 return *messageStreamPtr_;
00145 }
00146
00147
00148 Foam::error::operator dictionary() const
00149 {
00150 dictionary errDict;
00151
00152 string oneLineMessage(message());
00153 oneLineMessage.replaceAll('\n', ' ');
00154
00155 errDict.add("type", word("Foam::error"));
00156 errDict.add("message", oneLineMessage);
00157 errDict.add("function", functionName());
00158 errDict.add("sourceFile", sourceFileName());
00159 errDict.add("sourceFileLineNumber", sourceFileLineNumber());
00160
00161 return errDict;
00162 }
00163
00164
00165 Foam::string Foam::error::message() const
00166 {
00167 return messageStreamPtr_->str();
00168 }
00169
00170
00171 void Foam::error::exit(const int errNo)
00172 {
00173 if (!throwExceptions_ && JobInfo::constructed)
00174 {
00175 jobInfo.add("FatalError", operator dictionary());
00176 jobInfo.exit();
00177 }
00178
00179 if (abort_)
00180 {
00181 printStack(*this);
00182 Perr<< endl << *this << endl
00183 << "\nFOAM aborting (FOAM_ABORT set)\n" << endl;
00184 abort();
00185 }
00186
00187 if (Pstream::parRun())
00188 {
00189 Perr<< endl << *this << endl
00190 << "\nFOAM parallel run exiting\n" << endl;
00191 Pstream::exit(errNo);
00192 }
00193 else
00194 {
00195 if (throwExceptions_)
00196 {
00197 throw *this;
00198 }
00199 else
00200 {
00201 Perr<< endl << *this << endl
00202 << "\nFOAM exiting\n" << endl;
00203 ::exit(1);
00204 }
00205 }
00206 }
00207
00208
00209 void Foam::error::abort()
00210 {
00211 if (!throwExceptions_ && JobInfo::constructed)
00212 {
00213 jobInfo.add("FatalError", operator dictionary());
00214 jobInfo.abort();
00215 }
00216
00217 if (abort_)
00218 {
00219 printStack(*this);
00220 Perr<< endl << *this << endl
00221 << "\nFOAM aborting (FOAM_ABORT set)\n" << endl;
00222 ::abort();
00223 }
00224
00225 if (Pstream::parRun())
00226 {
00227 printStack(*this);
00228 Perr<< endl << *this << endl
00229 << "\nFOAM parallel run aborting\n" << endl;
00230 Pstream::abort();
00231 }
00232 else
00233 {
00234 if (throwExceptions_)
00235 {
00236 throw *this;
00237 }
00238 else
00239 {
00240 printStack(*this);
00241 Perr<< endl << *this << endl
00242 << "\nFOAM aborting\n" << endl;
00243 ::abort();
00244 }
00245 }
00246 }
00247
00248
00249 Foam::Ostream& Foam::operator<<(Ostream& os, const error& fErr)
00250 {
00251 os << endl << fErr.message().c_str();
00252
00253 if (error::level >= 2 && fErr.sourceFileLineNumber())
00254 {
00255 os << endl << endl
00256 << " From function " << fErr.functionName().c_str() << endl
00257 << " in file " << fErr.sourceFileName().c_str()
00258 << " at line " << fErr.sourceFileLineNumber() << '.';
00259 }
00260
00261 return os;
00262 }
00263
00264
00265
00266
00267
00268 Foam::error Foam::FatalError("--> FOAM FATAL ERROR : ");
00269
00270
Copyright © 2000-2009 OpenCFD Ltd