Commit b0a999ca authored by Mark OLESEN's avatar Mark OLESEN Committed by Andrew Heather
Browse files

ENH: improve reporting for optional dictionary entries (#1468)

- now reports the dictionary, entry, default. For example,

  Dictionary: <case>/0/nut.boundaryField.wall Entry: Cmu Default: 0.09
  Dictionary: <case>/0/nut.boundaryField.wall Entry: kappa Default: 0.41
  Dictionary: <case>/0/nut.boundaryField.wall Entry: E Default: 9.8

- suppress reporting for debug, optimisation switches since this
  generates extremely noisy output
parent e3d4d793
......@@ -32,6 +32,8 @@ License
#include "dictionaryEntry.H"
#include "regExp.H"
#include "OSHA1stream.H"
#include "argList.H"
#include "registerSwitch.H"
/* * * * * * * * * * * * * * * Static Member Data * * * * * * * * * * * * * */
......@@ -48,6 +50,31 @@ int Foam::dictionary::writeOptionalEntries
);
registerInfoSwitch
(
"writeOptionalEntries",
int,
Foam::dictionary::writeOptionalEntries
);
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
Foam::fileName Foam::dictionary::relativeName(const bool caseTag) const
{
const fileName caseDir(argList::envGlobalPath());
if (!caseDir.empty() && name().isAbsolute())
{
return name().relative(caseDir, caseTag);
}
else
{
return name();
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::dictionary::dictionary()
......
......@@ -284,11 +284,6 @@ private:
// Private Data
//- Report optional keywords and values if not present in dictionary
// For value greater than 1: fatal.
// Set/unset via an InfoSwitch
static int writeOptionalEntries;
//- The dictionary name
fileName name_;
......@@ -363,12 +358,29 @@ private:
//- Emit IOError about bad input for the entry
void raiseBadInput(const word& keyword) const;
//- The case-relative dictionary name. Uses FOAM_CASE
fileName relativeName(const bool caseTag=false) const;
//- Report (on stderr) that the keyword default value was used
template<class T>
void reportDefault
(
const word& keyword,
const T& deflt,
const bool added = false
) const;
public:
// Declare name of the class and its debug switch
ClassName("dictionary");
//- Report optional keywords and values if not present in dictionary
// For value greater than 1: fatal.
// Set/unset via an InfoSwitch
static int writeOptionalEntries;
//- An empty dictionary, which is also the parent for all dictionaries
static const dictionary null;
......
......@@ -65,9 +65,8 @@ Foam::dictionary::const_searcher Foam::dictionary::csearchCompat
if (finder.good())
{
// Only want a single warning (on master), but guard with a
// parRun check to avoid Pstream::master() when Pstream has not
// yet been initialized
// Want single warning (on master), but guard with parRun to avoid
// Pstream::master() when Pstream has not yet been initialized
if
(
shouldWarnVersion(iter.second)
......
......@@ -28,6 +28,30 @@ License
#include "dictionary.H"
#include "primitiveEntry.H"
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
template<class T>
void Foam::dictionary::reportDefault
(
const word& keyword,
const T& deflt,
const bool added
) const
{
InfoErr
<< "Dictionary: " << relativeName(true).c_str()
<< " Entry: " << keyword;
if (added)
{
InfoErr
<< " Added";
}
InfoErr
<< " Default: " << deflt << nl;
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Compare>
......@@ -118,15 +142,13 @@ T Foam::dictionary::getOrDefault
if (writeOptionalEntries > 1)
{
FatalIOErrorInFunction(*this)
<< "Optional entry '" << keyword
<< "' not found. Default '" << deflt << "' ignored" << nl
<< "No optional entry: " << keyword
<< " Default: " << deflt << nl
<< exit(FatalIOError);
}
else
{
IOInfoInFunction(*this)
<< "Optional entry '" << keyword
<< "' not found. Using default '" << deflt << "'" << nl;
reportDefault(keyword, deflt);
}
}
......@@ -160,15 +182,13 @@ T Foam::dictionary::getOrAdd
if (writeOptionalEntries > 1)
{
FatalIOErrorInFunction(*this)
<< "Optional entry '" << keyword
<< "' not found. Default '" << deflt << "' ignored" << nl
<< "No optional entry: " << keyword
<< " Default: " << deflt << nl
<< exit(FatalIOError);
}
else
{
IOInfoInFunction(*this)
<< "Optional entry '" << keyword
<< "' not found. Adding default '" << deflt << "'" << nl;
reportDefault(keyword, deflt, true);
}
}
......@@ -218,15 +238,13 @@ T Foam::dictionary::getCheckOrDefault
if (writeOptionalEntries > 1)
{
FatalIOErrorInFunction(*this)
<< "Optional entry '" << keyword
<< "' not found. Default '" << deflt << "' ignored" << nl
<< "No optional entry: " << keyword
<< " Default: " << deflt << nl
<< exit(FatalIOError);
}
else
{
IOInfoInFunction(*this)
<< "Optional entry '" << keyword
<< "' not found. Using default '" << deflt << "'" << nl;
reportDefault(keyword, deflt);
}
}
......@@ -275,15 +293,13 @@ T Foam::dictionary::getCheckOrAdd
if (writeOptionalEntries > 1)
{
FatalIOErrorInFunction(*this)
<< "Optional entry '" << keyword
<< "' not found. Default '" << deflt << "' ignored" << nl
<< "No optional entry: " << keyword
<< " Default: " << deflt << nl
<< exit(FatalIOError);
}
else
{
IOInfoInFunction(*this)
<< "Optional entry '" << keyword
<< "' not found. Adding default '" << deflt << "'" << nl;
reportDefault(keyword, deflt, true);
}
}
......@@ -449,15 +465,13 @@ T Foam::dictionary::getOrDefaultCompat
if (writeOptionalEntries > 1)
{
FatalIOErrorInFunction(*this)
<< "Optional entry '" << keyword
<< "' not found. Default '" << deflt << "' ignored" << nl
<< "No optional entry: " << keyword
<< " Default: " << deflt << nl
<< exit(FatalIOError);
}
else
{
IOInfoInFunction(*this)
<< "Optional entry '" << keyword
<< "' not found. Using default '" << deflt << "'" << nl;
reportDefault(keyword, deflt);
}
}
......
......@@ -62,13 +62,8 @@ simpleObjectRegistry* dimensionedConstantObjectsPtr_(nullptr);
// To ensure controlDictPtr_ is deleted at the end of the run
class deleteControlDictPtr
struct deleteControlDictPtr
{
public:
deleteControlDictPtr()
{}
~deleteControlDictPtr()
{
deleteDemandDrivenData(debugObjectsPtr_);
......@@ -92,6 +87,35 @@ deleteControlDictPtr deleteControlDictPtr_;
} // End namespace Foam
// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
namespace Foam
{
// Like dictionary getOrAdd (default), but circumventing
// writeOptionalEntries to avoid extremely noisy output
template<class T>
static inline T getOrAdd
(
dictionary& dict,
const char* name,
const T deflt
)
{
const entry* eptr = dict.findEntry(name, keyType::LITERAL);
if (eptr)
{
return eptr->get<T>();
}
dict.add(new primitiveEntry(name, deflt));
return deflt;
}
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Foam::dictionary& Foam::debug::controlDict()
......@@ -142,7 +166,8 @@ Foam::dictionary& Foam::debug::switchSet
if (!eptr || !eptr->isDict())
{
cerr<< "debug::switchSet(const char*, dictionary*&):\n"
std::cerr
<< "debug::switchSet(const char*, dictionary*&):\n"
<< " Cannot find " << subDictName << " in dictionary "
<< controlDict().name().c_str()
<< std::endl << std::endl;
......@@ -175,43 +200,31 @@ Foam::dictionary& Foam::debug::optimisationSwitches()
}
int Foam::debug::debugSwitch(const char* name, const int defaultValue)
int Foam::debug::debugSwitch(const char* name, const int deflt)
{
return debugSwitches().lookupOrAddDefault
(
name, defaultValue, keyType::LITERAL
);
return getOrAdd(debugSwitches(), name, deflt);
}
int Foam::debug::infoSwitch(const char* name, const int defaultValue)
int Foam::debug::infoSwitch(const char* name, const int deflt)
{
return infoSwitches().lookupOrAddDefault
(
name, defaultValue, keyType::LITERAL
);
return getOrAdd(infoSwitches(), name, deflt);
}
int Foam::debug::optimisationSwitch(const char* name, const int defaultValue)
int Foam::debug::optimisationSwitch(const char* name, const int deflt)
{
return optimisationSwitches().lookupOrAddDefault
(
name, defaultValue, keyType::LITERAL
);
return getOrAdd(optimisationSwitches(), name, deflt);
}
float Foam::debug::floatOptimisationSwitch
(
const char* name,
const float defaultValue
const float deflt
)
{
return optimisationSwitches().lookupOrAddDefault
(
name, defaultValue, keyType::LITERAL
);
return getOrAdd(optimisationSwitches(), name, deflt);
}
......@@ -393,7 +406,7 @@ Foam::simpleObjectRegistry& Foam::debug::dimensionedConstantObjects()
namespace Foam
{
void listSwitches
static void listSwitches
(
const wordList& debugSwitches,
const wordList& infoSwitches,
......@@ -431,26 +444,26 @@ void listSwitches
wordHashSet hashset;
hashset = debugSwitches;
hashset -= controlDictDebug;
Info<< "Unset DebugSwitches" << hashset.sortedToc() << endl;
Info<< "Unset DebugSwitches" << hashset.sortedToc() << nl;
hashset = infoSwitches;
hashset -= controlDictInfo;
Info<< "Unset InfoSwitches" << hashset.sortedToc() << endl;
Info<< "Unset InfoSwitches" << hashset.sortedToc() << nl;
hashset = optSwitches;
hashset -= controlDictOpt;
Info<< "Unset OptimisationSwitches" << hashset.sortedToc() << endl;
Info<< "Unset OptimisationSwitches" << hashset.sortedToc() << nl;
}
else
{
IOobject::writeDivider(Info);
Info<< "DebugSwitches" << debugSwitches << endl;
Info<< "InfoSwitches" << infoSwitches << endl;
Info<< "OptimisationSwitches" << optSwitches << endl;
Info<< "DebugSwitches" << debugSwitches << nl
<< "InfoSwitches" << infoSwitches << nl
<< "OptimisationSwitches" << optSwitches << nl;
}
}
}
} // End namespace Foam
void Foam::debug::listSwitches(const bool unset)
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2017 OpenFOAM Foundation
......@@ -78,20 +78,16 @@ namespace debug
dictionary& optimisationSwitches();
//- Lookup debug switch or add default value.
int debugSwitch(const char* name, const int defaultValue=0);
int debugSwitch(const char* name, const int deflt = 0);
//- Lookup info switch or add default value.
int infoSwitch(const char* name, const int defaultValue=0);
int infoSwitch(const char* name, const int deflt = 0);
//- Lookup optimisation switch or add default value.
int optimisationSwitch(const char* name, const int defaultValue=0);
int optimisationSwitch(const char* name, const int deflt = 0);
//- Lookup optimisation switch or add default value.
float floatOptimisationSwitch
(
const char* name,
const float defaultValue=0
);
float floatOptimisationSwitch(const char* name, const float deflt = 0);
//- Internal function to lookup a sub-dictionary from controlDict.
dictionary& switchSet(const char* subDictName, dictionary*& subDictPtr);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment