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(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 Loglevel.
162 * @param _loglevel Value to set Loglevel to.
164 void Logger::set_loglevel(const int _loglevel)
166 Loglevel = _loglevel;
171 * Getter for member Loglevel.
174 int Logger::get_loglevel() const
181 * Setter for member Syslog.
182 * @param _syslog Wether to log through syslog or not.
184 void Logger::set_syslog(const bool _syslog)
191 * Getter for member Syslog.
192 * @return True if logging through syslog is enabled, false otherwise.
194 bool Logger::get_syslog() const
201 * Initialize the logging facility.
203 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 )
205 Loglevel = _loglevel;
207 ExternalWarningLog = _external_error_log;
208 ExternalWarningLevel = _external_error_level;
209 ExternalLogOnlyOnce = _external_log_only_once;
212 openlog("bpdyndnsd",LOG_PID,LOG_DAEMON);
217 * Prints out the usage to the command line.
219 void Logger::print_usage(const Options_descriptionPtr opt_desc) const
222 if ( level <= Loglevel )
225 msg << "Usage: bpdyndnsd [Command line options]" << "\n" << endl;
226 msg << *opt_desc << endl;
227 log_notice(msg.str());
233 * Prints out the programm name and the given version string on stdout.
234 * @param version Version string.
236 void Logger::print_version() const
239 if ( level <= Loglevel )
242 msg << "Bullet proof dynamic dns daemon.\nbpdyndnsd " << VERSION << "." << REVISION << "." << RELEASE << endl;
243 log_notice(msg.str());
249 * Prints out the successful parsing of the command line options.
251 void Logger::print_cmd_parsed() const
254 if ( level <= Loglevel )
256 log_notice("Command line options successfully parsed.");
261 void Logger::print_conf_files_parsed() const
264 if ( level <= Loglevel )
266 log_notice("Config files successfully parsed.");
272 * Prints out the successful parsing of the config files.
273 * @param config_path The specified config path.
275 void Logger::print_conf_loaded(const string& config_path) const
278 if ( level <= Loglevel )
281 msg << "Config files successfully loaded in: " << config_path << endl;
282 log_notice(msg.str());
288 * Prints out the unsuccessful parsing of the config files.
289 * @param config_path The specified config path.
291 void Logger::print_conf_not_loaded(const string& config_path) const
294 if ( level <= Loglevel )
297 msg << "Config files couldn't be loaded in: " << config_path << endl;
298 log_error(msg.str());
304 * Prints out the unsuccessful parsing of the config files.
306 void Logger::print_conf_reload_failed_exit() const
309 if ( level <= Loglevel )
310 log_error("Config files couldn't be reloaded. Exiting.");
315 * A file could not be opened for reading.
316 * @param filename The filename.
318 void Logger::print_error_opening_r(const string& filename) const
321 if ( level <= Loglevel )
324 msg << "Error opening file for reading: " << filename << endl;
325 log_error(msg.str());
331 * A file could not be opened for writing.
332 * @param filename The filename.
334 void Logger::print_error_opening_rw(const string& filename) const
337 if ( level <= Loglevel )
340 msg << "Error opening file for writing: " << filename << endl;
341 log_error(msg.str());
347 * Desctructor of specified class was called.
348 * @param _class Name of the class.
350 void Logger::print_destructor_call(const string& _class) const
353 if ( level <= Loglevel )
356 msg << "Destructor call: " << _class << endl;
357 log_notice(msg.str());
363 * Constructor of specified class was called.
364 * @param _class Name of the class.
366 void Logger::print_constructor_call(const string& _class) const
369 if ( level <= Loglevel )
372 msg << "Constructor call: " << _class << endl;
373 log_notice(msg.str());
379 * Update method for specified service was called.
380 * @param service The service for which is the update running.
382 void Logger::print_update_service(const string& service) const
385 if ( level <= Loglevel )
388 msg << "Running update for service: " << service << endl;
389 log_notice(msg.str());
395 * An unknown option on the command line was detected.
396 * @param unknown_option The unknown option.
398 void Logger::print_unknown_cmd_option(const string& unknown_option) const
401 if ( level <= Loglevel )
404 msg << "Unknown option on command line detected: " << unknown_option << endl;
405 log_error(msg.str());
411 * An unknown protocol was specified.
412 * @param protocol The unknown protocol.
414 void Logger::print_unknown_protocol(const string& protocol) const
417 if ( level <= Loglevel )
420 msg << "Unknown protocol defined: " << protocol << endl;
421 log_error(msg.str());
427 * Loading a service config file.
428 * @param filename The service config file.
430 void Logger::print_load_service_conf(const string& filename) const
433 if ( level <= Loglevel )
436 msg << "Loading service config file: " << filename << endl;
437 log_notice(msg.str());
443 * Loading the main config file.
444 * @param filename The main config file.
446 void Logger::print_load_main_conf(const string& filename) const
449 if ( level <= Loglevel )
452 msg << "Loading main config file: " << filename << endl;
453 log_notice(msg.str());
459 * There is an unknown option in a service config file.
460 * @param unknown_option The unknown option.
462 void Logger::print_unknown_service_conf_option(const string& service_conf_file, const string& unknown_option) const
465 if ( level <= Loglevel )
468 msg << "Unknown option in service config file detected: " << service_conf_file << " Unknown option: " << unknown_option << endl;
469 log_error(msg.str());
475 * There is an unknown option in the main config file.
476 * @param unknown_option The unknown option.
478 void Logger::print_unknown_main_conf_option(const string& unknown_option) const
481 if ( level <= Loglevel )
484 msg << "Unknown option in main config file detected: " << unknown_option << endl;
485 log_error(msg.str());
491 * The defined config path doesn't exist or is not a directory.
492 * @param config_path The defined config path.
494 void Logger::print_error_config_path(const string& config_path) const
497 if ( level <= Loglevel )
500 msg << "Config path doesn't exists or is not a diretory: " << config_path << endl;
501 log_error(msg.str());
507 * There is a missing command line option to initialize a service object.
509 void Logger::print_missing_cmd_service_option() const
512 if ( level <= Loglevel )
514 log_error("Missing option to initialize service. Protocol, host, login and password must be specified.");
520 * Missing option in service config file.
521 * @param service_conf_file Service config file
523 void Logger::print_missing_service_conf_option(const string& service_conf_file) const
526 if ( level <= Loglevel )
529 msg << "Missing option in service config file " << service_conf_file << " to initialize service. Protocol, host, login and password must be specified." << endl;
530 log_error(msg.str());
536 * Process running as daemon.
537 * @param pid The pid of the daemon.
539 void Logger::print_runnig_as_daemon(const int pid) const
542 if ( level <= Loglevel )
545 msg << "Running as daemon: " << pid << endl;
546 log_notice(msg.str());
552 * Prints out the daemon mode.
553 * @param daemon_mode The daemon mode.
555 void Logger::print_daemon_mode(const bool daemon_mode) const
558 if ( level <= Loglevel )
560 string mode = "disabled";
561 if (daemon_mode == true)
564 msg << "Daemon mode is " << mode << "." << endl;
565 log_notice(msg.str());
571 * There was an error while trying to fork.
573 void Logger::print_error_fork() const
576 if ( level <= Loglevel )
578 log_error("Error while trying to fork.");
584 * A pid in the pidfile was found.
585 * @param pid The pid found in the pidfile.
587 void Logger::print_pid_found(const int pid) const
590 if ( level <= Loglevel )
593 msg << "Pidfile found: " << pid << ". Checking if process is still runnig..." << endl;
594 log_notice(msg.str());
600 * Another process is already running.
601 * @param pid The pid of the other process.
603 void Logger::print_process_already_running(const int pid) const
606 if ( level <= Loglevel )
609 msg << "Another bpdyndnsd process with PID " << pid << " is already running!" << endl;
610 log_error(msg.str());
618 void Logger::print_caught_sigterm() const
621 if ( level <= Loglevel )
623 log_notice("Caught SIGTERM. Exiting...");
631 void Logger::print_caught_siguser1() const
634 if ( level <= Loglevel )
636 log_notice("Caught SIGUSR1. Switching to offline mode...");
644 void Logger::print_caught_sighup() const
647 if ( level <= Loglevel )
649 log_notice("Caught SIGHUP. Reloading config...");
657 void Logger::print_caught_siguser2() const
660 if ( level <= Loglevel )
662 log_notice("Caught SIGUSR2. Switching to online mode...");
670 void Logger::print_caught_sigrtmin() const
673 if ( level <= Loglevel )
675 log_notice("Caught SIGRTMIN. Switching to online mode with webcheck enabled...");
683 void Logger::print_caught_sigrtmax(int new_loglevel) const
686 if ( level <= Loglevel )
689 msg << "Caught SIGRTMAX. Increasing log level to " << new_loglevel << endl;
690 log_error(msg.str());
696 * Error while setting signal handler.
698 void Logger::print_error_setting_signal(const string& signal) const
701 if ( level <= Loglevel )
704 msg << "Error while setting signal handler for: " << signal << endl;
705 log_error(msg.str());
711 * Error while setting signal handler.
713 void Logger::print_init_log_facility() const
716 if ( level <= Loglevel )
719 msg << "Initialized logging facility." << " Loglevel: " << Loglevel << " Syslog: " << Syslog << " ExternalLogOnlyOnce: " << ExternalLogOnlyOnce << endl;
720 log_notice(msg.str());
726 * Be verbose. Currently we are in offline mode.
728 void Logger::print_offline_mode() const
731 if ( level <= Loglevel )
733 log_notice("Offline mode...");
739 * Objects successfully serialized.
741 void Logger::print_serialized_objects_success() const
744 if ( level <= Loglevel )
746 log_notice("Serialized objects successfully.");
752 * Objects successfully de-serialized.
754 void Logger::print_deserialized_objects_success() const
757 if ( level <= Loglevel )
759 log_notice("De-serialized objects successfully.");
765 * Prints out the content of a service object.
766 * @param message Message to be added on output first.
767 * @param protocol Service's protocol.
768 * @param hostname Service's hostname.
769 * @param login Service's login.
770 * @param password Service's password.
771 * @param actual_ip Service's actual_ip.
772 * @param lastupdated Service's lastupdated.
774 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
777 if ( level <= Loglevel )
780 msg << message << endl;
781 msg << "\t" << "Protocol: " << protocol << endl;
782 msg << "\t" << "Hostname: " << hostname << endl;
783 msg << "\t" << "Login: " << login << endl;
784 msg << "\t" << "Password: " << password << endl;
785 msg << "\t" << "Update Interval: " << update_interval << endl;
786 msg << "\t" << "Max Updates: " << max_updates_within_interval << endl;
787 msg << "\t" << "DNS Cache TTL: " << dns_cache_ttl << endl;
788 msg << "\t" << "Actual_IP: " << actual_ip << endl;
789 BOOST_FOREACH( time_t update_time, lastupdated)
791 msg << "\t" << "Lastupdated: " << update_time << endl;
793 log_notice(msg.str());
799 * Caught exception while serialize.
800 * @param exception Exception message.
802 void Logger::print_exception_serialize(const string& errMsg) const
805 if ( level <= Loglevel )
808 msg << "Error while trying to serialize Serviceholder object: " << errMsg << endl;
809 log_error(msg.str());
815 * Caught exception while de-serialize.
816 * @param exception Exception message.
818 void Logger::print_exception_deserialize(const string& errMsg) const
821 if ( level <= Loglevel )
824 msg << "Error while trying to de-serialize Serviceholder object: " << errMsg << endl;
825 log_error(msg.str());
831 * Child couldn't be killed by parent.
832 * @param pid Pid of the child.
834 void Logger::print_error_kill_child(const int pid) const
837 if ( level <= Loglevel )
840 msg << "Could not kill child process with PID: " << pid << endl;
841 log_error(msg.str());
847 * Child was killed by parent because of error.
848 * @param pid The pid (child) killed.
850 void Logger::print_child_killed(const int pid) const
853 if ( level <= Loglevel )
856 msg << "Killed child process with PID: " << pid << endl;
857 log_notice(msg.str());
863 * There is no object file.
864 * @param object_file The object file.
866 void Logger::print_no_object_file(const string& object_file)
869 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
872 msg << "There is no object file: " << object_file << ". Continue without recovering state from old services!" << endl;
873 log_warning(msg.str(),level);
879 * Prints out the given hostname
880 * @param hostname Hostname as string.
882 void Logger::print_hostname(const string& hostname) const
885 if ( level <= Loglevel )
888 msg << "Detected following hostname of localhost: " << hostname << endl;
889 log_notice(msg.str());
895 * Prints out the detected own ipv4 address
896 * @param ip_addr String representation of the detected ip.
898 void Logger::print_own_ipv4(const string& ip_addr_v4, const string& hostname) const
901 if ( level <= Loglevel )
904 msg << "Detected following IPv4-Address of host: " << hostname << " : " << ip_addr_v4 << endl;
905 log_notice(msg.str());
911 * Prints out the detected own ipv5 address
912 * @param ip_addr String representation of the detected ip.
914 void Logger::print_own_ipv6(const string& ip_addr_v6, const string& hostname) const
917 if ( level <= Loglevel )
920 msg << "Detected following IPv6-Address of host: " << hostname << " : " << ip_addr_v6 << endl;
921 log_notice(msg.str());
927 * Exception while trying to resolve hostname to ip.
928 * @param exception The exception caught.
929 * @param hostname The hostname.
931 void Logger::print_error_hostname_to_ip(const string& errMsg, const string& hostname) const
934 if ( level <= Loglevel )
937 msg << "Could not resolve the hostname: " << hostname << " to an IP-Address: " << errMsg << endl;
938 log_error(msg.str());
944 * The update of the given service was successful.
945 * @param service The service.
947 void Logger::print_update_service_successful(const string& service) const
950 if ( level <= Loglevel )
953 msg << "Updated service successful: " << service << endl;
954 log_notice(msg.str());
960 * No ip could be determined through webcheck
962 void Logger::print_webcheck_no_ip()
965 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
967 log_warning("IP-Address of this host could not be determined through any configured webcheck url.",level);
973 * Connection problem while trying to get ip through webcheck url
974 * @param curl_err_buff Curl error message
977 void Logger::print_webcheck_url_connection_problem(const char * curl_err_buff, const string& url)
980 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
983 msg << "There was a problem while trying to connect to following URL: " << url << " CURL error: " << curl_err_buff << endl;
984 log_warning(msg.str(),level);
990 * Prints out curl error.
991 * @param curl_err_buff Curl error message.
994 void Logger::print_webcheck_error(const char * curl_err_buff, const string& url) const
997 if ( level <= Loglevel )
1000 msg << "There was an error while trying to connect to following URL: " << url << " CURL error: " << curl_err_buff << endl;
1001 log_error(msg.str());
1007 * Prints out the received data through curl.
1008 * @param curl_data Data string
1010 void Logger::print_received_curl_data(const string& curl_data) const
1013 if ( level <= Loglevel )
1016 msg << "Received CURL data: " << curl_data << endl;
1017 log_notice(msg.str());
1023 * IP was foudn through regex
1024 * @param ip The IP found.
1026 void Logger::print_regex_found_ip(const string& ip) const
1029 if ( level <= Loglevel )
1032 msg << "Found IP-Address via regex: " << ip << endl;
1033 log_notice(msg.str());
1039 * No IP was found through regex.
1040 * @param data The data string which should contain a valid IP.s
1042 void Logger::print_regex_ip_not_found(const string& data)
1045 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1048 msg << "Could not extract an IP-Address via regex from following data:\n" << data << endl;
1049 log_warning(msg.str(),level);
1055 * Detected multiple occurrences of the same option.
1056 * @param message Error message.
1058 void Logger::print_multiple_cmd_option(const string& message) const
1061 if ( level <= Loglevel )
1064 msg << "The same option is only allowed once: " << message << endl;
1065 log_error(msg.str());
1071 * An update would exceed the update interval. Prints out a warning message.
1072 * @param current_time Current time.
1073 * @param old_time Time of update #MaxUpdatesWithinInterval ago.
1074 * @param MaxUpdatesWithinInterval Number of allowed updates in one update interval.
1075 * @param service The service which exceeds update interval.
1077 void Logger::print_update_not_allowed(const time_t current_time, const time_t old_time, const int MaxUpdatesWithinInterval, const string& service)
1080 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1083 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;
1084 log_warning(msg.str(),level);
1090 * Failure while running update for service.
1091 * @param service Services' name.
1093 void Logger::print_update_service_failure(const string& service)
1096 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1099 msg << "Could not update service: " << service << endl;
1100 log_warning(msg.str(),level);
1107 void Logger::print_started() const
1110 if ( level <= Loglevel )
1112 log_notice("Started");
1120 void Logger::print_starting_shutdown() const
1123 if ( level <= Loglevel )
1125 log_notice("Shutting down ...");
1133 void Logger::print_shutdown_succeeded() const
1136 if ( level <= Loglevel )
1138 log_notice("Shutting down complete ...");
1144 * Shutdown parent succeeded
1146 void Logger::print_shutdown_parent_succeeded() const
1149 if ( level <= Loglevel )
1151 log_notice("Shutting down parent process completed ...");
1157 * Starting shutdown parent
1159 void Logger::print_starting_shutdown_parent() const
1162 if ( level <= Loglevel )
1164 log_notice("Shutting down parent process ...");
1170 * DNS cache record timeout
1171 * @param hostname Hostname
1172 * @param lastupdated Lastupdated
1173 * @param dns_cache_ttl DNS cache TTL
1174 * @param current_time Current time
1176 void Logger::print_recheck_dns_entry(const string& hostname, const int lastupdated, const int dns_cache_ttl, const int current_time) const
1179 if ( level <= Loglevel )
1182 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;
1183 log_notice(msg.str());
1189 * Missing proxy option on command line.
1191 void Logger::print_missing_cmd_proxy_option() const
1194 if ( level <= Loglevel )
1196 log_error("Missing option to initialize proxy. http_proxy and http_proxy_port must be specified.");
1202 * Multiple option in service config file.
1203 * @param service_conf_file Service config file
1204 * @param message Multiple option text
1206 void Logger::print_multiple_service_conf_option(const string& service_conf_file, const string& message) const
1209 if ( level <= Loglevel )
1212 msg << "Multiple occurrences of the same option in service config file detected: " << service_conf_file << " " << message << endl;
1213 log_error(msg.str());
1219 * Multiple option in main config file.
1220 * @param service_conf_file Service config file
1221 * @param message Multiple option text
1223 void Logger::print_multiple_main_conf_option(const string& main_conf_file, const string& message) const
1226 if ( level <= Loglevel )
1229 msg << "Multiple occurrences of the same option in main config file detected: " << main_conf_file << " " << message << endl;
1230 log_error(msg.str());
1236 * Missing proxy option in main config file.
1237 * @param main_conf_filename The concerning config file.
1239 void Logger::print_missing_conf_proxy_option(const string& main_conf_filename) const
1242 if ( level <= Loglevel )
1245 msg << "Missing option to initialize proxy in main config file: " << main_conf_filename << " http_proxy and http_proxy_port must be specified." << endl;
1246 log_error(msg.str());
1252 * There is no domain part in the given hostname
1253 * @param hostname The hostname with no domain part in it.
1255 void Logger::print_no_domain_part(const string& hostname) const
1258 if ( level <= Loglevel )
1261 msg << "There is no domain part in the given hostname: " << hostname << endl;
1262 log_notice(msg.str());
1268 * Service is not initialized properly.
1269 * @param service The service.
1271 void Logger::print_httphelper_not_initialized() const
1274 if ( level <= Loglevel )
1276 log_error("HTTP-Helper is not initialized properly.");
1282 * An curl error occured.
1283 * @param msg The error message
1284 * @param curl_err_code The resulting curl error code
1286 void Logger::print_curl_error_init(const std::string& err_msg, const CURLcode curl_err_code) const
1288 string curl_err = "";
1290 if ( curl_err_code == CURLE_FAILED_INIT )
1291 curl_err = "CURLE_FAILED_INIT";
1293 curl_err = "UNKNOWN";
1296 if ( (level <= Loglevel) )
1299 msg << "Curl error: " << err_msg << " Curl error code: " << curl_err_code << " " << curl_err << endl; /*lint !e641 */
1300 log_error(msg.str());
1306 * An curl error occured.
1307 * @param url The url requested by the curl operation
1308 * @param curl_err_code The resulting curl error code
1310 void Logger::print_curl_error(const string& url, const CURLcode curl_err_code) const
1312 string curl_err = "";
1314 if ( curl_err_code == CURLE_URL_MALFORMAT )
1315 curl_err = "CURLE_URL_MALFORMAT";
1316 else if ( curl_err_code == CURLE_COULDNT_RESOLVE_HOST )
1317 curl_err = "CURLE_COULDNT_RESOLVE_HOST";
1318 else if ( curl_err_code == CURLE_COULDNT_CONNECT )
1319 curl_err = "CURLE_COULDNT_CONNECT";
1321 curl_err = "UNKNOWN";
1324 if ( (level <= Loglevel) )
1327 msg << "Curl error while requesting following url: " << url << " Curl error code: " << curl_err_code << " " << curl_err << endl; /*lint !e641 */
1328 log_error(msg.str());
1334 * An curl error occured.
1335 * @param url The url requested by the curl operation
1336 * @param curl_err_code The resulting curl error code
1337 * @param curl_err_buff The curl error buffer
1339 void Logger::print_curl_error(const string& url, const CURLcode curl_err_code, const char * curl_err_buff) const
1341 string curl_err = "";
1343 if ( curl_err_code == CURLE_URL_MALFORMAT )
1344 curl_err = "CURLE_URL_MALFORMAT";
1345 else if ( curl_err_code == CURLE_COULDNT_RESOLVE_HOST )
1346 curl_err = "CURLE_COULDNT_RESOLVE_HOST";
1347 else if ( curl_err_code == CURLE_COULDNT_CONNECT )
1348 curl_err = "CURLE_COULDNT_CONNECT";
1350 curl_err = "UNKNOWN";
1353 if ( (level <= Loglevel) )
1356 msg << "Curl error while requesting following url: " << url << " Curl error code: " << curl_err_code << " " << curl_err << " " << curl_err_buff << endl; /*lint !e641 */
1357 log_error(msg.str());
1363 * Prints out the data received by curl operation
1364 * @param CurlWritedataBuff
1366 void Logger::print_curl_data(const string& curl_writedata_buff) const
1369 if ( level <= Loglevel )
1372 msg << "Data received by curl: " << curl_writedata_buff << endl;
1373 log_notice(msg.str());
1379 * Not authorized to perform requested update operation
1380 * @param service The requested service.
1381 * @param username Username
1382 * @param password Password
1384 void Logger::print_service_not_authorized(const string& service, const string& username, const string& password) const
1387 if ( level <= Loglevel )
1390 msg << "Not authorized to perform update operation on service: " << service << " Please check username and password: " << username << ":" << password << endl;
1391 log_notice(msg.str());
1397 * Prints out the http status code
1399 * @param output HTTP status code
1401 void Logger::print_http_status_code(const string& url, const long http_code) const
1404 if ( level <= Loglevel )
1407 msg << "Requested URL: " << url << " Received HTTP status code: " << http_code << endl;
1408 log_notice(msg.str());
1414 * Generic failure while trying to update service
1415 * @param url The requested URL
1416 * @param curl_data The received curl_data from the server
1418 void Logger::print_update_failure(const string& url, const string& curl_data) const
1421 if ( (level <= Loglevel) )
1424 msg << "Problem while trying to updating service. Requested URL: " << url << " Error Code from Server: " << curl_data << endl;
1425 log_error(msg.str());
1431 * Generic failure while trying to update service
1432 * @param url The requested URL
1433 * @param http_status_code The received http status code
1435 void Logger::print_update_failure(const string& url, const long http_status_code) const
1438 if ( level <= Loglevel )
1441 msg << "Problem while trying to updating service. Requested URL: " << url << " Error Code from Server: " << http_status_code << endl;
1442 log_error(msg.str());
1447 * Hostname is invalid, contains no or only one domain part.
1448 * @param hostname The full qualified host name.
1450 void Logger::print_invalid_hostname(const string& hostname)
1453 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1456 msg << "The configured hostname: " << hostname << " is invalid. Please add the corresponding domain part." << endl;
1457 log_warning(msg.str(),level);
1463 * An IP in a private range was detected
1464 * @param ip The private IP
1466 void Logger::print_ip_is_local(const string& ip)
1469 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1472 msg << "The detected IP is within a private IP range: " << ip << endl;
1473 log_warning(msg.str(),level);
1479 * Regex is matching in string
1480 * @param regex The regex pattern
1481 * @param matching_string The string
1483 void Logger::print_regex_match(const string& regex, const string& matching_string) const
1486 if ( level <= Loglevel )
1489 msg << "Regex: " << regex << " is matching in: " << matching_string << endl;
1490 log_notice(msg.str());
1496 * Regex is not matching
1497 * @param regex Regex
1498 * @param not_matching_string String
1500 void Logger::print_no_regex_match(const string& regex, const string& not_matching_string)
1503 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1506 msg << "Regex: " << regex << " is not matching in: " << not_matching_string << endl;
1507 log_warning(msg.str(),level);
1513 * Could not parse gnudip initial reply.
1514 * @param curl_data The data received from gnudip server which should contain salt, time and sign.
1516 void Logger::print_could_not_parse_received_data(const string& curl_data) const
1519 if ( (level <= Loglevel) )
1522 msg << "Could not parse salt, time and sign from initial gnudip server reply: " << curl_data << endl;
1523 log_error(msg.str());
1529 * Gnudip salt, time and sign could not be got from map
1531 void Logger::print_could_not_get_initial_gnudip_data() const
1534 if ( (level <= Loglevel) )
1536 log_error("Could not get salt, time and sign from map.");
1543 * Gnudip protocol requires explicit declaration of a servername.
1545 void Logger::print_gnudip_requires_servername()
1548 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1550 log_warning("Gnudip requires explicit definition of servername via config!",level);
1556 * An exception occured while computing the md5 sum.
1557 * @param what The exception occured.
1559 void Logger::print_exception_md5_sum(const string& what) const
1562 if ( level <= Loglevel )
1565 msg << "An exception occured while computing a md5 sum: " << what << endl;
1566 log_error(msg.str());
1572 * An network exception occured.
1573 * @param what The exception occured.
1575 void Logger::print_network_error(const string& what) const
1578 if ( level <= Loglevel )
1581 msg << "An netowrk exception occured: " << what << endl;
1582 log_error(msg.str());
1588 * An undefined protocol error occured.
1589 * @param protocol The protocol
1590 * @param error The error
1592 void Logger::print_undefined_protocol_error(const string& protocol, const string& error) const
1595 if ( level <= Loglevel )
1598 msg << "An undefined protocol error occured. Protocol: " << protocol << " Error: " << error << endl;
1599 log_error(msg.str());
1604 * Error while trying to log through external program.
1605 * @param external_prog The external program called.
1607 void Logger::print_error_external_logging(const string& external_prog) const
1610 if ( level <= Loglevel )
1613 msg << "Error while trying to log through external program: " << external_prog << endl;
1614 log_notice(msg.str());
1620 * Error while parsing config file.
1621 * @param error Error occured.
1622 * @param config_file Config file.
1624 void Logger::print_error_parsing_config_file(const string& filename, const string& error) const
1627 if ( level <= Loglevel )
1630 msg << "Error while parsing config file: " << filename << " Error: " << error << endl;
1631 log_notice(msg.str());
1637 * Error while parsing cmd option
1638 * @param error Error
1640 void Logger::print_error_parsing_cmd(const string& error) const
1643 if ( level <= Loglevel )
1646 msg << "Error while parsing cmd options: " << error << endl;
1647 log_error(msg.str());
1653 * The webcheck interval was exceeded, so webcheck not allowed.
1654 * @param last_webcheck Time of last webcheck.
1655 * @param webcheck_interval Webcheck interval time.
1656 * @param current_time Current system time.
1658 void Logger::print_webcheck_exceed_interval( const time_t last_webcheck, const int webcheck_interval, const time_t current_time ) const
1661 if ( level <= Loglevel )
1664 msg << "Exceeding webcheck interval: LastWebcheck " << last_webcheck << " + WebcheckInterval(sec) " << webcheck_interval << " >= CurrentTime " << current_time << endl;
1665 log_notice(msg.str());
1671 * Checking if hosts needs update.
1672 * @param hostname Hostname
1673 * @param current_time Current time
1674 * @param lastupdated Last updated
1676 void Logger::print_check_service_update(const string& hostname, const time_t current_time, const time_t lastupdated) const
1679 if ( level <= Loglevel )
1682 msg << "Checking if host: " << hostname << " needs update. Current time: " << current_time << " Last updated: " << lastupdated << endl;
1683 log_notice(msg.str());
1690 * @param hostname Hostname
1691 * @param ip_dns_recheck DNS recheck IP
1692 * @param ip_last_update IP set in last update
1693 * @param ip_host Hosts' IP
1695 void Logger::print_cached_dns_entry(const string& hostname, const string& ip_dns_recheck, const string& ip_last_update, const string& ip_host) const
1698 if ( level <= Loglevel )
1701 msg << "Cached DNS record for host <" << hostname << "> : " << ip_dns_recheck << " Last updated IP: " << ip_last_update << " Hosts IP: " << ip_host << endl;
1702 log_notice(msg.str());
1709 * @param hostname Hostname
1710 * @param ip_dns_recheck Cached DNS entry
1711 * @param ip_last_update IP set in last update
1712 * @param ip_host Hosts IP
1713 * @param lastupdated Lastupdated
1715 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{
1717 if ( level <= Loglevel )
1720 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;
1721 log_notice(msg.str());
1728 * @param hostname Hostname
1729 * @param ip_dns_recheck Cached DNS entry
1730 * @param ip_last_update IP set in last update
1731 * @param ip_host Hosts IP
1732 * @param lastupdated Last updated
1733 * @param dns_cache_ttl DNS cache ttl
1734 * @param current_time Current time
1736 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
1739 if ( level <= Loglevel )
1742 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;
1743 log_notice(msg.str());
1750 * @param override_log_level Override log level with zero if true
1751 * @param hostname Hostname
1752 * @param ip_dns_recheck Cached DNS entry
1753 * @param ip_last_update IP set in last update
1754 * @param ip_host Hosts IP
1755 * @param lastupdated Last updated
1756 * @param dns_cache_ttl DNS cache ttl
1757 * @param current_time Current time
1759 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
1763 if (override_log_level)
1766 if ( level <= Loglevel )
1769 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;
1770 log_notice(msg.str());
1777 * @param override_log_level Override log level with zero if true
1778 * @param hostname Hostname
1779 * @param ip_dns_recheck Cached DNS entry
1780 * @param ip_last_update IP set in last update
1781 * @param ip_host Hosts IP
1782 * @param lastupdated Last updated
1784 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
1788 if (override_log_level)
1791 if ( level <= Loglevel )
1794 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;
1795 log_notice(msg.str());
1801 * Error while trying to get local wan interface IP address through getifaddrs.
1802 * @param error The system call which raised the error.
1803 * @param error The error set by getifaddrs.
1805 void Logger::print_error_getting_local_wan_ip(const std::string& system_call, const std::string& error) const
1808 if ( level <= Loglevel )
1811 msg << "Error while trying to get local wan interface IP address through '" << system_call << "': " << error << endl;
1812 log_error(msg.str());
1818 * Could not get IP address of local wan interface.
1819 * @param override_log_level Override log level with zero if true
1821 void Logger::print_no_wan_ip(bool override_log_level) const
1825 if (override_log_level)
1828 if ( level <= Loglevel )
1831 msg << "Could not get IP address of local wan interface. Will retry later." << endl;
1832 log_error(msg.str());
1837 * Print extenral WAN IP
1838 * @param override_log_level Override log level with zero if true
1839 * @param ip External WAN IP
1841 void Logger::print_external_wan_ip(bool override_log_level, const std::string &ip) const
1845 if (override_log_level)
1848 if ( level <= Loglevel )
1851 msg << "Determined WAN interface IP: " << ip << endl;
1852 log_error(msg.str());
1857 * Could not resolve current IP for DNS name
1858 * @param override_log_level Override log level with zero if true
1860 void Logger::print_dns_lookup_failed(bool override_log_level, const std::string &hostname) const
1864 if (override_log_level)
1867 if ( level <= Loglevel )
1870 msg << "Could not resolve IP address for host " << hostname << ". Will retry later." << endl;
1871 log_error(msg.str());
1877 * Invalid service config was detected.
1879 void Logger::print_invalid_service_config() const
1882 if ( level <= Loglevel )
1884 log_error("Ignoring invalid service. Please check.\n");
1890 * Print simple message.
1892 void Logger::print_msg( const string& msg ) const
1895 if ( level <= Loglevel )