ENH: add prescan handling of -debug-switch etc

- allows use of registered switches before startup of UPstream etc.
This commit is contained in:
Mark Olesen
2025-02-10 10:13:03 +01:00
parent 6f2fadb48f
commit 3b40dd8c29
8 changed files with 176 additions and 87 deletions

View File

@ -123,15 +123,12 @@ int main(int argc, char *argv[])
argList::addDryRunOption("Just for testing");
argList::addVerboseOption("Increase verbosity");
// Check -verbose before initialisation
UPstream::debug = argList::verbose(argc, argv);
#include "setRootCase.H"
Pout<< "command-line ("
<< args.options().size() << " options, "
<< args.args().size() << " args)" << nl
<< " " << args.commandLine().c_str() << nl << nl;
<< " " << args.commandLine().data() << nl << nl;
Pout<< "rootPath: " << args.rootPath() << nl
<< "globalCase: " << args.globalCaseName() << nl

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2022-2023 OpenCFD Ltd.
Copyright (C) 2022-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -67,10 +67,6 @@ int main(int argc, char *argv[])
{
argList::noBanner();
argList::noCheckProcessorDirectories();
argList::addVerboseOption("Set UPstream::debug level");
// Check -verbose before initialisation
UPstream::debug = argList::verbose(argc, argv);
#include "setRootCase.H"

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2022-2023 OpenCFD Ltd.
Copyright (C) 2022-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -66,7 +66,6 @@ int main(int argc, char *argv[])
{
argList::noBanner();
argList::noCheckProcessorDirectories();
argList::addVerboseOption("Set UPstream::debug level");
argList::addBoolOption("info", "information");
argList::addBoolOption("print-tree", "Report tree(s) as graph");
argList::addBoolOption("comm-split", "Test simple comm split");
@ -74,9 +73,6 @@ int main(int argc, char *argv[])
argList::addBoolOption("host-comm", "Test Pstream host-comm");
argList::addBoolOption("host-broadcast", "Test host-base broadcasts");
// Check -verbose before initialisation
UPstream::debug = argList::verbose(argc, argv);
#include "setRootCase.H"
const bool optPrintTree = args.found("print-tree");

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019-2023 OpenCFD Ltd.
Copyright (C) 2019-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -125,13 +125,16 @@ int main(int argc, char *argv[])
argList::addVerboseOption("Set UPstream::debug level");
// Check -verbose before initialisation
UPstream::debug = argList::verbose(argc, argv);
if (!UPstream::debug)
{
UPstream::debug = argList::verbose(argc, argv);
}
startMPI();
#include "setRootCase.H"
Pout<< message().c_str();
Pout<< message().data();
stopMPI();

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2022-2023 OpenCFD Ltd.
Copyright (C) 2022-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -44,19 +44,15 @@ using namespace Foam;
int main(int argc, char *argv[])
{
argList::noFunctionObjects();
argList::addVerboseOption("Set UPstream::debug level");
argList::addBoolOption("comm-graph", "Test simple graph communicator");
argList::addNote
(
"Create graph of OpenFOAM mesh connections"
);
// Check -verbose before initialisation
UPstream::debug = argList::verbose(argc, argv);
#include "setRootCase.H"
if (!Pstream::parRun())
if (!UPstream::parRun())
{
FatalErrorInFunction
<< "Only meaningful in parallel"

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2015-2024 OpenCFD Ltd.
Copyright (C) 2015-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -916,6 +916,8 @@ Foam::argList::argList
// Pre-scan for some options needed for initial setup:
// -fileHandler (takes an argument)
// -mpi-threads (bool option)
// -debug-switch, -info-switch, -opt-switch
// so their values can also influence the initial setup
//
// Also handle -dry-run and -verbose counting
// (it is left to the application to decide what to do with them).
@ -963,6 +965,53 @@ Foam::argList::argList
emitErrorMessage = true;
}
}
else if (strcmp(optName, "debug-switch") == 0)
{
// The '-debug-switch' option:
// change registered debug switch
if (argi < argc-1)
{
++argi;
debug::debugObjects()
.setNamedValue(argv[argi], 1); // silent
}
else
{
// emit error on the second pass
}
}
else if (strcmp(optName, "info-switch") == 0)
{
// The '-info-switch' option:
// change registered info switch
if (argi < argc-1)
{
++argi;
DetailInfo << "info-switch ";
debug::infoObjects()
.setNamedValue(argv[argi], 1); // silent
}
else
{
// emit error on the second pass
}
}
else if (strcmp(optName, "opt-switch") == 0)
{
// The '-opt-switch' option:
// change registered optimisation switch
if (argi < argc-1)
{
++argi;
DetailInfo << "opt-switch ";
debug::optimisationObjects()
.setNamedValue(argv[argi], 1); // silent
}
else
{
// emit error on the second pass
}
}
else if (validParOptions.contains(optName))
{
// Contains a parallel run option
@ -977,8 +1026,6 @@ Foam::argList::argList
Info<< nl
<< "Error: option '-" << optName
<< "' requires an argument" << nl << nl;
//NO: UPstream::exit(1); // works for serial and parallel
}
}
}
@ -1102,26 +1149,26 @@ Foam::argList::argList
else if (strcmp(optName, "debug-switch") == 0)
{
// The '-debug-switch' option:
// change registered debug switch
// dryrun reporting only (already set above)
DetailInfo << "debug-switch ";
debug::debugObjects()
.setNamedValue(args_[argi], 1, true);
.setNamedValue(args_[argi], 1, true, true); // dryrun
}
else if (strcmp(optName, "info-switch") == 0)
{
// The '-info-switch' option:
// change registered info switch
// dryrun reporting only (already set above)
DetailInfo << "info-switch ";
debug::infoObjects()
.setNamedValue(args_[argi], 1, true);
.setNamedValue(args_[argi], 1, true, true); // dryrun
}
else if (strcmp(optName, "opt-switch") == 0)
{
// The '-opt-switch' option:
// change registered optimisation switch
// dryrun reporting only (already set above)
DetailInfo << "opt-switch ";
debug::optimisationObjects()
.setNamedValue(args_[argi], 1, true);
.setNamedValue(args_[argi], 1, true, true); // dryrun
}
else
{

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019-2022 OpenCFD Ltd.
Copyright (C) 2019-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -38,13 +38,17 @@ License
void Foam::simpleObjectRegistry::setValues
(
const dictionary& dict,
bool report
bool verbose,
bool dryrun
)
{
// Report enables output, but respect DetailInfo state as well.
// The local log variable captures this logic.
verbose = (verbose && Foam::infoDetailLevel > 0);
const bool log = (report && Foam::infoDetailLevel > 0);
if (dryrun && !verbose)
{
return;
}
for (const entry& dEntry : dict)
{
@ -52,10 +56,24 @@ void Foam::simpleObjectRegistry::setValues
simpleObjectRegistryEntry* objPtr = this->find(name);
if (objPtr)
if (verbose)
{
Log << " " << dEntry << nl;
if (objPtr)
{
Info<< " " << dEntry << nl;
}
else
{
Info<< " " << name << " (unregistered)" << nl;
}
}
if (dryrun)
{
// Nothing else to do
}
else if (objPtr)
{
const List<simpleRegIOobject*>& objects = *objPtr;
OCharStream os;
@ -85,66 +103,88 @@ void Foam::simpleObjectRegistry::setValues
}
}
}
else
{
Log << " " << name << " (unregistered)" << nl;
}
}
}
void Foam::simpleObjectRegistry::setNamedValue
(
std::string name,
const std::string_view name,
int val,
bool report
bool verbose,
bool dryrun
)
{
// Report enables output, but respect DetailInfo state as well.
// The local log variable captures this logic.
// Respect DetailInfo state
verbose = (verbose && Foam::infoDetailLevel > 0);
const bool log = (report && Foam::infoDetailLevel > 0);
if (dryrun && !verbose)
{
return;
}
token tok(static_cast<label>(val));
// Handle name=value
// Handle name=value,
// treating 'name=' like 'name' (ie, default value)
const auto eq = name.find('=');
std::string_view objName = name;
std::string_view param;
std::string key;
if (eq != std::string::npos)
if (eq != std::string_view::npos)
{
string strval(name.substr(eq+1));
name.erase(eq); // Truncate the name
key = name.substr(0, eq);
param = name.substr(eq+1);
objName = key;
}
// Treat 'name=' like 'name' (ie, default value)
if (strval.length())
simpleObjectRegistryEntry* objPtr = this->find(objName.data());
// Fail early
if (!objPtr)
{
if (verbose)
{
float fvalue(0);
if (Foam::readInt(strval, val))
{
// Parses as int
tok = static_cast<label>(val);
}
else if (Foam::readFloat(strval, fvalue))
{
// Parses as float
tok = fvalue;
}
else
{
// Accept 'name=string' for named enums,
tok = std::move(strval);
}
Info<< objName.data() << " (unregistered)" << nl;
}
return;
}
simpleObjectRegistryEntry* objPtr = this->find(name.c_str());
if (objPtr)
if (!param.empty())
{
Log << name.c_str() << '=' << tok << nl;
float fvalue(0);
if (Foam::readInt(param.data(), val))
{
// Parses as int
tok = static_cast<label>(val);
}
else if (Foam::readFloat(param.data(), fvalue))
{
// Parses as float
tok = fvalue;
}
else
{
// Accept 'name=string' for named enums
tok = Foam::string(param.data(), param.size());
}
}
if (verbose)
{
Info<< objName.data() << '=' << tok << nl;
}
if (dryrun)
{
// Nothing else to do
}
else if (objPtr)
{
// The generic interface requires an Istream.
ITstream is(tokenList(Foam::one{}, std::move(tok)));
@ -156,10 +196,6 @@ void Foam::simpleObjectRegistry::setNamedValue
obj->readData(is);
}
}
else
{
Log << name.c_str() << " (unregistered)" << nl;
}
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2012-2016 OpenFOAM Foundation
Copyright (C) 2019-2023 OpenCFD Ltd.
Copyright (C) 2019-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -101,21 +101,39 @@ public:
//- Set values (invoke callbacks) from dictionary entries
// Reporting honours the infoDetailLevel
void setValues(const dictionary& dict, bool report=false);
void setValues
(
const dictionary& dict,
//! Report changes to Info (respects infoDetailLevel)
bool verbose = false,
//! Report requested changes only, but do not effect them
bool dryrun = false
);
//- Set named value, but also handle embedded 'name=value' syntax
// Treats 'name=' and 'name' identically.
//
// Will also accept a floating point value, but the called objects
// need to handle that.
// Reporting honours the infoDetailLevel
void setNamedValue(std::string name, int val, bool report=false);
// Also accepts a floating point value (and even strings),
// but the called objects need to handle those.
void setNamedValue
(
const std::string_view name,
int val,
//! Report changes to Info (respects infoDetailLevel)
bool verbose = false,
//! Report requested changes only, but do not effect them
bool dryrun = false
);
//- Deprecated(2021-09) renamed to setNamedValue
// \deprecated(2021-09) renamed to setNamedValue
void setNamedInt(std::string name, int val, bool report=false)
void setNamedInt
(
const std::string_view name,
int val,
bool verbose = false
)
{
this->setNamedValue(name, val, report);
this->setNamedValue(name, val, verbose);
}
};