Logging through syslog or std(out/err) is now possible.
authorBjoern Sikora <bjoern.sikora@intra2net.com>
Tue, 4 Aug 2009 14:18:22 +0000 (16:18 +0200)
committerBjoern Sikora <bjoern.sikora@intra2net.com>
Tue, 4 Aug 2009 14:18:22 +0000 (16:18 +0200)
src/config.cpp
src/dhs.cpp
src/logger.cpp
src/logger.h
src/main.cpp
src/ods.cpp
src/updater.cpp

index a028643..ca5bf3e 100644 (file)
@@ -19,7 +19,6 @@ using namespace std;
  */
 Config::Config(LoggerPtr _log)
     : Daemon_mode(false)
-    , Logfile("/var/log/bpdyndns.log")
     , Loglevel(0)
     , Syslog(false)
     , Config_path("/etc/bpdyndnsd")
@@ -48,7 +47,6 @@ Config::Config(LoggerPtr _log)
     po::options_description opt_desc_generic("Generic config options");
     opt_desc_generic.add_options()
         ("daemon_mode",po::value<bool>()->default_value(false),"Run as system daemon.")
-        ("logfile",po::value<string>()->default_value("/var/log/bpdyndns.log"),"Where to log.")
         ("loglevel",po::value<int>()->default_value(0),"Loglevel.")
         ("syslog",po::value<bool>()->default_value(false),"Use syslog facility.")
     ;
@@ -143,6 +141,13 @@ int Config::parse_cmd_line(int argc, char *argv[])
                 return 1;
             }
         }
+
+        if ( Variables_map.count("loglevel") )
+            Loglevel = Variables_map["loglevel"].as<int>();
+
+        if ( Variables_map.count("syslog") )
+            Syslog = Variables_map["syslog"].as<bool>();
+
     }
     catch(po::unknown_option e)
     {
@@ -257,15 +262,14 @@ int Config::load_main_config_file(const string& full_filename)
             po::store(parsed_main_options,Variables_map);
             po::notify(Variables_map);
 
-            if(Variables_map.count("daemon_mode") && Variables_map.count("logfile") && Variables_map.count("loglevel") && Variables_map.count("syslog"))
+            if(Variables_map.count("daemon_mode") && Variables_map.count("loglevel") && Variables_map.count("syslog"))
             {
                 Daemon_mode = Variables_map["daemon_mode"].as<bool>();
-                Logfile = Variables_map["logfile"].as<string>();
                 Loglevel = Variables_map["loglevel"].as<int>();
                 Syslog = Variables_map["syslog"].as<bool>();
             }
         }
