Bugfix: Catch also generic program_options error.
authorBjoern Sikora <bjoern.sikora@intra2net.com>
Mon, 21 Sep 2009 09:53:20 +0000 (11:53 +0200)
committerBjoern Sikora <bjoern.sikora@intra2net.com>
Mon, 21 Sep 2009 09:53:20 +0000 (11:53 +0200)
Don't use pointer to std::list<int>, make a copy.

src/config.cpp
src/logger.cpp
src/logger.h
src/serializeservicecontainer.h
src/service.cpp
src/service.h
src/serviceholder.cpp
src/updater.cpp

index 8ca4025..3ab375b 100644 (file)
@@ -242,6 +242,12 @@ int Config::parse_cmd_line(int argc, char *argv[])
         Log->print_usage(OptDescCmd);
         return -1;
     }
+    catch( po::error e )
+    {
+        Log->print_error_parsing_cmd(e.what());
+        Log->print_usage(OptDescCmd);
+        return -1;
+    }
     return 0;
 }
 
@@ -350,7 +356,7 @@ int Config::load_service_config_file(const string& full_filename)
                 string protocol = vm["protocol"].as<string>();
                 string host = vm["host"].as<string>();
                 string login = vm["login"].as<string>();
-                string password = vm["password"].as<string>();
+                    string password = vm["password"].as<string>();
 
                 protocol = ba::to_lower_copy(protocol);
 
@@ -393,12 +399,18 @@ int Config::load_service_config_file(const string& full_filename)
             Log->print_unknown_service_conf_option(full_filename,e.what());
             return -1;
         }
-        catch(po::multiple_occurrences e)
+        catch( po::multiple_occurrences e )
         {
             service_config_file.close();
             Log->print_multiple_service_conf_option(full_filename,e.what());
             return -1;
         }
+        catch( po::error e )
+        {
+            service_config_file.close();
+            Log->print_error_parsing_config_file(full_filename,e.what());
+            return -1;
+        }
         service_config_file.close();
     }
     else
index bf8a58a..5986e70 100644 (file)
@@ -659,7 +659,7 @@ void Logger::print_deserialized_objects_success() const
  * @param actual_ip Service's actual_ip.
  * @param lastupdated Service's lastupdated.
  */
