Removed using of namespaces from header files.
authorBjoern Sikora <bjoern.sikora@intra2net.com>
Tue, 4 Aug 2009 09:51:09 +0000 (11:51 +0200)
committerBjoern Sikora <bjoern.sikora@intra2net.com>
Tue, 4 Aug 2009 11:05:49 +0000 (13:05 +0200)
Introduced logging facility.
Improved option handling when reloading through SIGHUP.
Improved signal handling.
Sorry for big check in ;-). Next time there will be smaller ones.

12 files changed:
src/config.cpp
src/config.h
src/dhs.cpp
src/dhs.h
src/logger.cpp
src/logger.h
src/main.cpp
src/ods.cpp
src/ods.h
src/service.h
src/updater.cpp
src/updater.h

index 1144eff..a028643 100644 (file)
@@ -7,17 +7,22 @@
  * @license GPLv2
 */
 
-
 #include "config.h"
 
+namespace po = boost::program_options;
+namespace fs = boost::filesystem;
+
+using namespace std;
 
 /**
  * Default Constructor. Available command line and config file options with their default values are defined here.
  */
 Config::Config(LoggerPtr _log)
     : Daemon_mode(false)
+    , Logfile("/var/log/bpdyndns.log")
     , Loglevel(0)
     , Syslog(false)
+    , Config_path("/etc/bpdyndnsd")
 {
     // initialize Logger
     Log = _log;
@@ -389,3 +394,26 @@ void Config::delete_services()
     }
     Services.clear();
 }
+
+
+/**
+ * Deletes the map with the previously parsed options.
+ * This is needed in case we reload the config and don't want the old cmd options to overwrite new config file options.
+ */
+void Config::delete_variables_map()
+{
+    Variables_map.clear();
+
+    po::variables_map _variables_map;
+    Variables_map = _variables_map;
+}
+
+
+/**
+ * Getter for member Syslog.
+ * @return True if logging through syslog is enabled, false otherwise.
+ */
+bool Config::get_syslog()
+{
+    return Syslog;
+}
index dbda160..439fb84 100644 (file)
 #include "dhs.h"
 #include "ods.h"
 
-namespace po = boost::program_options;
-namespace fs = boost::filesystem;
-
-using namespace std;
-
 typedef boost::shared_ptr<Service> ServicePtr;
 typedef boost::shared_ptr<Logger> LoggerPtr;
 
@@ -39,23 +34,23 @@ class Config
 
 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;
+    boost::program_options::options_description *Opt_desc_cmd;
+    boost::program_options::options_description *Opt_desc_conf_main;
+    boost::program_options::options_description *Opt_desc_conf_service;
+    boost::program_options::variables_map Variables_map;
 
-    list<ServicePtr> Services;
+    std::list<ServicePtr> Services;
     LoggerPtr Log;
 
     bool Daemon_mode;
-    string Logfile;
+    std::string Logfile;
     int Loglevel;
     bool Syslog;
-    string Config_path;
+    std::string Config_path;
 
-    ServicePtr create_service(const string&,const string&,const string&,const string&);
-    int load_main_config_file(const string&);
-    int load_service_config_file(const string&);
+    ServicePtr create_service(const std::string&,const std::string&,const std::string&,const std::string&);
+    int load_main_config_file(const std::string&);
+    int load_service_config_file(const std::string&);
 
 public:
 
@@ -67,20 +62,24 @@ public:
 
     int load_config_from_files();
 
-    list<ServicePtr> get_services();
+    std::list<ServicePtr> get_services();
 
-    po::options_description* get_opt_desc_cmd();
+    boost::program_options::options_description* get_opt_desc_cmd();
 
-    po::options_description* get_opt_desc_conf_main();
+    boost::program_options::options_description* get_opt_desc_conf_main();
 
-    po::options_description* get_opt_desc_conf_service();
-
-    int get_loglevel();
+    boost::program_options::options_description* get_opt_desc_conf_service();
 
     bool get_daemon_mode();
 
     void delete_services();
 
+    int get_loglevel();
+
+    bool get_syslog();
+
+    void delete_variables_map();
+
 };
 
 #endif
index b530739..f28284d 100644 (file)
@@ -9,6 +9,7 @@
 
 #include "dhs.h"
 
+using namespace std;
 
 /**
  * Constructor.
index 157316a..a3d3392 100644 (file)
--- a/src/dhs.h
+++ b/src/dhs.h
@@ -14,8 +14,6 @@
 #include "service.h"
 #include "logger.h"
 
-using namespace std;
-
 typedef boost::shared_ptr<Logger> LoggerPtr;
 
 class DHS : public Service
@@ -23,19 +21,19 @@ class DHS : public Service
 
 private:
 
-    string Hostname;
-    string Login;
-    string Password;
+    std::string Hostname;
+    std::string Login;
+    std::string Password;
 
     LoggerPtr Log;
 
 public:
 
-    DHS(const LoggerPtr&, const string&, const string&, const string&);
+    DHS(const LoggerPtr&, const std::string&, const std::string&, const std::string&);
 
     ~DHS();
 
-    void update(const string&);
+    void update(const std::string&);
 
 };
 
index 1686e11..c2425d6 100644 (file)
 
 #include "logger.h"
 
+namespace po = boost::program_options;
+
+using namespace std;
 
 /**
  * Default Constructor
  */
 Logger::Logger()
