15 #include <rad/assert.hpp>
16 #include <rad/config.hpp>
17 #include <rad/exceptions.hpp>
18 #include <rad/helper.hpp>
20 #include <boost/program_options.hpp>
21 #include <fmt/chrono.h>
22 #include <fmt/format.h>
28 namespace bpo = boost::program_options;
32 static auto Format(std::filesystem::path
const &t) {
60 {rad::KEY_CONFIG_SM_SCXML,
"State machine SCXML model file path"});
63 {rad::KEY_CONFIG_OLDB_CONN_TIMEOUT,
"CII OLDB timeout (sec)"});
68 {
"cfg/daq/workspace",
"OCM DAQ workspace"});
73 {
"cfg/dpm/timeout_sec",
"DPM timeout (sec)"});
77 {
"cfg/daq/stale_acquiring_hours",
"Age when DAQ acquisitions are considered stale (hours)"});
80 {
"cfg/daq/stale_merging_hours",
"Age when DAQ merges are considered stale (hours)"});
96 bpo::options_description options_desc(
"Options");
98 options_desc.add_options()
99 (
"help,h",
"Print help messages")
101 bpo::value<std::string>(),
104 bpo::value<std::string>(),
105 "Log level: ERROR, WARNING, STATE, EVENT, ACTION, INFO, DEBUG, TRACE")
107 bpo::value<std::string>(),
108 "Configuration filename")
110 bpo::value<std::string>(),
111 "*deprecated* In-memory DB host (ipaddr:port)");
116 bpo::variables_map options_map;
117 bpo::store(bpo::parse_command_line(argc, argv, options_desc), options_map);
118 if (options_map.count(
"help")) {
119 std::cout << options_desc << std::endl;
127 bpo::notify(options_map);
131 if (options_map.count(
"log-level")) {
134 auto ll_str = options_map[
"log-level"].as<std::string>();
138 if (options_map.count(
"proc-name")) {
139 auto value = options_map[
"proc-name"].as<std::string>();
148 if (options_map.count(
"config")) {
149 auto value = options_map[
"config"].as<std::string>();
152 if (options_map.count(
"db-host")) {
154 "Command line parameter --db-host|-d is deprecated and only provided "
155 "for compatibility");
157 }
catch (bpo::error &e) {
158 std::cerr <<
"ERROR: " << e.what() << std::endl << std::endl;
159 std::cerr << options_desc << std::endl;
160 throw std::invalid_argument(e.what());
170 std::string config_filename = filename;
171 if (config_filename ==
"") {
176 std::string resolved_config_filename = rad::Helper::FindFile(config_filename);
177 if (resolved_config_filename.size() == 0) {
178 LOG4CPLUS_ERROR(
GetLogger(),
"Cannot find <" << config_filename <<
">");
179 throw rad::Exception(
"Cannot find " + config_filename, resolved_config_filename);
182 "Loading configuration from <" << resolved_config_filename <<
">");
186 auto ifs = std::ifstream(resolved_config_filename);
187 ifs.exceptions(std::ios_base::badbit);
190 auto const &root =
m_config->GetInstanceRootNode();
193 if (
auto value = GetParamAs<std::string>(rad::KEY_CONFIG_LOG_PROPERTIES, root); value) {
197 if (
auto value = GetParamAs<std::string>(rad::KEY_CONFIG_REQ_ENDPOINT, root); value) {
200 throw rad::Exception(
"Invalid configuration",
201 fmt::format(
"invalid configuration for '{}': '{}' request "
202 "endpoint must be non-empty and have a trailing "
203 "path separator '/'.",
204 rad::KEY_CONFIG_REQ_ENDPOINT,
211 throw rad::Exception(
"Invalid configuration",
212 fmt::format(
"invalid configuration for '{}': '{}' publish "
213 "endpoint must be non-empty and have a trailing "
214 "path separator '/'.",
220 if (
auto value = GetParamAs<std::string>(rad::KEY_CONFIG_OLDB_URI_PREFIX, root); value) {
224 if (
auto value = GetParamAs<int>(rad::KEY_CONFIG_OLDB_CONN_TIMEOUT, root); value) {
228 if (
auto value = GetParamAs<std::string>(rad::KEY_CONFIG_SM_SCXML, root); value) {
238 throw rad::Exception(
264 throw rad::Exception(
274 throw rad::Exception(
281 }
catch (std::exception
const &e) {
283 throw rad::Exception(
"Failed to load configuration", filename);
bool Update(AttrType *ptr, T const &value, OriginInfo const &origin)
Update configuration value taking into account the origin of the change.
void Register(AttrType *ptr, Metadata const &meta)
Registers a configuration parameter/attribute.
const std::string & GetLogLevel() const
std::filesystem::path GetWorkspace() const
std::string m_pub_endpoint
daq::DpmClientParams m_dpm_params
const std::string & GetSmScxmlFilename() const
std::string m_log_properties
bool ParseOptions(int argc, char *argv[])
Disable assignment operator.
Config()
Default constructor.
daq::DpmClientParams const & GetDpmClientParams() const
const std::string & GetMsgReplierEndpoint() const
const std::string & GetConfigFilename() const
void LoadConfig(const std::string &filename="")
This method load from a configuration file the application configuration overriding the initializatio...
std::chrono::seconds GetDbTimeout() const
std::optional< elt::configng::CiiConfigDocument > m_config
std::chrono::hours m_stale_merging
std::chrono::hours m_stale_acquiring
std::string m_instrument_id
const std::string & GetLogProperties() const
std::string m_req_endpoint
std::string m_config_filename
daq::config::Manager m_mgr
std::filesystem::path m_workspace
const std::string & GetProcName() const
const std::string & GetPubEndpoint() const
std::string m_scxml_filename
const std::string & GetDbPrefix() const
daq::config::Manager and associated types.
@ Configuration
Configuration file.
@ Default
Built-in default value.
@ CommandLine
Command line argument.
@ EnvironmentVariable
Environment variable.
std::optional< T > GetParamAs(std::string const &query, elt::configng::CiiConfigInstanceNode const &node)
Performs lookup of parameters in the form root/node/leaf relative to the provided node.
Mutable metadata about a configuration attribute that describes where a value comes from.
std::chrono::seconds timeout
Connection parameters for DPM.
const std::string CONFIG_DEFAULT_WORKSPACE
const std::string KEY_CONFIG_PUB_ENDPOINT
Rad configuration keys.
const std::string KEY_CONFIG_DPM_TIMEOUT_SEC
const std::string KEY_CONFIG_STALE_DAQ_ACQUIRING
const std::string CONFIG_DEFAULT_SCXML_FILENAME
const std::string KEY_CONFIG_DATAROOT
const std::string CONFIG_DEFAULT_REQ_ENDPOINT
const std::string CONFIG_DEFAULT_OLDB_URI_PREFIX
const std::string KEY_CONFIG_DPM_PUB_ENDPOINT
const std::string KEY_CONFIG_WORKSPACE
const int CONFIG_DEFAULT_DB_TIMEOUT_SEC
const std::string CONFIG_DEFAULT_PUB_ENDPOINT
const std::string CONFIG_DEFAULT_LOG_LEVEL
const std::string KEY_CONFIG_INSTRUMENT_ID
const std::string CONFIG_ENVVAR_OUT_PATH
Application configuration environment variables.
const std::string CONFIG_DEFAULT_PROCNAME
Default application configuration values.
const std::string CONFIG_DEFAULT_FILENAME
const std::string KEY_CONFIG_STALE_DAQ_MERGING
const std::string KEY_CONFIG_DPM_REQ_ENDPOINT
log4cplus::Logger & GetLogger()
Config class header file.