-void Logger::print_service_object(const string& message, const string& protocol, const string& hostname, const string& login, const string& password, const int update_interval, const int max_updates_within_interval, const int dns_cache_ttl , const string& actual_ip, list<int>* lastupdated) const
+void Logger::print_service_object(const string& message, const string& protocol, const string& hostname, const string& login, const string& password, const int update_interval, const int max_updates_within_interval, const int dns_cache_ttl , const string& actual_ip, list<int> lastupdated) const
 {
     int level = 1;
     if ( level <= Loglevel )
@@ -674,7 +674,7 @@ void Logger::print_service_object(const string& message, const string& protocol,
         msg << "\t" << "Max Updates:      " << max_updates_within_interval << endl;
         msg << "\t" << "DNS Cache TTL:    " << dns_cache_ttl << endl;
         msg << "\t" << "Actual_IP:        " << actual_ip << endl;
-        BOOST_FOREACH( int update_time, *lastupdated)
+        BOOST_FOREACH( int update_time, lastupdated)
         {
             msg << "\t" << "Lastupdated:      " << update_time << endl;
         }
@@ -1411,7 +1411,7 @@ void Logger::print_gnudip_requires_servername() const
  * An exception occured while computing the md5 sum.
  * @param what The exception occured.
  */
-void Logger::print_exception_md5_sum(const std::string& what) const
+void Logger::print_exception_md5_sum(const string& what) const
 {
     int level = 0;
     if ( level <= Loglevel )
@@ -1427,7 +1427,7 @@ void Logger::print_exception_md5_sum(const std::string& what) const
  * An network exception occured.
  * @param what The exception occured.
  */
-void Logger::print_network_error(const std::string& what) const
+void Logger::print_network_error(const string& what) const
 {
     int level = 0;
     if ( level <= Loglevel )
@@ -1444,7 +1444,7 @@ void Logger::print_network_error(const std::string& what) const
  * @param protocol The protocol
  * @param error The error
  */
-void Logger::print_undefined_protocol_error(const std::string& protocol, const std::string& error) const
+void Logger::print_undefined_protocol_error(const string& protocol, const string& error) const
 {
     int level = 0;
     if ( level <= Loglevel )
@@ -1459,7 +1459,7 @@ void Logger::print_undefined_protocol_error(const std::string& protocol, const s
  * Error while trying to log through external program.
  * @param external_prog The external program called.
  */
-void Logger::print_error_external_logging(const std::string& external_prog) const
+void Logger::print_error_external_logging(const string& external_prog) const
 {
     int level = 0;
     if ( level <= Loglevel )
@@ -1469,3 +1469,36 @@ void Logger::print_error_external_logging(const std::string& external_prog) cons
         log_notice(msg.str(),level);
     }
 }
+
+
+/**
+ * Error while parsing config file.
+ * @param error Error occured.
+ * @param config_file Config file.
+ */
+void Logger::print_error_parsing_config_file(const string& filename, const string& error) const
+{
+    int level = 0;
+    if ( level <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Error while parsing config file: " << filename << " Error: " << error << endl;
+        log_notice(msg.str(),level);
+    }
+}
+
+
+/**
+ * Error while parsing cmd option
+ * @param error Error
+ */
+void Logger::print_error_parsing_cmd(const string& error) const
+{
+    int level = 0;
+    if ( level <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Error while parsing cmd options: " << error << endl;
+        log_error(msg.str(),level);
+    }
+}
index 97bec42..6381142 100644 (file)
@@ -116,7 +116,7 @@ public:
 
     void print_deserialized_objects_success() const;
 
-    void print_service_object(const std::string& message, const std::string& protocol, const std::string& hostname, const std::string& login, const std::string& password, const int update_interval, const int max_updates_within_interval, const int dns_cache_ttl , const std::string& actual_ip, std::list<int>* lastupdated) const;
+    void print_service_object(const std::string& message, const std::string& protocol, const std::string& hostname, const std::string& login, const std::string& password, const int update_interval, const int max_updates_within_interval, const int dns_cache_ttl , const std::string& actual_ip, std::list<int> lastupdated) const;
 
     void print_exception_serialize(const std::string& exception) const;
 
@@ -213,6 +213,10 @@ public:
     void print_undefined_protocol_error(const std::string& protocol, const std::string& error) const;
 
     void print_error_external_logging(const std::string& external_prog) const;
+
+    void print_error_parsing_config_file(const std::string& filename, const std::string& error) const;
+
+    void print_error_parsing_cmd(const std::string& error) const;
 };
 
 #endif
index b890865..85eac11 100644 (file)
@@ -14,6 +14,7 @@
 #include "logger.h"
 
 #include <boost/serialization/list.hpp>
+#include <boost/serialization/vector.hpp>
 #include <boost/serialization/shared_ptr.hpp>
 #include <boost/serialization/export.hpp>
 
index 7dc10e8..86ff045 100644 (file)
@@ -37,27 +37,6 @@ Service::~Service()
 
 
 /**
- * Although this is an abstract class, we need the serialize function that we can serialize derived classes through a Service *.
- * @param ar Archive.
- * @param version Version.
- */
-/*
-template<class Archive>
-void Service::serialize(Archive & ar, const unsigned int version)
-{
-    // protocol and hostname are the unique identifier for each service.
-    ar & Protocol;
-    ar & Hostname;
-    // lastupdated and actual_ip must also be serialized, cause these are essential infos of each service.
-    ar & LastUpdates;
-    ar & ActualIP;
-    ar & UpdateInterval;
-    ar & MaxUpdatesWithinInterval;
-    ar & DNSCacheTTL;
-}
-*/
-
-/**
  * Setter for member Protocol.
  * @param _protocol Value to set Protocol to.
  */
@@ -157,10 +136,10 @@ Logger::Ptr Service::get_logger() const
 }
 
 
-void Service::set_last_updates(std::list<int>* _last_updates)
+void Service::set_last_updates(std::list<int> _last_updates)
 {
     LastUpdates.clear();
-    BOOST_FOREACH( int update_time, *_last_updates )
+    BOOST_FOREACH( int update_time, _last_updates )
     {
         LastUpdates.push_back(update_time);
     }
@@ -171,9 +150,9 @@ void Service::set_last_updates(std::list<int>* _last_updates)
  * Getter for member Lastupdated.
  * @return Value of member Lastupdated.
  */
-list<int>* Service::get_last_updates()
+list<int> Service::get_last_updates()
 {
-    return &LastUpdates;
+    return LastUpdates;
 }
 
 
index 572a600..338f33e 100644 (file)
@@ -91,8 +91,8 @@ public:
     void set_password(const std::string& _password);
     std::string get_password() const;
 
-    void set_last_updates(std::list<int>* _last_updates);
-    std::list<int>* get_last_updates();
+    void set_last_updates(std::list<int> _last_updates);
+    std::list<int> get_last_updates();
 
     void set_actual_ip(const std::string& _actual_ip);
     std::string get_actual_ip() const;
index 7dfbd3f..2e5c395 100644 (file)
@@ -56,7 +56,7 @@ int Serviceholder::serialize_services()
 
     BOOST_FOREACH(Service::Ptr &service, OldServices)
     {
-        if ( ( service->get_last_updates()->front() + (service->get_update_interval()*60) ) >= current_time )  // UpdateInterval timeout of service isn't expired.
+        if ( ( service->get_last_updates().front() + (service->get_update_interval()*60) ) >= current_time )  // UpdateInterval timeout of service isn't expired.
             service_container->add_service(service);
     }
 
index 9d055f2..7b76c0a 100644 (file)
@@ -218,8 +218,8 @@ void Updater::update_services()
             int current_time = time(NULL);
 
             int lastupdated = 0;
-            if ( service->get_last_updates()->size() > 0 )
-                lastupdated = service->get_last_updates()->front();
+            if ( service->get_last_updates().size() > 0 )
+                lastupdated = service->get_last_updates().front();
 
             // If the dns cache ttl is expired or the service is updated for the first time, then get the actual ip of the dns record (this should be the IP in the last update)
             if ( ((lastupdated != 0) && ((lastupdated + service->get_dns_cache_ttl()) < current_time)) || (lastupdated == 0) )