+    : Loglevel(0)
+    , Syslog(1)
 {
     print_constructor_call("Logger");
 }
@@ -30,6 +35,59 @@ Logger::~Logger()
 
 
 /**
+ * Setter for member Loglevel.
+ * @param _loglevel Value to set Loglevel to.
+ */
+void Logger::set_loglevel(const int _loglevel)
+{
+    Loglevel = _loglevel;
+}
+
+
+/**
+ * Getter for member Loglevel.
+ * @return Loglevel.
+ */
+int Logger::get_loglevel()
+{
+    return Loglevel;
+}
+
+
+/**
+ * Setter for member Syslog.
+ * @param _syslog Wether to log through syslog or not.
+ */
+void Logger::set_syslog(const bool _syslog)
+{
+    Syslog = _syslog;
+}
+
+
+/**
+ * Getter for member Syslog.
+ * @return True if logging through syslog is enabled, false otherwise.
+ */
+bool Logger::get_syslog()
+{
+    return Syslog;
+}
+
+
+/**
+ * Initialize the logging facility.
+ */
+void Logger::set_log_facility(const int _loglevel, const bool _syslog)
+{
+    Loglevel = _loglevel;
+    Syslog = _syslog;
+
+    if ( Syslog )
+        openlog("bpdyndnsd",LOG_PID,LOG_DAEMON);
+}
+
+
+/**
  * Prints out the usage to the command line.
  */
 void Logger::print_usage(const po::options_description* opt_desc)
@@ -276,3 +334,29 @@ void Logger::print_caught_sighup()
 {
     cout << "Caught SIGHUP. Reloading config and switching to online mode..." << endl;
 }
+
+
+/**
+ * Error while setting signal handler.
+ */
+void Logger::print_error_setting_signal()
+{
+    cerr << "Error while setting signal handler." << endl;
+}
+
+
+/**
+ * Error while setting signal handler.
+ */
+void Logger::print_init_log_facility()
+{
+    cout << "Initialized logging facility." << " Loglevel: " << Loglevel << " Syslog: " << Syslog << endl;
+}
+
+/**
+ * Be verbose. Currently we are in offline mode.
+ */
+void Logger::print_offline_mode()
+{
+    cout << "Offline mode..." << endl;
+}
index 5ed40ae..870a09a 100644 (file)
 #ifndef LOGGER_H
 #define LOGGER_H
 
-namespace po = boost::program_options;
+#include <syslog.h>
 
-using namespace std;
 
 class Logger
 {
+private:
+    int Loglevel;
+    bool Syslog;
 
 public:
 
@@ -23,37 +25,47 @@ public:
 
     ~Logger();
 
-    void print_usage(const po::options_description*);
+    void set_loglevel(const int);
+
+    int get_loglevel();
+
+    void set_syslog(const bool);
+
+    bool get_syslog();
+
+    void set_log_facility(const int, const bool);
+
+    void print_usage(const boost::program_options::options_description*);
 
     void print_version();
 
     void print_cmd_parsed();
 
-    void print_destructor_call(const string&);
+    void print_destructor_call(const std::string&);
 
-    void print_constructor_call(const string&);
+    void print_constructor_call(const std::string&);
 
-    void print_update_service(const string&);
+    void print_update_service(const std::string&);
 
-    void print_unknown_cmd_option(const string&);
+    void print_unknown_cmd_option(const std::string&);
 
-    void print_unknown_protocol(const string&);
+    void print_unknown_protocol(const std::string&);
 
-    void print_load_service_conf(const string&);
+    void print_load_service_conf(const std::string&);
 
-    void print_load_main_conf(const string&);
+    void print_load_main_conf(const std::string&);
 
-    void print_unknown_service_conf_option(const string&);
+    void print_unknown_service_conf_option(const std::string&);
 
-    void print_unknown_main_conf_option(const string&);
+    void print_unknown_main_conf_option(const std::string&);
 
-    void print_error_opening(const string&);
+    void print_error_opening(const std::string&);
 
-    void print_error_config_path(const string&);
+    void print_error_config_path(const std::string&);
 
-    void print_conf_loaded(const string&);
+    void print_conf_loaded(const std::string&);
 
-    void print_conf_not_loaded(const string&);
+    void print_conf_not_loaded(const std::string&);
 
     void print_missing_cmd_service_option();
 
@@ -72,6 +84,12 @@ public:
     void print_caught_siguser1();
 
     void print_caught_sighup();
+
+    void print_error_setting_signal();
+
+    void print_init_log_facility();
+
+    void print_offline_mode();
 };
 
 #endif
