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)
36 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(message);
129 if ( system(external.c_str()) != 0 )
130 print_error_external_logging(external);
132 ExternalSendMessages.insert(msg); /*lint !e534 */
138 * Decides if Logging through syslog if enabled or through std.
139 * @param msg The message to log.
141 void Logger::log_error(const string& msg) const
144 syslog(LOG_ERR,msg.c_str());
151 * Setter for member Loglevel.
152 * @param _loglevel Value to set Loglevel to.
154 void Logger::set_external_log_only_once( const bool _external_log_only_once )
156 ExternalLogOnlyOnce = _external_log_only_once;
161 * Setter for member LogPasswords.
162 * @param _log_passwords If we want to log passwords or not.
164 void Logger::set_log_passwords( const bool _log_passwords )
166 LogPasswords = _log_passwords;
171 * Setter for member Loglevel.
172 * @param _loglevel Value to set Loglevel to.
174 void Logger::set_loglevel(const int _loglevel)
176 Loglevel = _loglevel;
181 * Getter for member Loglevel.
184 int Logger::get_loglevel() const
191 * Setter for member Syslog.
192 * @param _syslog Wether to log through syslog or not.
194 void Logger::set_syslog(const bool _syslog)
201 * Getter for member Syslog.
202 * @return True if logging through syslog is enabled, false otherwise.
204 bool Logger::get_syslog() const
211 * Initialize the logging facility.
213 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 )
215 Loglevel = _loglevel;
217 ExternalWarningLog = _external_error_log;
218 ExternalWarningLevel = _external_error_level;
219 ExternalLogOnlyOnce = _external_log_only_once;
222 openlog("bpdyndnsd",LOG_PID,LOG_DAEMON);
227 * Prints out the usage to the command line.
229 void Logger::print_usage(const Options_descriptionPtr opt_desc) const
232 if ( level <= Loglevel )
235 msg << "Usage: bpdyndnsd [Command line options]" << "\n" << endl;
236 msg << *opt_desc << endl;
237 log_notice(msg.str());
243 * Prints out the programm name and the given version string on stdout.
244 * @param version Version string.
246 void Logger::print_version() const
249 if ( level <= Loglevel )
252 msg << "Bullet proof dynamic dns daemon.\nbpdyndnsd " << MAJOR_VERSION << "." << MINOR_VERSION << endl;
253 log_notice(msg.str());
259 * Prints out the successful parsing of the command line options.
261 void Logger::print_cmd_parsed() const
264 if ( level <= Loglevel )
266 log_notice("Command line options successfully parsed.");
271 void Logger::print_conf_files_parsed() const
274 if ( level <= Loglevel )
276 log_notice("Config files successfully parsed.");
282 * Prints out the successful parsing of the config files.
283 * @param config_path The specified config path.
285 void Logger::print_conf_loaded(const string& config_path) const
288 if ( level <= Loglevel )
291 msg << "Config files successfully loaded in: " << config_path << endl;
292 log_notice(msg.str());
298 * Prints out the unsuccessful parsing of the config files.
299 * @param config_path The specified config path.
301 void Logger::print_conf_not_loaded(const string& config_path) const
304 if ( level <= Loglevel )
307 msg << "Config files couldn't be loaded in: " << config_path << endl;
308 log_error(msg.str());
314 * Prints out the unsuccessful parsing of the config files.
316 void Logger::print_conf_reload_failed_exit() const
319 if ( level <= Loglevel )
320 log_error("Config files couldn't be reloaded. Exiting.");
325 * A file could not be opened for reading.
326 * @param filename The filename.
328 void Logger::print_error_opening_r(const string& filename) const
331 if ( level <= Loglevel )
334 msg << "Error opening file for reading: " << filename << endl;
335 log_error(msg.str());
341 * A file could not be opened for writing.
342 * @param filename The filename.
344 void Logger::print_error_opening_rw(const string& filename) const
347 if ( level <= Loglevel )
350 msg << "Error opening file for writing: " << filename << endl;
351 log_error(msg.str());
357 * Desctructor of specified class was called.
358 * @param _class Name of the class.
360 void Logger::print_destructor_call(const string& _class) const
363 if ( level <= Loglevel )
366 msg << "Destructor call: " << _class << endl;
367 log_notice(msg.str());
373 * Constructor of specified class was called.
374 * @param _class Name of the class.
376 void Logger::print_constructor_call(const string& _class) const
379 if ( level <= Loglevel )
382 msg << "Constructor call: " << _class << endl;
383 log_notice(msg.str());
389 * Update method for specified service was called.
390 * @param service The service for which the update is running.
392 void Logger::print_update_service(const string& service) const
395 if ( level <= Loglevel )
398 msg << "Running update for service: " << service << endl;
399 log_notice(msg.str());
404 * Service is blocked for update because of too many errors
405 * @param service The service for which the update is running.
406 * @param remaining_seconds Remaining seconds this service is blocked
408 void Logger::print_update_service_is_blocked(const std::string& service, int remaining_seconds) const
411 if ( level <= Loglevel )
414 msg << "Service " << service << " is blocked for " << remaining_seconds << " seconds because of too many errors" << endl;
415 log_notice(msg.str());
421 * Service will be blocked because of update errors
422 * @param service The service for which the update is running.
423 * @param block_seconds Number of seconds this service is blocked
425 void Logger::print_block_service(const std::string& service, int block_seconds) const
428 if ( level <= Loglevel )
431 msg << "Service " << service << " will be blocked for " << block_seconds << " seconds because of too many errors" << endl;
432 log_notice(msg.str());
438 * An unknown option on the command line was detected.
439 * @param unknown_option The unknown option.
441 void Logger::print_unknown_cmd_option(const string& unknown_option) const
444 if ( level <= Loglevel )
447 msg << "Unknown option on command line detected: " << unknown_option << endl;
448 log_error(msg.str());
454 * An unknown protocol was specified.
455 * @param protocol The unknown protocol.
457 void Logger::print_unknown_protocol(const string& protocol) const
460 if ( level <= Loglevel )
463 msg << "Unknown protocol defined: " << protocol << endl;
464 log_error(msg.str());
470 * Loading a service config file.
471 * @param filename The service config file.
473 void Logger::print_load_service_conf(const string& filename) const
476 if ( level <= Loglevel )
479 msg << "Loading service config file: " << filename << endl;
480 log_notice(msg.str());
486 * Loading the main config file.
487 * @param filename The main config file.
489 void Logger::print_load_main_conf(const string& filename) const
492 if ( level <= Loglevel )
495 msg << "Loading main config file: " << filename << endl;
496 log_notice(msg.str());
502 * There is an unknown option in a service config file.
503 * @param unknown_option The unknown option.
505 void Logger::print_unknown_service_conf_option(const string& service_conf_file, const string& unknown_option) const
508 if ( level <= Loglevel )
511 msg << "Unknown option in service config file detected: " << service_conf_file << " Unknown option: " << unknown_option << endl;
512 log_error(msg.str());
518 * There is an unknown option in the main config file.
519 * @param unknown_option The unknown option.
521 void Logger::print_unknown_main_conf_option(const string& unknown_option) const
524 if ( level <= Loglevel )
527 msg << "Unknown option in main config file detected: " << unknown_option << endl;
528 log_error(msg.str());
534 * The defined config path doesn't exist or is not a directory.
535 * @param config_path The defined config path.
537 void Logger::print_error_config_path(const string& config_path) const
540 if ( level <= Loglevel )
543 msg << "Config path doesn't exists or is not a diretory: " << config_path << endl;
544 log_error(msg.str());
550 * There is a missing command line option to initialize a service object.
552 void Logger::print_missing_cmd_service_option() const
555 if ( level <= Loglevel )
557 log_error("Missing option to initialize service. Protocol, host, login and password must be specified.");
563 * Missing option in service config file.
564 * @param service_conf_file Service config file
566 void Logger::print_missing_service_conf_option(const string& service_conf_file) const
569 if ( level <= Loglevel )
572 msg << "Missing option in service config file " << service_conf_file << " to initialize service. Protocol, host, login and password must be specified." << endl;
573 log_error(msg.str());
579 * Process running as daemon.
580 * @param pid The pid of the daemon.
582 void Logger::print_runnig_as_daemon(const int pid) const
585 if ( level <= Loglevel )
588 msg << "Running as daemon: " << pid << endl;
589 log_notice(msg.str());
595 * Prints out the daemon mode.
596 * @param daemon_mode The daemon mode.
598 void Logger::print_daemon_mode(const bool daemon_mode) const
601 if ( level <= Loglevel )
603 string mode = "disabled";
604 if (daemon_mode == true)
607 msg << "Daemon mode is " << mode << "." << endl;
608 log_notice(msg.str());
614 * There was an error while trying to fork.
616 void Logger::print_error_fork() const
619 if ( level <= Loglevel )
621 log_error("Error while trying to fork.");
627 * A pid in the pidfile was found.
628 * @param pid The pid found in the pidfile.
630 void Logger::print_pid_found(const int pid) const
633 if ( level <= Loglevel )
636 msg << "Pidfile found: " << pid << ". Checking if process is still runnig..." << endl;
637 log_notice(msg.str());
643 * Another process is already running.
644 * @param pid The pid of the other process.
646 void Logger::print_process_already_running(const int pid) const
649 if ( level <= Loglevel )
652 msg << "Another bpdyndnsd process with PID " << pid << " is already running!" << endl;
653 log_error(msg.str());
661 void Logger::print_caught_sigterm() const
664 if ( level <= Loglevel )
666 log_notice("Caught SIGTERM. Exiting...");
674 void Logger::print_caught_siguser1() const
677 if ( level <= Loglevel )
679 log_notice("Caught SIGUSR1. Switching to offline mode...");
687 void Logger::print_caught_sighup() const
690 if ( level <= Loglevel )
692 log_notice("Caught SIGHUP. Reloading config...");
700 void Logger::print_caught_siguser2() const
703 if ( level <= Loglevel )
705 log_notice("Caught SIGUSR2. Switching to online mode...");
713 void Logger::print_caught_sigrtmin() const
716 if ( level <= Loglevel )
718 log_notice("Caught SIGRTMIN. Switching to online mode with webcheck enabled...");
726 void Logger::print_caught_sigrtmax(int new_loglevel) const
729 if ( level <= Loglevel )
732 msg << "Caught SIGRTMAX. Increasing log level to " << new_loglevel << endl;
733 log_error(msg.str());
739 * Error while setting signal handler.
741 void Logger::print_error_setting_signal(const string& signal) const
744 if ( level <= Loglevel )
747 msg << "Error while setting signal handler for: " << signal << endl;
748 log_error(msg.str());
754 * Error while setting signal handler.
756 void Logger::print_init_log_facility() const
759 if ( level <= Loglevel )
762 msg << "Initialized logging facility." << " Loglevel: " << Loglevel << " Syslog: " << Syslog << " ExternalLogOnlyOnce: " << ExternalLogOnlyOnce << endl;
763 log_notice(msg.str());
769 * Be verbose. Currently we are in offline mode.
771 void Logger::print_offline_mode() const
774 if ( level <= Loglevel )
776 log_notice("Offline mode...");
782 * Dialup mode: Print how long we plan to delay the next network traffic
783 * @param sleep_until Timestamp until we plan to keep quiet
785 void Logger::print_sleep_dialup_mode(time_t sleep_until) const
788 if ( level <= Loglevel )
791 msg << "Skipped update in dialup mode (" << sleep_until-time(NULL) << " more seconds)" << endl;
792 log_error(msg.str());
797 * Objects successfully serialized.
799 void Logger::print_serialized_objects_success() const
802 if ( level <= Loglevel )
804 log_notice("Serialized objects successfully.");
810 * Objects successfully de-serialized.
812 void Logger::print_deserialized_objects_success() const
815 if ( level <= Loglevel )
817 log_notice("De-serialized objects successfully.");
823 * Prints out the content of a service object.
824 * @param message Message to be added on output first.
825 * @param protocol Service's protocol.
826 * @param hostname Service's hostname.
827 * @param login Service's login.
828 * @param password Service's password.
829 * @param actual_ip Service's actual_ip.
830 * @param lastupdated Service's lastupdated.
831 * @param activated Service's activated.
833 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 max_equal_updates_in_succession, const int dns_cache_ttl , const string& actual_ip, std::map<time_t,std::string> lastupdated, const bool activated) const
836 if ( level <= Loglevel )
839 msg << message << endl;
840 msg << "\t" << "Protocol: " << protocol << endl;
841 msg << "\t" << "Hostname: " << hostname << endl;
842 msg << "\t" << "Login: " << login << endl;
845 msg << "\t" << "Password: " << password << endl;
847 msg << "\t" << "Password: (*hidden*)" << endl;
849 msg << "\t" << "Update Interval: " << update_interval << endl;
850 msg << "\t" << "Max Updates: " << max_updates_within_interval << endl;
851 msg << "\t" << "Max equal Updates:" << max_equal_updates_in_succession << endl;
852 msg << "\t" << "DNS Cache TTL: " << dns_cache_ttl << endl;
853 msg << "\t" << "Actual_IP: " << actual_ip << endl;
854 for ( std::map<time_t,std::string>::reverse_iterator r_iter = lastupdated.rbegin(); r_iter != lastupdated.rend(); r_iter++ )
856 msg << "\t" << "Lastupdated: " << r_iter->first << " -> " << r_iter->second << endl;
858 msg << "\t" << "Activated: " << activated << endl;
859 log_notice(msg.str());
865 * Caught exception while serialize.
866 * @param exception Exception message.
868 void Logger::print_exception_serialize(const string& errMsg) const
871 if ( level <= Loglevel )
874 msg << "Error while trying to serialize Serviceholder object: " << errMsg << endl;
875 log_error(msg.str());
881 * Caught exception while de-serialize.
882 * @param exception Exception message.
884 void Logger::print_exception_deserialize(const string& errMsg) const
887 if ( level <= Loglevel )
890 msg << "Error while trying to de-serialize Serviceholder object: " << errMsg << ". Continue without recovering state from old services!" << endl;
891 log_error(msg.str());
897 * Child couldn't be killed by parent.
898 * @param pid Pid of the child.
900 void Logger::print_error_kill_child(const int pid) const
903 if ( level <= Loglevel )
906 msg << "Could not kill child process with PID: " << pid << endl;
907 log_error(msg.str());
913 * Child was killed by parent because of error.
914 * @param pid The pid (child) killed.
916 void Logger::print_child_killed(const int pid) const
919 if ( level <= Loglevel )
922 msg << "Killed child process with PID: " << pid << endl;
923 log_notice(msg.str());
929 * There is no object file.
930 * @param object_file The object file.
932 void Logger::print_no_object_file(const string& object_file)
935 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
938 msg << "There is no object file: " << object_file << ". Continue without recovering state from old services!" << endl;
939 log_warning(msg.str(),level);
945 * Prints out the given hostname
946 * @param hostname Hostname as string.
948 void Logger::print_hostname(const string& hostname) const
951 if ( level <= Loglevel )
954 msg << "Detected following hostname of localhost: " << hostname << endl;
955 log_notice(msg.str());
961 * Prints out the detected own ipv4 address
962 * @param ip_addr String representation of the detected ip.
964 void Logger::print_own_ipv4(const string& ip_addr_v4, const string& hostname) const
967 if ( level <= Loglevel )
970 msg << "Detected following IPv4-Address of host: " << hostname << " : " << ip_addr_v4 << endl;
971 log_notice(msg.str());
977 * Prints out the detected own ipv5 address
978 * @param ip_addr String representation of the detected ip.
980 void Logger::print_own_ipv6(const string& ip_addr_v6, const string& hostname) const
983 if ( level <= Loglevel )
986 msg << "Detected following IPv6-Address of host: " << hostname << " : " << ip_addr_v6 << endl;
987 log_notice(msg.str());
993 * Exception while trying to resolve hostname to ip.
994 * @param exception The exception caught.
995 * @param hostname The hostname.
997 void Logger::print_error_hostname_to_ip(const string& errMsg, const string& hostname) const
1000 if ( level <= Loglevel )
1003 msg << "Could not resolve the hostname: " << hostname << " to an IP-Address: " << errMsg << endl;
1004 log_error(msg.str());
1010 * The update of the given service was successful.
1011 * @param service The service.
1013 void Logger::print_update_service_successful(const string& service, const std::string& ip_addr) const
1016 if ( level <= Loglevel )
1019 msg << "Updated service successful: " << service << " to IP: " << ip_addr << endl;
1020 log_notice(msg.str());
1026 * No ip could be determined through webcheck
1028 void Logger::print_webcheck_no_ip()
1031 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1033 log_warning("IP-Address of this host could not be determined through any configured webcheck url.",level);
1039 * Connection problem while trying to get ip through webcheck url
1040 * @param curl_err_buff Curl error message
1041 * @param url the url
1043 void Logger::print_webcheck_url_connection_problem(const char * curl_err_buff, const string& url)
1046 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1049 msg << "There was a problem while trying to connect to following URL: " << url << " CURL error: " << curl_err_buff << endl;
1050 log_warning(msg.str(),level);
1056 * Prints out curl error.
1057 * @param curl_err_buff Curl error message.
1060 void Logger::print_webcheck_error(const char * curl_err_buff, const string& url) const
1063 if ( level <= Loglevel )
1066 msg << "There was an error while trying to connect to following URL: " << url << " CURL error: " << curl_err_buff << endl;
1067 log_error(msg.str());
1073 * Prints out the received data through curl.
1074 * @param curl_data Data string
1076 void Logger::print_received_curl_data(const string& curl_data) const
1079 if ( level <= Loglevel )
1082 msg << "Received CURL data: " << curl_data << endl;
1083 log_notice(msg.str());
1089 * IP was foudn through regex
1090 * @param ip The IP found.
1092 void Logger::print_regex_found_ip(const string& ip) const
1095 if ( level <= Loglevel )
1098 msg << "Found IP-Address via regex: " << ip << endl;
1099 log_notice(msg.str());
1105 * No IP was found through regex.
1106 * @param data The data string which should contain a valid IP.s
1108 void Logger::print_regex_ip_not_found(const string& data)
1111 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1114 msg << "Could not extract an IP-Address via regex from following data:\n" << data << endl;
1115 log_warning(msg.str(),level);
1121 * Detected multiple occurrences of the same option.
1122 * @param message Error message.
1124 void Logger::print_multiple_cmd_option(const string& message) const
1127 if ( level <= Loglevel )
1130 msg << "The same option is only allowed once: " << message << endl;
1131 log_error(msg.str());
1137 * An update would exceed the update interval. Prints out a warning message.
1138 * @param override_log_level Override log level with zero if true
1139 * @param current_time Current time.
1140 * @param old_time Time of update #MaxUpdatesWithinInterval ago.
1141 * @param MaxUpdatesWithinInterval Number of allowed updates in one update interval.
1142 * @param service The service which exceeds update interval.
1144 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)
1148 if (override_log_level)
1151 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1154 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;
1155 log_warning(msg.str(),level);
1161 * Failure while running update for service.
1162 * @param service Services' name.
1164 void Logger::print_update_service_failure(const string& service)
1167 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1170 msg << "Could not update service: " << service << endl;
1171 log_warning(msg.str(),level);
1178 void Logger::print_started() const
1181 if ( level <= Loglevel )
1183 log_notice("Started");
1191 void Logger::print_starting_shutdown() const
1194 if ( level <= Loglevel )
1196 log_notice("Shutting down ...");
1204 void Logger::print_shutdown_succeeded() const
1207 if ( level <= Loglevel )
1209 log_notice("Shutting down complete ...");
1215 * Shutdown parent succeeded
1217 void Logger::print_shutdown_parent_succeeded() const
1220 if ( level <= Loglevel )
1222 log_notice("Shutting down parent process completed ...");
1228 * Starting shutdown parent
1230 void Logger::print_starting_shutdown_parent() const
1233 if ( level <= Loglevel )
1235 log_notice("Shutting down parent process ...");
1241 * DNS cache record timeout
1242 * @param hostname Hostname
1243 * @param lastupdated Lastupdated
1244 * @param dns_cache_ttl DNS cache TTL
1245 * @param current_time Current time
1247 void Logger::print_recheck_dns_entry(const string& hostname, const int lastupdated, const int dns_cache_ttl, const int current_time) const
1250 if ( level <= Loglevel )
1253 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;
1254 log_notice(msg.str());
1260 * Missing proxy option on command line.
1262 void Logger::print_missing_cmd_proxy_option() const
1265 if ( level <= Loglevel )
1267 log_error("Missing option to initialize proxy. http_proxy and http_proxy_port must be specified.");
1273 * Multiple option in service config file.
1274 * @param service_conf_file Service config file
1275 * @param message Multiple option text
1277 void Logger::print_multiple_service_conf_option(const string& service_conf_file, const string& message) const
1280 if ( level <= Loglevel )
1283 msg << "Multiple occurrences of the same option in service config file detected: " << service_conf_file << " " << message << endl;
1284 log_error(msg.str());
1290 * Multiple option in main config file.
1291 * @param service_conf_file Service config file
1292 * @param message Multiple option text
1294 void Logger::print_multiple_main_conf_option(const string& main_conf_file, const string& message) const
1297 if ( level <= Loglevel )
1300 msg << "Multiple occurrences of the same option in main config file detected: " << main_conf_file << " " << message << endl;
1301 log_error(msg.str());
1307 * Missing proxy option in main config file.
1308 * @param main_conf_filename The concerning config file.
1310 void Logger::print_missing_conf_proxy_option(const string& main_conf_filename) const
1313 if ( level <= Loglevel )
1316 msg << "Missing option to initialize proxy in main config file: " << main_conf_filename << " http_proxy and http_proxy_port must be specified." << endl;
1317 log_error(msg.str());
1323 * There is no domain part in the given hostname
1324 * @param hostname The hostname with no domain part in it.
1326 void Logger::print_no_domain_part(const string& hostname) const
1329 if ( level <= Loglevel )
1332 msg << "There is no domain part in the given hostname: " << hostname << endl;
1333 log_notice(msg.str());
1339 * Service is not initialized properly.
1340 * @param service The service.
1342 void Logger::print_httphelper_not_initialized() const
1345 if ( level <= Loglevel )
1347 log_error("HTTP-Helper is not initialized properly.");
1353 * A curl error occured.
1354 * @param msg The error message
1355 * @param curl_err_code The resulting curl error code
1357 void Logger::print_curl_error_init(const std::string& err_msg, const CURLcode curl_err_code) const
1359 string curl_err = "";
1361 if ( curl_err_code == CURLE_FAILED_INIT )
1362 curl_err = "CURLE_FAILED_INIT";
1364 curl_err = "UNKNOWN";
1367 if ( (level <= Loglevel) )
1370 msg << "Curl error: " << err_msg << " Curl error code: " << curl_err_code << " " << curl_err << endl; /*lint !e641 */
1371 log_error(msg.str());
1377 * A curl error occured.
1378 * @param url The url requested by the curl operation
1379 * @param curl_err_code The resulting curl error code
1381 void Logger::print_curl_error(const string& url, const CURLcode curl_err_code) const
1383 string curl_err = "";
1385 if ( curl_err_code == CURLE_URL_MALFORMAT )
1386 curl_err = "CURLE_URL_MALFORMAT";
1387 else if ( curl_err_code == CURLE_COULDNT_RESOLVE_HOST )
1388 curl_err = "CURLE_COULDNT_RESOLVE_HOST";
1389 else if ( curl_err_code == CURLE_COULDNT_CONNECT )
1390 curl_err = "CURLE_COULDNT_CONNECT";
1392 curl_err = "UNKNOWN";
1395 if ( (level <= Loglevel) )
1398 msg << "Curl error while requesting following url: " << url << " Curl error code: " << curl_err_code << " " << curl_err << endl; /*lint !e641 */
1399 log_error(msg.str());
1405 * A curl error occured.
1406 * @param url The url requested by the curl operation
1407 * @param curl_err_code The resulting curl error code
1408 * @param curl_err_buff The curl error buffer
1410 void Logger::print_curl_error(const string& url, const CURLcode curl_err_code, const char * curl_err_buff) const
1412 string curl_err = "";
1414 if ( curl_err_code == CURLE_URL_MALFORMAT )
1415 curl_err = "CURLE_URL_MALFORMAT";
1416 else if ( curl_err_code == CURLE_COULDNT_RESOLVE_HOST )
1417 curl_err = "CURLE_COULDNT_RESOLVE_HOST";
1418 else if ( curl_err_code == CURLE_COULDNT_CONNECT )
1419 curl_err = "CURLE_COULDNT_CONNECT";
1421 curl_err = "UNKNOWN";
1424 if ( (level <= Loglevel) )
1427 msg << "Curl error while requesting following url: " << url << " Curl error code: " << curl_err_code << " " << curl_err << " " << curl_err_buff << endl; /*lint !e641 */
1428 log_error(msg.str());
1434 * Prints out the data received by curl operation
1435 * @param CurlWritedataBuff
1437 void Logger::print_curl_data(const string& curl_writedata_buff) const
1440 if ( level <= Loglevel )
1443 msg << "Data received by curl: " << curl_writedata_buff << endl;
1444 log_notice(msg.str());
1450 * Not authorized to perform requested update operation
1451 * @param service The requested service.
1452 * @param username Username
1453 * @param password Password
1455 void Logger::print_service_not_authorized(const string& service, const string& username, const string& password) const
1458 if ( level <= Loglevel )
1461 msg << "Not authorized to perform update operation on service: " << service << " Please check username and password: " << username << ":";
1464 msg << password << endl;
1466 msg << "(*hidden*)" << endl;
1468 log_notice(msg.str());
1474 * Prints out the http status code
1476 * @param output HTTP status code
1478 void Logger::print_http_status_code(const string& url, const long http_code) const
1481 if ( level <= Loglevel )
1484 msg << "Requested URL: " << url << " Received HTTP status code: " << http_code << endl;
1485 log_notice(msg.str());
1491 * Generic failure while trying to update service
1492 * @param url The requested URL
1493 * @param curl_data The received curl_data from the server
1495 void Logger::print_update_failure(const string& url, const string& curl_data) const
1498 if ( (level <= Loglevel) )
1501 msg << "Problem while trying to updating service. Requested URL: " << url << " Error Code from Server: " << curl_data << endl;
1502 log_error(msg.str());
1508 * Generic failure while trying to update service
1509 * @param url The requested URL
1510 * @param http_status_code The received http status code
1512 void Logger::print_update_failure(const string& url, const long http_status_code) const
1515 if ( level <= Loglevel )
1518 msg << "Problem while trying to updating service. Requested URL: " << url << " Error Code from Server: " << http_status_code << endl;
1519 log_error(msg.str());
1524 * Hostname is invalid, contains no or only one domain part.
1525 * @param hostname The full qualified host name.
1527 void Logger::print_invalid_hostname(const string& hostname)
1530 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1533 msg << "The configured hostname: " << hostname << " is invalid. Please add the corresponding domain part." << endl;
1534 log_warning(msg.str(),level);
1540 * An IP in a private range was detected
1541 * @param ip The private IP
1543 void Logger::print_ip_is_local(const string& ip)
1546 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1549 msg << "The detected IP is within a private IP range: " << ip << endl;
1550 log_warning(msg.str(),level);
1556 * Regex is matching in string
1557 * @param regex The regex pattern
1558 * @param matching_string The string
1560 void Logger::print_regex_match(const string& regex, const string& matching_string) const
1563 if ( level <= Loglevel )
1566 msg << "Regex: " << regex << " is matching in: " << matching_string << endl;
1567 log_notice(msg.str());
1573 * Regex is not matching
1574 * @param regex Regex
1575 * @param not_matching_string String
1577 void Logger::print_no_regex_match(const string& regex, const string& not_matching_string)
1580 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1583 msg << "Regex: " << regex << " is not matching in: " << not_matching_string << endl;
1584 log_warning(msg.str(),level);
1590 * Could not parse gnudip initial reply.
1591 * @param curl_data The data received from gnudip server which should contain salt, time and sign.
1593 void Logger::print_could_not_parse_received_data(const string& curl_data) const
1596 if ( (level <= Loglevel) )
1599 msg << "Could not parse salt, time and sign from initial gnudip server reply: " << curl_data << endl;
1600 log_error(msg.str());
1606 * Gnudip salt, time and sign could not be got from map
1608 void Logger::print_could_not_get_initial_gnudip_data() const
1611 if ( (level <= Loglevel) )
1613 log_error("Could not get salt, time and sign from map.");
1620 * Gnudip protocol requires explicit declaration of a servername.
1622 void Logger::print_gnudip_requires_servername()
1625 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1627 log_warning("Gnudip requires explicit definition of servername via config!",level);
1633 * An exception occured while computing the md5 sum.
1634 * @param what The exception occured.
1636 void Logger::print_exception_md5_sum(const string& what) const
1639 if ( level <= Loglevel )
1642 msg << "An exception occured while computing a md5 sum: " << what << endl;
1643 log_error(msg.str());
1649 * A network exception occured.
1650 * @param what The exception occured.
1652 void Logger::print_network_error(const string& what) const
1655 if ( level <= Loglevel )
1658 msg << "A network exception occured: " << what << endl;
1659 log_error(msg.str());
1665 * An undefined protocol error occured.
1666 * @param protocol The protocol
1667 * @param error The error
1669 void Logger::print_undefined_protocol_error(const string& protocol, const string& error) const
1672 if ( level <= Loglevel )
1675 msg << "An undefined protocol error occured. Protocol: " << protocol << " Error: " << error << endl;
1676 log_error(msg.str());
1681 * Error while trying to log through external program.
1682 * @param external_prog The external program called.
1684 void Logger::print_error_external_logging(const string& external_prog) const
1687 if ( level <= Loglevel )
1690 msg << "Error while trying to log through external program: " << external_prog << endl;
1691 log_notice(msg.str());
1697 * Error while parsing config file.
1698 * @param error Error occured.
1699 * @param config_file Config file.
1701 void Logger::print_error_parsing_config_file(const string& filename, const string& error) const
1704 if ( level <= Loglevel )
1707 msg << "Error while parsing config file: " << filename << " Error: " << error << endl;
1708 log_notice(msg.str());
1714 * Error while parsing cmd option
1715 * @param error Error
1717 void Logger::print_error_parsing_cmd(const string& error) const
1720 if ( level <= Loglevel )
1723 msg << "Error while parsing cmd options: " << error << endl;
1724 log_error(msg.str());
1730 * The webcheck interval was exceeded, so webcheck not allowed.
1731 * @param last_webcheck Time of last webcheck.
1732 * @param webcheck_interval Webcheck interval time in seconds.
1733 * @param current_time Current system time.
1735 void Logger::print_webcheck_exceed_interval( const time_t last_webcheck, const int webcheck_interval, const time_t current_time ) const
1738 if ( level <= Loglevel )
1741 msg << "Skipped webcheck in idle period (" << (last_webcheck+webcheck_interval)-current_time << " more seconds";
1742 msg << ", last_webcheck: " << last_webcheck << ", webcheck_interval(sec): " << webcheck_interval << ")" << endl;
1743 log_notice(msg.str());
1749 * Checking if hosts needs update.
1750 * @param hostname Hostname
1751 * @param current_time Current time
1752 * @param lastupdated Last updated
1754 void Logger::print_check_service_update(const string& hostname, const time_t current_time, const time_t lastupdated) const
1757 if ( level <= Loglevel )
1760 msg << "Checking if host: " << hostname << " needs update. Current time: " << current_time << " Last updated: " << lastupdated << endl;
1761 log_notice(msg.str());
1768 * @param hostname Hostname
1769 * @param ip_dns_recheck DNS recheck IP
1770 * @param ip_last_update IP set in last update
1771 * @param ip_host Hosts' IP
1773 void Logger::print_cached_dns_entry(const string& hostname, const string& ip_dns_recheck, const string& ip_last_update, const string& ip_host) const
1776 if ( level <= Loglevel )
1779 msg << "Cached DNS record for host <" << hostname << "> : " << ip_dns_recheck << " Last updated IP: " << ip_last_update << " Hosts IP: " << ip_host << endl;
1780 log_notice(msg.str());
1787 * @param hostname Hostname
1788 * @param ip_dns_recheck Cached DNS entry
1789 * @param ip_last_update IP set in last update
1790 * @param ip_host Hosts IP
1791 * @param lastupdated Lastupdated
1793 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{
1795 if ( level <= Loglevel )
1798 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;
1799 log_notice(msg.str());
1806 * @param hostname Hostname
1807 * @param ip_dns_recheck Cached DNS entry
1808 * @param ip_last_update IP set in last update
1809 * @param ip_host Hosts IP
1810 * @param lastupdated Last updated
1811 * @param dns_cache_ttl DNS cache ttl
1812 * @param current_time Current time
1814 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
1817 if ( level <= Loglevel )
1820 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;
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
1834 * @param dns_cache_ttl DNS cache ttl
1835 * @param current_time Current time
1837 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
1841 if (override_log_level)
1844 if ( level <= Loglevel )
1847 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;
1848 log_notice(msg.str());
1855 * @param override_log_level Override log level with zero if true
1856 * @param hostname Hostname
1857 * @param ip_dns_recheck Cached DNS entry
1858 * @param ip_last_update IP set in last update
1859 * @param ip_host Hosts IP
1860 * @param lastupdated Last updated
1862 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
1866 if (override_log_level)
1869 if ( level <= Loglevel )
1872 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;
1873 log_notice(msg.str());
1879 * Error while trying to get local wan interface IP address through getifaddrs.
1880 * @param error The system call which raised the error.
1881 * @param error The error set by getifaddrs.
1883 void Logger::print_error_getting_local_wan_ip(const std::string& system_call, const std::string& error) const
1886 if ( level <= Loglevel )
1889 msg << "Error while trying to get local wan interface IP address through '" << system_call << "': " << error << endl;
1890 log_error(msg.str());
1896 * Could not get IP address of local wan interface.
1897 * @param override_log_level Override log level with zero if true
1899 void Logger::print_no_wan_ip(bool override_log_level) const
1903 if (override_log_level)
1906 if ( level <= Loglevel )
1909 msg << "Could not get IP address of local wan interface. Will retry later." << endl;
1910 log_error(msg.str());
1915 * Print extenral WAN IP
1916 * @param override_log_level Override log level with zero if true
1917 * @param ip External WAN IP
1919 void Logger::print_external_wan_ip(bool override_log_level, const std::string &ip) const
1923 if (override_log_level)
1926 if ( level <= Loglevel )
1929 msg << "Determined WAN interface IP: " << ip << endl;
1930 log_error(msg.str());
1935 * Could not resolve current IP for DNS name
1936 * @param override_log_level Override log level with zero if true
1938 void Logger::print_dns_lookup_failed(bool override_log_level, const std::string &hostname) const
1942 if (override_log_level)
1945 if ( level <= Loglevel )
1948 msg << "Could not resolve IP address for host " << hostname << ". Will retry later." << endl;
1949 log_error(msg.str());
1955 * Invalid service config was detected.
1957 void Logger::print_invalid_service_config() const
1960 if ( level <= Loglevel )
1962 log_error("Ignoring invalid service. Please check.\n");
1968 * Print simple message.
1970 void Logger::print_msg( const string& msg ) const
1973 if ( level <= Loglevel )
1981 * Print the last updates.
1982 * @param ip_host Actual host IP.
1983 * @param max_equal_updates_in_succession Maximal number of updates for the same IP in succession.
1984 * @param last_updates Map with the last updates in it.
1985 * @param hostname The service hostname.
1987 void Logger::print_last_updates( const std::string& ip_host, const time_t current_time, const int update_interval, const int max_updates_within_interval, const int max_equal_updates_in_succession, const std::map<time_t,std::string>& last_updates, const std::string& servicename ) const
1991 if ( level <= Loglevel )
1994 msg << "Last updates for " << servicename << " : ";
1998 for ( std::map<time_t,std::string>::const_reverse_iterator r_iter = last_updates.rbegin(); (r_iter != last_updates.rend()) && (i < max_equal_updates_in_succession); r_iter++ )
2000 msg << r_iter->first << "->" << r_iter->second;
2004 msg << ". Actual internet IP: " << ip_host
2005 << " Current time: " << current_time
2006 << " Update interval: " << update_interval
2007 << " Max updates within interval: " << max_updates_within_interval
2008 << " Max equal updates in succession: " << max_equal_updates_in_succession << endl;
2010 log_notice(msg.str());
2016 * Print the burnt IP.
2017 * @param ip_host Actual host IP which is burnt.
2018 * @param hostname The service hostname.
2020 void Logger::print_ip_burnt( const std::string& ip_host, const std::string& hostname ) const
2023 if ( level <= Loglevel )
2026 msg << "IP address " << ip_host << " was updated too often in succession, host " << hostname << " blocked until IP address will be different." << endl;
2027 log_error(msg.str());