ENH: report dictionary defaults with Executable prefix

- provides better context when default values are accessed from
  a dictionary than reporting a source file location.
This commit is contained in:
Mark Olesen
2021-11-17 16:04:46 +01:00
parent bb771a3caf
commit 3d889b6dbf
3 changed files with 51 additions and 11 deletions

View File

@ -33,6 +33,7 @@ License
#include "dictionaryEntry.H" #include "dictionaryEntry.H"
#include "regExp.H" #include "regExp.H"
#include "OSHA1stream.H" #include "OSHA1stream.H"
#include "OSstream.H"
#include "argList.H" #include "argList.H"
#include "registerSwitch.H" #include "registerSwitch.H"
@ -43,6 +44,8 @@ namespace Foam
defineTypeNameAndDebug(dictionary, 0); defineTypeNameAndDebug(dictionary, 0);
} }
Foam::refPtr<Foam::OSstream> Foam::dictionary::reportingOutput(nullptr);
const Foam::dictionary Foam::dictionary::null; const Foam::dictionary Foam::dictionary::null;
int Foam::dictionary::writeOptionalEntries int Foam::dictionary::writeOptionalEntries
@ -59,6 +62,14 @@ registerInfoSwitch
); );
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
Foam::word Foam::dictionary::executableName()
{
return argList::envExecutable();
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::dictionary::dictionary() Foam::dictionary::dictionary()

View File

@ -97,6 +97,7 @@ SeeAlso
#include "HashTable.H" #include "HashTable.H"
#include "wordList.H" #include "wordList.H"
#include "className.H" #include "className.H"
#include "refPtr.H"
// Some common data types // Some common data types
#include "label.H" #include "label.H"
@ -110,6 +111,7 @@ namespace Foam
// Forward Declarations // Forward Declarations
class dictionary; class dictionary;
class OSstream;
class SHA1Digest; class SHA1Digest;
Istream& operator>>(Istream& is, dictionary& dict); Istream& operator>>(Istream& is, dictionary& dict);
@ -360,7 +362,11 @@ private:
//- Emit IOError about bad input for the entry //- Emit IOError about bad input for the entry
void raiseBadInput(const ITstream& is, const word& keyword) const; void raiseBadInput(const ITstream& is, const word& keyword) const;
//- Report (stderr) that the keyword default value was used. //- The currently known executable name,
//- obtained from argList envExecutable
static word executableName();
//- Report (usually stderr) that the keyword default value was used,
//- or FatalIOError when writeOptionalEntries greater than 1 //- or FatalIOError when writeOptionalEntries greater than 1
template<class T> template<class T>
void reportDefault void reportDefault
@ -386,6 +392,9 @@ public:
//- An empty dictionary, which is also the parent for all dictionaries //- An empty dictionary, which is also the parent for all dictionaries
static const dictionary null; static const dictionary null;
//- Output location when reporting default values
static refPtr<OSstream> reportingOutput;
// Static Member Functions // Static Member Functions

View File

@ -41,23 +41,41 @@ void Foam::dictionary::reportDefault
{ {
if (writeOptionalEntries > 1) if (writeOptionalEntries > 1)
{ {
FatalIOErrorInFunction(*this) FatalIOError(dictionary::executableName(), *this)
<< "No optional entry: " << keyword << "No optional entry: " << keyword
<< " Default: " << deflt << nl << " Default: " << deflt << nl
<< exit(FatalIOError); << exit(FatalIOError);
} }
InfoErr OSstream& os = InfoErr.stream(reportingOutput.get());
<< "Dictionary: " << this->relativeName().c_str()
<< " Entry: " << keyword; // Tag with "-- " prefix to make the message stand out
os << "-- Executable: "
<< dictionary::executableName()
<< " Dictionary: ";
// Double-quote dictionary and entry for more reliably parsing,
// especially if the keyword contains regular expressions.
if (this->isNullDict())
{
// Output as "", but could have "(null)" etc
os << token::DQUOTE << token::DQUOTE;
}
else
{
os.writeQuoted(this->relativeName(), true);
}
os << " Entry: ";
os.writeQuoted(keyword, true);
os << " Default: " << deflt;
if (added) if (added)
{ {
InfoErr os << " Added: true";
<< " Added";
} }
InfoErr os << nl;
<< " Default: " << deflt << nl;
} }
@ -195,14 +213,15 @@ T Foam::dictionary::getCheckOrDefault
enum keyType::option matchOpt enum keyType::option matchOpt
) const ) const
{ {
#ifdef FULLDEBUG
if (!pred(deflt)) if (!pred(deflt))
{ {
// Could be as FULLDEBUG instead?
FatalIOErrorInFunction(*this) FatalIOErrorInFunction(*this)
<< "Entry '" << keyword << "' with invalid default in dictionary " << "Entry '" << keyword << "' with invalid default in dictionary "
<< name() << name()
<< exit(FatalIOError); << exit(FatalIOError);
} }
#endif
const const_searcher finder(csearch(keyword, matchOpt)); const const_searcher finder(csearch(keyword, matchOpt));
@ -240,14 +259,15 @@ T Foam::dictionary::getCheckOrAdd
enum keyType::option matchOpt enum keyType::option matchOpt
) )
{ {
#ifdef FULLDEBUG
if (!pred(deflt)) if (!pred(deflt))
{ {
// Could be as FULLDEBUG instead?
FatalIOErrorInFunction(*this) FatalIOErrorInFunction(*this)
<< "Entry '" << keyword << "' with invalid default in dictionary " << "Entry '" << keyword << "' with invalid default in dictionary "
<< name() << name()
<< exit(FatalIOError); << exit(FatalIOError);
} }
#endif
const const_searcher finder(csearch(keyword, matchOpt)); const const_searcher finder(csearch(keyword, matchOpt));