-        catch ( po::unknown_option e )
+        catch ( po::unknown_option e )      // at the moment 04-08-2009 this exception is never thrown :-(
         {
             // unknown option in main config file detected
             main_config_file.close();
index f28284d..b4e966f 100644 (file)
@@ -45,8 +45,4 @@ DHS::~DHS()
 void DHS::update(const string& ip)
 {
     Log->print_update_service("DHS");
-
-    cout << "Hostname: " << Hostname << endl;
-    cout << "Login: " << Login << endl;
-    cout << "Password: " << Password << endl;
 }
index c2425d6..5bfa424 100644 (file)
@@ -19,8 +19,9 @@ using namespace std;
  */
 Logger::Logger()
     : Loglevel(0)
-    , Syslog(1)
+    , Syslog(0)
 {
+    set_log_facility(Loglevel,Syslog);
     print_constructor_call("Logger");
 }
 
@@ -35,6 +36,45 @@ Logger::~Logger()
 
 
 /**
+ * Decides if Logging through syslog if enabled or through std.
+ * @param msg The message to log.
+ */
+void Logger::log_notice(const string& msg)
+{
+    if ( Syslog )
+        syslog(LOG_NOTICE,msg.c_str());
+    else
+        cout << msg;
+}
+
+
+/**
+ * Decides if Logging through syslog if enabled or through std.
+ * @param msg The message to log.
+ */
+void Logger::log_warning(const string& msg)
+{
+    if ( Syslog )
+        syslog(LOG_WARNING,msg.c_str());
+    else
+        cout << msg;
+}
+
+
+/**
+ * Decides if Logging through syslog if enabled or through std.
+ * @param msg The message to log.
+ */
+void Logger::log_error(const string& msg)
+{
+    if ( Syslog )
+        syslog(LOG_ERR,msg.c_str());
+    else
+        cerr << msg;
+}
+
+
+/**
  * Setter for member Loglevel.
  * @param _loglevel Value to set Loglevel to.
  */
@@ -92,8 +132,13 @@ void Logger::set_log_facility(const int _loglevel, const bool _syslog)
  */
 void Logger::print_usage(const po::options_description* opt_desc)
 {
-    cout << "Usage: bpdyndnsd [Command line options]" << "\n" << endl;
-    cout << *opt_desc << endl;
+    if ( 0 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Usage: bpdyndnsd [Command line options]" << "\n" << endl;
+        msg << *opt_desc << endl;
+        log_notice(msg.str());
+    }
 }
 
 
@@ -103,9 +148,12 @@ void Logger::print_usage(const po::options_description* opt_desc)
  */
 void Logger::print_version()
 {
-    ostringstream version_string;
-    version_string << VERSION << "." << REVISION << "." << RELEASE;
-    cout << "Bullet proof dynamic dns daemon.\nbpdyndnsd " << version_string.str() << endl;
+    if ( 0 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Bullet proof dynamic dns daemon.\nbpdyndnsd " << VERSION << "." << REVISION << "." << RELEASE << endl;
+        log_notice(msg.str());
+    }
 }
 
 
@@ -114,7 +162,12 @@ void Logger::print_version()
  */
 void Logger::print_cmd_parsed()
 {
-    cout << "Command line options successfully parsed." << endl;
+    if ( 1 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Command line options successfully parsed." << endl;
+        log_notice(msg.str());
+    }
 }
 
 
@@ -124,7 +177,12 @@ void Logger::print_cmd_parsed()
  */
 void Logger::print_conf_loaded(const string& config_path)
 {
-    cout << "Config files successfully loaded in: " << config_path << endl;
+    if ( 1 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Config files successfully loaded in: " << config_path << endl;
+        log_notice(msg.str());
+    }
 }
 
 
@@ -134,7 +192,12 @@ void Logger::print_conf_loaded(const string& config_path)
  */
 void Logger::print_conf_not_loaded(const string& config_path)
 {
-    cerr << "Config files couldn't be loaded in: " << config_path << endl;
+    if ( 0 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Config files couldn't be loaded in: " << config_path << endl;
+        log_error(msg.str());
+    }
 }
 
 
@@ -144,7 +207,12 @@ void Logger::print_conf_not_loaded(const string& config_path)
  */
 void Logger::print_error_opening(const string& filename)
 {
-    cerr << "Error opening file for reading: " << filename << endl;
+    if ( 0 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Error opening file for reading: " << filename << endl;
+        log_error(msg.str());
+    }
 }
 
 
@@ -154,7 +222,12 @@ void Logger::print_error_opening(const string& filename)
  */
 void Logger::print_destructor_call(const string& _class)
 {
-    cout << "Destructor call: " << _class << endl;
+    if ( 1 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Destructor call: " << _class << endl;
+        log_notice(msg.str());
+    }
 }
 
 
@@ -164,7 +237,12 @@ void Logger::print_destructor_call(const string& _class)
  */
 void Logger::print_constructor_call(const string& _class)
 {
-    cout << "Constructor call: " << _class << endl;
+    if ( 1 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Constructor call: " << _class << endl;
+        log_notice(msg.str());
+    }
 }
 
 
@@ -174,7 +252,12 @@ void Logger::print_constructor_call(const string& _class)
  */
 void Logger::print_update_service(const string& service)
 {
-    cout << "Running update for service: " << service << endl;
+    if ( 0 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Running update for service: " << service << endl;
+        log_notice(msg.str());
+    }
 }
 
 
@@ -184,7 +267,12 @@ void Logger::print_update_service(const string& service)
  */
 void Logger::print_unknown_cmd_option(const string& unknown_option)
 {
-    cerr << "Unknown option on command line detected: " << unknown_option << endl;
+    if ( 0 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Unknown option on command line detected: " << unknown_option << endl;
+        log_error(msg.str());
+    }
 }
 
 
@@ -194,7 +282,12 @@ void Logger::print_unknown_cmd_option(const string& unknown_option)
  */
 void Logger::print_unknown_protocol(const string& protocol)
 {
-    cerr << "Unknown protocol defined: " << protocol << endl;
+    if ( 0 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Unknown protocol defined: " << protocol << endl;
+        log_error(msg.str());
+    }
 }
 
 
@@ -204,7 +297,12 @@ void Logger::print_unknown_protocol(const string& protocol)
  */
 void Logger::print_load_service_conf(const string& filename)
 {
-    cout << "Loading service config file: " << filename << endl;
+    if ( 1 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Loading service config file: " << filename << endl;
+        log_notice(msg.str());
+    }
 }
 
 
@@ -214,7 +312,12 @@ void Logger::print_load_service_conf(const string& filename)
  */
 void Logger::print_load_main_conf(const string& filename)
 {
-    cout << "Loading main config file: " << filename << endl;
+    if ( 1 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Loading main config file: " << filename << endl;
+        log_notice(msg.str());
+    }
 }
 
 
@@ -224,7 +327,12 @@ void Logger::print_load_main_conf(const string& filename)
  */
 void Logger::print_unknown_service_conf_option(const string& unknown_option)
 {
-    cerr << "Unknown option in service config file detected: " << unknown_option << endl;
+    if ( 0 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Unknown option in service config file detected: " << unknown_option << endl;
+        log_error(msg.str());
+    }
 }
 
 
@@ -234,7 +342,12 @@ void Logger::print_unknown_service_conf_option(const string& unknown_option)
  */
 void Logger::print_unknown_main_conf_option(const string& unknown_option)
 {
-    cerr << "Unknown option in main config file detected: " << unknown_option << endl;
+    if ( 0 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Unknown option in main config file detected: " << unknown_option << endl;
+        log_error(msg.str());
+    }
 }
 
 
@@ -244,7 +357,12 @@ void Logger::print_unknown_main_conf_option(const string& unknown_option)
  */
 void Logger::print_error_config_path(const string& config_path)
 {
-    cerr << "Config path doesn't exists or is not a diretory: " << config_path << endl;
+    if ( 0 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Config path doesn't exists or is not a diretory: " << config_path << endl;
+        log_error(msg.str());
+    }
 }
 
 
@@ -253,7 +371,12 @@ void Logger::print_error_config_path(const string& config_path)
  */
 void Logger::print_missing_cmd_service_option()
 {
-    cerr << "Missing option to initialize service. Protocol, host, login and password must be specified." << endl;
+    if ( 0 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Missing option to initialize service. Protocol, host, login and password must be specified." << endl;
+        log_error(msg.str());
+    }
 }
 
 
@@ -263,7 +386,12 @@ void Logger::print_missing_cmd_service_option()
  */
 void Logger::print_runnig_as_daemon(const int pid)
 {
-    cout << "Runnig as daemon: " << pid << endl;
+    if ( 1 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Runnig as daemon: " << pid << endl;
+        log_notice(msg.str());
+    }
 }
 
 
@@ -273,10 +401,15 @@ void Logger::print_runnig_as_daemon(const int pid)
  */
 void Logger::print_daemon_mode(const bool daemon_mode)
 {
-    string mode = "disabled";
-    if (daemon_mode == true)
-        mode = "enabled";
-    cout << "Daemon mode is " << mode << "." << endl;
+    if ( 1 <= Loglevel )
+    {
+        string mode = "disabled";
+        if (daemon_mode == true)
+            mode = "enabled";
+        ostringstream msg;
+        msg << "Daemon mode is " << mode << "." << endl;
+        log_notice(msg.str());
+    }
 }
 
 
@@ -285,7 +418,12 @@ void Logger::print_daemon_mode(const bool daemon_mode)
  */
 void Logger::print_error_fork()
 {
-    cerr << "Error while trying to fork." << endl;
+    if ( 0 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Error while trying to fork." << endl;
+        log_notice(msg.str());
+    }
 }
 
 
@@ -295,7 +433,12 @@ void Logger::print_error_fork()
  */
 void Logger::print_pid_found(const int pid)
 {
-    cout << "Pidfile found: " << pid << ". Checking if process is still runnig..." << endl;
+    if ( 1 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Pidfile found: " << pid << ". Checking if process is still runnig..." << endl;
+        log_notice(msg.str());
+    }
 }
 
 
@@ -305,7 +448,12 @@ void Logger::print_pid_found(const int pid)
  */
 void Logger::print_process_already_running(const int pid)
 {
-    cerr << "Another bpdyndnsd process with PID " << pid << " is already running!" << endl;
+    if ( 0 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Another bpdyndnsd process with PID " << pid << " is already running!" << endl;
+        log_error(msg.str());
+    }
 }
 
 
@@ -314,7 +462,12 @@ void Logger::print_process_already_running(const int pid)
  */
 void Logger::print_caught_sigterm()
 {
-    cout << "Caught SIGTERM. Exiting..." << endl;
+    if ( 0 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Caught SIGTERM. Exiting..." << endl;
+        log_notice(msg.str());
+    }
 }
 
 
@@ -323,7 +476,12 @@ void Logger::print_caught_sigterm()
  */
 void Logger::print_caught_siguser1()
 {
-    cout << "Caught SIGUSR1. Switching to offline mode..." << endl;
+    if ( 0 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Caught SIGUSR1. Switching to offline mode..." << endl;
+        log_notice(msg.str());
+    }
 }
 
 
@@ -332,7 +490,12 @@ void Logger::print_caught_siguser1()
  */
 void Logger::print_caught_sighup()
 {
-    cout << "Caught SIGHUP. Reloading config and switching to online mode..." << endl;
+    if ( 0 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Caught SIGHUP. Reloading config and switching to online mode..." << endl;
+        log_notice(msg.str());
+    }
 }
 
 
@@ -341,7 +504,12 @@ void Logger::print_caught_sighup()
  */
 void Logger::print_error_setting_signal()
 {
-    cerr << "Error while setting signal handler." << endl;
+    if ( 0 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Error while setting signal handler." << endl;
+        log_error(msg.str());
+    }
 }
 
 
@@ -350,7 +518,12 @@ void Logger::print_error_setting_signal()
  */
 void Logger::print_init_log_facility()
 {
-    cout << "Initialized logging facility." << " Loglevel: " << Loglevel << " Syslog: " << Syslog << endl;
+    if ( 1 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Initialized logging facility." << " Loglevel: " << Loglevel << " Syslog: " << Syslog << endl;
+        log_notice(msg.str());
+    }
 }
 
 /**
@@ -358,5 +531,10 @@ void Logger::print_init_log_facility()
  */
 void Logger::print_offline_mode()
 {
-    cout << "Offline mode..." << endl;
+    if ( 0 <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Offline mode..." << endl;
+        log_notice(msg.str());
+    }
 }
index 870a09a..78638da 100644 (file)
@@ -11,7 +11,7 @@
 #define LOGGER_H
 
 #include <syslog.h>
-
+#include <sstream>
 
 class Logger
 {
@@ -25,6 +25,12 @@ public:
 
     ~Logger();
 
+    void log_notice(const std::string&);
+
+    void log_warning(const std::string&);
+
+    void log_error(const std::string&);
+
     void set_loglevel(const int);
 
     int get_loglevel();
index a2b358b..185385f 100644 (file)
@@ -162,9 +162,6 @@ 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;
@@ -177,11 +174,8 @@ int main(int argc, char *argv[])
     }
 
     // initialize daemon mode if configured
-    bool daemon_mode = updater->get_config()->get_daemon_mode();
-    updater->get_logger()->print_daemon_mode(daemon_mode);
-
-    // fork if daemon mode is enabled
-    if ( daemon_mode == 1 )
+    updater->get_logger()->print_daemon_mode(updater->get_config()->get_daemon_mode());
+    if ( updater->get_config()->get_daemon_mode() == 1 )
     {
         int pid = fork();
         if ( pid < 0 )
@@ -200,6 +194,7 @@ int main(int argc, char *argv[])
         // child starts here
     }
 
+
     // service processing starts here
     do
     {
@@ -212,9 +207,11 @@ int main(int argc, char *argv[])
         {
             updater->get_logger()->print_offline_mode();
         }
-        sleep(4);
 
-    }while ( daemon_mode == 1 );
+        if ( updater->get_config()->get_daemon_mode() == 1 )
+            sleep(10);  // TODO: in a final release, correct the sleep value to something suitable
+
+    }while ( updater->get_config()->get_daemon_mode() == 1 );
 
     return 0;
 }