index 5beb295..a2b358b 100644 (file)
@@ -106,6 +106,7 @@ void terminate(int param)
  */
 void switch_to_offline(int param)
 {
+    updater->get_logger()->print_caught_siguser1();
     online_mode = 0;
 }
 
@@ -116,6 +117,7 @@ void switch_to_offline(int param)
  */
 void reload_config(int param)
 {
+    updater->get_logger()->print_caught_sighup();
     updater->reload_config();
     online_mode = 1;
 }
@@ -124,11 +126,18 @@ void reload_config(int param)
 /**
  * Initialize the signals we handle.
  */
-void init_signals()
+int init_signals()
 {
-    signal(SIGTERM,terminate);
-    signal(SIGUSR1,switch_to_offline);
-    signal(SIGHUP,reload_config);
+    sighandler_t ret_val;
+    ret_val = signal(SIGTERM,terminate);
+    if ( ret_val == SIG_ERR )
+        return -1;
+    ret_val = signal(SIGUSR1,switch_to_offline);
+    if ( ret_val == SIG_ERR )
+        return -1;
+    ret_val = signal(SIGHUP,reload_config);
+    if ( ret_val == SIG_ERR )
+        return -1;
 }
 
 
@@ -153,14 +162,19 @@ int main(int argc, char *argv[])
     if ( updater->init_config_from_files() != 0 )
         return 0;
 
+    // init the loggin facility, default stdout
+    updater->init_log_facility();
+
     // open pidfile and check for running process
     if ( check_for_running_process() != 0)
         return 0;
 
     // init signal handling
-    init_signals();
-
-    // set the configured loggin facility, default stdout
+    if ( init_signals() != 0)
+    {
+        updater->get_logger()->print_error_setting_signal();
+        return 0;
+    }
 
     // initialize daemon mode if configured
     bool daemon_mode = updater->get_config()->get_daemon_mode();
@@ -194,7 +208,11 @@ int main(int argc, char *argv[])
             // update all configured services
             updater->update_services();
         }
-        sleep(2);
+        else
+        {
+            updater->get_logger()->print_offline_mode();
+        }
+        sleep(4);
 
     }while ( daemon_mode == 1 );
 
index 24a7484..aad99d9 100644 (file)
@@ -9,6 +9,7 @@
 
 #include "ods.h"
 
+using namespace std;
 
 /**
  * Constructor.
index c9f0129..869098c 100644 (file)
--- a/src/ods.h
+++ b/src/ods.h
@@ -14,8 +14,6 @@
 #include "service.h"
 #include "logger.h"
 
-using namespace std;
-
 typedef boost::shared_ptr<Logger> LoggerPtr;
 
 class ODS : public Service
@@ -23,19 +21,19 @@ class ODS : public Service
 
 private:
 
-    string Hostname;
-    string Login;
-    string Password;
+    std::string Hostname;
+    std::string Login;
+    std::string Password;
 
     LoggerPtr Log;
 
 public:
 
-    ODS(const LoggerPtr&, const string&, const string&, const string&);
+    ODS(const LoggerPtr&, const std::string&, const std::string&, const std::string&);
 
     ~ODS();
 
-    void update(const string&);
+    void update(const std::string&);
 
 };
 
index 044887b..591252f 100644 (file)
@@ -12,8 +12,6 @@
 
 #include <string>
 
-using namespace std;
-
 class Service
 {
 public:
@@ -21,7 +19,7 @@ public:
 
     virtual ~Service();
 
-    virtual void update(const string&)=0;
+    virtual void update(const std::string&)=0;
 
 };
 
index 48a90c7..7bec25a 100644 (file)
@@ -11,6 +11,7 @@
 
 #include <boost/foreach.hpp>
 
+using namespace std;
 
 /**
  * Default constructor which initializes the member Conf.
@@ -96,8 +97,24 @@ LoggerPtr Updater::get_logger()
  */
 void Updater::reload_config()
 {
+    // delete all service objects
     Conf->delete_services();
+
+    // delete the actual Variables_map, perhaps with old cmd options which would overwrite new config file options.
+    Conf->delete_variables_map();
+
+    // load only config files
     init_config_from_files();
+
+    // re_init log facility, perhaps new config file options for logger are set.
+    init_log_facility();
+}
+
+
+void Updater::init_log_facility()
+{
+    Log->set_log_facility(Conf->get_loglevel(),Conf->get_syslog());
+    Log->print_init_log_facility();
 }
 
 
index 89a8c63..b148e8a 100644 (file)
@@ -38,6 +38,8 @@ public:
     LoggerPtr get_logger();
 
     void reload_config();
+
+    void init_log_facility();
 };
 
 #endif