From f425424eef1f4767f2d8c50dac8931278c1e357b Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Thu, 15 Oct 2020 17:31:47 +0200
Subject: [PATCH] ENH: add api/patch information to FatalError (#1880)

- can be useful when confronted with partial outputs

- provision for redirecting error/warning
---
 src/OpenFOAM/db/error/IOerror.C       | 11 +++++++++--
 src/OpenFOAM/db/error/error.C         | 10 +++++++++-
 src/OpenFOAM/db/error/messageStream.C | 26 +++++++++++++-------------
 src/OpenFOAM/db/error/messageStream.H |  7 ++++++-
 4 files changed, 37 insertions(+), 17 deletions(-)

diff --git a/src/OpenFOAM/db/error/IOerror.C b/src/OpenFOAM/db/error/IOerror.C
index 6d632a254ee..a60012f037d 100644
--- a/src/OpenFOAM/db/error/IOerror.C
+++ b/src/OpenFOAM/db/error/IOerror.C
@@ -268,9 +268,16 @@ void Foam::IOerror::write(Ostream& os, const bool includeTitle) const
     os  << nl;
     if (includeTitle && !title().empty())
     {
-        os  << title().c_str() << nl;
-    }
+        os  << title().c_str()
+            << "(openfoam-" << foamVersion::api;
 
+        if (foamVersion::patched())
+        {
+            // Patch-level, when defined
+            os  << " patch=" << foamVersion::patch.c_str();
+        }
+        os  << ')' << nl;
+    }
     os  << message().c_str();
 
 
diff --git a/src/OpenFOAM/db/error/error.C b/src/OpenFOAM/db/error/error.C
index 380d5216212..d6ac9f1d2fd 100644
--- a/src/OpenFOAM/db/error/error.C
+++ b/src/OpenFOAM/db/error/error.C
@@ -300,7 +300,15 @@ void Foam::error::write(Ostream& os, const bool includeTitle) const
     os  << nl;
     if (includeTitle && !title().empty())
     {
-        os  << title().c_str() << nl;
+        os  << title().c_str()
+            << "(openfoam-" << foamVersion::api;
+
+        if (foamVersion::patched())
+        {
+            // Patch-level, when defined
+            os  << " patch=" << foamVersion::patch.c_str();
+        }
+        os  << ')' << nl;
     }
     os  << message().c_str();
 
diff --git a/src/OpenFOAM/db/error/messageStream.C b/src/OpenFOAM/db/error/messageStream.C
index 03cb1962c3b..0d6778ef35c 100644
--- a/src/OpenFOAM/db/error/messageStream.C
+++ b/src/OpenFOAM/db/error/messageStream.C
@@ -35,6 +35,8 @@ License
 // Default is 2 : report source file name and line number if available
 int Foam::messageStream::level(Foam::debug::debugSwitch("level", 2));
 
+int Foam::messageStream::redirect(0);
+
 // Default is 1 : report to Info
 int Foam::infoDetailLevel(1);
 
@@ -212,35 +214,33 @@ Foam::messageStream::operator Foam::OSstream&()
 {
     if (level)
     {
-        // stderr instead of stdout
-        // - INFO_STDERR
-        // - WARNING when infoDetailLevel == 0
-        const bool useSerr =
-        (
-            (severity_ == INFO_STDERR)
-         || (severity_ == WARNING && Foam::infoDetailLevel == 0)
-        );
-
         const bool collect =
         (
             severity_ == INFO
          || severity_ == WARNING
-         || useSerr
+         || severity_ == INFO_STDERR
         );
 
-
         // Could add guard with parRun
         if (collect && !Pstream::master())
         {
             return Snull;
         }
 
+        // Use stderr instead of stdout
+        // - INFO_STDERR
+        // - WARNING when infoDetailLevel == 0
+        const bool useStderr =
+        (
+            (severity_ == INFO_STDERR)
+         || (severity_ == WARNING && Foam::infoDetailLevel == 0)
+        );
 
         OSstream& os =
         (
             (collect || !Pstream::parRun())
-          ? (useSerr ? Serr : Sout)
-          : Pout
+          ? (useStderr ? Serr : Sout)
+          : (useStderr ? Perr : Pout)
         );
 
 
diff --git a/src/OpenFOAM/db/error/messageStream.H b/src/OpenFOAM/db/error/messageStream.H
index cff2c8a1983..1ab03aa95be 100644
--- a/src/OpenFOAM/db/error/messageStream.H
+++ b/src/OpenFOAM/db/error/messageStream.H
@@ -98,7 +98,7 @@ public:
 
     // Static Data
 
-        //- Control the output verbosity of messageStream
+        //- The output level (verbosity) of messages
         //
         //  - level == 0 : suppress all output
         //  - level == 1 : normal output
@@ -107,6 +107,11 @@ public:
         // \note The default level is normally 2.
         static int level;
 
+        //- The output redirection of messages
+        //
+        //  - redirect == 2 : use stderr instead of stdout
+        static int redirect;
+
 
     // Constructors
 
-- 
GitLab