Improved conf file and cmd option parsing and corresponding logging.
authorBjoern Sikora <bjoern.sikora@intra2net.com>
Wed, 26 Aug 2009 09:45:11 +0000 (11:45 +0200)
committerBjoern Sikora <bjoern.sikora@intra2net.com>
Wed, 26 Aug 2009 09:45:11 +0000 (11:45 +0200)
src/config.cpp
src/logger.cpp
src/logger.h

index f1fc0ea..ab55b22 100644 (file)
@@ -74,8 +74,8 @@ Config::Config(Logger::Ptr _log, Serviceholder::Ptr _serviceholder)
         ("enable_ipv6",po::value<bool>()->default_value(false),"Try to use IPv6.")
         ("webcheck_url",po::value<string>()->default_value(""),"Use this URL to determine IP.")
         ("webcheck_url_alt",po::value<string>()->default_value(""),"Use this alternative URL to determine IP.")
-        ("http_proxy",po::value<string>()->default_value(""),"Use this proxy for all http requests.")
-        ("http_proxy_port",po::value<int>()->default_value(0),"Port of the proxy.")
+        ("http_proxy",po::value<string>(),"Use this proxy for all http requests.")
+        ("http_proxy_port",po::value<int>(),"Port of the proxy.")
     ;
 
     // Define valid command line parameters
@@ -201,11 +201,18 @@ int Config::parse_cmd_line(int argc, char *argv[])
         if ( VariablesMap.count("webcheck_url_alt") )
             WebcheckIpUrlAlt = VariablesMap["webcheck_url_alt"].as<string>();
 
-        if ( VariablesMap.count("http_proxy") )
+        if ( VariablesMap.count("http_proxy") && VariablesMap.count("http_proxy_port") )
+        {
             Proxy = VariablesMap["http_proxy"].as<string>();
-
-        if ( VariablesMap.count("http_proxy_port") )
             ProxyPort = VariablesMap["http_proxy_port"].as<int>();
+        }
+        else if ( VariablesMap.count("http_proxy") || VariablesMap.count("http_proxy_port") )
+        {
+            Log->print_missing_cmd_proxy_option();
+            Log->print_usage(OptDescCmd);
+            return -1;
+        }
+
 
     }
     catch(po::unknown_option e)
@@ -283,15 +290,15 @@ int Config::load_service_config_file(const string& full_filename)
                 protocol = ba::to_lower_copy(protocol);
 
                 int update_interval = 0;
-                if ( VariablesMap.count("update_interval") )
+                if ( vm.count("update_interval") )
                     update_interval = VariablesMap["update_interval"].as<int>();
 
                 int max_updates_within_interval = 0;
-                if ( VariablesMap.count("max_updates_within_interval") )
+                if ( vm.count("max_updates_within_interval") )
                     max_updates_within_interval = VariablesMap["max_updates_within_interval"].as<int>();
 
                 int dns_cache_ttl = 0;
-                if ( VariablesMap.count("dns_cache_ttl") )
+                if ( vm.count("dns_cache_ttl") )
                     dns_cache_ttl = VariablesMap["dns_cache_ttl"].as<int>();
 
                 Service::Ptr service = create_service(protocol,host,login,password,update_interval,max_updates_within_interval,dns_cache_ttl);
@@ -303,12 +310,24 @@ int Config::load_service_config_file(const string& full_filename)
                 else
                     return -1;
             }
+            else if ( vm.count("protocol") || vm.count("host") || vm.count("login") || vm.count("password") )
+            {
+                service_config_file.close();
+                Log->print_missing_service_conf_option(full_filename);
+                return -1;
+            }
         }
         catch ( po::unknown_option e )
         {
             // unknown option in config file detected
             service_config_file.close();
-            Log->print_unknown_service_conf_option(e.what());
+            Log->print_unknown_service_conf_option(full_filename,e.what());
+            return -1;
+        }
+        catch(po::multiple_occurrences e)
+        {
+            service_config_file.close();
+            Log->print_multiple_service_conf_option(full_filename,e.what());
             return -1;
         }
         service_config_file.close();
@@ -341,29 +360,35 @@ int Config::load_main_config_file(const string& full_filename)
             po::store(parsed_main_options,VariablesMap);
             po::notify(VariablesMap);
 
