diff --git a/src/OpenFOAM/db/error/IOerror.C b/src/OpenFOAM/db/error/IOerror.C index 59f47f0751980df6c0825158598778902bac8b2c..c0cacb3185eead7e811f73cc91c6db236d7e9fc1 100644 --- a/src/OpenFOAM/db/error/IOerror.C +++ b/src/OpenFOAM/db/error/IOerror.C @@ -147,7 +147,7 @@ void Foam::IOerror::SafeFatalIOError << msg << nl << "file: " << ioStream.name() << " at line " << ioStream.lineNumber() << '.' << nl << nl - << " From function " << functionName << nl + << " From " << functionName << nl << " in file " << sourceFileName << " at line " << sourceFileLineNumber << '.' << std::endl; std::exit(1); @@ -172,17 +172,27 @@ Foam::IOerror::operator Foam::dictionary() const // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -void Foam::IOerror::exit(const int) +void Foam::IOerror::exitOrAbort(const int, const bool isAbort) { if (!throwExceptions_ && JobInfo::constructed) { jobInfo.add("FatalIOError", operator dictionary()); - jobInfo.exit(); + if (isAbort) + { + jobInfo.abort(); + } + else + { + jobInfo.exit(); + } } if (env("FOAM_ABORT")) { - abort(); + Perr<< nl << *this << nl + << "\nFOAM aborting (FOAM_ABORT set)\n" << endl; + printStack(Perr); + std::abort(); } else if (throwExceptions_) { @@ -196,108 +206,104 @@ void Foam::IOerror::exit(const int) } else if (Pstream::parRun()) { - Perr<< nl << *this << nl - << "\nFOAM parallel run exiting\n" << endl; - Pstream::exit(1); + if (isAbort) + { + Perr<< nl << *this << nl + << "\nFOAM parallel run aborting\n" << endl; + printStack(Perr); + Pstream::abort(); + } + else + { + Perr<< nl << *this << nl + << "\nFOAM parallel run exiting\n" << endl; + Pstream::exit(1); + } } else { - Perr<< nl << *this << nl - << "\nFOAM exiting\n" << endl; - std::exit(1); + if (isAbort) + { + Perr<< nl << *this << nl + << "\nFOAM aborting\n" << endl; + printStack(Perr); + + #ifdef _WIN32 + std::exit(1); // Prefer exit() to avoid unnecessary warnings + #else + std::abort(); + #endif + } + else + { + Perr<< nl << *this << nl + << "\nFOAM exiting\n" << endl; + std::exit(1); + } } } +void Foam::IOerror::exit(const int) +{ + exitOrAbort(1, env("FOAM_ABORT")); +} + + void Foam::IOerror::abort() { - if (!throwExceptions_ && JobInfo::constructed) - { - jobInfo.add("FatalIOError", operator dictionary()); - jobInfo.abort(); - } + exitOrAbort(1, true); +} - if (env("FOAM_ABORT")) + +void Foam::IOerror::write(Ostream& os, const bool includeTitle) const +{ + if (os.bad()) { - Perr<< nl << *this << nl - << "\nFOAM aborting (FOAM_ABORT set)\n" << endl; - printStack(Perr); - std::abort(); + return; } - else if (throwExceptions_) - { - // Make a copy of the error to throw - IOerror errorException(*this); - - // Reset the message buffer for the next error message - messageStreamPtr_->reset(); - throw errorException; - } - else if (Pstream::parRun()) + os << nl; + if (includeTitle && !title().empty()) { - Perr<< nl << *this << nl - << "\nFOAM parallel run aborting\n" << endl; - printStack(Perr); - Pstream::abort(); + os << title().c_str() << nl; } - else - { - Perr<< nl << *this << nl - << "\nFOAM aborting\n" << endl; - printStack(Perr); - #ifdef _WIN32 - std::exit(1); // Prefer exit() to avoid unnecessary warnings - #else - std::abort(); - #endif - } -} + os << message().c_str(); -void Foam::IOerror::write(Ostream& os, const bool includeTitle) const -{ - if (!os.bad()) + if (!ioFileName().empty()) { - os << nl; - if (includeTitle && !title().empty()) - { - os << title().c_str() << nl; - } - - os << message().c_str() << nl << nl; - - const bool hasFile = !ioFileName().empty(); + os << nl << nl + << "file: " << ioFileName().c_str(); - if (hasFile) + if (ioStartLineNumber() >= 0) { - os << "file: " << ioFileName().c_str(); - - if (ioStartLineNumber() >= 0) + os << " at line " << ioStartLineNumber(); + if (ioStartLineNumber() < ioEndLineNumber()) { - if (ioStartLineNumber() < ioEndLineNumber()) - { - os << " from line " << ioStartLineNumber() - << " to line " << ioEndLineNumber() << '.'; - } - else - { - os << " at line " << ioStartLineNumber() << '.'; - } + os << " to " << ioEndLineNumber(); } + os << '.'; } + } + + + const label lineNo = sourceFileLineNumber(); - if (IOerror::level >= 2 && sourceFileLineNumber()) + if (IOerror::level >= 2 && lineNo && !functionName().empty()) + { + os << nl << nl + << " From " << functionName().c_str() << nl; + + if (!sourceFileName().empty()) { - if (hasFile) + os << " in file " << sourceFileName().c_str(); + + if (lineNo > 0) { - os << nl << nl; + os << " at line " << lineNo << '.'; } - - os << " From function " << functionName().c_str() << nl - << " in file " << sourceFileName().c_str() - << " at line " << sourceFileLineNumber() << '.'; } } } diff --git a/src/OpenFOAM/db/error/error.C b/src/OpenFOAM/db/error/error.C index e113db8ae7d8f55c9d82ab41fd975b2125675cbf..dff61cfac9ad503417f7db4bd770b93bc74ff6d4 100644 --- a/src/OpenFOAM/db/error/error.C +++ b/src/OpenFOAM/db/error/error.C @@ -85,7 +85,7 @@ Foam::error::error(const string& title) if (!messageStreamPtr_->good()) { Perr<< nl - << "error::error(const string& title) : cannot open error stream" + << "error::error(const string&) : cannot open error stream" << endl; exit(1); } @@ -105,8 +105,7 @@ Foam::error::error(const dictionary& errDict) if (!messageStreamPtr_->good()) { Perr<< nl - << "error::error(const dictionary& errDict) : " - "cannot open error stream" + << "error::error(const dictionary&) : cannot open error stream" << endl; exit(1); } @@ -122,9 +121,7 @@ Foam::error::error(const error& err) sourceFileLineNumber_(err.sourceFileLineNumber_), throwExceptions_(err.throwExceptions_), messageStreamPtr_(new OStringStream(*err.messageStreamPtr_)) -{ - //*messageStreamPtr_ << err.message(); -} +{} // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // @@ -137,6 +134,19 @@ Foam::error::~error() noexcept // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // +Foam::OSstream& Foam::error::operator() +( + const string& functionName +) +{ + functionName_ = functionName; + sourceFileName_.clear(); + sourceFileLineNumber_ = -1; + + return operator OSstream&(); +} + + Foam::OSstream& Foam::error::operator() ( const char* functionName, @@ -213,17 +223,27 @@ void Foam::error::clear() const } -void Foam::error::exit(const int errNo) +void Foam::error::exitOrAbort(const int errNo, const bool isAbort) { if (!throwExceptions_ && JobInfo::constructed) { jobInfo.add("FatalError", operator dictionary()); - jobInfo.exit(); + if (isAbort) + { + jobInfo.abort(); + } + else + { + jobInfo.exit(); + } } if (env("FOAM_ABORT")) { - abort(); + Perr<< nl << *this << nl + << "\nFOAM aborting (FOAM_ABORT set)\n" << endl; + printStack(Perr); + std::abort(); } else if (throwExceptions_) { @@ -237,81 +257,87 @@ void Foam::error::exit(const int errNo) } else if (Pstream::parRun()) { - Perr<< nl << *this << nl - << "\nFOAM parallel run exiting\n" << endl; - Pstream::exit(errNo); + if (isAbort) + { + Perr<< nl << *this << nl + << "\nFOAM parallel run aborting\n" << endl; + printStack(Perr); + Pstream::abort(); + } + else + { + Perr<< nl << *this << nl + << "\nFOAM parallel run exiting\n" << endl; + Pstream::exit(errNo); + } } else { - Perr<< nl << *this << nl - << "\nFOAM exiting\n" << endl; - std::exit(errNo); + if (isAbort) + { + Perr<< nl << *this << nl + << "\nFOAM aborting\n" << endl; + printStack(Perr); + + #ifdef _WIN32 + std::exit(1); // Prefer exit() to avoid unnecessary warnings + #else + std::abort(); + #endif + } + else + { + Perr<< nl << *this << nl + << "\nFOAM exiting\n" << endl; + std::exit(errNo); + } } } -void Foam::error::abort() +void Foam::error::exit(const int errNo) { - if (!throwExceptions_ && JobInfo::constructed) - { - jobInfo.add("FatalError", operator dictionary()); - jobInfo.abort(); - } - - if (env("FOAM_ABORT")) - { - Perr<< nl << *this << nl - << "\nFOAM aborting (FOAM_ABORT set)\n" << endl; - printStack(Perr); - std::abort(); - } - else if (throwExceptions_) - { - // Make a copy of the error to throw - error errorException(*this); - - // Reset the message buffer for the next error message - messageStreamPtr_->reset(); + exitOrAbort(errNo, env("FOAM_ABORT")); +} - throw errorException; - } - else if (Pstream::parRun()) - { - Perr<< nl << *this << nl - << "\nFOAM parallel run aborting\n" << endl; - printStack(Perr); - Pstream::abort(); - } - else - { - Perr<< nl << *this << nl - << "\nFOAM aborting\n" << endl; - printStack(Perr); - #ifdef _WIN32 - std::exit(1); // Prefer exit() to avoid unnecessary warnings - #else - std::abort(); - #endif - } +void Foam::error::abort() +{ + exitOrAbort(1, true); } void Foam::error::write(Ostream& os, const bool includeTitle) const { + if (os.bad()) + { + return; + } + os << nl; - if (includeTitle) + if (includeTitle && !title().empty()) { os << title().c_str() << nl; } os << message().c_str(); - if (error::level >= 2 && sourceFileLineNumber()) + + const label lineNo = sourceFileLineNumber(); + + if (error::level >= 2 && lineNo && !functionName().empty()) { os << nl << nl - << " From function " << functionName().c_str() << nl - << " in file " << sourceFileName().c_str() - << " at line " << sourceFileLineNumber() << '.'; + << " From " << functionName().c_str() << nl; + + if (!sourceFileName().empty()) + { + os << " in file " << sourceFileName().c_str(); + + if (lineNo > 0) + { + os << " at line " << lineNo << '.'; + } + } } } diff --git a/src/OpenFOAM/db/error/error.H b/src/OpenFOAM/db/error/error.H index 650fae89cde64ee81065f8c4082a402dcd717e67..671ecbba1744bd7deb2a770ca914cb258a20e793 100644 --- a/src/OpenFOAM/db/error/error.H +++ b/src/OpenFOAM/db/error/error.H @@ -67,6 +67,12 @@ class error public std::exception, public messageStream { + // Private Member Functions + + //- Common code for exit or abort + void exitOrAbort(const int errNo, const bool isAbort); + + protected: // Protected Data @@ -113,23 +119,23 @@ public: //- Clear any messages void clear() const; - inline const string& functionName() const + const string& functionName() const { return functionName_; } - inline const string& sourceFileName() const + const string& sourceFileName() const { return sourceFileName_; } - inline label sourceFileLineNumber() const + label sourceFileLineNumber() const { return sourceFileLineNumber_; } //- Return the current exception throwing (on or off) - inline bool throwing() const + bool throwing() const { return throwExceptions_; } @@ -157,8 +163,15 @@ public: return throwExceptions(false); } - //- Convert to OSstream - // Prints basic message and returns OSstream for further info. + //- Define basic print message + // \return OSstream for further info. + OSstream& operator() + ( + const string& functionName + ); + + //- Define basic print message + // \return OSstream for further info. OSstream& operator() ( const char* functionName, @@ -166,8 +179,8 @@ public: const int sourceFileLineNumber = 0 ); - //- Convert to OSstream - // Prints basic message and returns OSstream for further info. + //- Define basic print message + // \return OSstream for further info. OSstream& operator() ( const string& functionName, @@ -175,16 +188,15 @@ public: const int sourceFileLineNumber = 0 ); - //- Convert to OSstream - // Prints basic message and returns OSstream for further info. - operator OSstream&(); - - //- Explicitly convert to OSstream for << operations + //- Explicit convert to OSstream for << operations OSstream& operator()() { return operator OSstream&(); } + //- Convert to OSstream + operator OSstream&(); + //- Create and return a dictionary representation of the error operator dictionary() const; @@ -197,7 +209,7 @@ public: static void printStack(Ostream& os); //- Exit : can be called for any error to exit program. - // Prints stack before exiting, when FOAM_ABORT is on. + // Redirects to abort() when FOAM_ABORT is on. void exit(const int errNo = 1); //- Abort : used to stop code for fatal errors. @@ -225,6 +237,12 @@ class IOerror label ioEndLineNumber_; + // Private Member Functions + + //- Common code for exit or abort + void exitOrAbort(const int errNo, const bool isAbort); + + public: // Constructors @@ -242,17 +260,17 @@ public: // Member Functions - inline const string& ioFileName() const + const string& ioFileName() const { return ioFileName_; } - inline label ioStartLineNumber() const + label ioStartLineNumber() const { return ioStartLineNumber_; } - inline label ioEndLineNumber() const + label ioEndLineNumber() const { return ioEndLineNumber_; } diff --git a/src/OpenFOAM/db/error/messageStream.C b/src/OpenFOAM/db/error/messageStream.C index 6d56bc634f0f3454af89c9a45d35b75972db7cdf..bb8a7e29ccf008c3c61a161efdb62afb7a8923b3 100644 --- a/src/OpenFOAM/db/error/messageStream.C +++ b/src/OpenFOAM/db/error/messageStream.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2017-2019 OpenCFD Ltd. + Copyright (C) 2017-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -85,6 +85,20 @@ Foam::OSstream& Foam::messageStream::masterStream(const label communicator) // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // +Foam::OSstream& Foam::messageStream::operator() +( + const string& functionName +) +{ + OSstream& os = operator OSstream&(); + + os << nl + << " From " << functionName.c_str() << nl; + + return os; +} + + Foam::OSstream& Foam::messageStream::operator() ( const char* functionName, @@ -95,7 +109,7 @@ Foam::OSstream& Foam::messageStream::operator() OSstream& os = operator OSstream&(); os << nl - << " From function " << functionName << nl + << " From " << functionName << nl << " in file " << sourceFileName << " at line " << sourceFileLineNumber << endl << " "; @@ -133,21 +147,18 @@ Foam::OSstream& Foam::messageStream::operator() OSstream& os = operator OSstream&(); os << nl - << " From function " << functionName << nl + << " From " << functionName << nl << " in file " << sourceFileName << " at line " << sourceFileLineNumber << nl << " Reading " << ioFileName; if (ioStartLineNumber >= 0) { + os << " at line " << ioStartLineNumber; + if (ioStartLineNumber < ioEndLineNumber) { - os << " from line " << ioStartLineNumber - << " to line " << ioEndLineNumber; - } - else - { - os << " at line " << ioStartLineNumber; + os << " to " << ioEndLineNumber; } } diff --git a/src/OpenFOAM/db/error/messageStream.H b/src/OpenFOAM/db/error/messageStream.H index 43bfd6d1f5e5fd152f9ea4d1f0190621e7c7b85b..7f1fcc92c9c32c0e590b8c8b0240341564f2f25b 100644 --- a/src/OpenFOAM/db/error/messageStream.H +++ b/src/OpenFOAM/db/error/messageStream.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2016-2019 OpenCFD Ltd. + Copyright (C) 2016-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -58,7 +58,7 @@ SourceFiles namespace Foam { -// Forward declarations +// Forward Declarations class IOstream; class Ostream; class OSstream; @@ -71,7 +71,6 @@ class dictionary; class messageStream { - public: //- Message type, or error severity flags @@ -87,7 +86,7 @@ public: protected: - // Private data + // Private Data string title_; errorSeverity severity_; @@ -97,9 +96,9 @@ protected: public: - // Static data + // Static Data - //- Controls the output verbosity of messageStream + //- Control the output verbosity of messageStream // // - level == 0 : suppress all output // - level == 1 : normal output @@ -119,27 +118,26 @@ public: const int maxErrors = 0 ); - //- Construct as Fatal from dictionary, extracting the 'title'. - messageStream(const dictionary& dict); + explicit messageStream(const dictionary& dict); // Member functions - //- Return the title of this error type + //- The title of this error type const string& title() const { return title_; } - //- Return the maximum number of errors before program termination + //- The maximum number of errors before program termination int maxErrors() const { return maxErrors_; } - //- Return non-const access to the maximum number of errors before - // program termination to enable user to reset it + //- Non-const access to the maximum number of errors before + //- program termination to enable user to reset it int& maxErrors() { return maxErrors_; @@ -149,9 +147,15 @@ public: // Prints to Pout for the master stream OSstream& masterStream(const label communicator); + //- Print basic message + // \return OSstream for further info. + OSstream& operator() + ( + const string& functionName + ); - //- Convert to OSstream - // Prints basic message and returns OSstream for further info. + //- Print basic message + // \return OSstream for further info. OSstream& operator() ( const char* functionName, @@ -159,8 +163,8 @@ public: const int sourceFileLineNumber = 0 ); - //- Convert to OSstream - // Prints basic message and returns OSstream for further info. + //- Print basic message + // \return OSstream for further info. OSstream& operator() ( const string& functionName, @@ -168,8 +172,8 @@ public: const int sourceFileLineNumber = 0 ); - //- Convert to OSstream - // Prints basic message and returns OSstream for further info. + //- Print basic message + // \return OSstream for further info. OSstream& operator() ( const char* functionName, @@ -180,8 +184,8 @@ public: const label ioEndLineNumber = -1 ); - //- Convert to OSstream - // Prints basic message and returns OSstream for further info. + //- Print basic message + // \return OSstream for further info. OSstream& operator() ( const char* functionName, @@ -190,8 +194,8 @@ public: const IOstream& ); - //- Convert to OSstream - // Prints basic message and returns OSstream for further info. + //- Print basic message + // \return OSstream for further info. OSstream& operator() ( const char* functionName,