2 * @brief Logger class implementation. This class implements the Logging facility.
6 * @copyright Intra2net AG
17 #include <boost/foreach.hpp>
23 namespace po = boost::program_options;
25 typedef boost::shared_ptr<boost::program_options::options_description> Options_descriptionPtr;
35 , ExternalWarningLevel(0)
36 , ExternalLogOnlyOnce(false)
38 set_log_facility(Loglevel,Syslog,ExternalWarningLog,ExternalWarningLevel,ExternalLogOnlyOnce);
51 * Decides if a external log message can be send.
52 * @param msg The message to log.
54 bool Logger::is_allowed_to_send( const string& msg ) const
56 if ( (ExternalLogOnlyOnce) && (ExternalSendMessages.find(msg) != ExternalSendMessages.end()) )
63 * Clears the external send messages set.
65 void Logger::clear_external_send_messages()
67 ExternalSendMessages.clear();
72 * Decides if Logging through syslog if enabled or through std.
73 * @param msg The message to log.
75 void Logger::log_notice(const string& msg) const
78 syslog(LOG_NOTICE,msg.c_str());
85 * Escape shell arguments.
86 * @param input The input to escape.
87 * @return The escaped string ready to use for the shell.
89 string Logger::escape_shellarg(const string &input)
92 string::const_iterator it, it_end = input.end();
93 for (it = input.begin(); it != it_end; it++)
107 * Decides if Logging through syslog if enabled or through std.
108 * @param msg The message to log.
110 void Logger::log_warning(const string& msg, int level)
113 syslog(LOG_WARNING,msg.c_str());
117 if ( (level <= ExternalWarningLevel) && (!ExternalWarningLog.empty()) && (is_allowed_to_send(msg)) )
119 string message = msg;
120 // Remove endline from msg.
121 if (!message.empty() && message[message.length()-1] == '\n')
122 message.erase(message.length()-1); /*lint !e534 */
124 message = escape_shellarg(message);
126 string external = ExternalWarningLog;
127 external.append(" ");
128 external.append("\"");
129 external.append(message);
130 external.append("\"");
131 if ( system(external.c_str()) != 0 )
132 print_error_external_logging(external);
134 ExternalSendMessages.insert(msg); /*lint !e534 */
140 * Decides if Logging through syslog if enabled or through std.
141 * @param msg The message to log.
143 void Logger::log_error(const string& msg) const
146 syslog(LOG_ERR,msg.c_str());
153 * Setter for member Loglevel.
154 * @param _loglevel Value to set Loglevel to.
156 void Logger::set_external_log_only_once( const bool _external_log_only_once )
158 ExternalLogOnlyOnce = _external_log_only_once;
163 * Setter for member Loglevel.
164 * @param _loglevel Value to set Loglevel to.
166 void Logger::set_loglevel(const int _loglevel)
168 Loglevel = _loglevel;
173 * Getter for member Loglevel.
176 int Logger::get_loglevel() const
183 * Setter for member Syslog.
184 * @param _syslog Wether to log through syslog or not.
186 void Logger::set_syslog(const bool _syslog)
193 * Getter for member Syslog.
194 * @return True if logging through syslog is enabled, false otherwise.
196 bool Logger::get_syslog() const
203 * Initialize the logging facility.
205 void Logger::set_log_facility(const int _loglevel, const bool _syslog, const string& _external_error_log, const int _external_error_level, const bool _external_log_only_once )
207 Loglevel = _loglevel;
209 ExternalWarningLog = _external_error_log;
210 ExternalWarningLevel = _external_error_level;
211 ExternalLogOnlyOnce = _external_log_only_once;
214 openlog("bpdyndnsd",LOG_PID,LOG_DAEMON);
219 * Prints out the usage to the command line.
221 void Logger::print_usage(const Options_descriptionPtr opt_desc) const
224 if ( level <= Loglevel )
227 msg << "Usage: bpdyndnsd [Command line options]" << "\n" << endl;
228 msg << *opt_desc << endl;
229 log_notice(msg.str());
235 * Prints out the programm name and the given version string on stdout.
236 * @param version Version string.
238 void Logger::print_version() const
241 if ( level <= Loglevel )
244 msg << "Bullet proof dynamic dns daemon.\nbpdyndnsd " << VERSION << "." << REVISION << "." << RELEASE << endl;
245 log_notice(msg.str());
251 * Prints out the successful parsing of the command line options.
253 void Logger::print_cmd_parsed() const
256 if ( level <= Loglevel )
258 log_notice("Command line options successfully parsed.");
263 void Logger::print_conf_files_parsed() const
266 if ( level <= Loglevel )
268 log_notice("Config files successfully parsed.");
274 * Prints out the successful parsing of the config files.
275 * @param config_path The specified config path.
277 void Logger::print_conf_loaded(const string& config_path) const
280 if ( level <= Loglevel )
283 msg << "Config files successfully loaded in: " << config_path << endl;
284 log_notice(msg.str());
290 * Prints out the unsuccessful parsing of the config files.
291 * @param config_path The specified config path.
293 void Logger::print_conf_not_loaded(const string& config_path) const
296 if ( level <= Loglevel )
299 msg << "Config files couldn't be loaded in: " << config_path << endl;
300 log_error(msg.str());
306 * A file could not be opened for reading.
307 * @param filename The filename.
309 void Logger::print_error_opening_r(const string& filename) const
312 if ( level <= Loglevel )
315 msg << "Error opening file for reading: " << filename << endl;
316 log_error(msg.str());
322 * A file could not be opened for writing.
323 * @param filename The filename.
325 void Logger::print_error_opening_rw(const string& filename) const
328 if ( level <= Loglevel )
331 msg << "Error opening file for writing: " << filename << endl;
332 log_error(msg.str());
338 * Desctructor of specified class was called.
339 * @param _class Name of the class.
341 void Logger::print_destructor_call(const string& _class) const
344 if ( level <= Loglevel )
347 msg << "Destructor call: " << _class << endl;
348 log_notice(msg.str());
354 * Constructor of specified class was called.
355 * @param _class Name of the class.
357 void Logger::print_constructor_call(const string& _class) const
360 if ( level <= Loglevel )
363 msg << "Constructor call: " << _class << endl;
364 log_notice(msg.str());
370 * Update method for specified service was called.
371 * @param service The service for which is the update running.
373 void Logger::print_update_service(const string& service) const
376 if ( level <= Loglevel )
379 msg << "Running update for service: " << service << endl;
380 log_notice(msg.str());
386 * An unknown option on the command line was detected.
387 * @param unknown_option The unknown option.
389 void Logger::print_unknown_cmd_option(const string& unknown_option) const
392 if ( level <= Loglevel )
395 msg << "Unknown option on command line detected: " << unknown_option << endl;
396 log_error(msg.str());
402 * An unknown protocol was specified.
403 * @param protocol The unknown protocol.
405 void Logger::print_unknown_protocol(const string& protocol) const
408 if ( level <= Loglevel )
411 msg << "Unknown protocol defined: " << protocol << endl;
412 log_error(msg.str());
418 * Loading a service config file.
419 * @param filename The service config file.
421 void Logger::print_load_service_conf(const string& filename) const
424 if ( level <= Loglevel )
427 msg << "Loading service config file: " << filename << endl;
428 log_notice(msg.str());
434 * Loading the main config file.
435 * @param filename The main config file.
437 void Logger::print_load_main_conf(const string& filename) const
440 if ( level <= Loglevel )
443 msg << "Loading main config file: " << filename << endl;
444 log_notice(msg.str());
450 * There is an unknown option in a service config file.
451 * @param unknown_option The unknown option.
453 void Logger::print_unknown_service_conf_option(const string& service_conf_file, const string& unknown_option) const
456 if ( level <= Loglevel )
459 msg << "Unknown option in service config file detected: " << service_conf_file << " Unknown option: " << unknown_option << endl;
460 log_error(msg.str());
466 * There is an unknown option in the main config file.
467 * @param unknown_option The unknown option.
469 void Logger::print_unknown_main_conf_option(const string& unknown_option) const
472 if ( level <= Loglevel )
475 msg << "Unknown option in main config file detected: " << unknown_option << endl;
476 log_error(msg.str());
482 * The defined config path doesn't exist or is not a directory.
483 * @param config_path The defined config path.
485 void Logger::print_error_config_path(const string& config_path) const
488 if ( level <= Loglevel )
491 msg << "Config path doesn't exists or is not a diretory: " << config_path << endl;
492 log_error(msg.str());
498 * There is a missing command line option to initialize a service object.
500 void Logger::print_missing_cmd_service_option() const
503 if ( level <= Loglevel )
505 log_error("Missing option to initialize service. Protocol, host, login and password must be specified.");
511 * Missing option in service config file.
512 * @param service_conf_file Service config file
514 void Logger::print_missing_service_conf_option(const string& service_conf_file) const
517 if ( level <= Loglevel )
520 msg << "Missing option in service config file " << service_conf_file << " to initialize service. Protocol, host, login and password must be specified." << endl;
521 log_error(msg.str());
527 * Process running as daemon.
528 * @param pid The pid of the daemon.
530 void Logger::print_runnig_as_daemon(const int pid) const
533 if ( level <= Loglevel )
536 msg << "Runnig as daemon: " << pid << endl;
537 log_notice(msg.str());
543 * Prints out the daemon mode.
544 * @param daemon_mode The daemon mode.
546 void Logger::print_daemon_mode(const bool daemon_mode) const
549 if ( level <= Loglevel )
551 string mode = "disabled";
552 if (daemon_mode == true)
555 msg << "Daemon mode is " << mode << "." << endl;
556 log_notice(msg.str());
562 * There was an error while trying to fork.
564 void Logger::print_error_fork() const
567 if ( level <= Loglevel )
569 log_error("Error while trying to fork.");
575 * A pid in the pidfile was found.
576 * @param pid The pid found in the pidfile.
578 void Logger::print_pid_found(const int pid) const
581 if ( level <= Loglevel )
584 msg << "Pidfile found: " << pid << ". Checking if process is still runnig..." << endl;
585 log_notice(msg.str());
591 * Another process is already running.
592 * @param pid The pid of the other process.
594 void Logger::print_process_already_running(const int pid) const
597 if ( level <= Loglevel )
600 msg << "Another bpdyndnsd process with PID " << pid << " is already running!" << endl;
601 log_error(msg.str());
609 void Logger::print_caught_sigterm() const
612 if ( level <= Loglevel )
614 log_notice("Caught SIGTERM. Exiting...");
622 void Logger::print_caught_siguser1() const
625 if ( level <= Loglevel )
627 log_notice("Caught SIGUSR1. Switching to offline mode...");
635 void Logger::print_caught_sighup() const
638 if ( level <= Loglevel )
640 log_notice("Caught SIGHUP. Reloading config...");
648 void Logger::print_caught_siguser2() const
651 if ( level <= Loglevel )
653 log_notice("Caught SIGUSR2. Switching to online mode...");
661 void Logger::print_caught_sigrtmin() const
664 if ( level <= Loglevel )
666 log_notice("Caught SIGRTMIN. Switching to online mode with webcheck enabled...");
672 * Error while setting signal handler.
674 void Logger::print_error_setting_signal(const string& signal) const
677 if ( level <= Loglevel )
680 msg << "Error while setting signal handler for: " << signal << endl;
681 log_error(msg.str());
687 * Error while setting signal handler.
689 void Logger::print_init_log_facility() const
692 if ( level <= Loglevel )
695 msg << "Initialized logging facility." << " Loglevel: " << Loglevel << " Syslog: " << Syslog << " ExternalLogOnlyOnce: " << ExternalLogOnlyOnce << endl;
696 log_notice(msg.str());
702 * Be verbose. Currently we are in offline mode.
704 void Logger::print_offline_mode() const
707 if ( level <= Loglevel )
709 log_notice("Offline mode...");
715 * Objects successfully serialized.
717 void Logger::print_serialized_objects_success() const
720 if ( level <= Loglevel )
722 log_notice("Serialized objects successfully.");
728 * Objects successfully de-serialized.
730 void Logger::print_deserialized_objects_success() const
733 if ( level <= Loglevel )
735 log_notice("De-serialized objects successfully.");
741 * Prints out the content of a service object.
742 * @param message Message to be added on output first.
743 * @param protocol Service's protocol.
744 * @param hostname Service's hostname.
745 * @param login Service's login.
746 * @param password Service's password.
747 * @param actual_ip Service's actual_ip.
748 * @param lastupdated Service's lastupdated.
750 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<time_t> lastupdated) const
753 if ( level <= Loglevel )
756 msg << message << endl;
757 msg << "\t" << "Protocol: " << protocol << endl;
758 msg << "\t" << "Hostname: " << hostname << endl;
759 msg << "\t" << "Login: " << login << endl;
760 msg << "\t" << "Password: " << password << endl;
761 msg << "\t" << "Update Interval: " << update_interval << endl;
762 msg << "\t" << "Max Updates: " << max_updates_within_interval << endl;
763 msg << "\t" << "DNS Cache TTL: " << dns_cache_ttl << endl;
764 msg << "\t" << "Actual_IP: " << actual_ip << endl;
765 BOOST_FOREACH( time_t update_time, lastupdated)
767 msg << "\t" << "Lastupdated: " << update_time << endl;
769 log_notice(msg.str());
775 * Caught exception while serialize.
776 * @param exception Exception message.
778 void Logger::print_exception_serialize(const string& errMsg) const
781 if ( level <= Loglevel )
784 msg << "Error while trying to serialize Serviceholder object: " << errMsg << endl;
785 log_error(msg.str());
791 * Caught exception while de-serialize.
792 * @param exception Exception message.
794 void Logger::print_exception_deserialize(const string& errMsg) const
797 if ( level <= Loglevel )
800 msg << "Error while trying to de-serialize Serviceholder object: " << errMsg << endl;
801 log_error(msg.str());
807 * Child couldn't be killed by parent.
808 * @param pid Pid of the child.
810 void Logger::print_error_kill_child(const int pid) const
813 if ( level <= Loglevel )
816 msg << "Could not kill child process with PID: " << pid << endl;
817 log_error(msg.str());
823 * Child was killed by parent because of error.
824 * @param pid The pid (child) killed.
826 void Logger::print_child_killed(const int pid) const
829 if ( level <= Loglevel )
832 msg << "Killed child process with PID: " << pid << endl;
833 log_notice(msg.str());
839 * There is no object file.
840 * @param object_file The object file.
842 void Logger::print_no_object_file(const string& object_file)
845 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
848 msg << "There is no object file: " << object_file << ". Continue without recovering state from old services!" << endl;
849 log_warning(msg.str(),level);
855 * Prints out the given hostname
856 * @param hostname Hostname as string.
858 void Logger::print_hostname(const string& hostname) const
861 if ( level <= Loglevel )
864 msg << "Detected following hostname of localhost: " << hostname << endl;
865 log_notice(msg.str());
871 * Prints out the detected own ipv4 address
872 * @param ip_addr String representation of the detected ip.
874 void Logger::print_own_ipv4(const string& ip_addr_v4, const string& hostname) const
877 if ( level <= Loglevel )
880 msg << "Detected following IPv4-Address of host: " << hostname << " : " << ip_addr_v4 << endl;
881 log_notice(msg.str());
887 * Prints out the detected own ipv5 address
888 * @param ip_addr String representation of the detected ip.
890 void Logger::print_own_ipv6(const string& ip_addr_v6, const string& hostname) const
893 if ( level <= Loglevel )
896 msg << "Detected following IPv6-Address of host: " << hostname << " : " << ip_addr_v6 << endl;
897 log_notice(msg.str());
903 * Exception while trying to resolve hostname to ip.
904 * @param exception The exception caught.
905 * @param hostname The hostname.
907 void Logger::print_error_hostname_to_ip(const string& errMsg, const string& hostname) const
910 if ( level <= Loglevel )
913 msg << "Could not resolve the hostname: " << hostname << " to an IP-Address: " << errMsg << endl;
914 log_error(msg.str());
920 * The update of the given service was successful.
921 * @param service The service.
923 void Logger::print_update_service_successful(const string& service) const
926 if ( level <= Loglevel )
929 msg << "Updated service successful: " << service << endl;
930 log_notice(msg.str());
936 * No ip could be determined through webcheck
938 void Logger::print_webcheck_no_ip()
941 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
943 log_warning("IP-Address of this host could not be determined through any configured webcheck url.",level);
949 * Connection problem while trying to get ip through webcheck url
950 * @param curl_err_buff Curl error message
953 void Logger::print_webcheck_url_connection_problem(const char * curl_err_buff, const string& url)
956 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
959 msg << "There was a problem while trying to connect to following URL: " << url << " CURL error: " << curl_err_buff << endl;
960 log_warning(msg.str(),level);
966 * Prints out curl error.
967 * @param curl_err_buff Curl error message.
970 void Logger::print_webcheck_error(const char * curl_err_buff, const string& url) const
973 if ( level <= Loglevel )
976 msg << "There was an error while trying to connect to following URL: " << url << " CURL error: " << curl_err_buff << endl;
977 log_error(msg.str());
983 * Prints out the received data through curl.
984 * @param curl_data Data string
986 void Logger::print_received_curl_data(const string& curl_data) const
989 if ( level <= Loglevel )
992 msg << "Received CURL data: " << curl_data << endl;
993 log_notice(msg.str());
999 * IP was foudn through regex
1000 * @param ip The IP found.
1002 void Logger::print_regex_found_ip(const string& ip) const
1005 if ( level <= Loglevel )
1008 msg << "Found IP-Address via regex: " << ip << endl;
1009 log_notice(msg.str());
1015 * No IP was found through regex.
1016 * @param data The data string which should contain a valid IP.s
1018 void Logger::print_regex_ip_not_found(const string& data)
1021 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1024 msg << "Could not extract an IP-Address via regex from following data:\n" << data << endl;
1025 log_warning(msg.str(),level);
1031 * Detected multiple occurrences of the same option.
1032 * @param message Error message.
1034 void Logger::print_multiple_cmd_option(const string& message) const
1037 if ( level <= Loglevel )
1040 msg << "The same option is only allowed once: " << message << endl;
1041 log_error(msg.str());
1047 * An update would exceed the update interval. Prints out a warning message.
1048 * @param current_time Current time.
1049 * @param old_time Time of update #MaxUpdatesWithinInterval ago.
1050 * @param MaxUpdatesWithinInterval Number of allowed updates in one update interval.
1051 * @param service The service which exceeds update interval.
1053 void Logger::print_update_not_allowed(const time_t current_time, const time_t old_time, const int MaxUpdatesWithinInterval, const string& service)
1056 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1059 msg << "Update not allowed for service: " << service << ". Too many updates within max update interval. Current time: " << current_time << ". Update time before " << MaxUpdatesWithinInterval << " updates: " << old_time << endl;
1060 log_warning(msg.str(),level);
1066 * Failure while running update for service.
1067 * @param service Services' name.
1069 void Logger::print_update_service_failure(const string& service)
1072 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1075 msg << "Could not update service: " << service << endl;
1076 log_warning(msg.str(),level);
1084 void Logger::print_starting_shutdown() const
1087 if ( level <= Loglevel )
1089 log_notice("Shutting down ...");
1097 void Logger::print_shutdown_succeeded() const
1100 if ( level <= Loglevel )
1102 log_notice("Shutting down complete ...");
1108 * Shutdown parent succeeded
1110 void Logger::print_shutdown_parent_succeeded() const
1113 if ( level <= Loglevel )
1115 log_notice("Shutting down parent process completed ...");
1121 * Starting shutdown parent
1123 void Logger::print_starting_shutdown_parent() const
1126 if ( level <= Loglevel )
1128 log_notice("Shutting down parent process ...");
1134 * DNS cache record timeout
1135 * @param hostname Hostname
1136 * @param lastupdated Lastupdated
1137 * @param dns_cache_ttl DNS cache TTL
1138 * @param current_time Current time
1140 void Logger::print_recheck_dns_entry(const string& hostname, const int lastupdated, const int dns_cache_ttl, const int current_time) const
1143 if ( level <= Loglevel )
1146 msg << "DNS cache record for host <" << hostname << "> expired or host will be updated for the first time: Lastupdated: " << lastupdated << " DNS cache ttl: " << dns_cache_ttl << " Current time: " << current_time << " Checking current DNS cache status." << endl;
1147 log_notice(msg.str());
1153 * Missing proxy option on command line.
1155 void Logger::print_missing_cmd_proxy_option() const
1158 if ( level <= Loglevel )
1160 log_error("Missing option to initialize proxy. http_proxy and http_proxy_port must be specified.");
1166 * Multiple option in service config file.
1167 * @param service_conf_file Service config file
1168 * @param message Multiple option text
1170 void Logger::print_multiple_service_conf_option(const string& service_conf_file, const string& message) const
1173 if ( level <= Loglevel )
1176 msg << "Multiple occurrences of the same option in service config file detected: " << service_conf_file << " " << message << endl;
1177 log_error(msg.str());
1183 * Multiple option in main config file.
1184 * @param service_conf_file Service config file
1185 * @param message Multiple option text
1187 void Logger::print_multiple_main_conf_option(const string& main_conf_file, const string& message) const
1190 if ( level <= Loglevel )
1193 msg << "Multiple occurrences of the same option in main config file detected: " << main_conf_file << " " << message << endl;
1194 log_error(msg.str());
1200 * Missing proxy option in main config file.
1201 * @param main_conf_filename The concerning config file.
1203 void Logger::print_missing_conf_proxy_option(const string& main_conf_filename) const
1206 if ( level <= Loglevel )
1209 msg << "Missing option to initialize proxy in main config file: " << main_conf_filename << " http_proxy and http_proxy_port must be specified." << endl;
1210 log_error(msg.str());
1216 * There is no domain part in the given hostname
1217 * @param hostname The hostname with no domain part in it.
1219 void Logger::print_no_domain_part(const string& hostname) const
1222 if ( level <= Loglevel )
1225 msg << "There is no domain part in the given hostname: " << hostname << endl;
1226 log_notice(msg.str());
1232 * Service is not initialized properly.
1233 * @param service The service.
1235 void Logger::print_httphelper_not_initialized() const
1238 if ( level <= Loglevel )
1240 log_error("HTTP-Helper is not initialized properly.");
1246 * An curl error occured.
1247 * @param msg The error message
1248 * @param curl_err_code The resulting curl error code
1250 void Logger::print_curl_error_init(const std::string& err_msg, const CURLcode curl_err_code) const
1252 string curl_err = "";
1254 if ( curl_err_code == CURLE_FAILED_INIT )
1255 curl_err = "CURLE_FAILED_INIT";
1257 curl_err = "UNKNOWN";
1260 if ( (level <= Loglevel) )
1263 msg << "Curl error: " << err_msg << " Curl error code: " << curl_err_code << " " << curl_err << endl; /*lint !e641 */
1264 log_error(msg.str());
1270 * An curl error occured.
1271 * @param url The url requested by the curl operation
1272 * @param curl_err_code The resulting curl error code
1274 void Logger::print_curl_error(const string& url, const CURLcode curl_err_code) const
1276 string curl_err = "";
1278 if ( curl_err_code == CURLE_URL_MALFORMAT )
1279 curl_err = "CURLE_URL_MALFORMAT";
1280 else if ( curl_err_code == CURLE_COULDNT_RESOLVE_HOST )
1281 curl_err = "CURLE_COULDNT_RESOLVE_HOST";
1282 else if ( curl_err_code == CURLE_COULDNT_CONNECT )
1283 curl_err = "CURLE_COULDNT_CONNECT";
1285 curl_err = "UNKNOWN";
1288 if ( (level <= Loglevel) )
1291 msg << "Curl error while requesting following url: " << url << " Curl error code: " << curl_err_code << " " << curl_err << endl; /*lint !e641 */
1292 log_error(msg.str());
1298 * An curl error occured.
1299 * @param url The url requested by the curl operation
1300 * @param curl_err_code The resulting curl error code
1301 * @param curl_err_buff The curl error buffer
1303 void Logger::print_curl_error(const string& url, const CURLcode curl_err_code, const char * curl_err_buff) const
1305 string curl_err = "";
1307 if ( curl_err_code == CURLE_URL_MALFORMAT )
1308 curl_err = "CURLE_URL_MALFORMAT";
1309 else if ( curl_err_code == CURLE_COULDNT_RESOLVE_HOST )
1310 curl_err = "CURLE_COULDNT_RESOLVE_HOST";
1311 else if ( curl_err_code == CURLE_COULDNT_CONNECT )
1312 curl_err = "CURLE_COULDNT_CONNECT";
1314 curl_err = "UNKNOWN";
1317 if ( (level <= Loglevel) )
1320 msg << "Curl error while requesting following url: " << url << " Curl error code: " << curl_err_code << " " << curl_err << " " << curl_err_buff << endl; /*lint !e641 */
1321 log_error(msg.str());
1327 * Prints out the data received by curl operation
1328 * @param CurlWritedataBuff
1330 void Logger::print_curl_data(const string& curl_writedata_buff) const
1333 if ( level <= Loglevel )
1336 msg << "Data received by curl: " << curl_writedata_buff << endl;
1337 log_notice(msg.str());
1343 * Not authorized to perform requested update operation
1344 * @param service The requested service.
1345 * @param username Username
1346 * @param password Password
1348 void Logger::print_service_not_authorized(const string& service, const string& username, const string& password) const
1351 if ( level <= Loglevel )
1354 msg << "Not authorized to perform update operation on service: " << service << " Please check username and password: " << username << ":" << password << endl;
1355 log_notice(msg.str());
1361 * Prints out the http status code
1363 * @param output HTTP status code
1365 void Logger::print_http_status_code(const string& url, const long http_code) const
1368 if ( level <= Loglevel )
1371 msg << "Requested URL: " << url << " Received HTTP status code: " << http_code << endl;
1372 log_notice(msg.str());
1378 * Generic failure while trying to update service
1379 * @param url The requested URL
1380 * @param curl_data The received curl_data from the server
1382 void Logger::print_update_failure(const string& url, const string& curl_data) const
1385 if ( (level <= Loglevel) )
1388 msg << "Problem while trying to updating service. Requested URL: " << url << " Error Code from Server: " << curl_data << endl;
1389 log_error(msg.str());
1395 * Generic failure while trying to update service
1396 * @param url The requested URL
1397 * @param http_status_code The received http status code
1399 void Logger::print_update_failure(const string& url, const long http_status_code) const
1402 if ( level <= Loglevel )
1405 msg << "Problem while trying to updating service. Requested URL: " << url << " Error Code from Server: " << http_status_code << endl;
1406 log_error(msg.str());
1411 * Hostname is invalid, contains no or only one domain part.
1412 * @param hostname The full qualified host name.
1414 void Logger::print_invalid_hostname(const string& hostname)
1417 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1420 msg << "The configured hostname: " << hostname << " is invalid. Please add the corresponding domain part." << endl;
1421 log_warning(msg.str(),level);
1427 * An IP in a private range was detected
1428 * @param ip The private IP
1430 void Logger::print_ip_is_local(const string& ip)
1433 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1436 msg << "The detected IP is within a private IP range: " << ip << endl;
1437 log_warning(msg.str(),level);
1443 * Regex is matching in string
1444 * @param regex The regex pattern
1445 * @param matching_string The string
1447 void Logger::print_regex_match(const string& regex, const string& matching_string) const
1450 if ( level <= Loglevel )
1453 msg << "Regex: " << regex << " is matching in: " << matching_string << endl;
1454 log_notice(msg.str());
1460 * Regex is not matching
1461 * @param regex Regex
1462 * @param not_matching_string String
1464 void Logger::print_no_regex_match(const string& regex, const string& not_matching_string)
1467 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1470 msg << "Regex: " << regex << " is not matching in: " << not_matching_string << endl;
1471 log_warning(msg.str(),level);
1477 * Could not parse gnudip initial reply.
1478 * @param curl_data The data received from gnudip server which should contain salt, time and sign.
1480 void Logger::print_could_not_parse_received_data(const string& curl_data) const
1483 if ( (level <= Loglevel) )
1486 msg << "Could not parse salt, time and sign from initial gnudip server reply: " << curl_data << endl;
1487 log_error(msg.str());
1493 * Gnudip salt, time and sign could not be got from map
1495 void Logger::print_could_not_get_initial_gnudip_data() const
1498 if ( (level <= Loglevel) )
1500 log_error("Could not get salt, time and sign from map.");
1507 * Gnudip protocol requires explicit declaration of a servername.
1509 void Logger::print_gnudip_requires_servername()
1512 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1514 log_warning("Gnudip requires explicit definition of servername via config!",level);
1520 * An exception occured while computing the md5 sum.
1521 * @param what The exception occured.
1523 void Logger::print_exception_md5_sum(const string& what) const
1526 if ( level <= Loglevel )
1529 msg << "An exception occured while computing a md5 sum: " << what << endl;
1530 log_error(msg.str());
1536 * An network exception occured.
1537 * @param what The exception occured.
1539 void Logger::print_network_error(const string& what) const
1542 if ( level <= Loglevel )
1545 msg << "An netowrk exception occured: " << what << endl;
1546 log_error(msg.str());
1552 * An undefined protocol error occured.
1553 * @param protocol The protocol
1554 * @param error The error
1556 void Logger::print_undefined_protocol_error(const string& protocol, const string& error) const
1559 if ( level <= Loglevel )
1562 msg << "An undefined protocol error occured. Protocol: " << protocol << " Error: " << error << endl;
1563 log_error(msg.str());
1568 * Error while trying to log through external program.
1569 * @param external_prog The external program called.
1571 void Logger::print_error_external_logging(const string& external_prog) const
1574 if ( level <= Loglevel )
1577 msg << "Error while trying to log through external program: " << external_prog << endl;
1578 log_notice(msg.str());
1584 * Error while parsing config file.
1585 * @param error Error occured.
1586 * @param config_file Config file.
1588 void Logger::print_error_parsing_config_file(const string& filename, const string& error) const
1591 if ( level <= Loglevel )
1594 msg << "Error while parsing config file: " << filename << " Error: " << error << endl;
1595 log_notice(msg.str());
1601 * Error while parsing cmd option
1602 * @param error Error
1604 void Logger::print_error_parsing_cmd(const string& error) const
1607 if ( level <= Loglevel )
1610 msg << "Error while parsing cmd options: " << error << endl;
1611 log_error(msg.str());
1617 * The webcheck interval was exceeded, so webcheck not allowed.
1618 * @param last_webcheck Time of last webcheck.
1619 * @param webcheck_interval Webcheck interval time.
1620 * @param current_time Current system time.
1622 void Logger::print_webcheck_exceed_interval( const time_t last_webcheck, const int webcheck_interval, const time_t current_time ) const
1625 if ( level <= Loglevel )
1628 msg << "Exceeding webcheck interval: LastWebcheck " << last_webcheck << " + WebcheckInterval(sec) " << webcheck_interval << " >= CurrentTime " << current_time << endl;
1629 log_notice(msg.str());
1635 * Checking if hosts needs update.
1636 * @param hostname Hostname
1637 * @param current_time Current time
1638 * @param lastupdated Last updated
1640 void Logger::print_check_service_update(const string& hostname, const time_t current_time, const time_t lastupdated) const
1643 if ( level <= Loglevel )
1646 msg << "Checking if host: " << hostname << " needs update. Current time: " << current_time << " Last updated: " << lastupdated << endl;
1647 log_notice(msg.str());
1654 * @param hostname Hostname
1655 * @param ip_dns_recheck DNS recheck IP
1656 * @param ip_last_update IP set in last update
1657 * @param ip_host Hosts' IP
1659 void Logger::print_cached_dns_entry(const string& hostname, const string& ip_dns_recheck, const string& ip_last_update, const string& ip_host) const
1662 if ( level <= Loglevel )
1665 msg << "Cached DNS record for host <" << hostname << "> : " << ip_dns_recheck << " Last updated IP: " << ip_last_update << " Hosts IP: " << ip_host << endl;
1666 log_notice(msg.str());
1672 * Updating service for the first time.
1673 * @param hostname Hostname
1674 * @param ip_dns_recheck Cached DNS entry
1675 * @param ip_host Hosts IP
1677 void Logger::print_update_service_firttime(const string& hostname, const string& ip_dns_recheck, const string& ip_host) const
1680 if ( level <= Loglevel )
1683 msg << "Updating service for the first time. Hostname: " << hostname << " DNS-Record: " << ip_dns_recheck << " Hosts IP: " << ip_host << endl;
1684 log_notice(msg.str());
1691 * @param hostname Hostname
1692 * @param ip_dns_recheck Cached DNS entry
1693 * @param ip_last_update IP set in last update
1694 * @param ip_host Hosts IP
1695 * @param lastupdated Lastupdated
1697 void Logger::print_update_service(const string& hostname, const string& ip_dns_recheck, const string& ip_last_update, const string& ip_host, const time_t lastupdated) const{
1699 if ( level <= Loglevel )
1702 msg << "Updating service. Hostname: " << hostname << " DNS-Record: " << ip_dns_recheck << " IP set in last update: " << ip_last_update << " Lastupdated: " << lastupdated << " Hosts IP: " << ip_host << endl;
1703 log_notice(msg.str());
1710 * @param hostname Hostname
1711 * @param ip_dns_recheck Cached DNS entry
1712 * @param ip_last_update IP set in last update
1713 * @param ip_host Hosts IP
1714 * @param lastupdated Last updated
1715 * @param dns_cache_ttl DNS cache ttl
1716 * @param current_time Current time
1718 void Logger::print_update_service_ttl_expired(const string& hostname, const string& ip_dns_recheck, const string& ip_last_update, const string& ip_host, const time_t lastupdated, const int dns_cache_ttl, const time_t current_time) const
1721 if ( level <= Loglevel )
1724 msg << "TTL for service expired and still pointing to old IP. Hostname: " << hostname << " DNS-Record: " << ip_dns_recheck << " IP set in last update: " << ip_last_update << " Lastupdated: " << lastupdated << "DNS Cache TTL: " << dns_cache_ttl << " Current Time: " << current_time << " Hosts IP: " << ip_host << endl;
1725 log_notice(msg.str());
1732 * @param hostname Hostname
1733 * @param ip_dns_recheck Cached DNS entry
1734 * @param ip_last_update IP set in last update
1735 * @param ip_host Hosts IP
1736 * @param lastupdated Last updated
1737 * @param dns_cache_ttl DNS cache ttl
1738 * @param current_time Current time
1740 void Logger::print_update_service_ttl_not_expired(const string& hostname, const string& ip_dns_recheck, const string& ip_last_update, const string& ip_host, const time_t lastupdated, const int dns_cache_ttl, const time_t current_time) const
1743 if ( level <= Loglevel )
1746 msg << "Waiting for DNS cache TTL to expire. Hostname: " << hostname << " DNS-Record: " << ip_dns_recheck << " IP set in last update: " << ip_last_update << " Lastupdated: " << lastupdated << "DNS Cache TTL: " << dns_cache_ttl << " Current Time: " << current_time << " Hosts IP: " << ip_host << endl;
1747 log_notice(msg.str());
1754 * @param hostname Hostname
1755 * @param ip_dns_recheck Cached DNS entry
1756 * @param ip_last_update IP set in last update
1757 * @param ip_host Hosts IP
1758 * @param lastupdated Last updated
1760 void Logger::print_no_update_needed(const string& hostname, const string& ip_dns_recheck, const string& ip_last_update, const string& ip_host, const time_t lastupdated) const
1763 if ( level <= Loglevel )
1766 msg << "No update needed for host: " << hostname << " Cached DNS record: " << ip_dns_recheck << " IP set in last update: " << ip_last_update << " Hosts IP: " << ip_host << " Last updated: " << lastupdated << endl;
1767 log_notice(msg.str());
1773 * Error while trying to get local wan interface IP address through getifaddrs.
1774 * @param error The system call which raised the error.
1775 * @param error The error set by getifaddrs.
1777 void Logger::print_error_getting_local_wan_ip(const std::string& system_call, const std::string& error) const
1780 if ( level <= Loglevel )
1783 msg << "Error while trying to get local wan interface IP address through '" << system_call << "': " << error << endl;
1784 log_error(msg.str());
1790 * Could not get IP address of local wan interface.
1792 void Logger::print_no_wan_ip() const
1795 if ( level <= Loglevel )
1798 msg << "Could not get IP address of local wan interface." << endl;
1799 log_error(msg.str());
1805 * Invalid service config was detected.
1807 void Logger::print_invalid_service_config() const
1810 if ( level <= Loglevel )
1812 log_error("Ignoring invalid service. Please check.\n");
1818 * Print simple message.
1820 void Logger::print_msg( const string& msg ) const
1823 if ( level <= Loglevel )