index aad99d9..81d3c96 100644 (file)
@@ -45,8 +45,4 @@ ODS::~ODS()
 void ODS::update(const string& ip)
 {
     Log->print_update_service("ODS");
-
-    cout << "Hostname: " << Hostname << endl;
-    cout << "Login: " << Login << endl;
-    cout << "Password: " << Password << endl;
 }
index 7bec25a..8b38d29 100644 (file)
@@ -51,6 +51,9 @@ int Updater::init_config_from_cmd(int argc, char *argv[])
     if( Conf->parse_cmd_line( argc, argv ) != 0)
         return 1;
 
+    // If we have loaded the cmd options we need to init the log facility immediately in case debugging is enabled from cmd.
+    init_log_facility();
+
     // successful parsed
     Log->print_cmd_parsed();
     return 0;
@@ -67,6 +70,10 @@ int Updater::init_config_from_files()
     if( Conf->load_config_from_files() != 0)
         return 1;
 
+    // Re-init log facility, perhaps new config file options for logger are set.
+    // These config file options will only overwrite the cmd options if the SIGHUP (reload config) is caught.
+    init_log_facility();
+
     // successful loaded
     return 0;
 }
@@ -105,9 +112,6 @@ void Updater::reload_config()
 
     // load only config files
     init_config_from_files();
-
-    // re_init log facility, perhaps new config file options for logger are set.
-    init_log_facility();
 }