-        if ( VariablesMap.count("daemon_mode") )
-            DaemonMode = VariablesMap["daemon_mode"].as<bool>();
+            if ( VariablesMap.count("daemon_mode") )
+                DaemonMode = VariablesMap["daemon_mode"].as<bool>();
 
-        if ( VariablesMap.count("loglevel") )
-            Loglevel = VariablesMap["loglevel"].as<int>();
+            if ( VariablesMap.count("loglevel") )
+                Loglevel = VariablesMap["loglevel"].as<int>();
 
-        if ( VariablesMap.count("syslog") )
-            Syslog = VariablesMap["syslog"].as<bool>();
+            if ( VariablesMap.count("syslog") )
+                Syslog = VariablesMap["syslog"].as<bool>();
 
-        if ( VariablesMap.count("enable_ipv6") )
-            EnableIPv6 = VariablesMap["enable_ipv6"].as<bool>();
-
-        if ( VariablesMap.count("webcheck_url") )
-            WebcheckIpUrl = VariablesMap["webcheck_url"].as<string>();
+            if ( VariablesMap.count("enable_ipv6") )
+                EnableIPv6 = VariablesMap["enable_ipv6"].as<bool>();
 
-        if ( VariablesMap.count("webcheck_url_alt") )
-            WebcheckIpUrlAlt = VariablesMap["webcheck_url_alt"].as<string>();
+            if ( VariablesMap.count("webcheck_url") )
+                WebcheckIpUrl = VariablesMap["webcheck_url"].as<string>();
 
-        if ( VariablesMap.count("http_proxy") )
-            Proxy = VariablesMap["http_proxy"].as<string>();
+            if ( VariablesMap.count("webcheck_url_alt") )
+                WebcheckIpUrlAlt = VariablesMap["webcheck_url_alt"].as<string>();
 
-        if ( VariablesMap.count("http_proxy_port") )
-            ProxyPort = VariablesMap["http_proxy_port"].as<int>();
+            if ( VariablesMap.count("http_proxy") && VariablesMap.count("http_proxy_port") )
+            {
+                Proxy = VariablesMap["http_proxy"].as<string>();
+                ProxyPort = VariablesMap["http_proxy_port"].as<int>();
+            }
+            else if ( VariablesMap.count("http_proxy") || VariablesMap.count("http_proxy_port") )
+            {
+                main_config_file.close();
+                Log->print_missing_conf_proxy_option(full_filename);
+                return -1;
+            }
 
         }
         catch ( po::unknown_option e )      // at the moment 04-08-2009 this exception is never thrown :-(
@@ -373,6 +398,12 @@ int Config::load_main_config_file(const string& full_filename)
             Log->print_unknown_main_conf_option(e.what());
             return -1;
         }
+        catch(po::multiple_occurrences e)
+        {
+            main_config_file.close();
+            Log->print_multiple_main_conf_option(full_filename,e.what());
+            return -1;
+        }
         main_config_file.close();
     }
     else
index a45b42a..a94c784 100644 (file)
@@ -355,12 +355,12 @@ void Logger::print_load_main_conf(const string& filename) const
  * There is an unknown option in a service config file.
  * @param unknown_option The unknown option.
  */
-void Logger::print_unknown_service_conf_option(const string& unknown_option) const
+void Logger::print_unknown_service_conf_option(const string& service_conf_file, const string& unknown_option) const
 {
     if ( 0 <= Loglevel )
     {
         ostringstream msg;
-        msg << "Unknown option in service config file detected: " << unknown_option << endl;
+        msg << "Unknown option in service config file detected: " << service_conf_file << " Unknown option: " << unknown_option << endl;
         log_error(msg.str());
     }
 }
@@ -411,6 +411,21 @@ void Logger::print_missing_cmd_service_option() const
 
 
 /**
+ * Missing option in service config file.
+ * @param service_conf_file Service config file
+ */
+void Logger::print_missing_service_conf_option(const string& service_conf_file) const
+{
+    if ( 0 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Missing option in service config file " << service_conf_file << " to initialize service. Protocol, host, login and password must be specified." << endl;
+        log_error(msg.str());
+    }
+}
+
+
+/**
  * Process running as daemon.
  * @param pid The pid of the daemon.
  */
