2 * @brief Logger class implementation. This class implements the Logging facility.
6 * @copyright Intra2net AG
12 #include "version_info.h"
18 #include <boost/foreach.hpp>
20 namespace po = boost::program_options;
22 typedef boost::shared_ptr<boost::program_options::options_description> Options_descriptionPtr;
32 , ExternalWarningLevel(0)
33 , ExternalLogOnlyOnce(false)
35 set_log_facility(Loglevel,Syslog,ExternalWarningLog,ExternalWarningLevel,ExternalLogOnlyOnce);
48 * Decides if a external log message can be send.
49 * @param msg The message to log.
51 bool Logger::is_allowed_to_send( const string& msg ) const
53 if ( (ExternalLogOnlyOnce) && (ExternalSendMessages.find(msg) != ExternalSendMessages.end()) )
60 * Clears the external send messages set.
62 void Logger::clear_external_send_messages()
64 ExternalSendMessages.clear();
69 * Decides if Logging through syslog if enabled or through std.
70 * @param msg The message to log.
72 void Logger::log_notice(const string& msg) const
75 syslog(LOG_NOTICE,msg.c_str());
82 * Escape shell arguments.
83 * @param input The input to escape.
84 * @return The escaped string ready to use for the shell.
86 string Logger::escape_shellarg(const string &input)
89 string::const_iterator it, it_end = input.end();
90 for (it = input.begin(); it != it_end; it++)
104 * Decides if Logging through syslog if enabled or through std.
105 * @param msg The message to log.
107 void Logger::log_warning(const string& msg, int level)
110 syslog(LOG_WARNING,msg.c_str());
114 if ( (level <= ExternalWarningLevel) && (!ExternalWarningLog.empty()) && (is_allowed_to_send(msg)) )
116 string message = msg;
117 // Remove endline from msg.
118 if (!message.empty() && message[message.length()-1] == '\n')
119 message.erase(message.length()-1); /*lint !e534 */
121 message = escape_shellarg(message);
123 string external = ExternalWarningLog;
124 external.append(" ");
125 external.append(message);
126 if ( system(external.c_str()) != 0 )
127 print_error_external_logging(external);
129 ExternalSendMessages.insert(msg); /*lint !e534 */
135 * Decides if Logging through syslog if enabled or through std.
136 * @param msg The message to log.
138 void Logger::log_error(const string& msg) const
141 syslog(LOG_ERR,msg.c_str());
148 * Setter for member Loglevel.
149 * @param _loglevel Value to set Loglevel to.
151 void Logger::set_external_log_only_once( const bool _external_log_only_once )
153 ExternalLogOnlyOnce = _external_log_only_once;
158 * Setter for member Loglevel.
159 * @param _loglevel Value to set Loglevel to.
161 void Logger::set_loglevel(const int _loglevel)
163 Loglevel = _loglevel;
168 * Getter for member Loglevel.
171 int Logger::get_loglevel() const
178 * Setter for member Syslog.
179 * @param _syslog Wether to log through syslog or not.
181 void Logger::set_syslog(const bool _syslog)
188 * Getter for member Syslog.
189 * @return True if logging through syslog is enabled, false otherwise.
191 bool Logger::get_syslog() const
198 * Initialize the logging facility.
200 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 )
202 Loglevel = _loglevel;
204 ExternalWarningLog = _external_error_log;
205 ExternalWarningLevel = _external_error_level;
206 ExternalLogOnlyOnce = _external_log_only_once;
209 openlog("bpdyndnsd",LOG_PID,LOG_DAEMON);
214 * Prints out the usage to the command line.
216 void Logger::print_usage(const Options_descriptionPtr opt_desc) const
219 if ( level <= Loglevel )
222 msg << "Usage: bpdyndnsd [Command line options]" << "\n" << endl;
223 msg << *opt_desc << endl;
224 log_notice(msg.str());
230 * Prints out the programm name and the given version string on stdout.
231 * @param version Version string.
233 void Logger::print_version() const
236 if ( level <= Loglevel )
239 msg << "Bullet proof dynamic dns daemon.\nbpdyndnsd " << MAJOR_VERSION << "." << MINOR_VERSION << endl;
240 log_notice(msg.str());
246 * Prints out the successful parsing of the command line options.
248 void Logger::print_cmd_parsed() const
251 if ( level <= Loglevel )
253 log_notice("Command line options successfully parsed.");
258 void Logger::print_conf_files_parsed() const
261 if ( level <= Loglevel )
263 log_notice("Config files successfully parsed.");
269 * Prints out the successful parsing of the config files.
270 * @param config_path The specified config path.
272 void Logger::print_conf_loaded(const string& config_path) const
275 if ( level <= Loglevel )
278 msg << "Config files successfully loaded in: " << config_path << endl;
279 log_notice(msg.str());
285 * Prints out the unsuccessful parsing of the config files.
286 * @param config_path The specified config path.
288 void Logger::print_conf_not_loaded(const string& config_path) const
291 if ( level <= Loglevel )
294 msg << "Config files couldn't be loaded in: " << config_path << endl;
295 log_error(msg.str());
301 * Prints out the unsuccessful parsing of the config files.
303 void Logger::print_conf_reload_failed_exit() const
306 if ( level <= Loglevel )
307 log_error("Config files couldn't be reloaded. Exiting.");
312 * A file could not be opened for reading.
313 * @param filename The filename.
315 void Logger::print_error_opening_r(const string& filename) const
318 if ( level <= Loglevel )
321 msg << "Error opening file for reading: " << filename << endl;
322 log_error(msg.str());
328 * A file could not be opened for writing.
329 * @param filename The filename.
331 void Logger::print_error_opening_rw(const string& filename) const
334 if ( level <= Loglevel )
337 msg << "Error opening file for writing: " << filename << endl;
338 log_error(msg.str());
344 * Desctructor of specified class was called.
345 * @param _class Name of the class.
347 void Logger::print_destructor_call(const string& _class) const
350 if ( level <= Loglevel )
353 msg << "Destructor call: " << _class << endl;
354 log_notice(msg.str());
360 * Constructor of specified class was called.
361 * @param _class Name of the class.
363 void Logger::print_constructor_call(const string& _class) const
366 if ( level <= Loglevel )
369 msg << "Constructor call: " << _class << endl;
370 log_notice(msg.str());
376 * Update method for specified service was called.
377 * @param service The service for which the update is running.
379 void Logger::print_update_service(const string& service) const
382 if ( level <= Loglevel )
385 msg << "Running update for service: " << service << endl;
386 log_notice(msg.str());
391 * Service is blocked for update because of too many errors
392 * @param service The service for which the update is running.
393 * @param remaining_seconds Remaining seconds this service is blocked
395 void Logger::print_update_service_is_blocked(const std::string& service, int remaining_seconds) const
398 if ( level <= Loglevel )
401 msg << "Service " << service << " is blocked for " << remaining_seconds << " seconds because of too many errors" << endl;
402 log_notice(msg.str());
408 * Service will be blocked because of update errors
409 * @param service The service for which the update is running.
410 * @param block_seconds Number of seconds this service is blocked
412 void Logger::print_block_service(const std::string& service, int block_seconds) const
415 if ( level <= Loglevel )
418 msg << "Service " << service << " will be blocked for " << block_seconds << " seconds because of too many errors" << endl;
419 log_notice(msg.str());
425 * An unknown option on the command line was detected.
426 * @param unknown_option The unknown option.
428 void Logger::print_unknown_cmd_option(const string& unknown_option) const
431 if ( level <= Loglevel )
434 msg << "Unknown option on command line detected: " << unknown_option << endl;
435 log_error(msg.str());
441 * An unknown protocol was specified.
442 * @param protocol The unknown protocol.
444 void Logger::print_unknown_protocol(const string& protocol) const
447 if ( level <= Loglevel )
450 msg << "Unknown protocol defined: " << protocol << endl;
451 log_error(msg.str());
457 * Loading a service config file.
458 * @param filename The service config file.
460 void Logger::print_load_service_conf(const string& filename) const
463 if ( level <= Loglevel )
466 msg << "Loading service config file: " << filename << endl;
467 log_notice(msg.str());
473 * Loading the main config file.
474 * @param filename The main config file.
476 void Logger::print_load_main_conf(const string& filename) const
479 if ( level <= Loglevel )
482 msg << "Loading main config file: " << filename << endl;
483 log_notice(msg.str());
489 * There is an unknown option in a service config file.
490 * @param unknown_option The unknown option.
492 void Logger::print_unknown_service_conf_option(const string& service_conf_file, const string& unknown_option) const
495 if ( level <= Loglevel )
498 msg << "Unknown option in service config file detected: " << service_conf_file << " Unknown option: " << unknown_option << endl;
499 log_error(msg.str());
505 * There is an unknown option in the main config file.
506 * @param unknown_option The unknown option.
508 void Logger::print_unknown_main_conf_option(const string& unknown_option) const
511 if ( level <= Loglevel )
514 msg << "Unknown option in main config file detected: " << unknown_option << endl;
515 log_error(msg.str());
521 * The defined config path doesn't exist or is not a directory.
522 * @param config_path The defined config path.
524 void Logger::print_error_config_path(const string& config_path) const
527 if ( level <= Loglevel )
530 msg << "Config path doesn't exists or is not a diretory: " << config_path << endl;
531 log_error(msg.str());
537 * There is a missing command line option to initialize a service object.
539 void Logger::print_missing_cmd_service_option() const
542 if ( level <= Loglevel )
544 log_error("Missing option to initialize service. Protocol, host, login and password must be specified.");
550 * Missing option in service config file.
551 * @param service_conf_file Service config file
553 void Logger::print_missing_service_conf_option(const string& service_conf_file) const
556 if ( level <= Loglevel )
559 msg << "Missing option in service config file " << service_conf_file << " to initialize service. Protocol, host, login and password must be specified." << endl;
560 log_error(msg.str());
566 * Process running as daemon.
567 * @param pid The pid of the daemon.
569 void Logger::print_runnig_as_daemon(const int pid) const
572 if ( level <= Loglevel )
575 msg << "Running as daemon: " << pid << endl;
576 log_notice(msg.str());
582 * Prints out the daemon mode.
583 * @param daemon_mode The daemon mode.
585 void Logger::print_daemon_mode(const bool daemon_mode) const
588 if ( level <= Loglevel )
590 string mode = "disabled";
591 if (daemon_mode == true)
594 msg << "Daemon mode is " << mode << "." << endl;
595 log_notice(msg.str());
601 * There was an error while trying to fork.
603 void Logger::print_error_fork() const
606 if ( level <= Loglevel )
608 log_error("Error while trying to fork.");
614 * A pid in the pidfile was found.
615 * @param pid The pid found in the pidfile.
617 void Logger::print_pid_found(const int pid) const
620 if ( level <= Loglevel )
623 msg << "Pidfile found: " << pid << ". Checking if process is still runnig..." << endl;
624 log_notice(msg.str());
630 * Another process is already running.
631 * @param pid The pid of the other process.
633 void Logger::print_process_already_running(const int pid) const
636 if ( level <= Loglevel )
639 msg << "Another bpdyndnsd process with PID " << pid << " is already running!" << endl;
640 log_error(msg.str());
648 void Logger::print_caught_sigterm() const
651 if ( level <= Loglevel )
653 log_notice("Caught SIGTERM. Exiting...");
661 void Logger::print_caught_siguser1() const
664 if ( level <= Loglevel )
666 log_notice("Caught SIGUSR1. Switching to offline mode...");
674 void Logger::print_caught_sighup() const
677 if ( level <= Loglevel )
679 log_notice("Caught SIGHUP. Reloading config...");
687 void Logger::print_caught_siguser2() const
690 if ( level <= Loglevel )
692 log_notice("Caught SIGUSR2. Switching to online mode...");
700 void Logger::print_caught_sigrtmin() const
703 if ( level <= Loglevel )
705 log_notice("Caught SIGRTMIN. Switching to online mode with webcheck enabled...");
713 void Logger::print_caught_sigrtmax(int new_loglevel) const
716 if ( level <= Loglevel )
719 msg << "Caught SIGRTMAX. Increasing log level to " << new_loglevel << endl;
720 log_error(msg.str());
726 * Error while setting signal handler.
728 void Logger::print_error_setting_signal(const string& signal) const
731 if ( level <= Loglevel )
734 msg << "Error while setting signal handler for: " << signal << endl;
735 log_error(msg.str());
741 * Error while setting signal handler.
743 void Logger::print_init_log_facility() const
746 if ( level <= Loglevel )
749 msg << "Initialized logging facility." << " Loglevel: " << Loglevel << " Syslog: " << Syslog << " ExternalLogOnlyOnce: " << ExternalLogOnlyOnce << endl;
750 log_notice(msg.str());
756 * Be verbose. Currently we are in offline mode.
758 void Logger::print_offline_mode() const
761 if ( level <= Loglevel )
763 log_notice("Offline mode...");
769 * Dialup mode: Print how long we plan to delay the next network traffic
770 * @param sleep_until Timestamp until we plan to keep quiet
772 void Logger::print_sleep_dialup_mode(time_t sleep_until) const
775 if ( level <= Loglevel )
778 msg << "Skipped update in dialup mode (" << sleep_until-time(NULL) << " more seconds)" << endl;
779 log_error(msg.str());
784 * Objects successfully serialized.
786 void Logger::print_serialized_objects_success() const
789 if ( level <= Loglevel )
791 log_notice("Serialized objects successfully.");
797 * Objects successfully de-serialized.
799 void Logger::print_deserialized_objects_success() const
802 if ( level <= Loglevel )
804 log_notice("De-serialized objects successfully.");
810 * Prints out the content of a service object.
811 * @param message Message to be added on output first.
812 * @param protocol Service's protocol.
813 * @param hostname Service's hostname.
814 * @param login Service's login.
815 * @param password Service's password.
816 * @param actual_ip Service's actual_ip.
817 * @param lastupdated Service's lastupdated.
819 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
822 if ( level <= Loglevel )
825 msg << message << endl;
826 msg << "\t" << "Protocol: " << protocol << endl;
827 msg << "\t" << "Hostname: " << hostname << endl;
828 msg << "\t" << "Login: " << login << endl;
829 msg << "\t" << "Password: " << password << endl;
830 msg << "\t" << "Update Interval: " << update_interval << endl;
831 msg << "\t" << "Max Updates: " << max_updates_within_interval << endl;
832 msg << "\t" << "DNS Cache TTL: " << dns_cache_ttl << endl;
833 msg << "\t" << "Actual_IP: " << actual_ip << endl;
834 BOOST_FOREACH( time_t update_time, lastupdated)
836 msg << "\t" << "Lastupdated: " << update_time << endl;
838 log_notice(msg.str());
844 * Caught exception while serialize.
845 * @param exception Exception message.
847 void Logger::print_exception_serialize(const string& errMsg) const
850 if ( level <= Loglevel )
853 msg << "Error while trying to serialize Serviceholder object: " << errMsg << endl;
854 log_error(msg.str());
860 * Caught exception while de-serialize.
861 * @param exception Exception message.
863 void Logger::print_exception_deserialize(const string& errMsg) const
866 if ( level <= Loglevel )
869 msg << "Error while trying to de-serialize Serviceholder object: " << errMsg << endl;
870 log_error(msg.str());
876 * Child couldn't be killed by parent.
877 * @param pid Pid of the child.
879 void Logger::print_error_kill_child(const int pid) const
882 if ( level <= Loglevel )
885 msg << "Could not kill child process with PID: " << pid << endl;
886 log_error(msg.str());
892 * Child was killed by parent because of error.
893 * @param pid The pid (child) killed.
895 void Logger::print_child_killed(const int pid) const
898 if ( level <= Loglevel )
901 msg << "Killed child process with PID: " << pid << endl;
902 log_notice(msg.str());
908 * There is no object file.
909 * @param object_file The object file.
911 void Logger::print_no_object_file(const string& object_file)
914 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
917 msg << "There is no object file: " << object_file << ". Continue without recovering state from old services!" << endl;
918 log_warning(msg.str(),level);
924 * Prints out the given hostname
925 * @param hostname Hostname as string.
927 void Logger::print_hostname(const string& hostname) const
930 if ( level <= Loglevel )
933 msg << "Detected following hostname of localhost: " << hostname << endl;
934 log_notice(msg.str());
940 * Prints out the detected own ipv4 address
941 * @param ip_addr String representation of the detected ip.
943 void Logger::print_own_ipv4(const string& ip_addr_v4, const string& hostname) const
946 if ( level <= Loglevel )
949 msg << "Detected following IPv4-Address of host: " << hostname << " : " << ip_addr_v4 << endl;
950 log_notice(msg.str());
956 * Prints out the detected own ipv5 address
957 * @param ip_addr String representation of the detected ip.
959 void Logger::print_own_ipv6(const string& ip_addr_v6, const string& hostname) const
962 if ( level <= Loglevel )
965 msg << "Detected following IPv6-Address of host: " << hostname << " : " << ip_addr_v6 << endl;
966 log_notice(msg.str());
972 * Exception while trying to resolve hostname to ip.
973 * @param exception The exception caught.
974 * @param hostname The hostname.
976 void Logger::print_error_hostname_to_ip(const string& errMsg, const string& hostname) const
979 if ( level <= Loglevel )
982 msg << "Could not resolve the hostname: " << hostname << " to an IP-Address: " << errMsg << endl;
983 log_error(msg.str());
989 * The update of the given service was successful.
990 * @param service The service.
992 void Logger::print_update_service_successful(const string& service) const
995 if ( level <= Loglevel )
998 msg << "Updated service successful: " << service << endl;
999 log_notice(msg.str());
1005 * No ip could be determined through webcheck
1007 void Logger::print_webcheck_no_ip()
1010 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1012 log_warning("IP-Address of this host could not be determined through any configured webcheck url.",level);
1018 * Connection problem while trying to get ip through webcheck url
1019 * @param curl_err_buff Curl error message
1020 * @param url the url
1022 void Logger::print_webcheck_url_connection_problem(const char * curl_err_buff, const string& url)
1025 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1028 msg << "There was a problem while trying to connect to following URL: " << url << " CURL error: " << curl_err_buff << endl;
1029 log_warning(msg.str(),level);
1035 * Prints out curl error.
1036 * @param curl_err_buff Curl error message.
1039 void Logger::print_webcheck_error(const char * curl_err_buff, const string& url) const
1042 if ( level <= Loglevel )
1045 msg << "There was an error while trying to connect to following URL: " << url << " CURL error: " << curl_err_buff << endl;
1046 log_error(msg.str());
1052 * Prints out the received data through curl.
1053 * @param curl_data Data string
1055 void Logger::print_received_curl_data(const string& curl_data) const
1058 if ( level <= Loglevel )
1061 msg << "Received CURL data: " << curl_data << endl;
1062 log_notice(msg.str());
1068 * IP was foudn through regex
1069 * @param ip The IP found.
1071 void Logger::print_regex_found_ip(const string& ip) const
1074 if ( level <= Loglevel )
1077 msg << "Found IP-Address via regex: " << ip << endl;
1078 log_notice(msg.str());
1084 * No IP was found through regex.
1085 * @param data The data string which should contain a valid IP.s
1087 void Logger::print_regex_ip_not_found(const string& data)
1090 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1093 msg << "Could not extract an IP-Address via regex from following data:\n" << data << endl;
1094 log_warning(msg.str(),level);
1100 * Detected multiple occurrences of the same option.
1101 * @param message Error message.
1103 void Logger::print_multiple_cmd_option(const string& message) const
1106 if ( level <= Loglevel )
1109 msg << "The same option is only allowed once: " << message << endl;
1110 log_error(msg.str());
1116 * An update would exceed the update interval. Prints out a warning message.
1117 * @param override_log_level Override log level with zero if true
1118 * @param current_time Current time.
1119 * @param old_time Time of update #MaxUpdatesWithinInterval ago.
1120 * @param MaxUpdatesWithinInterval Number of allowed updates in one update interval.
1121 * @param service The service which exceeds update interval.
1123 void Logger::print_update_not_allowed(bool override_log_level, const time_t current_time, const time_t old_time, const int MaxUpdatesWithinInterval, const string& service)
1127 if (override_log_level)
1130 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1133 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;
1134 log_warning(msg.str(),level);
1140 * Failure while running update for service.
1141 * @param service Services' name.
1143 void Logger::print_update_service_failure(const string& service)
1146 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1149 msg << "Could not update service: " << service << endl;
1150 log_warning(msg.str(),level);
1157 void Logger::print_started() const
1160 if ( level <= Loglevel )
1162 log_notice("Started");
1170 void Logger::print_starting_shutdown() const
1173 if ( level <= Loglevel )
1175 log_notice("Shutting down ...");
1183 void Logger::print_shutdown_succeeded() const
1186 if ( level <= Loglevel )
1188 log_notice("Shutting down complete ...");
1194 * Shutdown parent succeeded
1196 void Logger::print_shutdown_parent_succeeded() const
1199 if ( level <= Loglevel )
1201 log_notice("Shutting down parent process completed ...");
1207 * Starting shutdown parent
1209 void Logger::print_starting_shutdown_parent() const
1212 if ( level <= Loglevel )
1214 log_notice("Shutting down parent process ...");
1220 * DNS cache record timeout
1221 * @param hostname Hostname
1222 * @param lastupdated Lastupdated
1223 * @param dns_cache_ttl DNS cache TTL
1224 * @param current_time Current time
1226 void Logger::print_recheck_dns_entry(const string& hostname, const int lastupdated, const int dns_cache_ttl, const int current_time) const
1229 if ( level <= Loglevel )
1232 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;
1233 log_notice(msg.str());
1239 * Missing proxy option on command line.
1241 void Logger::print_missing_cmd_proxy_option() const
1244 if ( level <= Loglevel )
1246 log_error("Missing option to initialize proxy. http_proxy and http_proxy_port must be specified.");
1252 * Multiple option in service config file.
1253 * @param service_conf_file Service config file
1254 * @param message Multiple option text
1256 void Logger::print_multiple_service_conf_option(const string& service_conf_file, const string& message) const
1259 if ( level <= Loglevel )
1262 msg << "Multiple occurrences of the same option in service config file detected: " << service_conf_file << " " << message << endl;
1263 log_error(msg.str());
1269 * Multiple option in main config file.
1270 * @param service_conf_file Service config file
1271 * @param message Multiple option text
1273 void Logger::print_multiple_main_conf_option(const string& main_conf_file, const string& message) const
1276 if ( level <= Loglevel )
1279 msg << "Multiple occurrences of the same option in main config file detected: " << main_conf_file << " " << message << endl;
1280 log_error(msg.str());
1286 * Missing proxy option in main config file.
1287 * @param main_conf_filename The concerning config file.
1289 void Logger::print_missing_conf_proxy_option(const string& main_conf_filename) const
1292 if ( level <= Loglevel )
1295 msg << "Missing option to initialize proxy in main config file: " << main_conf_filename << " http_proxy and http_proxy_port must be specified." << endl;
1296 log_error(msg.str());
1302 * There is no domain part in the given hostname
1303 * @param hostname The hostname with no domain part in it.
1305 void Logger::print_no_domain_part(const string& hostname) const
1308 if ( level <= Loglevel )
1311 msg << "There is no domain part in the given hostname: " << hostname << endl;
1312 log_notice(msg.str());
1318 * Service is not initialized properly.
1319 * @param service The service.
1321 void Logger::print_httphelper_not_initialized() const
1324 if ( level <= Loglevel )
1326 log_error("HTTP-Helper is not initialized properly.");
1332 * A curl error occured.
1333 * @param msg The error message
1334 * @param curl_err_code The resulting curl error code
1336 void Logger::print_curl_error_init(const std::string& err_msg, const CURLcode curl_err_code) const
1338 string curl_err = "";
1340 if ( curl_err_code == CURLE_FAILED_INIT )
1341 curl_err = "CURLE_FAILED_INIT";
1343 curl_err = "UNKNOWN";
1346 if ( (level <= Loglevel) )
1349 msg << "Curl error: " << err_msg << " Curl error code: " << curl_err_code << " " << curl_err << endl; /*lint !e641 */
1350 log_error(msg.str());
1356 * A curl error occured.
1357 * @param url The url requested by the curl operation
1358 * @param curl_err_code The resulting curl error code
1360 void Logger::print_curl_error(const string& url, const CURLcode curl_err_code) const
1362 string curl_err = "";
1364 if ( curl_err_code == CURLE_URL_MALFORMAT )
1365 curl_err = "CURLE_URL_MALFORMAT";
1366 else if ( curl_err_code == CURLE_COULDNT_RESOLVE_HOST )
1367 curl_err = "CURLE_COULDNT_RESOLVE_HOST";
1368 else if ( curl_err_code == CURLE_COULDNT_CONNECT )
1369 curl_err = "CURLE_COULDNT_CONNECT";
1371 curl_err = "UNKNOWN";
1374 if ( (level <= Loglevel) )
1377 msg << "Curl error while requesting following url: " << url << " Curl error code: " << curl_err_code << " " << curl_err << endl; /*lint !e641 */
1378 log_error(msg.str());
1384 * A curl error occured.
1385 * @param url The url requested by the curl operation
1386 * @param curl_err_code The resulting curl error code
1387 * @param curl_err_buff The curl error buffer
1389 void Logger::print_curl_error(const string& url, const CURLcode curl_err_code, const char * curl_err_buff) const
1391 string curl_err = "";
1393 if ( curl_err_code == CURLE_URL_MALFORMAT )
1394 curl_err = "CURLE_URL_MALFORMAT";
1395 else if ( curl_err_code == CURLE_COULDNT_RESOLVE_HOST )
1396 curl_err = "CURLE_COULDNT_RESOLVE_HOST";
1397 else if ( curl_err_code == CURLE_COULDNT_CONNECT )
1398 curl_err = "CURLE_COULDNT_CONNECT";
1400 curl_err = "UNKNOWN";
1403 if ( (level <= Loglevel) )
1406 msg << "Curl error while requesting following url: " << url << " Curl error code: " << curl_err_code << " " << curl_err << " " << curl_err_buff << endl; /*lint !e641 */
1407 log_error(msg.str());
1413 * Prints out the data received by curl operation
1414 * @param CurlWritedataBuff
1416 void Logger::print_curl_data(const string& curl_writedata_buff) const
1419 if ( level <= Loglevel )
1422 msg << "Data received by curl: " << curl_writedata_buff << endl;
1423 log_notice(msg.str());
1429 * Not authorized to perform requested update operation
1430 * @param service The requested service.
1431 * @param username Username
1432 * @param password Password
1434 void Logger::print_service_not_authorized(const string& service, const string& username, const string& password) const
1437 if ( level <= Loglevel )
1440 msg << "Not authorized to perform update operation on service: " << service << " Please check username and password: " << username << ":" << password << endl;
1441 log_notice(msg.str());
1447 * Prints out the http status code
1449 * @param output HTTP status code
1451 void Logger::print_http_status_code(const string& url, const long http_code) const
1454 if ( level <= Loglevel )
1457 msg << "Requested URL: " << url << " Received HTTP status code: " << http_code << endl;
1458 log_notice(msg.str());
1464 * Generic failure while trying to update service
1465 * @param url The requested URL
1466 * @param curl_data The received curl_data from the server
1468 void Logger::print_update_failure(const string& url, const string& curl_data) const
1471 if ( (level <= Loglevel) )
1474 msg << "Problem while trying to updating service. Requested URL: " << url << " Error Code from Server: " << curl_data << endl;
1475 log_error(msg.str());
1481 * Generic failure while trying to update service
1482 * @param url The requested URL
1483 * @param http_status_code The received http status code
1485 void Logger::print_update_failure(const string& url, const long http_status_code) const
1488 if ( level <= Loglevel )
1491 msg << "Problem while trying to updating service. Requested URL: " << url << " Error Code from Server: " << http_status_code << endl;
1492 log_error(msg.str());
1497 * Hostname is invalid, contains no or only one domain part.
1498 * @param hostname The full qualified host name.
1500 void Logger::print_invalid_hostname(const string& hostname)
1503 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1506 msg << "The configured hostname: " << hostname << " is invalid. Please add the corresponding domain part." << endl;
1507 log_warning(msg.str(),level);
1513 * An IP in a private range was detected
1514 * @param ip The private IP
1516 void Logger::print_ip_is_local(const string& ip)
1519 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1522 msg << "The detected IP is within a private IP range: " << ip << endl;
1523 log_warning(msg.str(),level);
1529 * Regex is matching in string
1530 * @param regex The regex pattern
1531 * @param matching_string The string
1533 void Logger::print_regex_match(const string& regex, const string& matching_string) const
1536 if ( level <= Loglevel )
1539 msg << "Regex: " << regex << " is matching in: " << matching_string << endl;
1540 log_notice(msg.str());
1546 * Regex is not matching
1547 * @param regex Regex
1548 * @param not_matching_string String
1550 void Logger::print_no_regex_match(const string& regex, const string& not_matching_string)
1553 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1556 msg << "Regex: " << regex << " is not matching in: " << not_matching_string << endl;
1557 log_warning(msg.str(),level);
1563 * Could not parse gnudip initial reply.
1564 * @param curl_data The data received from gnudip server which should contain salt, time and sign.
1566 void Logger::print_could_not_parse_received_data(const string& curl_data) const
1569 if ( (level <= Loglevel) )
1572 msg << "Could not parse salt, time and sign from initial gnudip server reply: " << curl_data << endl;
1573 log_error(msg.str());
1579 * Gnudip salt, time and sign could not be got from map
1581 void Logger::print_could_not_get_initial_gnudip_data() const
1584 if ( (level <= Loglevel) )
1586 log_error("Could not get salt, time and sign from map.");
1593 * Gnudip protocol requires explicit declaration of a servername.
1595 void Logger::print_gnudip_requires_servername()
1598 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1600 log_warning("Gnudip requires explicit definition of servername via config!",level);
1606 * An exception occured while computing the md5 sum.
1607 * @param what The exception occured.
1609 void Logger::print_exception_md5_sum(const string& what) const
1612 if ( level <= Loglevel )
1615 msg << "An exception occured while computing a md5 sum: " << what << endl;
1616 log_error(msg.str());
1622 * A network exception occured.
1623 * @param what The exception occured.
1625 void Logger::print_network_error(const string& what) const
1628 if ( level <= Loglevel )
1631 msg << "A network exception occured: " << what << endl;
1632 log_error(msg.str());
1638 * An undefined protocol error occured.
1639 * @param protocol The protocol
1640 * @param error The error
1642 void Logger::print_undefined_protocol_error(const string& protocol, const string& error) const
1645 if ( level <= Loglevel )
1648 msg << "An undefined protocol error occured. Protocol: " << protocol << " Error: " << error << endl;
1649 log_error(msg.str());
1654 * Error while trying to log through external program.
1655 * @param external_prog The external program called.
1657 void Logger::print_error_external_logging(const string& external_prog) const
1660 if ( level <= Loglevel )
1663 msg << "Error while trying to log through external program: " << external_prog << endl;
1664 log_notice(msg.str());
1670 * Error while parsing config file.
1671 * @param error Error occured.
1672 * @param config_file Config file.
1674 void Logger::print_error_parsing_config_file(const string& filename, const string& error) const
1677 if ( level <= Loglevel )
1680 msg << "Error while parsing config file: " << filename << " Error: " << error << endl;
1681 log_notice(msg.str());
1687 * Error while parsing cmd option
1688 * @param error Error
1690 void Logger::print_error_parsing_cmd(const string& error) const
1693 if ( level <= Loglevel )
1696 msg << "Error while parsing cmd options: " << error << endl;
1697 log_error(msg.str());
1703 * The webcheck interval was exceeded, so webcheck not allowed.
1704 * @param last_webcheck Time of last webcheck.
1705 * @param webcheck_interval Webcheck interval time in seconds.
1706 * @param current_time Current system time.
1708 void Logger::print_webcheck_exceed_interval( const time_t last_webcheck, const int webcheck_interval, const time_t current_time ) const
1711 if ( level <= Loglevel )
1714 msg << "Skipped webcheck in idle period (" << (last_webcheck+webcheck_interval)-current_time << " more seconds";
1715 msg << ", last_webcheck: " << last_webcheck << ", webcheck_interval(sec): " << webcheck_interval << ")" << endl;
1716 log_notice(msg.str());
1722 * Checking if hosts needs update.
1723 * @param hostname Hostname
1724 * @param current_time Current time
1725 * @param lastupdated Last updated
1727 void Logger::print_check_service_update(const string& hostname, const time_t current_time, const time_t lastupdated) const
1730 if ( level <= Loglevel )
1733 msg << "Checking if host: " << hostname << " needs update. Current time: " << current_time << " Last updated: " << lastupdated << endl;
1734 log_notice(msg.str());
1741 * @param hostname Hostname
1742 * @param ip_dns_recheck DNS recheck IP
1743 * @param ip_last_update IP set in last update
1744 * @param ip_host Hosts' IP
1746 void Logger::print_cached_dns_entry(const string& hostname, const string& ip_dns_recheck, const string& ip_last_update, const string& ip_host) const
1749 if ( level <= Loglevel )
1752 msg << "Cached DNS record for host <" << hostname << "> : " << ip_dns_recheck << " Last updated IP: " << ip_last_update << " Hosts IP: " << ip_host << endl;
1753 log_notice(msg.str());
1760 * @param hostname Hostname
1761 * @param ip_dns_recheck Cached DNS entry
1762 * @param ip_last_update IP set in last update
1763 * @param ip_host Hosts IP
1764 * @param lastupdated Lastupdated
1766 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{
1768 if ( level <= Loglevel )
1771 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;
1772 log_notice(msg.str());
1779 * @param hostname Hostname
1780 * @param ip_dns_recheck Cached DNS entry
1781 * @param ip_last_update IP set in last update
1782 * @param ip_host Hosts IP
1783 * @param lastupdated Last updated
1784 * @param dns_cache_ttl DNS cache ttl
1785 * @param current_time Current time
1787 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
1790 if ( level <= Loglevel )
1793 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;
1794 log_notice(msg.str());
1801 * @param override_log_level Override log level with zero if true
1802 * @param hostname Hostname
1803 * @param ip_dns_recheck Cached DNS entry
1804 * @param ip_last_update IP set in last update
1805 * @param ip_host Hosts IP
1806 * @param lastupdated Last updated
1807 * @param dns_cache_ttl DNS cache ttl
1808 * @param current_time Current time
1810 void Logger::print_update_service_ttl_not_expired(bool override_log_level, 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
1814 if (override_log_level)
1817 if ( level <= Loglevel )
1820 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;
1821 log_notice(msg.str());
1828 * @param override_log_level Override log level with zero if true
1829 * @param hostname Hostname
1830 * @param ip_dns_recheck Cached DNS entry
1831 * @param ip_last_update IP set in last update
1832 * @param ip_host Hosts IP
1833 * @param lastupdated Last updated
1835 void Logger::print_no_update_needed(bool override_log_level, const string& hostname, const string& ip_dns_recheck, const string& ip_last_update, const string& ip_host, const time_t lastupdated) const
1839 if (override_log_level)
1842 if ( level <= Loglevel )
1845 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;
1846 log_notice(msg.str());
1852 * Error while trying to get local wan interface IP address through getifaddrs.
1853 * @param error The system call which raised the error.
1854 * @param error The error set by getifaddrs.
1856 void Logger::print_error_getting_local_wan_ip(const std::string& system_call, const std::string& error) const
1859 if ( level <= Loglevel )
1862 msg << "Error while trying to get local wan interface IP address through '" << system_call << "': " << error << endl;
1863 log_error(msg.str());
1869 * Could not get IP address of local wan interface.
1870 * @param override_log_level Override log level with zero if true
1872 void Logger::print_no_wan_ip(bool override_log_level) const
1876 if (override_log_level)
1879 if ( level <= Loglevel )
1882 msg << "Could not get IP address of local wan interface. Will retry later." << endl;
1883 log_error(msg.str());
1888 * Print extenral WAN IP
1889 * @param override_log_level Override log level with zero if true
1890 * @param ip External WAN IP
1892 void Logger::print_external_wan_ip(bool override_log_level, const std::string &ip) const
1896 if (override_log_level)
1899 if ( level <= Loglevel )
1902 msg << "Determined WAN interface IP: " << ip << endl;
1903 log_error(msg.str());
1908 * Could not resolve current IP for DNS name
1909 * @param override_log_level Override log level with zero if true
1911 void Logger::print_dns_lookup_failed(bool override_log_level, const std::string &hostname) const
1915 if (override_log_level)
1918 if ( level <= Loglevel )
1921 msg << "Could not resolve IP address for host " << hostname << ". Will retry later." << endl;
1922 log_error(msg.str());
1928 * Invalid service config was detected.
1930 void Logger::print_invalid_service_config() const
1933 if ( level <= Loglevel )
1935 log_error("Ignoring invalid service. Please check.\n");
1941 * Print simple message.
1943 void Logger::print_msg( const string& msg ) const
1946 if ( level <= Loglevel )