Add orthogonal OK/ERROR states.

Replace state check mutex with atomic.

Update DDS example documentation.
This commit is contained in:
Alexey Rybalchenko
2015-08-24 17:35:30 +02:00
committed by Mohammad Al-Turany
parent a7ab33a10e
commit fbf7dbf2ba
38 changed files with 838 additions and 615 deletions

View File

@@ -13,7 +13,7 @@
*/
#include "FairMQParser.h"
#include "FairMQLogger.h"
#include "FairLogger.h"
#include <boost/property_tree/xml_parser.hpp>
// WARNING : pragma commands to hide boost (1.54.0) warning
@@ -34,19 +34,21 @@
using namespace std;
namespace FairMQParser
{
// TODO : add key-value map<string,string> parameter for replacing/updating values from keys
// function that convert property tree (given the xml or json structure) to FairMQMap
FairMQMap ptreeToMQMap(const boost::property_tree::ptree& pt, const std::string& deviceId, const std::string& rootNode, const std::string& formatFlag)
FairMQMap ptreeToMQMap(const boost::property_tree::ptree& pt, const string& deviceId, const string& rootNode, const string& formatFlag)
{
// Create fair mq map
FairMQMap channelMap;
// variables to create key for the mq map. Note: maybe device name and id useless here
std::string deviceIdKey;
std::string channelKey;
string deviceIdKey;
string channelKey;
// do a first loop just to print the device-id in xml/json input
for(const auto& p : pt.get_child(rootNode))
@@ -59,13 +61,13 @@ FairMQMap ptreeToMQMap(const boost::property_tree::ptree& pt, const std::string&
//get id attribute to choose the device
if (formatFlag == "xml")
{
deviceIdKey = p.second.get<std::string>("<xmlattr>.id");
deviceIdKey = p.second.get<string>("<xmlattr>.id");
LOG(DEBUG) << "Found device id '" << deviceIdKey << "' in XML input";
}
if (formatFlag == "json")
{
deviceIdKey = p.second.get<std::string>("id");
deviceIdKey = p.second.get<string>("id");
LOG(DEBUG) << "Found device id '"<< deviceIdKey << "' in JSON input";
}
}
@@ -82,12 +84,12 @@ FairMQMap ptreeToMQMap(const boost::property_tree::ptree& pt, const std::string&
//get id attribute to choose the device
if (formatFlag == "xml")
{
deviceIdKey = p.second.get<std::string>("<xmlattr>.id");
deviceIdKey = p.second.get<string>("<xmlattr>.id");
}
if (formatFlag == "json")
{
deviceIdKey = p.second.get<std::string>("id");
deviceIdKey = p.second.get<string>("id");
}
// if not correct device id, do not fill MQMap
@@ -97,9 +99,8 @@ FairMQMap ptreeToMQMap(const boost::property_tree::ptree& pt, const std::string&
}
// print if DEBUG log level set
std::stringstream deviceStream;
deviceStream << "[node = " << p.first
<< "] id = " << deviceIdKey;
stringstream deviceStream;
deviceStream << "[node = " << p.first << "] id = " << deviceIdKey;
LOG(DEBUG) << deviceStream.str();
// for each channel in device
@@ -113,22 +114,21 @@ FairMQMap ptreeToMQMap(const boost::property_tree::ptree& pt, const std::string&
// get name attribute to form key
if (formatFlag == "xml")
{
channelKey = q.second.get<std::string>("<xmlattr>.name");
channelKey = q.second.get<string>("<xmlattr>.name");
}
if (formatFlag == "json")
{
channelKey = q.second.get<std::string>("name");
channelKey = q.second.get<string>("name");
}
// print if DEBUG log level set
std::stringstream channelStream;
channelStream << "\t [node = " << q.first
<< "] name = " << channelKey;
stringstream channelStream;
channelStream << "\t [node = " << q.first << "] name = " << channelKey;
LOG(DEBUG) << channelStream.str();
// temporary FairMQChannel container
std::vector<FairMQChannel> channelList;
vector<FairMQChannel> channelList;
int socketCounter = 0;
// for each socket in channel
@@ -143,20 +143,19 @@ FairMQMap ptreeToMQMap(const boost::property_tree::ptree& pt, const std::string&
FairMQChannel channel;
// print if DEBUG log level set
std::stringstream socket;
socket << "\t \t [node = " << r.first
<< "] socket index = " << socketCounter;
stringstream socket;
socket << "\t \t [node = " << r.first << "] socket index = " << socketCounter;
LOG(DEBUG) << socket.str();
LOG(DEBUG) << "\t \t \t type = " << r.second.get<std::string>("type", channel.GetType());
LOG(DEBUG) << "\t \t \t method = " << r.second.get<std::string>("method", channel.GetMethod());
LOG(DEBUG) << "\t \t \t address = " << r.second.get<std::string>("address", channel.GetAddress());
LOG(DEBUG) << "\t \t \t type = " << r.second.get<string>("type", channel.GetType());
LOG(DEBUG) << "\t \t \t method = " << r.second.get<string>("method", channel.GetMethod());
LOG(DEBUG) << "\t \t \t address = " << r.second.get<string>("address", channel.GetAddress());
LOG(DEBUG) << "\t \t \t sndBufSize = " << r.second.get<int>("sndBufSize", channel.GetSndBufSize());
LOG(DEBUG) << "\t \t \t rcvBufSize = " << r.second.get<int>("rcvBufSize", channel.GetRcvBufSize());
LOG(DEBUG) << "\t \t \t rateLogging = " << r.second.get<int>("rateLogging", channel.GetRateLogging());
channel.UpdateType(r.second.get<std::string>("type", channel.GetType()));
channel.UpdateMethod(r.second.get<std::string>("method", channel.GetMethod()));
channel.UpdateAddress(r.second.get<std::string>("address", channel.GetAddress()));
channel.UpdateType(r.second.get<string>("type", channel.GetType()));
channel.UpdateMethod(r.second.get<string>("method", channel.GetMethod()));
channel.UpdateAddress(r.second.get<string>("address", channel.GetAddress()));
channel.UpdateSndBufSize(r.second.get<int>("sndBufSize", channel.GetSndBufSize())); // int
channel.UpdateRcvBufSize(r.second.get<int>("rcvBufSize", channel.GetRcvBufSize())); // int
channel.UpdateRateLogging(r.second.get<int>("rateLogging", channel.GetRateLogging())); // int
@@ -165,7 +164,7 @@ FairMQMap ptreeToMQMap(const boost::property_tree::ptree& pt, const std::string&
}// end socket loop
//fill mq map option
channelMap.insert(std::make_pair(channelKey,std::move(channelList)));
channelMap.insert(make_pair(channelKey, move(channelList)));
}
}
@@ -182,37 +181,36 @@ FairMQMap ptreeToMQMap(const boost::property_tree::ptree& pt, const std::string&
LOG(WARN) << "---- No channel-keys found for device-id " << deviceId;
LOG(WARN) << "---- Check the "<< formatFlag << " inputs and/or command line inputs";
}
return channelMap;
}
////////////////////////////////////////////////////////////////////////////
FairMQMap JSON::UserParser(const std::string& filename, const std::string& deviceId, const std::string& rootNode)
FairMQMap JSON::UserParser(const string& filename, const string& deviceId, const string& rootNode)
{
boost::property_tree::ptree pt;
boost::property_tree::read_json(filename, pt);
return ptreeToMQMap(pt, deviceId, rootNode,"json");
}
FairMQMap JSON::UserParser(std::stringstream& input, const std::string& deviceId, const std::string& rootNode)
FairMQMap JSON::UserParser(stringstream& input, const string& deviceId, const string& rootNode)
{
boost::property_tree::ptree pt;
boost::property_tree::read_json(input, pt);
return ptreeToMQMap(pt, deviceId, rootNode,"json");
}
////////////////////////////////////////////////////////////////////////////
FairMQMap XML::UserParser(const std::string& filename, const std::string& deviceId, const std::string& rootNode)
FairMQMap XML::UserParser(const string& filename, const string& deviceId, const string& rootNode)
{
boost::property_tree::ptree pt;
boost::property_tree::read_xml(filename, pt);
return ptreeToMQMap(pt,deviceId,rootNode,"xml");
return ptreeToMQMap(pt, deviceId, rootNode, "xml");
}
FairMQMap XML::UserParser(std::stringstream& input, const std::string& deviceId, const std::string& rootNode)
FairMQMap XML::UserParser(stringstream& input, const string& deviceId, const string& rootNode)
{
boost::property_tree::ptree pt;
boost::property_tree::read_xml(input, pt);
return ptreeToMQMap(pt,deviceId,rootNode,"xml");
return ptreeToMQMap(pt, deviceId, rootNode, "xml");
}
} // end FairMQParser namespace

View File

@@ -23,7 +23,7 @@
namespace FairMQParser
{
typedef std::unordered_map< std::string,std::vector<FairMQChannel> > FairMQMap;
typedef std::unordered_map<std::string, std::vector<FairMQChannel>> FairMQMap;
FairMQMap ptreeToMQMap(const boost::property_tree::ptree& pt, const std::string& deviceId, const std::string& rootNode, const std::string& formatFlag = "json");
@@ -35,8 +35,8 @@ struct JSON
struct XML
{
FairMQMap UserParser(const std::string& filename, const std::string& deviceId, const std::string& root_node="fairMQOptions");
FairMQMap UserParser(std::stringstream& input, const std::string& deviceId, const std::string& rootNode="fairMQOptions");
FairMQMap UserParser(const std::string& filename, const std::string& deviceId, const std::string& rootNode = "fairMQOptions");
FairMQMap UserParser(std::stringstream& input, const std::string& deviceId, const std::string& rootNode = "fairMQOptions");
};
} // FairMQParser namespace

View File

@@ -5,7 +5,6 @@
* GNU Lesser General Public Licence version 3 (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
/*
* File: FairMQProgOptions.cxx
* Author: winckler
@@ -16,26 +15,28 @@
#include "FairMQProgOptions.h"
#include <algorithm>
using namespace std;
FairMQProgOptions::FairMQProgOptions()
: FairProgOptions()
, fMQParserOptions("MQ-Device parser options")
, fMQOptionsInCmd("MQ-Device options")
, fMQOptionsInCfg("MQ-Device options")
, fMQtree()
, fFairMQmap()
, fFairMQMap()
{
}
FairMQProgOptions::~FairMQProgOptions()
FairMQProgOptions::~FairMQProgOptions()
{
}
int FairMQProgOptions::ParseAll(const int argc, char** argv, bool AllowUnregistered)
{
int FairMQProgOptions::ParseAll(const int argc, char** argv, bool allowUnregistered)
{
// init description
InitOptionDescription();
// parse command line options
if (ParseCmdLine(argc,argv,fCmdline_options,fvarmap,AllowUnregistered))
if (ParseCmdLine(argc, argv, fCmdLineOptions, fVarMap, allowUnregistered))
{
return 1;
}
@@ -46,20 +47,22 @@ int FairMQProgOptions::ParseAll(const int argc, char** argv, bool AllowUnregiste
// check if file exist
if (fs::exists(fConfigFile))
{
if (ParseCfgFile(fConfigFile.string(), fConfig_file_options, fvarmap, AllowUnregistered))
if (ParseCfgFile(fConfigFile.string(), fConfigFileOptions, fVarMap, allowUnregistered))
{
return 1;
}
}
else
{
LOG(ERROR)<<"config file '"<< fConfigFile <<"' not found";
LOG(ERROR) << "config file '" << fConfigFile << "' not found";
return 1;
}
}
// set log level before printing (default is 0 = DEBUG level)
std::string verbose=GetValue<std::string>("verbose");
//SET_LOG_LEVEL(DEBUG);
if(fSeverity_map.count(verbose))
if (fSeverity_map.count(verbose))
{
set_global_log_level(log_op::operation::GREATER_EQ_THAN,fSeverity_map.at(verbose));
}
@@ -70,44 +73,44 @@ int FairMQProgOptions::ParseAll(const int argc, char** argv, bool AllowUnregiste
}
PrintOptions();
// check if one of required MQ config option is there
auto parserOption_shptr = fMQParserOptions.options();
bool option_exists=false;
std::vector<std::string> MQParserKeys;
for(const auto& p : parserOption_shptr)
bool optionExists = false;
vector<string> MQParserKeys;
for (const auto& p : parserOption_shptr)
{
MQParserKeys.push_back( p->canonical_display_name() );
if( fvarmap.count( p->canonical_display_name() ) )
MQParserKeys.push_back(p->canonical_display_name());
if (fVarMap.count(p->canonical_display_name()))
{
option_exists=true;
optionExists = true;
break;
}
}
if(!option_exists)
if (!optionExists)
{
LOG(ERROR)<<"Required option to configure the MQ device is not there.";
LOG(ERROR)<<"Please provide the value of one of the following key:";
for(const auto& p : MQParserKeys)
LOG(ERROR) << "Required option to configure the MQ device is not there.";
LOG(ERROR) << "Please provide the value of one of the following key:";
for (const auto& p : MQParserKeys)
{
LOG(ERROR)<<p;
LOG(ERROR) << p;
}
return 1;
}
return 0;
}
int FairMQProgOptions::NotifySwitchOption()
{
if ( fvarmap.count("help") )
if (fVarMap.count("help"))
{
LOG(INFO) << "***** FAIRMQ Program Options ***** \n" << fVisible_options;
LOG(INFO) << "***** FAIRMQ Program Options ***** \n" << fVisibleOptions;
return 1;
}
if (fvarmap.count("version"))
if (fVarMap.count("version"))
{
LOG(INFO) << "Beta version 0.1\n";
return 1;
@@ -116,42 +119,39 @@ int FairMQProgOptions::NotifySwitchOption()
return 0;
}
void FairMQProgOptions::InitOptionDescription()
{
// Id required in command line if config txt file not enabled
if (fUseConfigFile)
{
fMQOptionsInCmd.add_options()
("id", po::value< std::string >(), "Device ID (required argument)")
("io-threads", po::value<int>()->default_value(1), "io threads number");
("id", po::value<string>(), "Device ID (required argument).")
("io-threads", po::value<int>()->default_value(1), "Number of I/O threads.");
fMQOptionsInCfg.add_options()
("id", po::value< std::string >()->required(), "Device ID (required argument)")
("io-threads", po::value<int>()->default_value(1), "io threads number");
("id", po::value<string>()->required(), "Device ID (required argument).")
("io-threads", po::value<int>()->default_value(1), "Number of I/O threads.");
}
else
{
fMQOptionsInCmd.add_options()
("id", po::value< std::string >()->required(), "Device ID (required argument)")
("io-threads", po::value<int>()->default_value(1), "io threads number");
("id", po::value<string>()->required(), "Device ID (required argument)")
("io-threads", po::value<int>()->default_value(1), "Number of I/O threads");
}
fMQParserOptions.add_options()
("config-xml-string", po::value< std::vector<std::string> >()->multitoken(), "XML input as command line string.")
("config-xml-file", po::value< std::string >(), "XML input as file.")
("config-json-string", po::value< std::vector<std::string> >()->multitoken(), "JSON input as command line string.")
("config-json-file", po::value< std::string >(), "JSON input as file.");
("config-xml-string", po::value<vector<string>>()->multitoken(), "XML input as command line string.")
("config-xml-file", po::value<string>(), "XML input as file.")
("config-json-string", po::value<vector<string>>()->multitoken(), "JSON input as command line string.")
("config-json-file", po::value<string>(), "JSON input as file.");
AddToCmdLineOptions(fGenericDesc);
AddToCmdLineOptions(fMQOptionsInCmd);
AddToCmdLineOptions(fMQParserOptions);
if (fUseConfigFile)
{
AddToCfgFileOptions(fMQOptionsInCfg,false);
AddToCfgFileOptions(fMQParserOptions,false);
}
}
}

View File

@@ -14,7 +14,7 @@
*/
#ifndef FAIRMQPROGOPTIONS_H
#define FAIRMQPROGOPTIONS_H
#define FAIRMQPROGOPTIONS_H
#include <unordered_map>
@@ -24,16 +24,17 @@
#include <boost/property_tree/ptree.hpp>
namespace pt = boost::property_tree;
class FairMQProgOptions : public FairProgOptions
{
protected:
typedef std::unordered_map< std::string,std::vector<FairMQChannel> > FairMQMap;
protected:
typedef std::unordered_map<std::string, std::vector<FairMQChannel>> FairMQMap;
public:
public:
FairMQProgOptions();
virtual ~FairMQProgOptions();
virtual int ParseAll(const int argc, char** argv, bool AllowUnregistered = false);
virtual int ParseAll(const int argc, char** argv, bool allowUnregistered = false);
// external parser, store function
template <typename T, typename ...Args>
@@ -53,7 +54,7 @@ public:
int Store(const po::variables_map& vm)
{
fvarmap = vm;
fVarMap = vm;
return 0;
}
@@ -65,26 +66,26 @@ public:
int Store(const FairMQMap& channels)
{
fFairMQmap = channels;
fFairMQMap = channels;
return 0;
}
FairMQMap GetFairMQMap()
{
return fFairMQmap;
return fFairMQMap;
}
protected:
protected:
po::options_description fMQParserOptions;
po::options_description fMQOptionsInCfg;
po::options_description fMQOptionsInCmd;
pt::ptree fMQtree;
FairMQMap fFairMQmap;
FairMQMap fFairMQMap;
virtual int NotifySwitchOption(); // for custom help & version printing
void InitOptionDescription();
};
#endif /* FAIRMQPROGOPTIONS_H */
#endif /* FAIRMQPROGOPTIONS_H */

View File

@@ -5,7 +5,6 @@
* GNU Lesser General Public Licence version 3 (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
/*
* File: FairProgOptions.cxx
* Author: winckler
@@ -15,21 +14,20 @@
#include "FairProgOptions.h"
using namespace std;
/// //////////////////////////////////////////////////////////////////////////////////////////////////////
/// Constructor / destructor
/// Constructor
FairProgOptions::FairProgOptions() :
fGenericDesc("Generic options description"),
fConfigDesc("Configuration options description"),
fHiddenDesc("Hidden options description"),
fGenericDesc("Generic options description"),
fConfigDesc("Configuration options description"),
fHiddenDesc("Hidden options description"),
fEnvironmentDesc("Environment Variables"),
fCmdline_options("Command line options"),
fConfig_file_options("Configuration file options"),
fVisible_options("Visible options"),
fCmdLineOptions("Command line options"),
fConfigFileOptions("Configuration file options"),
fVisibleOptions("Visible options"),
fVerboseLvl("INFO"), fUseConfigFile(false), fConfigFile()
{
// //////////////////////////////////
// define generic options
fGenericDesc.add_options()
("help,h", "produce help")
@@ -45,7 +43,7 @@ FairProgOptions::FairProgOptions() :
" NOLOG"
)
;
fSeverity_map["TRACE"] = fairmq::severity_level::TRACE;
fSeverity_map["DEBUG"] = fairmq::severity_level::DEBUG;
fSeverity_map["RESULTS"] = fairmq::severity_level::RESULTS;
@@ -54,45 +52,48 @@ FairProgOptions::FairProgOptions() :
fSeverity_map["ERROR"] = fairmq::severity_level::ERROR;
fSeverity_map["STATE"] = fairmq::severity_level::STATE;
fSeverity_map["NOLOG"] = fairmq::severity_level::NOLOG;
}
FairProgOptions::~FairProgOptions()
/// Destructor
FairProgOptions::~FairProgOptions()
{
}
/// //////////////////////////////////////////////////////////////////////////////////////////////////////
/// Add option descriptions
int FairProgOptions::AddToCmdLineOptions(const po::options_description& optdesc, bool visible)
int FairProgOptions::AddToCmdLineOptions(const po::options_description& optDesc, bool visible)
{
fCmdline_options.add(optdesc);
if(visible)
fVisible_options.add(optdesc);
fCmdLineOptions.add(optDesc);
if (visible)
{
fVisibleOptions.add(optDesc);
}
return 0;
}
int FairProgOptions::AddToCfgFileOptions(const po::options_description& optdesc, bool visible)
int FairProgOptions::AddToCfgFileOptions(const po::options_description& optDesc, bool visible)
{
//if UseConfigFile() not yet called, then enable it with required file name to be provided by command line
if(!fUseConfigFile)
if (!fUseConfigFile)
{
UseConfigFile();
fConfig_file_options.add(optdesc);
if(visible)
fVisible_options.add(optdesc);
}
fConfigFileOptions.add(optDesc);
if (visible)
{
fVisibleOptions.add(optDesc);
}
return 0;
}
int FairProgOptions::AddToEnvironmentOptions(const po::options_description& optdesc)
int FairProgOptions::AddToEnvironmentOptions(const po::options_description& optDesc)
{
fEnvironmentDesc.add(optdesc);
fEnvironmentDesc.add(optDesc);
return 0;
}
void FairProgOptions::UseConfigFile(const std::string& filename)
void FairProgOptions::UseConfigFile(const string& filename)
{
fUseConfigFile = true;
if (filename.empty())
@@ -110,176 +111,168 @@ void FairProgOptions::UseConfigFile(const std::string& filename)
/// //////////////////////////////////////////////////////////////////////////////////////////////////////
/// Parser
int FairProgOptions::ParseCmdLine(const int argc, char** argv, const po::options_description& desc, po::variables_map& varmap, bool AllowUnregistered)
int FairProgOptions::ParseCmdLine(const int argc, char** argv, const po::options_description& desc, po::variables_map& varmap, bool allowUnregistered)
{
// //////////////////////////////////
// get options from cmd line and store in variable map
// here we use command_line_parser instead of parse_command_line, to allow unregistered and positional options
if(AllowUnregistered)
if (allowUnregistered)
{
po::command_line_parser parser{argc, argv};
parser.options(desc).allow_unregistered();
po::parsed_options parsedOptions = parser.run();
po::store(parsedOptions,varmap);
po::store(parsedOptions, varmap);
}
else
{
po::store(po::parse_command_line(argc, argv, desc), varmap);
}
// //////////////////////////////////
// call the virtual NotifySwitchOption method to handle switch options like e.g. "--help" or "--version"
// return 1 if switch options found in varmap
if(NotifySwitchOption())
if (NotifySwitchOption())
{
return 1;
}
po::notify(varmap);
return 0;
}
int FairProgOptions::ParseCmdLine(const int argc, char** argv, const po::options_description& desc, bool AllowUnregistered)
int FairProgOptions::ParseCmdLine(const int argc, char** argv, const po::options_description& desc, bool allowUnregistered)
{
return ParseCmdLine(argc,argv,desc,fvarmap,AllowUnregistered);
return ParseCmdLine(argc,argv,desc,fVarMap,allowUnregistered);
}
int FairProgOptions::ParseCfgFile(std::ifstream& ifs, const po::options_description& desc, po::variables_map& varmap, bool AllowUnregistered)
int FairProgOptions::ParseCfgFile(ifstream& ifs, const po::options_description& desc, po::variables_map& varmap, bool allowUnregistered)
{
if (!ifs)
{
std::cout << "can not open configuration file \n";
cout << "can not open configuration file \n";
return -1;
}
else
{
po:store(parse_config_file(ifs, desc, AllowUnregistered), varmap);
po:store(parse_config_file(ifs, desc, allowUnregistered), varmap);
po::notify(varmap);
}
return 0;
}
int FairProgOptions::ParseCfgFile(const std::string& filename, const po::options_description& desc, po::variables_map& varmap, bool AllowUnregistered)
int FairProgOptions::ParseCfgFile(const string& filename, const po::options_description& desc, po::variables_map& varmap, bool allowUnregistered)
{
std::ifstream ifs(filename.c_str());
ifstream ifs(filename.c_str());
if (!ifs)
{
std::cout << "can not open configuration file: " << filename << "\n";
cout << "can not open configuration file: " << filename << "\n";
return -1;
}
else
{
po:store(parse_config_file(ifs, desc, AllowUnregistered), varmap);
po:store(parse_config_file(ifs, desc, allowUnregistered), varmap);
po::notify(varmap);
}
return 0;
}
int FairProgOptions::ParseCfgFile(const std::string& filename, const po::options_description& desc, bool AllowUnregistered)
int FairProgOptions::ParseCfgFile(const string& filename, const po::options_description& desc, bool allowUnregistered)
{
return ParseCfgFile(filename,desc,fvarmap,AllowUnregistered);
return ParseCfgFile(filename,desc,fVarMap,allowUnregistered);
}
int FairProgOptions::ParseCfgFile(std::ifstream& ifs, const po::options_description& desc, bool AllowUnregistered)
int FairProgOptions::ParseCfgFile(ifstream& ifs, const po::options_description& desc, bool allowUnregistered)
{
return ParseCfgFile(ifs,desc,fvarmap,AllowUnregistered);
return ParseCfgFile(ifs,desc,fVarMap,allowUnregistered);
}
int FairProgOptions::ParseEnvironment(const std::function<std::string(std::string)> & environment_mapper)
int FairProgOptions::ParseEnvironment(const function<string(string)>& environmentMapper)
{
po::store(po::parse_environment(fEnvironmentDesc, environment_mapper), fvarmap);
po::notify(fvarmap);
po::store(po::parse_environment(fEnvironmentDesc, environmentMapper), fVarMap);
po::notify(fVarMap);
return 0;
}
// Given a key, convert the variable value to string
std::string FairProgOptions::GetStringValue(const std::string& key)
string FairProgOptions::GetStringValue(const string& key)
{
string valueStr;
try
{
std::string val_str;
try
if (fVarMap.count(key))
{
if ( fvarmap.count(key) )
auto& value = fVarMap[key].value();
// string albeit useless here
if (auto q = boost::any_cast<string>(&value))
{
auto& value = fvarmap[key].value();
valueStr = VariableValueToString<string>(fVarMap[key]);
return valueStr;
}
// vector<string>
if (auto q = boost::any_cast<vector<string>>(&value))
{
valueStr = VariableValueToString<vector<string>>(fVarMap[key]);
return valueStr;
}
// string albeit useless here
if(auto q = boost::any_cast< std::string >(&value ))
{
val_str = variable_value_to_string< std::string >(fvarmap[key]);
return val_str;
}
// vector<string>
if(auto q = boost::any_cast< std::vector<std::string> >(&value ))
{
val_str = variable_value_to_string< std::vector<std::string> >(fvarmap[key]);
return val_str;
}
// int
if(auto q = boost::any_cast< int >(&value ))
{
val_str = variable_value_to_string< int >(fvarmap[key]);
return val_str;
}
// vector<int>
if(auto q = boost::any_cast< std::vector<int> >(&value ))
{
val_str = variable_value_to_string< std::vector<int> >(fvarmap[key]);
return val_str;
}
// float
if(auto q = boost::any_cast< float >(&value ))
{
val_str = variable_value_to_string< float >(fvarmap[key]);
return val_str;
}
// vector float
if(auto q = boost::any_cast< std::vector<float> >(&value ))
{
val_str = variable_value_to_string< std::vector<float> >(fvarmap[key]);
return val_str;
}
// double
if(auto q = boost::any_cast< double >(&value ))
{
val_str = variable_value_to_string< double >(fvarmap[key]);
return val_str;
}
// vector double
if(auto q = boost::any_cast< std::vector<double> >(&value ))
{
val_str = variable_value_to_string< std::vector<double> >(fvarmap[key]);
return val_str;
}
// int
if (auto q = boost::any_cast<int>(&value))
{
valueStr = VariableValueToString<int>(fVarMap[key]);
return valueStr;
}
// vector<int>
if (auto q = boost::any_cast<vector<int>>(&value))
{
valueStr = VariableValueToString<vector<int>>(fVarMap[key]);
return valueStr;
}
// float
if (auto q = boost::any_cast<float>(&value))
{
valueStr = VariableValueToString<float>(fVarMap[key]);
return valueStr;
}
// vector float
if (auto q = boost::any_cast<vector<float>>(&value))
{
valueStr = VariableValueToString<vector<float>>(fVarMap[key]);
return valueStr;
}
// double
if (auto q = boost::any_cast<double>(&value))
{
valueStr = VariableValueToString<double>(fVarMap[key]);
return valueStr;
}
// vector double
if (auto q = boost::any_cast<vector<double>>(&value))
{
valueStr = VariableValueToString<vector<double>>(fVarMap[key]);
return valueStr;
}
}
catch(std::exception& e)
{
LOG(ERROR) << "Exception thrown for the key '" << key << "'";
LOG(ERROR) << e.what();
}
return val_str;
}
catch(exception& e)
{
LOG(ERROR) << "Exception thrown for the key '" << key << "'";
LOG(ERROR) << e.what();
}
return valueStr;
}
/// //////////////////////////////////////////////////////////////////////////////////////////////////////
/// Print/notify options
int FairProgOptions::PrintHelp() const
{
std::cout << fVisible_options << "\n";
cout << fVisibleOptions << "\n";
return 0;
}
@@ -289,205 +282,210 @@ int FairProgOptions::PrintOptions()
// Method to overload.
// -> loop over variable map and print its content
// -> In this example the following types are supported:
// std::string, int, float, double, boost::filesystem::path
// std::vector<std::string>, std::vector<int>, std::vector<float>, std::vector<double>
// string, int, float, double, boost::filesystem::path
// vector<string>, vector<int>, vector<float>, vector<double>
MapVarValInfo_t mapinfo;
// get string length for formatting and convert varmap values into string
int maxlen_1st = 0;
int maxlen_2nd = 0;
int maxlen_TypeInfo = 0;
int maxlen_default =0;
int maxlen_empty = 0;
int total_len=0;
for (const auto& m : fvarmap)
int maxLength1st = 0;
int maxLength2nd = 0;
int maxLengthTypeInfo = 0;
int maxLengthDefault = 0;
int maxLengthEmpty = 0;
int totalLength = 0;
for (const auto& m : fVarMap)
{
Max(maxlen_1st, m.first.length());
Max(maxLength1st, m.first.length());
VarValInfo_t valinfo=Get_variable_value_info(m.second);
mapinfo[m.first]=valinfo;
std::string val_str;
std::string typeInfo_str;
std::string default_str;
std::string empty_str;
std::tie(val_str,typeInfo_str,default_str,empty_str)=valinfo;
Max(maxlen_2nd, val_str.length());
Max(maxlen_TypeInfo, typeInfo_str.length());
Max(maxlen_default, default_str.length());
Max(maxlen_empty, empty_str.length());
VarValInfo_t valinfo = GetVariableValueInfo(m.second);
mapinfo[m.first] = valinfo;
string valueStr;
string typeInfoStr;
string defaultStr;
string emptyStr;
tie(valueStr, typeInfoStr, defaultStr, emptyStr) = valinfo;
Max(maxLength2nd, valueStr.length());
Max(maxLengthTypeInfo, typeInfoStr.length());
Max(maxLengthDefault, defaultStr.length());
Max(maxLengthEmpty, emptyStr.length());
}
// TODO : limit the value length field in a better way
if(maxlen_2nd>100)
maxlen_2nd=100;
total_len=maxlen_1st+maxlen_2nd+maxlen_TypeInfo+maxlen_default+maxlen_empty;
//maxlen_2nd=200;
if (maxLength2nd > 100)
{
maxLength2nd = 100;
}
totalLength = maxLength1st + maxLength2nd + maxLengthTypeInfo + maxLengthDefault + maxLengthEmpty;
// maxLength2nd = 200;
// formatting and printing
LOG(INFO)<<std::setfill ('*') << std::setw (total_len+3)<<"*";// +3 because of string " = "
std::string PrintOptionsTitle=" Program options found ";
LOG(INFO) << setfill ('*') << setw (totalLength + 3) << "*";// +3 because of string " = "
string PrintOptionsTitle = " Program options found ";
int leftSpace_len=0;
int rightSpace_len=0;
int leftTitle_shift_len=0;
int rightTitle_shift_len=0;
int leftSpaceLength = 0;
int rightSpaceLength = 0;
int leftTitleShiftLength = 0;
int rightTitleShiftLength = 0;
leftTitle_shift_len=PrintOptionsTitle.length()/2;
leftTitleShiftLength = PrintOptionsTitle.length() / 2;
if ((PrintOptionsTitle.length()) % 2)
rightTitle_shift_len=leftTitle_shift_len+1;
rightTitleShiftLength = leftTitleShiftLength + 1;
else
rightTitle_shift_len=leftTitle_shift_len;
rightTitleShiftLength = leftTitleShiftLength;
leftSpace_len=(total_len+3)/2-leftTitle_shift_len;
if ((total_len+3) % 2)
rightSpace_len=(total_len+3)/2-rightTitle_shift_len+1;
leftSpaceLength = (totalLength + 3) / 2 - leftTitleShiftLength;
if ((totalLength + 3) % 2)
{
rightSpaceLength = (totalLength + 3) / 2 - rightTitleShiftLength + 1;
}
else
rightSpace_len=(total_len+3)/2-rightTitle_shift_len;
{
rightSpaceLength = (totalLength + 3) / 2 - rightTitleShiftLength;
}
LOG(INFO) << setfill ('*') << setw(leftSpaceLength) << "*"
<< setw(PrintOptionsTitle.length()) << PrintOptionsTitle
<< setfill ('*') << setw(rightSpaceLength) << "*";
LOG(INFO) << std::setfill ('*') << std::setw(leftSpace_len) <<"*"
<< std::setfill(' ')
<< std::setw(PrintOptionsTitle.length()) << PrintOptionsTitle
<< std::setfill ('*') << std::setw(rightSpace_len) <<"*";
LOG(INFO) <<std::setfill ('*') << std::setw (total_len+3)<<"*";
LOG(INFO) << setfill ('*') << setw (totalLength+3) << "*";
for (const auto& p : mapinfo)
{
std::string key_str;
std::string val_str;
std::string typeInfo_str;
std::string default_str;
std::string empty_str;
key_str=p.first;
std::tie(val_str,typeInfo_str,default_str,empty_str)=p.second;
LOG(INFO) << std::setfill(' ')
<< std::setw(maxlen_1st)<<std::left
<< p.first << " = "
<< std::setw(maxlen_2nd)
<< val_str
<< std::setw(maxlen_TypeInfo)
<< typeInfo_str
<< std::setw(maxlen_default)
<< default_str
<< std::setw(maxlen_empty)
<< empty_str;
string keyStr;
string valueStr;
string typeInfoStr;
string defaultStr;
string emptyStr;
keyStr = p.first;
tie(valueStr, typeInfoStr, defaultStr, emptyStr) = p.second;
LOG(INFO) << std::setfill(' ')
<< setw(maxLength1st) << left
<< p.first << " = "
<< setw(maxLength2nd)
<< valueStr
<< setw(maxLengthTypeInfo)
<< typeInfoStr
<< setw(maxLengthDefault)
<< defaultStr
<< setw(maxLengthEmpty)
<< emptyStr;
}
LOG(INFO)<<std::setfill ('*') << std::setw (total_len+3)<<"*";// +3 for " = "
LOG(INFO) << setfill ('*') << setw (totalLength + 3) << "*";// +3 for " = "
return 0;
}
int FairProgOptions::NotifySwitchOption()
{
// //////////////////////////////////
// Method to overload.
if ( fvarmap.count("help") )
if (fVarMap.count("help"))
{
std::cout << "***** FAIR Program Options ***** \n" << fVisible_options;
cout << "***** FAIR Program Options ***** \n" << fVisibleOptions;
return 1;
}
if (fvarmap.count("version"))
if (fVarMap.count("version"))
{
std::cout << "alpha version 0.0\n";
cout << "alpha version 0.0\n";
return 1;
}
return 0;
}
FairProgOptions::VarValInfo_t FairProgOptions::Get_variable_value_info(const po::variable_value& var_val)
FairProgOptions::VarValInfo_t FairProgOptions::GetVariableValueInfo(const po::variable_value& varValue)
{
// tuple<valueStr, type_info_str, defaultStr, empty>
auto& value = varValue.value();
string defaultedValue;
string emptyValue;
if (varValue.empty())
{
// tuple<val_str, type_info_str, default_str, empty>
auto& value = var_val.value();
std::string defaulted_val;
std::string empty_val;
if(var_val.empty())
empty_val=" [empty]";
emptyValue = " [empty]";
}
else
{
if (varValue.defaulted())
{
defaultedValue = " [default value]";
}
else
if(var_val.defaulted())
defaulted_val=" [default value]";
else
defaulted_val=" [provided value]";
empty_val+=" *";
// string
if(auto q = boost::any_cast< std::string >(&value))
{
std::string val_str = *q;
return std::make_tuple(val_str,std::string(" [Type=string]"),defaulted_val,empty_val);
defaultedValue = " [provided value]";
}
// vector<string>
if(auto q = boost::any_cast< std::vector<std::string> >(&value))
{
std::string val_str = variable_value_to_string< std::vector<std::string> >(var_val);
return std::make_tuple(val_str,std::string(" [Type=vector<string>]"),defaulted_val,empty_val);
}
// int
if(auto q = boost::any_cast< int >(&value))
{
std::string val_str = variable_value_to_string< int >(var_val);
return std::make_tuple(val_str,std::string(" [Type=int]"),defaulted_val,empty_val);
}
// vector<int>
if(auto q = boost::any_cast< std::vector<int> >(&value))
{
std::string val_str = variable_value_to_string< std::vector<int> >(var_val);
return std::make_tuple(val_str,std::string(" [Type=vector<int>]"),defaulted_val,empty_val);
}
// float
if(auto q = boost::any_cast< float >(&value))
{
std::string val_str = variable_value_to_string< float >(var_val);
return std::make_tuple(val_str,std::string(" [Type=float]"),defaulted_val,empty_val);
}
// vector<float>
if(auto q = boost::any_cast< std::vector<float> >(&value))
{
std::string val_str = variable_value_to_string< std::vector<float> >(var_val);
return std::make_tuple(val_str,std::string(" [Type=vector<float>]"),defaulted_val,empty_val);
}
// double
if(auto q = boost::any_cast< double >(&value))
{
std::string val_str = variable_value_to_string< double >(var_val);
return std::make_tuple(val_str,std::string(" [Type=double]"),defaulted_val,empty_val);
}
// vector<double>
if(auto q = boost::any_cast< std::vector<double> >(&value))
{
std::string val_str = variable_value_to_string< std::vector<double> >(var_val);
return std::make_tuple(val_str,std::string(" [Type=vector<double>]"),defaulted_val,empty_val);
}
// boost::filesystem::path
if(auto q = boost::any_cast<boost::filesystem::path>(&value))
{
std::string val_str = (*q).string();
//std::string val_str = (*q).filename().generic_string();
return std::make_tuple(val_str,std::string(" [Type=boost::filesystem::path]"),defaulted_val,empty_val);
}
// if we get here, the type is not supported return unknown info
return std::make_tuple(std::string("Unknown value"), std::string(" [Type=Unknown]"),defaulted_val,empty_val);
}
emptyValue += " *";
// string
if (auto q = boost::any_cast<string>(&value))
{
string valueStr = *q;
return make_tuple(valueStr, string(" [Type=string]"), defaultedValue, emptyValue);
}
// vector<string>
if (auto q = boost::any_cast<vector<string>>(&value))
{
string valueStr = VariableValueToString<vector<string>>(varValue);
return make_tuple(valueStr, string(" [Type=vector<string>]"), defaultedValue, emptyValue);
}
// int
if (auto q = boost::any_cast<int>(&value))
{
string valueStr = VariableValueToString<int>(varValue);
return make_tuple(valueStr, string(" [Type=int]"), defaultedValue, emptyValue);
}
// vector<int>
if (auto q = boost::any_cast<vector<int>>(&value))
{
string valueStr = VariableValueToString<vector<int>>(varValue);
return make_tuple(valueStr, string(" [Type=vector<int>]"), defaultedValue, emptyValue);
}
// float
if (auto q = boost::any_cast<float>(&value))
{
string valueStr = VariableValueToString<float>(varValue);
return make_tuple(valueStr, string(" [Type=float]"), defaultedValue, emptyValue);
}
// vector<float>
if (auto q = boost::any_cast<vector<float>>(&value))
{
string valueStr = VariableValueToString<vector<float>>(varValue);
return make_tuple(valueStr, string(" [Type=vector<float>]"), defaultedValue, emptyValue);
}
// double
if (auto q = boost::any_cast<double>(&value))
{
string valueStr = VariableValueToString<double>(varValue);
return make_tuple(valueStr, string(" [Type=double]"), defaultedValue, emptyValue);
}
// vector<double>
if (auto q = boost::any_cast<vector<double>>(&value))
{
string valueStr = VariableValueToString<vector<double>>(varValue);
return make_tuple(valueStr, string(" [Type=vector<double>]"), defaultedValue, emptyValue);
}
// boost::filesystem::path
if (auto q = boost::any_cast<boost::filesystem::path>(&value))
{
string valueStr = (*q).string();
//string valueStr = (*q).filename().generic_string();
return make_tuple(valueStr, string(" [Type=boost::filesystem::path]"), defaultedValue, emptyValue);
}
// if we get here, the type is not supported return unknown info
return make_tuple(string("Unknown value"), string(" [Type=Unknown]"), defaultedValue, emptyValue);
}

View File

@@ -5,7 +5,6 @@
* GNU Lesser General Public Licence version 3 (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
/*
* File: FairProgOptions.h
* Author: winckler
@@ -14,7 +13,7 @@
*/
#ifndef FAIRPROGOPTIONS_H
#define FAIRPROGOPTIONS_H
#define FAIRPROGOPTIONS_H
#include "FairMQLogger.h"
#include <boost/program_options.hpp>
@@ -39,15 +38,17 @@
* public :
* MyOptions() : FairProgOptions()
* {
* fCmdline_options.add(fGenericDesc);
* fVisible_options.add(fCmdline_options);
* fCmdlineOptions.add(fGenericDesc);
* fVisibleOptions.add(fCmdlineOptions);
* }
* virtual ~MyOptions(){}
* virtual int ParseAll(const int argc, char** argv, bool AllowUnregistered = false)
* virtual ~MyOptions() {}
* virtual int ParseAll(const int argc, char** argv, bool allowUnregistered = false)
* {
* if(ParseCmdLine(argc,argv,fCmdline_options,fvarmap,AllowUnregistered))
* if(ParseCmdLine(argc, argv, fCmdlineOptions, fVarMap, allowUnregistered))
* {
* return 1;
*
* }
*
* PrintOptions();
* return 0;
* }
@@ -66,17 +67,17 @@ namespace fs = boost::filesystem;
class FairProgOptions
{
public:
public:
FairProgOptions();
virtual ~FairProgOptions();
// add options_description
int AddToCmdLineOptions(const po::options_description& optdesc, bool visible = true);
int AddToCfgFileOptions(const po::options_description& optdesc, bool visible = true);
int AddToEnvironmentOptions(const po::options_description& optdesc);
int AddToCmdLineOptions(const po::options_description& optDesc, bool visible = true);
int AddToCfgFileOptions(const po::options_description& optDesc, bool visible = true);
int AddToEnvironmentOptions(const po::options_description& optDesc);
void UseConfigFile(const std::string& filename = "");
// get value corresponding to the key
template<typename T>
T GetValue(const std::string& key) const
@@ -84,9 +85,9 @@ public:
T val = T();
try
{
if (fvarmap.count(key))
if (fVarMap.count(key))
{
val = fvarmap[key].as<T>();
val = fVarMap[key].as<T>();
}
}
catch(std::exception& e)
@@ -102,27 +103,27 @@ public:
// convert value to string that corresponds to the key
std::string GetStringValue(const std::string& key);
const po::variables_map& GetVarMap() const {return fvarmap;}
const po::variables_map& GetVarMap() const {return fVarMap;}
// boost prog options parsers
int ParseCmdLine(const int argc, char** argv, const po::options_description& desc, po::variables_map& varmap, bool AllowUnregistered = false);
int ParseCmdLine(const int argc, char** argv, const po::options_description& desc, bool AllowUnregistered = false);
int ParseCmdLine(const int argc, char** argv, const po::options_description& desc, po::variables_map& varmap, bool allowUnregistered = false);
int ParseCmdLine(const int argc, char** argv, const po::options_description& desc, bool allowUnregistered = false);
int ParseCfgFile(const std::string& filename, const po::options_description& desc, po::variables_map& varmap, bool AllowUnregistered = false);
int ParseCfgFile(const std::string& filename, const po::options_description& desc, bool AllowUnregistered = false);
int ParseCfgFile(std::ifstream& ifs, const po::options_description& desc, po::variables_map& varmap, bool AllowUnregistered = false);
int ParseCfgFile(std::ifstream& ifs, const po::options_description& desc, bool AllowUnregistered = false);
int ParseCfgFile(const std::string& filename, const po::options_description& desc, po::variables_map& varmap, bool allowUnregistered = false);
int ParseCfgFile(const std::string& filename, const po::options_description& desc, bool allowUnregistered = false);
int ParseCfgFile(std::ifstream& ifs, const po::options_description& desc, po::variables_map& varmap, bool allowUnregistered = false);
int ParseCfgFile(std::ifstream& ifs, const po::options_description& desc, bool allowUnregistered = false);
int ParseEnvironment(const std::function<std::string(std::string)>&);
virtual int ParseAll(const int argc, char** argv, bool AllowUnregistered = false) = 0;
virtual int ParseAll(const int argc, char** argv, bool allowUnregistered = false) = 0;
virtual int PrintOptions();
int PrintHelp() const;
protected:
protected:
// options container
po::variables_map fvarmap;
po::variables_map fVarMap;
// basic description categories
po::options_description fGenericDesc;
@@ -131,14 +132,13 @@ protected:
po::options_description fHiddenDesc;
// Description of cmd line and simple configuration file (configuration file like txt, but not like xml json ini)
po::options_description fCmdline_options;
po::options_description fConfig_file_options;
po::options_description fCmdLineOptions;
po::options_description fConfigFileOptions;
// Description which is printed in help command line
po::options_description fVisible_options;
// to handle logger severity
std::map<std::string,fairmq::severity_level> fSeverity_map;
std::map<std::string,fairmq::severity_level> fSeverityMap;
po::options_description fVisibleOptions;
std::string fVerboseLvl;
bool fUseConfigFile;
@@ -149,7 +149,7 @@ protected:
template<typename T>
void UpadateVarMap(const std::string& key, const T& val)
{
replace(fvarmap, key, val);
replace(fVarMap, key, val);
}
template<typename T>
@@ -158,25 +158,24 @@ protected:
vm[opt].value() = boost::any(val);
}
private:
// /////////////////////////////////////////////
private:
// Methods below are helper functions used in the PrintOptions method
typedef std::tuple<std::string, std::string,std::string, std::string> VarValInfo_t;
typedef std::map<std::string, VarValInfo_t > MapVarValInfo_t;
typedef std::map<std::string, VarValInfo_t> MapVarValInfo_t;
VarValInfo_t Get_variable_value_info(const po::variable_value& var_val);
VarValInfo_t GetVariableValueInfo(const po::variable_value& varValue);
template<typename T>
std::string variable_value_to_string(const po::variable_value& var_val)
std::string VariableValueToString(const po::variable_value& varValue)
{
auto& value = var_val.value();
auto& value = varValue.value();
std::ostringstream ostr;
if (auto q = boost::any_cast<T>(&value))
{
ostr << *q;
}
std::string val_str = ostr.str();
return val_str;
std::string valStr = ostr.str();
return valStr;
}
static void Max(int &val, const int &comp)
@@ -188,6 +187,4 @@ private:
}
};
#endif /* FAIRPROGOPTIONS_H */
#endif /* FAIRPROGOPTIONS_H */