From 3434b35fb234ead94d9d4b0b45797294e9995603 Mon Sep 17 00:00:00 2001 From: Bjoern Sikora Date: Thu, 30 Jul 2009 17:52:12 +0200 Subject: [PATCH] CMD options have higher preference than config options. --- bpdyndnsd.conf | 3 +- dhs.conf | 1 - ods.conf | 5 ++ src/config.cpp | 113 +++++++++++++++++++++++++++++++----------------------- src/config.h | 3 + src/main.cpp | 2 + src/updater.cpp | 20 ++++++++++ src/updater.h | 4 ++ 8 files changed, 100 insertions(+), 51 deletions(-) create mode 100644 ods.conf diff --git a/bpdyndnsd.conf b/bpdyndnsd.conf index 9619bca..974c890 100644 --- a/bpdyndnsd.conf +++ b/bpdyndnsd.conf @@ -1,5 +1,4 @@ -[main] daemon_mode=0 logfile=/var/log/bpdyndnsd.log -loglevel=0 +loglevel=1 syslog=1 diff --git a/dhs.conf b/dhs.conf index d254944..6eba345 100644 --- a/dhs.conf +++ b/dhs.conf @@ -1,4 +1,3 @@ -[service] protocol=dhs host=hostname login=maxmuster diff --git a/ods.conf b/ods.conf new file mode 100644 index 0000000..9fc43b4 --- /dev/null +++ b/ods.conf @@ -0,0 +1,5 @@ +protocol=ods +host=hostname2 +login=maxmuster2 +password=secret2 + diff --git a/src/config.cpp b/src/config.cpp index 1253ab5..d8bd029 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -22,35 +22,45 @@ Config::Config(LoggerPtr _log) // initialize Logger Log = _log; - // Define valid command line parameters - Opt_desc_cmd = new po::options_description("Command line options"); - Opt_desc_cmd->add_options() + // Available service description config options + po::options_description opt_desc_service("Service description options"); + opt_desc_service.add_options() + ("protocol",po::value(),"The service protocol.") + ("host",po::value(),"The hostname to update.") + ("login",po::value(),"Login name.") + ("password",po::value(),"Corresponding password.") + ; + + // Available command line only options + po::options_description opt_desc_cmd_only("Command line only options"); + opt_desc_cmd_only.add_options() ("help,?","Show help.") ("version,v","Show version.") ("config,c",po::value()->default_value("/etc/bpdyndnsd"),"Set the config path.") - ("protocol,q",po::value(),"Set the service protocol type.") - ("host,h",po::value(),"Set the hostname to update.") - ("login,l",po::value(),"Set the login.") - ("password,p",po::value(),"Set the password.") ; - // Define valid config file main section parameters - Opt_desc_conf_main = new po::options_description("Config file main section options"); - Opt_desc_conf_main->add_options() - ("main.daemon_mode",po::value()->default_value(false),"Run as system daemon.") - ("main.logfile",po::value()->default_value("/var/log/bpdyndns.log"),"Where to log.") - ("main.loglevel",po::value()->default_value(0),"Loglevel.") - ("main.syslog",po::value()->default_value(false),"Use syslog facility.") + // Available generic options. Valid on cmd or in config file. + po::options_description opt_desc_generic("Generic config options"); + opt_desc_generic.add_options() + ("daemon_mode",po::value()->default_value(false),"Run as system daemon.") + ("logfile",po::value()->default_value("/var/log/bpdyndns.log"),"Where to log.") + ("loglevel",po::value()->default_value(0),"Loglevel.") + ("syslog",po::value()->default_value(false),"Use syslog facility.") ; - // Define valid config file main section parameters - Opt_desc_conf_service = new po::options_description("Config file service section options"); - Opt_desc_conf_service->add_options() - ("service.protocol",po::value(),"The service protocol.") - ("service.host",po::value(),"The hostname to update.") - ("service.login",po::value(),"Login name.") - ("service.password",po::value(),"Corresponding password.") - ; + // Define valid command line parameters + Opt_desc_cmd = new po::options_description("Command line options"); + Opt_desc_cmd->add(opt_desc_cmd_only); + Opt_desc_cmd->add(opt_desc_generic); + Opt_desc_cmd->add(opt_desc_service); + + // Define valid config file options + Opt_desc_conf_main = new po::options_description("Config file options"); + Opt_desc_conf_main->add(opt_desc_generic); + + // Define valid service file options + Opt_desc_conf_service = new po::options_description("Service file options"); + Opt_desc_conf_service->add(opt_desc_service); Log->print_constructor_call("Config"); } @@ -79,29 +89,28 @@ int Config::parse_cmd_line(int argc, char *argv[]) { try { - po::variables_map vm; - po::store(po::parse_command_line(argc, argv, *this->Opt_desc_cmd), vm); - po::notify(vm); + po::store(po::parse_command_line(argc, argv, *this->Opt_desc_cmd), Variables_map); + po::notify(Variables_map); - if ( vm.count("help") ) + if ( Variables_map.count("help") ) { Log->print_usage(Opt_desc_cmd); return 1; } - else if ( vm.count("version") ) + else if ( Variables_map.count("version") ) { Log->print_version(); return 1; } // Create a service object if all needed options are set on the command line - if ( vm.count("protocol") && vm.count("host") && vm.count("login") && vm.count("password") ) + if ( Variables_map.count("protocol") && Variables_map.count("host") && Variables_map.count("login") && Variables_map.count("password") ) { // Get the cmd parameter values for protocol host login and password - string protocol = vm["protocol"].as(); - string host = vm["host"].as(); - string login = vm["login"].as(); - string password = vm["password"].as(); + string protocol = Variables_map["protocol"].as(); + string host = Variables_map["host"].as(); + string login = Variables_map["login"].as(); + string password = Variables_map["password"].as(); //TODO: convert protocol option to lowercase @@ -111,16 +120,16 @@ int Config::parse_cmd_line(int argc, char *argv[]) else return 1; } - else if ( vm.count("protocol") || vm.count("host") || vm.count("login") || vm.count("password") ) + else if ( Variables_map.count("protocol") || Variables_map.count("host") || Variables_map.count("login") || Variables_map.count("password") ) { Log->print_missing_cmd_service_option(); Log->print_usage(Opt_desc_cmd); return 1; } - if ( vm.count("config") ) + if ( Variables_map.count("config") ) { - fs::path full_config_path = fs::system_complete(fs::path(vm["config"].as())); + fs::path full_config_path = fs::system_complete(fs::path(Variables_map["config"].as())); Config_path = full_config_path.string(); if ( !fs::exists(full_config_path) || !fs::is_directory(full_config_path) ) { @@ -188,13 +197,13 @@ int Config::load_service_config_file(const string& full_filename) po::store(parsed_service_options,vm); po::notify(vm); - if(vm.count("service.protocol") && vm.count("service.host") && vm.count("service.login") && vm.count("service.password")) + if(vm.count("protocol") && vm.count("host") && vm.count("login") && vm.count("password")) { // create the corresponding service - string protocol = vm["service.protocol"].as(); - string host = vm["service.host"].as(); - string login = vm["service.login"].as(); - string password = vm["service.password"].as(); + string protocol = vm["protocol"].as(); + string host = vm["host"].as(); + string login = vm["login"].as(); + string password = vm["password"].as(); // TODO: convert protocol to lowercase //protocol = tolower(protocol.c_str()); @@ -239,17 +248,16 @@ int Config::load_main_config_file(const string& full_filename) { try { - po::variables_map vm; po::parsed_options parsed_main_options = po::parse_config_file(main_config_file,*this->Opt_desc_conf_main,true); - po::store(parsed_main_options,vm); - po::notify(vm); + po::store(parsed_main_options,Variables_map); + po::notify(Variables_map); - if(vm.count("main.daemon_mode") && vm.count("main.logfile") && vm.count("main.loglevel") && vm.count("main.syslog")) + if(Variables_map.count("daemon_mode") && Variables_map.count("logfile") && Variables_map.count("loglevel") && Variables_map.count("syslog")) { - Daemon_mode = vm["main.daemon_mode"].as(); - Logfile = vm["main.logfile"].as(); - Loglevel = vm["main.loglevel"].as(); - Syslog = vm["main.syslog"].as(); + Daemon_mode = Variables_map["daemon_mode"].as(); + Logfile = Variables_map["logfile"].as(); + Loglevel = Variables_map["loglevel"].as(); + Syslog = Variables_map["syslog"].as(); } } catch ( po::unknown_option e ) @@ -348,3 +356,12 @@ po::options_description* Config::get_opt_desc_conf_service() { return Opt_desc_conf_service; } + +/** + * Getter for member Loglevel. + * @return Member Loglevel. + */ +int Config::get_loglevel() +{ + return Loglevel; +} diff --git a/src/config.h b/src/config.h index a208a54..657a2e3 100644 --- a/src/config.h +++ b/src/config.h @@ -42,6 +42,7 @@ private: po::options_description *Opt_desc_cmd; po::options_description *Opt_desc_conf_main; po::options_description *Opt_desc_conf_service; + po::variables_map Variables_map; list Services; LoggerPtr Log; @@ -74,6 +75,8 @@ public: po::options_description* get_opt_desc_conf_service(); + int get_loglevel(); + }; #endif diff --git a/src/main.cpp b/src/main.cpp index a6a9492..ca779c5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -53,6 +53,8 @@ int main(int argc, char *argv[]) if ( updater->init_config_from_files() != 0 ) return 0; + cout << "Loglevel: " << updater->get_config()->get_loglevel() << endl; + // set the configured loggin facility, default stdout // initialize daemon mode if configured diff --git a/src/updater.cpp b/src/updater.cpp index 4d75c1e..6f97121 100644 --- a/src/updater.cpp +++ b/src/updater.cpp @@ -72,6 +72,26 @@ int Updater::init_config_from_files() /** + * Getter for member Config. + * @return Member Config. + */ +ConfigPtr Updater::get_config() +{ + return Conf; +} + + +/** + * Getter for member Logger. + * @return Member Logger. + */ +LoggerPtr Updater::get_logger() +{ + return Log; +} + + +/** * Update all configured services. */ void Updater::update_services() diff --git a/src/updater.h b/src/updater.h index 51c9a6b..89ab806 100644 --- a/src/updater.h +++ b/src/updater.h @@ -32,6 +32,10 @@ public: int init_config_from_cmd(int, char **); int init_config_from_files(); + + ConfigPtr get_config(); + + LoggerPtr get_logger(); }; #endif -- 1.7.1