diff --git a/src/OpenFOAM/db/error/IOerror.C b/src/OpenFOAM/db/error/IOerror.C index 9ec6377dff15e52e6ce037a205a5dc03471ea36f..6d632a254eebf1c4d761a9911f456279c86d624a 100644 --- a/src/OpenFOAM/db/error/IOerror.C +++ b/src/OpenFOAM/db/error/IOerror.C @@ -170,14 +170,14 @@ Foam::IOerror::operator Foam::dictionary() const } -// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // -void Foam::IOerror::exitOrAbort(const int, const bool isAbort) +void Foam::IOerror::exitOrAbort(const int errNo, const bool isAbort) { - if (!throwExceptions_ && JobInfo::constructed) + if (!throwing_ && JobInfo::constructed) { jobInfo.add("FatalIOError", operator dictionary()); - if (isAbort) + if (isAbort || hasEnv("FOAM_ABORT")) { jobInfo.abort(); } @@ -187,14 +187,7 @@ void Foam::IOerror::exitOrAbort(const int, const bool isAbort) } } - if (hasEnv("FOAM_ABORT")) - { - Perr<< nl << *this << nl - << "\nFOAM aborting (FOAM_ABORT set)\n" << endl; - printStack(Perr); - std::abort(); - } - else if (throwExceptions_) + if (throwing_ && !isAbort) { // Make a copy of the error to throw IOerror errorException(*this); @@ -204,20 +197,27 @@ void Foam::IOerror::exitOrAbort(const int, const bool isAbort) throw errorException; } + else if (hasEnv("FOAM_ABORT")) + { + Perr<< nl << *this << nl + << "\nFOAM aborting (FOAM_ABORT set)\n" << endl; + error::printStack(Perr); + std::abort(); + } else if (Pstream::parRun()) { if (isAbort) { Perr<< nl << *this << nl << "\nFOAM parallel run aborting\n" << endl; - printStack(Perr); + error::printStack(Perr); Pstream::abort(); } else { Perr<< nl << *this << nl << "\nFOAM parallel run exiting\n" << endl; - Pstream::exit(1); + Pstream::exit(errNo); } } else @@ -226,7 +226,7 @@ void Foam::IOerror::exitOrAbort(const int, const bool isAbort) { Perr<< nl << *this << nl << "\nFOAM aborting\n" << endl; - printStack(Perr); + error::printStack(Perr); #ifdef _WIN32 std::exit(1); // Prefer exit() to avoid unnecessary warnings @@ -238,15 +238,17 @@ void Foam::IOerror::exitOrAbort(const int, const bool isAbort) { Perr<< nl << *this << nl << "\nFOAM exiting\n" << endl; - std::exit(1); + std::exit(errNo); } } } +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + void Foam::IOerror::exit(const int) { - exitOrAbort(1, hasEnv("FOAM_ABORT")); + exitOrAbort(1, false); } diff --git a/src/OpenFOAM/db/error/error.C b/src/OpenFOAM/db/error/error.C index 897b80a4cad18de99c238a3642de4fdd163f1f7b..380d521621267427be99f2f9600065005be1f777 100644 --- a/src/OpenFOAM/db/error/error.C +++ b/src/OpenFOAM/db/error/error.C @@ -79,17 +79,9 @@ Foam::error::error(const string& title) functionName_("unknown"), sourceFileName_("unknown"), sourceFileLineNumber_(0), - throwExceptions_(false), + throwing_(false), messageStreamPtr_(new OStringStream()) -{ - if (!messageStreamPtr_->good()) - { - Perr<< nl - << "error::error(const string&) : cannot open error stream" - << endl; - exit(1); - } -} +{} Foam::error::error(const dictionary& errDict) @@ -99,17 +91,9 @@ Foam::error::error(const dictionary& errDict) functionName_(errDict.get<string>("functionName")), sourceFileName_(errDict.get<string>("sourceFileName")), sourceFileLineNumber_(errDict.get<label>("sourceFileLineNumber")), - throwExceptions_(false), + throwing_(false), messageStreamPtr_(new OStringStream()) -{ - if (!messageStreamPtr_->good()) - { - Perr<< nl - << "error::error(const dictionary&) : cannot open error stream" - << endl; - exit(1); - } -} +{} Foam::error::error(const error& err) @@ -119,7 +103,7 @@ Foam::error::error(const error& err) functionName_(err.functionName_), sourceFileName_(err.sourceFileName_), sourceFileLineNumber_(err.sourceFileLineNumber_), - throwExceptions_(err.throwExceptions_), + throwing_(err.throwing_), messageStreamPtr_(new OStringStream(*err.messageStreamPtr_)) {} @@ -127,9 +111,7 @@ Foam::error::error(const error& err) // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // Foam::error::~error() noexcept -{ - delete messageStreamPtr_; -} +{} // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // @@ -208,27 +190,14 @@ Foam::error::operator Foam::dictionary() const return errDict; } - -// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // - -Foam::string Foam::error::message() const -{ - return messageStreamPtr_->str(); -} - - -void Foam::error::clear() const -{ - return messageStreamPtr_->reset(); -} - +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // void Foam::error::exitOrAbort(const int errNo, const bool isAbort) { - if (!throwExceptions_ && JobInfo::constructed) + if (!throwing_ && JobInfo::constructed) { jobInfo.add("FatalError", operator dictionary()); - if (isAbort) + if (isAbort || hasEnv("FOAM_ABORT")) { jobInfo.abort(); } @@ -238,14 +207,7 @@ void Foam::error::exitOrAbort(const int errNo, const bool isAbort) } } - if (hasEnv("FOAM_ABORT")) - { - Perr<< nl << *this << nl - << "\nFOAM aborting (FOAM_ABORT set)\n" << endl; - printStack(Perr); - std::abort(); - } - else if (throwExceptions_) + if (throwing_ && !isAbort) { // Make a copy of the error to throw error errorException(*this); @@ -255,13 +217,20 @@ void Foam::error::exitOrAbort(const int errNo, const bool isAbort) throw errorException; } + else if (hasEnv("FOAM_ABORT")) + { + Perr<< nl << *this << nl + << "\nFOAM aborting (FOAM_ABORT set)\n" << endl; + error::printStack(Perr); + std::abort(); + } else if (Pstream::parRun()) { if (isAbort) { Perr<< nl << *this << nl << "\nFOAM parallel run aborting\n" << endl; - printStack(Perr); + error::printStack(Perr); Pstream::abort(); } else @@ -277,7 +246,7 @@ void Foam::error::exitOrAbort(const int errNo, const bool isAbort) { Perr<< nl << *this << nl << "\nFOAM aborting\n" << endl; - printStack(Perr); + error::printStack(Perr); #ifdef _WIN32 std::exit(1); // Prefer exit() to avoid unnecessary warnings @@ -295,9 +264,23 @@ void Foam::error::exitOrAbort(const int errNo, const bool isAbort) } +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +Foam::string Foam::error::message() const +{ + return messageStreamPtr_->str(); +} + + +void Foam::error::clear() const +{ + return messageStreamPtr_->reset(); +} + + void Foam::error::exit(const int errNo) { - exitOrAbort(errNo, hasEnv("FOAM_ABORT")); + exitOrAbort(errNo, false); } diff --git a/src/OpenFOAM/db/error/error.H b/src/OpenFOAM/db/error/error.H index 6a9382a6097042606f1e25ff5633aee84cb43a28..0655d6935776af762fe2d5d41ddef3bec8653b55 100644 --- a/src/OpenFOAM/db/error/error.H +++ b/src/OpenFOAM/db/error/error.H @@ -45,6 +45,11 @@ Usage SourceFiles error.C + IOerror.C + +SeeAlso + Foam::errorManip + Foam::errorManipArg \*---------------------------------------------------------------------------*/ @@ -52,6 +57,7 @@ SourceFiles #define error_H #include "messageStream.H" +#include <memory> // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -80,8 +86,8 @@ protected: string functionName_; string sourceFileName_; label sourceFileLineNumber_; - bool throwExceptions_; - OStringStream* messageStreamPtr_; + bool throwing_; + std::unique_ptr<OStringStream> messageStreamPtr_; public: @@ -138,15 +144,15 @@ public: //- Return the current exception throwing (on or off) bool throwing() const { - return throwExceptions_; + return throwing_; } //- Activate/deactivate exception throwing // \return the previous throwing state inline bool throwExceptions(bool doThrow) { - const bool prev = throwExceptions_; - throwExceptions_ = doThrow; + const bool prev = throwing_; + throwing_ = doThrow; return prev; } diff --git a/src/OpenFOAM/db/error/errorManip.H b/src/OpenFOAM/db/error/errorManip.H index 2017ae69ff13e1da1c4fab6ec196249498765efa..a0be325613d7532ce0ab7e8944a3d644f0ecd826 100644 --- a/src/OpenFOAM/db/error/errorManip.H +++ b/src/OpenFOAM/db/error/errorManip.H @@ -135,17 +135,17 @@ exit(error& err, const int errNo = 1) } -inline errorManip<error> -abort(error& err) +inline errorManipArg<IOerror, int> +exit(IOerror& err, const int errNo = 1) { - return errorManip<error>(&error::abort, err); + return errorManipArg<IOerror, int>(&IOerror::exit, err, errNo); } -inline errorManipArg<IOerror, int> -exit(IOerror& err, const int errNo = 1) +inline errorManip<error> +abort(error& err) { - return errorManipArg<IOerror, int>(&IOerror::exit, err, errNo); + return errorManip<error>(&error::abort, err); }