*/
Config::Config(LoggerPtr _log)
: Daemon_mode(false)
- , Logfile("/var/log/bpdyndns.log")
, Loglevel(0)
, Syslog(false)
, Config_path("/etc/bpdyndnsd")
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.")
;
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)
{
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();
*/
Logger::Logger()
: Loglevel(0)
- , Syslog(1)
+ , Syslog(0)
{
+ set_log_facility(Loglevel,Syslog);
print_constructor_call("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.
*/
*/
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());
+ }
}
*/
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());
+ }
}
*/
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());
+ }
}
*/
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());
+ }
}
*/
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());
+ }
}
*/
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());
+ }
}
*/
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());
+ }
}
*/
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());
+ }
}
*/
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());
+ }
}
*/
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());
+ }
}
*/
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());
+ }
}
*/
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());
+ }
}
*/
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());
+ }
}
*/
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());
+ }
}
*/
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());
+ }
}
*/
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());
+ }
}
*/
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());
+ }
}
*/
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());
+ }
}
*/
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());
+ }
}
*/
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());
+ }
}
*/
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());
+ }
}
*/
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());
+ }
}
*/
void Logger::print_caught_sigterm()
{
- cout << "Caught SIGTERM. Exiting..." << endl;
+ if ( 0 <= Loglevel )
+ {
+ ostringstream msg;
+ msg << "Caught SIGTERM. Exiting..." << endl;
+ log_notice(msg.str());
+ }
}
*/
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());
+ }
}
*/
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());
+ }
}
*/
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());
+ }
}
*/
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());
+ }
}
/**
*/
void Logger::print_offline_mode()
{
- cout << "Offline mode..." << endl;
+ if ( 0 <= Loglevel )
+ {
+ ostringstream msg;
+ msg << "Offline mode..." << endl;
+ log_notice(msg.str());
+ }
}