Goby3  3.1.5a
2024.05.23
configurator.h
Go to the documentation of this file.
1 // Copyright 2018-2024:
2 // GobySoft, LLC (2013-)
3 // Community contributors (see AUTHORS file)
4 // File authors:
5 // Toby Schneider <toby@gobysoft.org>
6 //
7 //
8 // This file is part of the Goby Underwater Autonomy Project Libraries
9 // ("The Goby Libraries").
10 //
11 // The Goby Libraries are free software: you can redistribute them and/or modify
12 // them under the terms of the GNU Lesser General Public License as published by
13 // the Free Software Foundation, either version 2.1 of the License, or
14 // (at your option) any later version.
15 //
16 // The Goby Libraries are distributed in the hope that they will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public License
22 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
23 
24 #ifndef GOBY_MIDDLEWARE_APPLICATION_CONFIGURATOR_H
25 #define GOBY_MIDDLEWARE_APPLICATION_CONFIGURATOR_H
26 
29 
30 namespace goby
31 {
32 namespace middleware
33 {
38 template <typename Config> class ConfiguratorInterface
39 {
40  public:
42  const Config& cfg() const { return cfg_; }
43 
46  virtual const protobuf::AppConfig& app_configuration() const { return app_configuration_; }
47 
51  virtual void validate() const {}
52 
55  {
56  std::cerr << "Invalid configuration: " << e.what() << std::endl;
57  }
58 
60  virtual std::string str() const = 0;
61 
62  protected:
64  Config& mutable_cfg() { return cfg_; }
65 
67  virtual protobuf::AppConfig& mutable_app_configuration() { return app_configuration_; }
68 
69  private:
70  Config cfg_;
71  protobuf::AppConfig app_configuration_;
72 };
73 
77 template <typename Config> class ProtobufConfigurator : public ConfiguratorInterface<Config>
78 {
79  public:
84  ProtobufConfigurator(int argc, char* argv[]);
85 
86  const protobuf::AppConfig& app_configuration() const override { return this->cfg().app(); }
87 
88  protected:
89  virtual void validate() const override
90  {
91  middleware::ConfigReader::check_required_cfg(this->cfg(), this->cfg().app().binary());
92  }
93 
94  private:
95  virtual std::string str() const override { return this->cfg().DebugString(); }
96 
97  void handle_config_error(middleware::ConfigException& e) const override
98  {
99  if (tool_has_help_action())
100  {
101  std::cerr << "Invalid command: " << e.what() << std::endl;
102  }
103  else
104  {
105  std::cerr << "Invalid configuration: use --help and/or --example_config for more help: "
106  << e.what() << std::endl;
107  }
108  }
109 
110  protobuf::AppConfig& mutable_app_configuration() override
111  {
112  return *this->mutable_cfg().mutable_app();
113  }
114 
115  void merge_app_base_cfg(protobuf::AppConfig* base_cfg,
116  const boost::program_options::variables_map& var_map);
117 
118  bool tool_has_help_action() const
119  {
120  return Config::descriptor()
121  ->options()
122  .GetExtension(goby::msg)
123  .cfg()
124  .tool()
125  .has_help_action();
126  }
127 };
128 
129 template <typename Config>
131 {
132  //
133  // read the configuration
134  //
135  Config& cfg = this->mutable_cfg();
136 
137  boost::program_options::variables_map var_map;
138  try
139  {
140  std::string application_name;
141  std::string binary_name;
142 
143  // we will check it later in validate()
144  bool check_required_cfg = false;
145  boost::program_options::options_description od{"All options"};
146  int read_argc = middleware::ConfigReader::read_cfg(
147  argc, argv, &cfg, &application_name, &binary_name, &od, &var_map, check_required_cfg);
148 
149  // extra command line parameters for tool mode
150  for (int a = read_argc; a < argc; ++a)
151  cfg.mutable_app()->mutable_tool_cfg()->add_extra_cli_param(argv[a]);
152 
153  cfg.mutable_app()->set_name(application_name);
154  cfg.mutable_app()->set_binary(binary_name);
155  // incorporate some parts of the AppBaseConfig that are middleware
156  // with gobyd (e.g. Verbosity)
157  merge_app_base_cfg(cfg.mutable_app(), var_map);
158  }
160  {
161  handle_config_error(e);
162 
163  // allow default action to take place (usually "help")
164  if (!tool_has_help_action())
165  throw;
166  }
167 
168  // TODO: convert to C++ struct app3 configuration format
169  this->mutable_app_configuration() = *cfg.mutable_app();
170 }
171 
172 template <typename Config>
175  const boost::program_options::variables_map& var_map)
176 {
177  if (var_map.count("ncurses"))
178  {
179  base_cfg->mutable_glog_config()->set_show_gui(true);
180  }
181 
182  if (var_map.count("verbose"))
183  {
184  switch (var_map["verbose"].as<std::size_t>())
185  {
186  case 0: break;
187  case 1:
190  break;
191  case 2:
194  break;
195  case 3:
198  break;
199  default:
200  case 4:
203  break;
204  }
205  }
206 
207  if (var_map.count("glog_file_verbose"))
208  {
209  switch (var_map["glog_file_verbose"].as<std::size_t>())
210  {
211  case 0: break;
212 
213  case 1:
216  break;
217  case 2:
220  break;
221  case 3:
224  break;
225  default:
226  case 4:
229  break;
230  }
231  }
232 
233  if (var_map.count("glog_file_dir"))
234  {
236  var_map["glog_file_dir"].as<std::string>());
237  }
238 }
239 
240 } // namespace middleware
241 } // namespace goby
242 
243 #endif
goby::util::protobuf::GLogConfig::DEBUG3
static const Verbosity DEBUG3
Definition: debug_logger.pb.h:532
goby::middleware::protobuf::AppConfig
Definition: app_config.pb.h:767
goby::util::protobuf::GLogConfig::VERBOSE
static const Verbosity VERBOSE
Definition: debug_logger.pb.h:526
goby::middleware::ConfigReader::read_cfg
static int read_cfg(int argc, char *argv[], google::protobuf::Message *message, std::string *application_name, std::string *binary_name, boost::program_options::options_description *od_all, boost::program_options::variables_map *var_map, bool check_required_configuration=true)
Read the configuration into a Protobuf message using the command line parameters.
goby::util::protobuf::GLogConfig_FileLog::set_file_dir
void set_file_dir(const ::std::string &value)
Definition: debug_logger.pb.h:757
goby::middleware::ProtobufConfigurator::ProtobufConfigurator
ProtobufConfigurator(int argc, char *argv[])
Constructs a ProtobufConfigurator. Typically passed as a parameter to goby::run.
Definition: configurator.h:130
goby
The global namespace for the Goby project.
Definition: acomms_constants.h:33
goby::middleware::ConfiguratorInterface::handle_config_error
virtual void handle_config_error(middleware::ConfigException &e) const
Override to customize how ConfigException errors are handled.
Definition: configurator.h:54
goby::util::e
constexpr T e
Definition: constants.h:35
goby::util::protobuf::GLogConfig_FileLog::set_verbosity
void set_verbosity(::goby::util::protobuf::GLogConfig_Verbosity value)
Definition: debug_logger.pb.h:823
goby::util::protobuf::GLogConfig::mutable_file_log
::goby::util::protobuf::GLogConfig_FileLog * mutable_file_log()
Definition: debug_logger.pb.h:995
goby::middleware::ConfiguratorInterface::str
virtual std::string str() const =0
Override to output the configuration object as a string.
goby::middleware::ConfigException
indicates a problem with the runtime command line or .cfg file configuration (or –help was given)
Definition: configuration_reader.h:56
goby::middleware::protobuf::AppConfig::mutable_glog_config
::goby::util::protobuf::GLogConfig * mutable_glog_config()
Definition: app_config.pb.h:1455
goby::middleware::ProtobufConfigurator
Implementation of ConfiguratorInterface for Google Protocol buffers.
Definition: configurator.h:77
goby::util::protobuf::GLogConfig::set_tty_verbosity
void set_tty_verbosity(::goby::util::protobuf::GLogConfig_Verbosity value)
Definition: debug_logger.pb.h:934
goby::util::protobuf::GLogConfig::DEBUG2
static const Verbosity DEBUG2
Definition: debug_logger.pb.h:530
goby::util::protobuf::GLogConfig::set_show_gui
void set_show_gui(bool value)
Definition: debug_logger.pb.h:959
goby::util::protobuf::GLogConfig::DEBUG1
static const Verbosity DEBUG1
Definition: debug_logger.pb.h:528
goby::middleware::ConfiguratorInterface::cfg
const Config & cfg() const
The configuration object produced from the command line parameters.
Definition: configurator.h:42
goby::middleware::ConfiguratorInterface::app_configuration
virtual const protobuf::AppConfig & app_configuration() const
Subset of the configuration used to configure the Application itself.
Definition: configurator.h:46
goby::msg
extern ::google::protobuf::internal::ExtensionIdentifier< ::google::protobuf::MessageOptions, ::google::protobuf::internal::MessageTypeTraits< ::goby::GobyMessageOptions >, 11, false > msg
Definition: option_extensions.pb.h:1327
goby::middleware::ConfigReader::check_required_cfg
static void check_required_cfg(const google::protobuf::Message &message, const std::string &binary)
Checks that all required fields are set (either via the command line or the configuration file) in th...
goby::middleware::ConfiguratorInterface::validate
virtual void validate() const
Override to validate the configuration.
Definition: configurator.h:51
goby::middleware::ProtobufConfigurator::app_configuration
const protobuf::AppConfig & app_configuration() const override
Subset of the configuration used to configure the Application itself.
Definition: configurator.h:86
goby::middleware::ProtobufConfigurator::validate
virtual void validate() const override
Override to validate the configuration.
Definition: configurator.h:89
goby::middleware::ConfiguratorInterface::mutable_app_configuration
virtual protobuf::AppConfig & mutable_app_configuration()
Derived classes can modify the application configuration as needed in their constructor.
Definition: configurator.h:67
configuration_reader.h
goby::middleware::ConfiguratorInterface::mutable_cfg
Config & mutable_cfg()
Derived classes can modify the configuration as needed in their constructor.
Definition: configurator.h:64
goby::middleware::ConfiguratorInterface
Defines the interface to a "configurator", a class that can read command line parameters (argc,...
Definition: configurator.h:38
app_config.pb.h