From 410d38b9d93f353c2ce208092d402197c5dac892 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 23 Oct 2020 16:55:21 +0200 Subject: [PATCH] ENH: support true/false, yes/no values for FOAM_ABORT (#1896) - consistent with FOAM_SIGFPE etc. - centralize code as error::useAbort() static function to avoid scattering the logic throughout the code. ENH: also accept "0" and "1" string values for Switch - not the normal path for Switch input (eg, from a dictionary), but consistent with bool definitions and simplifies string parsing. This means that `FOAM_SIGFPE=1 application` will now also work. --- etc/bashrc | 4 ++++ etc/cshrc | 4 ++++ src/OpenFOAM/db/error/IOerror.C | 4 ++-- src/OpenFOAM/db/error/error.C | 12 ++++++++++-- src/OpenFOAM/db/error/error.H | 3 +++ .../functionObjectList/functionObjectList.C | 2 +- src/OpenFOAM/primitives/bools/Switch/Switch.C | 6 +++--- src/OpenFOAM/primitives/bools/Switch/Switch.H | 3 ++- 8 files changed, 29 insertions(+), 9 deletions(-) diff --git a/etc/bashrc b/etc/bashrc index 4d67de5324..d84355827d 100644 --- a/etc/bashrc +++ b/etc/bashrc @@ -129,6 +129,10 @@ projectDir="$HOME/OpenFOAM/OpenFOAM-$WM_PROJECT_VERSION" # = true | false #export FOAM_SETNAN=false +# [FOAM_ABORT] - Treat exit() on FatalError as abort() +# = true | false +#export FOAM_ABORT=false + # [FOAM_CODE_TEMPLATES] - dynamicCode templates # - unset: uses 'foamEtcFile -list codeTemplates/dynamicCode' ##export FOAM_CODE_TEMPLATES="$WM_PROJECT_DIR/etc/codeTemplates/dynamicCode" diff --git a/etc/cshrc b/etc/cshrc index ef1f205a70..f15bf120a2 100644 --- a/etc/cshrc +++ b/etc/cshrc @@ -131,6 +131,10 @@ set projectDir=`lsof +p $$ |& sed -ne 's#^[^/]*##;\@/'"$projectName"'[^/]*/etc/c # = true | false #setenv FOAM_SETNAN false +# [FOAM_ABORT] - Treat exit() on FatalError as abort() +# = true | false +#setenv FOAM_ABORT false + # [FOAM_CODE_TEMPLATES] - dynamicCode templates # - unset: uses 'foamEtcFile -list codeTemplates/dynamicCode' ##setenv FOAM_CODE_TEMPLATES "$WM_PROJECT_DIR/etc/codeTemplates/dynamicCode" diff --git a/src/OpenFOAM/db/error/IOerror.C b/src/OpenFOAM/db/error/IOerror.C index a60012f037..588aea1639 100644 --- a/src/OpenFOAM/db/error/IOerror.C +++ b/src/OpenFOAM/db/error/IOerror.C @@ -177,7 +177,7 @@ void Foam::IOerror::exitOrAbort(const int errNo, const bool isAbort) if (!throwing_ && JobInfo::constructed) { jobInfo.add("FatalIOError", operator dictionary()); - if (isAbort || hasEnv("FOAM_ABORT")) + if (isAbort || error::useAbort()) { jobInfo.abort(); } @@ -197,7 +197,7 @@ void Foam::IOerror::exitOrAbort(const int errNo, const bool isAbort) throw errorException; } - else if (hasEnv("FOAM_ABORT")) + else if (error::useAbort()) { Perr<< nl << *this << nl << "\nFOAM aborting (FOAM_ABORT set)\n" << endl; diff --git a/src/OpenFOAM/db/error/error.C b/src/OpenFOAM/db/error/error.C index d6ac9f1d2f..def5ac8386 100644 --- a/src/OpenFOAM/db/error/error.C +++ b/src/OpenFOAM/db/error/error.C @@ -34,6 +34,7 @@ License #include "Pstream.H" #include "foamVersion.H" #include "OSspecific.H" +#include "Switch.H" // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // @@ -70,6 +71,13 @@ void Foam::error::warnAboutAge(const char* what, const int version) } +bool Foam::error::useAbort() +{ + // FOAM_ABORT env set and contains bool-type value + return static_cast(Switch::find(Foam::getEnv("FOAM_ABORT"))); +} + + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::error::error(const string& title) @@ -197,7 +205,7 @@ void Foam::error::exitOrAbort(const int errNo, const bool isAbort) if (!throwing_ && JobInfo::constructed) { jobInfo.add("FatalError", operator dictionary()); - if (isAbort || hasEnv("FOAM_ABORT")) + if (isAbort || error::useAbort()) { jobInfo.abort(); } @@ -217,7 +225,7 @@ void Foam::error::exitOrAbort(const int errNo, const bool isAbort) throw errorException; } - else if (hasEnv("FOAM_ABORT")) + else if (error::useAbort()) { Perr<< nl << *this << nl << "\nFOAM aborting (FOAM_ABORT set)\n" << endl; diff --git a/src/OpenFOAM/db/error/error.H b/src/OpenFOAM/db/error/error.H index 0655d69357..3ce8c20db2 100644 --- a/src/OpenFOAM/db/error/error.H +++ b/src/OpenFOAM/db/error/error.H @@ -215,6 +215,9 @@ public: //- Helper function to print a stack static void printStack(Ostream& os); + //- True if FOAM_ABORT is on. + static bool useAbort(); + //- Exit : can be called for any error to exit program. // Redirects to abort() when FOAM_ABORT is on. void exit(const int errNo = 1); diff --git a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C index b012a5c3a4..837402f0fa 100644 --- a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C +++ b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C @@ -71,7 +71,7 @@ namespace Foam //- Mimic exit handling of the error class static void exitNow(const error& err) { - if (hasEnv("FOAM_ABORT")) + if (error::useAbort()) { Perr<< nl << err << nl << "\nFOAM aborting (FOAM_ABORT set)\n" << endl; diff --git a/src/OpenFOAM/primitives/bools/Switch/Switch.C b/src/OpenFOAM/primitives/bools/Switch/Switch.C index 6ef876d200..e8bac8c15e 100644 --- a/src/OpenFOAM/primitives/bools/Switch/Switch.C +++ b/src/OpenFOAM/primitives/bools/Switch/Switch.C @@ -92,13 +92,13 @@ Foam::Switch::switchType Foam::Switch::parse { switch (str.size()) { - case 1: // (f|n|t|y) - single-character forms + case 1: // (0|1|f|t|n|y) { switch (str[0]) { - case 'f': return switchType::FALSE; + case '0': case 'f': return switchType::FALSE; + case '1': case 't': return switchType::TRUE; case 'n': return switchType::NO; - case 't': return switchType::TRUE; case 'y': return switchType::YES; } break; diff --git a/src/OpenFOAM/primitives/bools/Switch/Switch.H b/src/OpenFOAM/primitives/bools/Switch/Switch.H index 4c099f51f7..067e208d88 100644 --- a/src/OpenFOAM/primitives/bools/Switch/Switch.H +++ b/src/OpenFOAM/primitives/bools/Switch/Switch.H @@ -29,7 +29,8 @@ Class Description A simple wrapper around bool so that it can be read as a word: - true/false, on/off, yes/no, y/n, t/f, or none. + true/false, on/off, yes/no, any/none. + Also accepts 0/1 as a string and shortcuts t/f, y/n. SourceFiles Switch.C