From: Bjoern Sikora Date: Tue, 4 Aug 2009 09:51:09 +0000 (+0200) Subject: Removed using of namespaces from header files. X-Git-Tag: v1.1~257 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=8bca3c5d6b2dab83eebda6e90eedda7995143d29;p=bpdyndnsd Removed using of namespaces from header files. 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. --- diff --git a/src/config.cpp b/src/config.cpp index 1144eff..a028643 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -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; +} diff --git a/src/config.h b/src/config.h index dbda160..439fb84 100644 --- a/src/config.h +++ b/src/config.h @@ -26,11 +26,6 @@ #include "dhs.h" #include "ods.h" -namespace po = boost::program_options; -namespace fs = boost::filesystem; - -using namespace std; - typedef boost::shared_ptr ServicePtr; typedef boost::shared_ptr 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 Services; + std::list 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 get_services(); + std::list 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 diff --git a/src/dhs.cpp b/src/dhs.cpp index b530739..f28284d 100644 --- a/src/dhs.cpp +++ b/src/dhs.cpp @@ -9,6 +9,7 @@ #include "dhs.h" +using namespace std; /** * Constructor. diff --git a/src/dhs.h b/src/dhs.h index 157316a..a3d3392 100644 --- 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 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&); }; diff --git a/src/logger.cpp b/src/logger.cpp index 1686e11..c2425d6 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -10,11 +10,16 @@ #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; +} diff --git a/src/logger.h b/src/logger.h index 5ed40ae..870a09a 100644 --- a/src/logger.h +++ b/src/logger.h @@ -10,12 +10,14 @@ #ifndef LOGGER_H #define LOGGER_H -namespace po = boost::program_options; +#include -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 diff --git a/src/main.cpp b/src/main.cpp index 5beb295..a2b358b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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 ); diff --git a/src/ods.cpp b/src/ods.cpp index 24a7484..aad99d9 100644 --- a/src/ods.cpp +++ b/src/ods.cpp @@ -9,6 +9,7 @@ #include "ods.h" +using namespace std; /** * Constructor. diff --git a/src/ods.h b/src/ods.h index c9f0129..869098c 100644 --- 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 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&); }; diff --git a/src/service.h b/src/service.h index 044887b..591252f 100644 --- a/src/service.h +++ b/src/service.h @@ -12,8 +12,6 @@ #include -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; }; diff --git a/src/updater.cpp b/src/updater.cpp index 48a90c7..7bec25a 100644 --- a/src/updater.cpp +++ b/src/updater.cpp @@ -11,6 +11,7 @@ #include +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(); } diff --git a/src/updater.h b/src/updater.h index 89a8c63..b148e8a 100644 --- a/src/updater.h +++ b/src/updater.h @@ -38,6 +38,8 @@ public: LoggerPtr get_logger(); void reload_config(); + + void init_log_facility(); }; #endif