@@ -984,7 +999,7 @@ void Logger::print_starting_shutdown_parent() const
  * @param dns_cache_ttl DNS cache TTL
  * @param current_time Current time
  */
-void Logger::print_recheck_dns_entry(const std::string& hostname, const int lastupdated, const int dns_cache_ttl, const int current_time) const
+void Logger::print_recheck_dns_entry(const string& hostname, const int lastupdated, const int dns_cache_ttl, const int current_time) const
 {
     if ( 1 <= Loglevel )
     {
@@ -1000,7 +1015,7 @@ void Logger::print_recheck_dns_entry(const std::string& hostname, const int last
  * @param hostname Hostname
  * @param cached_dns_entry IP
  */
-void Logger::print_cached_dns_entry(const std::string& hostname, const std::string& cached_dns_entry, const std::string& lastupdated_ip) const
+void Logger::print_cached_dns_entry(const string& hostname, const string& cached_dns_entry, const string& lastupdated_ip) const
 {
     if ( 1 <= Loglevel )
     {
@@ -1009,3 +1024,63 @@ void Logger::print_cached_dns_entry(const std::string& hostname, const std::stri
         log_notice(msg.str());
     }
 }
+
+
+/**
+ * Missing proxy option on command line.
+ */
+void Logger::print_missing_cmd_proxy_option() const
+{
+    if ( 0 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Missing option to initialize proxy. http_proxy and http_proxy_port must be specified." << endl;
+        log_error(msg.str());
+    }
+}
+
+
+/**
+ * Multiple option in service config file.
+ * @param service_conf_file Service config file
+ * @param message Multiple option text
+ */
+void Logger::print_multiple_service_conf_option(const string& service_conf_file, const string& message) const
+{
+    if ( 0 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Multiple occurrences of the same option in service config file detected: " << service_conf_file << " " << message << endl;
+        log_error(msg.str());
+    }
+}
+
+
+/**
+ * Multiple option in main config file.
+ * @param service_conf_file Service config file
+ * @param message Multiple option text
+ */
+void Logger::print_multiple_main_conf_option(const string& main_conf_file, const string& message) const
+{
+    if ( 0 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Multiple occurrences of the same option in main config file detected: " << main_conf_file << " " << message << endl;
+        log_error(msg.str());
+    }
+}
+
+
+/**
+ * Missing proxy option in main config file.
+ */
+void Logger::print_missing_conf_proxy_option(const string& main_conf_filename) const
+{
+    if ( 0 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Missing option to initialize proxy in main config file: " << main_conf_filename << " http_proxy and http_proxy_port must be specified." << endl;
+        log_error(msg.str());
+    }
+}
index 085ed67..bba853f 100644 (file)
@@ -71,7 +71,7 @@ public:
 
     void print_load_main_conf(const std::string& filename) const;
 
-    void print_unknown_service_conf_option(const std::string& unknown_option) const;
+    void print_unknown_service_conf_option(const std::string& service_conf_file, const std::string& unknown_option) const;
 
     void print_unknown_main_conf_option(const std::string& unknown_option) const;
 
@@ -87,6 +87,8 @@ public:
 
     void print_missing_cmd_service_option() const;
 
+    void print_missing_service_conf_option(const std::string& service_conf_file) const;
+
     void print_runnig_as_daemon(const int pid) const;
 
     void print_daemon_mode(const bool daemon_mode) const;
@@ -149,6 +151,10 @@ public:
 
     void print_multiple_cmd_option(const std::string& message) const;
 
+    void print_multiple_service_conf_option(const std::string& service_conf_file, const std::string& message) const;
+
+    void print_multiple_main_conf_option(const std::string& main_conf_file, const std::string& message) const;
+
     void print_update_not_allowed(const int current_time, const int old_time, const int MaxUpdatesWithinInterval, const std::string& service) const;
 
     void print_update_service_failure(const std::string& service) const;
@@ -164,6 +170,10 @@ public:
     void print_recheck_dns_entry(const std::string& hostname, const int lastupdated, const int dns_cache_ttl, const int current_time) const;
 
     void print_cached_dns_entry(const std::string& hostname, const std::string& cached_dns_entry, const std::string& lastupdated_ip) const;
+
+    void print_missing_cmd_proxy_option() const;
+
+    void print_missing_conf_proxy_option(const std::string& main_conf_filename) const;
 };
 
 #endif