From bbde236be57e876de95b1eefed6d46241b609962 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 17 May 2024 10:12:41 +0200 Subject: [PATCH] ENH: simpler internal handling of stderr in message streams - delay construction of message buffer - OStringStream count() method to test if anything has been streamed STYLE: explicit use of std::ios_base in IOstreams - document the return information of set flag methods --- applications/test/OCountStream/Make/files | 2 +- ...t-OCountStream.C => Test-OCountStream.cxx} | 6 +- src/OpenFOAM/db/IOstreams/IOstreams/IOmanip.H | 4 +- .../db/IOstreams/IOstreams/IOstream.C | 12 +-- .../db/IOstreams/IOstreams/IOstream.H | 24 ++--- .../db/IOstreams/Pstreams/UIPstream.H | 14 +-- .../db/IOstreams/Pstreams/UOPstream.H | 25 +++-- src/OpenFOAM/db/IOstreams/Sstreams/ISstream.H | 11 +- src/OpenFOAM/db/IOstreams/Sstreams/OSstream.H | 11 +- .../db/IOstreams/StringStreams/StringStream.H | 3 + src/OpenFOAM/db/IOstreams/Tstreams/ITstream.H | 14 +-- src/OpenFOAM/db/IOstreams/Tstreams/OTstream.H | 14 +-- .../db/IOstreams/dummy/dummyISstream.H | 14 +-- src/OpenFOAM/db/error/IOerror.C | 13 +-- src/OpenFOAM/db/error/error.C | 40 ++++--- src/OpenFOAM/db/error/error.H | 16 ++- src/OpenFOAM/db/error/messageStream.C | 102 +++++++++++------- src/OpenFOAM/db/error/messageStream.H | 56 +++++++--- .../adjoint/objectives/objective/objective.C | 5 +- src/sampling/probes/probes.C | 2 +- 20 files changed, 238 insertions(+), 150 deletions(-) rename applications/test/OCountStream/{Test-OCountStream.C => Test-OCountStream.cxx} (96%) diff --git a/applications/test/OCountStream/Make/files b/applications/test/OCountStream/Make/files index 87df932eea..504d53099d 100644 --- a/applications/test/OCountStream/Make/files +++ b/applications/test/OCountStream/Make/files @@ -1,3 +1,3 @@ -Test-OCountStream.C +Test-OCountStream.cxx EXE = $(FOAM_USER_APPBIN)/Test-OCountStream diff --git a/applications/test/OCountStream/Test-OCountStream.C b/applications/test/OCountStream/Test-OCountStream.cxx similarity index 96% rename from applications/test/OCountStream/Test-OCountStream.C rename to applications/test/OCountStream/Test-OCountStream.cxx index 85ffab4e0c..1d073efe43 100644 --- a/applications/test/OCountStream/Test-OCountStream.C +++ b/applications/test/OCountStream/Test-OCountStream.cxx @@ -65,11 +65,11 @@ int main(int argc, char *argv[]) OCountStream cnt; OCharStream cstr; - OStringStream str; + OStringStream sstr; ocountstream plain; generateOutput(cstr); - generateOutput(str); + generateOutput(sstr); generateOutput(cnt); generateOutput(plain); @@ -77,7 +77,7 @@ int main(int argc, char *argv[]) Info<< "counter state: " << (cnt.stdStream().rdstate()) << nl << "via char-stream: " << label(cstr.view().size()) << " chars" << nl - << "via string-stream: " << str.str().size() << " chars" << nl + << "via string-stream: " << label(sstr.count()) << " chars" << nl << "via ocountstream: " << plain.count() << " chars" << endl; fileName outputName; diff --git a/src/OpenFOAM/db/IOstreams/IOstreams/IOmanip.H b/src/OpenFOAM/db/IOstreams/IOstreams/IOmanip.H index 4df38b3276..08456800d5 100644 --- a/src/OpenFOAM/db/IOstreams/IOstreams/IOmanip.H +++ b/src/OpenFOAM/db/IOstreams/IOstreams/IOmanip.H @@ -166,9 +166,9 @@ inline Ostream& operator<<(Ostream& os, const Omanip& m) // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -inline Smanip setf(const ios_base::fmtflags flags) +inline Smanip setf(std::ios_base::fmtflags flags) { - return Smanip(&IOstream::setf, flags); + return Smanip(&IOstream::setf, flags); } diff --git a/src/OpenFOAM/db/IOstreams/IOstreams/IOstream.C b/src/OpenFOAM/db/IOstreams/IOstreams/IOstream.C index a627e382bf..889b2e19f6 100644 --- a/src/OpenFOAM/db/IOstreams/IOstreams/IOstream.C +++ b/src/OpenFOAM/db/IOstreams/IOstreams/IOstream.C @@ -114,23 +114,19 @@ void Foam::IOstream::print(Ostream& os, const int streamState) const { if (streamState == std::ios_base::goodbit) { - os << "ios_base::goodbit set : the last operation on stream succeeded" - << endl; + os << "goodbit set : the last operation on stream succeeded" << endl; } else if (streamState & std::ios_base::badbit) { - os << "ios_base::badbit set : characters possibly lost" - << endl; + os << "badbit set : characters possibly lost" << endl; } else if (streamState & std::ios_base::failbit) { - os << "ios_base::failbit set : some type of formatting error" - << endl; + os << "failbit set : some type of formatting error" << endl; } else if (streamState & std::ios_base::eofbit) { - os << "ios_base::eofbit set : at end of stream" - << endl; + os << "eofbit set : at end of stream" << endl; } } diff --git a/src/OpenFOAM/db/IOstreams/IOstreams/IOstream.H b/src/OpenFOAM/db/IOstreams/IOstreams/IOstream.H index 01546afc1b..b67807efcd 100644 --- a/src/OpenFOAM/db/IOstreams/IOstreams/IOstream.H +++ b/src/OpenFOAM/db/IOstreams/IOstreams/IOstream.H @@ -339,9 +339,6 @@ public: return old; } - //- Return flags of stream - virtual ios_base::fmtflags flags() const = 0; - //- Return the default precision static unsigned int defaultPrecision() noexcept { @@ -387,27 +384,30 @@ public: ioState_ |= std::ios_base::badbit; } - //- Set flags of stream - virtual ios_base::fmtflags flags(const ios_base::fmtflags f) = 0; + //- Return current stream flags + virtual std::ios_base::fmtflags flags() const = 0; - //- Set flags of stream - ios_base::fmtflags setf(const ios_base::fmtflags f) + //- Set stream flags, return old stream flags + virtual std::ios_base::fmtflags flags(std::ios_base::fmtflags) = 0; + + //- Set stream flag(s), return old stream flags + std::ios_base::fmtflags setf(std::ios_base::fmtflags f) { return flags(flags() | f); } - //- Set flags of given field of stream - ios_base::fmtflags setf + //- Set stream flag(s) with mask, return old stream flags + std::ios_base::fmtflags setf ( - const ios_base::fmtflags f, - const ios_base::fmtflags mask + const std::ios_base::fmtflags f, + const std::ios_base::fmtflags mask ) { return flags((flags() & ~mask) | (f & mask)); } //- Unset flags of stream - void unsetf(const ios_base::fmtflags f) + void unsetf(std::ios_base::fmtflags f) { flags(flags() & ~f); } diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.H b/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.H index 138dc465a8..eb2deb9a87 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.H +++ b/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.H @@ -151,16 +151,18 @@ public: // Stream State Functions - //- Return stream flags - virtual ios_base::fmtflags flags() const override + //- Return current stream flags. + //- Dummy for parallel stream, returns 0. + virtual std::ios_base::fmtflags flags() const override { - return ios_base::fmtflags(0); + return std::ios_base::fmtflags(0); } - //- Set flags of stream flags - virtual ios_base::fmtflags flags(const ios_base::fmtflags) override + //- Set stream flags, return old stream flags. + //- Dummy for parallel stream, returns 0. + virtual std::ios_base::fmtflags flags(std::ios_base::fmtflags) override { - return ios_base::fmtflags(0); + return std::ios_base::fmtflags(0); } diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.H b/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.H index 8519e03f81..e96a05d875 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.H +++ b/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.H @@ -144,12 +144,20 @@ public: // Member Functions - // Inquiry + // Stream State Functions - //- Return flags of output stream - virtual ios_base::fmtflags flags() const override + //- Return current stream flags. + //- Dummy for parallel stream, returns 0. + virtual std::ios_base::fmtflags flags() const override { - return ios_base::fmtflags(0); + return std::ios_base::fmtflags(0); + } + + //- Set stream flags, return old stream flags. + //- Dummy for parallel stream, returns 0. + virtual std::ios_base::fmtflags flags(std::ios_base::fmtflags) override + { + return std::ios_base::fmtflags(0); } @@ -281,15 +289,6 @@ public: virtual void rewind(); - // Edit - - //- Set flags of stream - virtual ios_base::fmtflags flags(const ios_base::fmtflags) override - { - return ios_base::fmtflags(0); - } - - // Print //- Print stream description to Ostream diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.H b/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.H index cb09f060fe..223784367a 100644 --- a/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.H +++ b/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.H @@ -147,14 +147,17 @@ public: // Stream State - //- Return flags of output stream - virtual ios_base::fmtflags flags() const override + //- Return current stream flags + virtual std::ios_base::fmtflags flags() const override { return is_.flags(); } - //- Set stream flags - virtual ios_base::fmtflags flags(const ios_base::fmtflags f) override + //- Set stream flags, return old stream flags + virtual std::ios_base::fmtflags flags + ( + std::ios_base::fmtflags f + ) override { return is_.flags(f); } diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.H b/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.H index dd76e3d8b0..3344fc7739 100644 --- a/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.H +++ b/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.H @@ -138,14 +138,17 @@ public: // Stream State - //- Get stream flags - virtual ios_base::fmtflags flags() const override + //- Get current stream flags + virtual std::ios_base::fmtflags flags() const override { return os_.flags(); } - //- Set stream flags - virtual ios_base::fmtflags flags(const ios_base::fmtflags f) override + //- Set stream flags, return old stream flags + virtual std::ios_base::fmtflags flags + ( + std::ios_base::fmtflags f + ) override { return os_.flags(f); } diff --git a/src/OpenFOAM/db/IOstreams/StringStreams/StringStream.H b/src/OpenFOAM/db/IOstreams/StringStreams/StringStream.H index 0950df267a..63089e40e9 100644 --- a/src/OpenFOAM/db/IOstreams/StringStreams/StringStream.H +++ b/src/OpenFOAM/db/IOstreams/StringStreams/StringStream.H @@ -215,6 +215,9 @@ public: // Member Functions + //- The number of bytes outputted + std::streamsize count() { return stream_.tellp(); } + //- Get the string. //- As Foam::string instead of std::string (may change in future) Foam::string str() const { return Foam::string(stream_.str()); } diff --git a/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.H b/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.H index 1ec2d0feb8..37e5571e8d 100644 --- a/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.H +++ b/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.H @@ -499,16 +499,18 @@ public: // Stream State Functions - //- Get stream flags - always 0 - virtual ios_base::fmtflags flags() const override + //- Return current stream flags. + //- Dummy for token stream, returns 0. + virtual std::ios_base::fmtflags flags() const override { - return ios_base::fmtflags(0); + return std::ios_base::fmtflags(0); } - //- Set flags of stream - ignored - ios_base::fmtflags flags(const ios_base::fmtflags) override + //- Set stream flags, return old stream flags. + //- Dummy for token stream, returns 0. + std::ios_base::fmtflags flags(std::ios_base::fmtflags) override { - return ios_base::fmtflags(0); + return std::ios_base::fmtflags(0); } diff --git a/src/OpenFOAM/db/IOstreams/Tstreams/OTstream.H b/src/OpenFOAM/db/IOstreams/Tstreams/OTstream.H index b369608326..7af4253e98 100644 --- a/src/OpenFOAM/db/IOstreams/Tstreams/OTstream.H +++ b/src/OpenFOAM/db/IOstreams/Tstreams/OTstream.H @@ -181,16 +181,18 @@ public: // Stream State Functions - //- Get flags of output stream - virtual ios_base::fmtflags flags() const override + //- Return current stream flags. + //- Dummy for token stream, returns 0. + virtual std::ios_base::fmtflags flags() const override { - return ios_base::fmtflags(0); + return std::ios_base::fmtflags(0); } - //- Set flags of stream - ignored - std::ios_base::fmtflags flags(const ios_base::fmtflags) override + //- Set stream flags, return old stream flags. + //- Dummy for token stream, returns 0. + std::ios_base::fmtflags flags(std::ios_base::fmtflags) override { - return ios_base::fmtflags(0); + return std::ios_base::fmtflags(0); } //- Flush stream diff --git a/src/OpenFOAM/db/IOstreams/dummy/dummyISstream.H b/src/OpenFOAM/db/IOstreams/dummy/dummyISstream.H index 6b50f50c6b..ba9fce3320 100644 --- a/src/OpenFOAM/db/IOstreams/dummy/dummyISstream.H +++ b/src/OpenFOAM/db/IOstreams/dummy/dummyISstream.H @@ -73,16 +73,18 @@ public: // Stream-state - //- Return flags of stream - virtual ios_base::fmtflags flags() const override + //- Return current stream flags. + //- Dummy for dummy stream, returns 0. + virtual std::ios_base::fmtflags flags() const override { - return ios_base::fmtflags(0); + return std::ios_base::fmtflags(0); } - //- Set flags of stream - virtual ios_base::fmtflags flags(const ios_base::fmtflags) override + //- Set stream flags, return old stream flags. + //- Dummy for dummy stream, returns 0. + virtual std::ios_base::fmtflags flags(std::ios_base::fmtflags) override { - return ios_base::fmtflags(0); + return std::ios_base::fmtflags(0); } diff --git a/src/OpenFOAM/db/error/IOerror.C b/src/OpenFOAM/db/error/IOerror.C index ebde730a09..23029959ff 100644 --- a/src/OpenFOAM/db/error/IOerror.C +++ b/src/OpenFOAM/db/error/IOerror.C @@ -30,15 +30,15 @@ Note \*---------------------------------------------------------------------------*/ #include "error.H" -#include "StringStream.H" #include "fileName.H" #include "dictionary.H" #include "JobInfo.H" #include "Pstream.H" +#include "StringStream.H" // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -Foam::IOerror::IOerror(const string& title) +Foam::IOerror::IOerror(const char* title) : error(title), ioFileName_("unknown"), @@ -171,7 +171,7 @@ void Foam::IOerror::SafeFatalIOError const char* sourceFileName, const int sourceFileLineNumber, const IOstream& ioStream, - const string& msg + const std::string& msg ) { if (JobInfo::constructed) @@ -182,10 +182,11 @@ void Foam::IOerror::SafeFatalIOError sourceFileName, sourceFileLineNumber, ioStream - ) << msg << Foam::exit(FatalIOError); + ) << msg.c_str() << Foam::exit(FatalIOError); } else { + // Without (openfoam=API patch=NN) since it is rarely used std::cerr << nl << "--> FOAM FATAL IO ERROR:" << nl @@ -225,7 +226,7 @@ void Foam::IOerror::exiting(const int errNo, const bool isAbort) IOerror errorException(*this); // Reset the message buffer for the next error message - messageStreamPtr_->reset(); + error::clear(); throw errorException; return; @@ -297,7 +298,7 @@ void Foam::IOerror::write(Ostream& os, const bool withTitle) const const label lineNo = sourceFileLineNumber(); - if (IOerror::level >= 2 && lineNo && !functionName().empty()) + if (messageStream::level >= 2 && lineNo && !functionName().empty()) { os << nl << nl << " From " << functionName().c_str() << nl; diff --git a/src/OpenFOAM/db/error/error.C b/src/OpenFOAM/db/error/error.C index 120ac2427a..c4f327c95d 100644 --- a/src/OpenFOAM/db/error/error.C +++ b/src/OpenFOAM/db/error/error.C @@ -123,7 +123,7 @@ bool Foam::error::useAbort() // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -Foam::error::error(const string& title) +Foam::error::error(const char* title) : std::exception(), messageStream(title, messageStream::FATAL), @@ -131,7 +131,7 @@ Foam::error::error(const string& title) sourceFileName_("unknown"), sourceFileLineNumber_(0), throwing_(false), - messageStreamPtr_(new OStringStream()) + messageStreamPtr_(nullptr) {} @@ -143,7 +143,7 @@ Foam::error::error(const dictionary& errDict) sourceFileName_(errDict.get("sourceFileName")), sourceFileLineNumber_(errDict.get