Fix 'occurred' typo
[bpdyndnsd] / src / logger.cpp
CommitLineData
254bbf53
BS
1/** @file
2 * @brief Logger class implementation. This class implements the Logging facility.
3 *
4 *
5 *
6 * @copyright Intra2net AG
7 * @license GPLv2
8*/
9
10
4de6a9b8 11#include "logger.hpp"
77e65070 12#include "version_info.h"
254bbf53 13
ca5d6889 14#include <iostream>
88a594e8
BS
15#include <syslog.h>
16#include <sstream>
17
3c0cd271
BS
18#include <boost/foreach.hpp>
19
8bca3c5d
BS
20namespace po = boost::program_options;
21
92beaba3
BS
22typedef boost::shared_ptr<boost::program_options::options_description> Options_descriptionPtr;
23
8bca3c5d 24using namespace std;
254bbf53
BS
25
26/**
27 * Default Constructor
28 */
29Logger::Logger()
8bca3c5d 30 : Loglevel(0)
2e956a36 31 , Syslog(false)
cbbdeb6c 32 , ExternalWarningLevel(0)
e8787e2e 33 , ExternalLogOnlyOnce(false)
04d261b9 34 , LogPasswords(false)
254bbf53 35{
e8787e2e 36 set_log_facility(Loglevel,Syslog,ExternalWarningLog,ExternalWarningLevel,ExternalLogOnlyOnce);
254bbf53
BS
37}
38
39
40/**
41 * Default Destructor
42 */
43Logger::~Logger()
44{
254bbf53
BS
45}
46
47
04d261b9
GE
48
49
254bbf53 50/**
e8787e2e
BS
51 * Decides if a external log message can be send.
52 * @param msg The message to log.
53 */
54bool Logger::is_allowed_to_send( const string& msg ) const
55{
56 if ( (ExternalLogOnlyOnce) && (ExternalSendMessages.find(msg) != ExternalSendMessages.end()) )
57 return false;
58 return true;
59}
60
61
62/**
63 * Clears the external send messages set.
64 */
65void Logger::clear_external_send_messages()
66{
67 ExternalSendMessages.clear();
68}
69
70
71/**
59c8d63c
BS
72 * Decides if Logging through syslog if enabled or through std.
73 * @param msg The message to log.
74 */
ce70569b 75void Logger::log_notice(const string& msg) const
59c8d63c
BS
76{
77 if ( Syslog )
78 syslog(LOG_NOTICE,msg.c_str());
79 else
27baf279 80 cout << msg << endl;;
59c8d63c
BS
81}
82
83
84/**
47e0ee60
BS
85* Escape shell arguments.
86* @param input The input to escape.
87* @return The escaped string ready to use for the shell.
88*/
89string Logger::escape_shellarg(const string &input)
90{
91 string output = "'";
92 string::const_iterator it, it_end = input.end();
93 for (it = input.begin(); it != it_end; it++)
94 {
95 if ( (*it) == '\'')
96 output += "'\\'";
97
98 output += *it;
99 }
100
101 output += "'";
102 return output;
103}
104
105
106/**
59c8d63c
BS
107 * Decides if Logging through syslog if enabled or through std.
108 * @param msg The message to log.
109 */
e8787e2e 110void Logger::log_warning(const string& msg, int level)
59c8d63c
BS
111{
112 if ( Syslog )
113 syslog(LOG_WARNING,msg.c_str());
114 else
27baf279 115 cout << msg << endl;
cbbdeb6c 116
e8787e2e 117 if ( (level <= ExternalWarningLevel) && (!ExternalWarningLog.empty()) && (is_allowed_to_send(msg)) )
cbbdeb6c 118 {
e8787e2e
BS
119 string message = msg;
120 // Remove endline from msg.
121 if (!message.empty() && message[message.length()-1] == '\n')
43f4565e 122 message.erase(message.length()-1); /*lint !e534 */
e8787e2e 123
47e0ee60
BS
124 message = escape_shellarg(message);
125
cbbdeb6c
BS
126 string external = ExternalWarningLog;
127 external.append(" ");
e8787e2e 128 external.append(message);
d77313ea 129 if ( system(external.c_str()) != 0 )
d77313ea 130 print_error_external_logging(external);
e8787e2e 131 else
43f4565e 132 ExternalSendMessages.insert(msg); /*lint !e534 */
cbbdeb6c 133 }
59c8d63c
BS
134}
135
136
137/**
138 * Decides if Logging through syslog if enabled or through std.
139 * @param msg The message to log.
140 */
ce70569b 141void Logger::log_error(const string& msg) const
59c8d63c
BS
142{
143 if ( Syslog )
144 syslog(LOG_ERR,msg.c_str());
145 else
27baf279 146 cerr << msg << endl;
59c8d63c
BS
147}
148
149
150/**
8bca3c5d
BS
151 * Setter for member Loglevel.
152 * @param _loglevel Value to set Loglevel to.
153 */
e8787e2e
BS
154void Logger::set_external_log_only_once( const bool _external_log_only_once )
155{
156 ExternalLogOnlyOnce = _external_log_only_once;
157}
158
159
160/**
04d261b9
GE
161 * Setter for member LogPasswords.
162 * @param _log_passwords If we want to log passwords or not.
163 */
164void Logger::set_log_passwords( const bool _log_passwords )
165{
166 LogPasswords = _log_passwords;
167}
168
169
170/**
e8787e2e
BS
171 * Setter for member Loglevel.
172 * @param _loglevel Value to set Loglevel to.
173 */
8bca3c5d
BS
174void Logger::set_loglevel(const int _loglevel)
175{
176 Loglevel = _loglevel;
177}
178
179
180/**
181 * Getter for member Loglevel.
182 * @return Loglevel.
183 */
b38684ce 184int Logger::get_loglevel() const
8bca3c5d
BS
185{
186 return Loglevel;
187}
188
189
190/**
191 * Setter for member Syslog.
192 * @param _syslog Wether to log through syslog or not.
193 */
194void Logger::set_syslog(const bool _syslog)
195{
196 Syslog = _syslog;
197}
198
199
200/**
201 * Getter for member Syslog.
202 * @return True if logging through syslog is enabled, false otherwise.
203 */
b38684ce 204bool Logger::get_syslog() const
8bca3c5d
BS
205{
206 return Syslog;
207}
208
209
210/**
211 * Initialize the logging facility.
212 */
e8787e2e 213void 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 )
8bca3c5d
BS
214{
215 Loglevel = _loglevel;
216 Syslog = _syslog;
cbbdeb6c
BS
217 ExternalWarningLog = _external_error_log;
218 ExternalWarningLevel = _external_error_level;
e8787e2e 219 ExternalLogOnlyOnce = _external_log_only_once;
8bca3c5d
BS
220
221 if ( Syslog )
222 openlog("bpdyndnsd",LOG_PID,LOG_DAEMON);
223}
224
225
226/**
254bbf53
BS
227 * Prints out the usage to the command line.
228 */
b38684ce 229void Logger::print_usage(const Options_descriptionPtr opt_desc) const
254bbf53 230{
cbbdeb6c 231 int level = 0;
d77313ea 232 if ( level <= Loglevel )
59c8d63c
BS
233 {
234 ostringstream msg;
235 msg << "Usage: bpdyndnsd [Command line options]" << "\n" << endl;
236 msg << *opt_desc << endl;
ce70569b 237 log_notice(msg.str());
59c8d63c 238 }
254bbf53
BS
239}
240
241
242/**
243 * Prints out the programm name and the given version string on stdout.
244 * @param version Version string.
245 */
b38684ce 246void Logger::print_version() const
254bbf53 247{
cbbdeb6c 248 int level = 0;
d77313ea 249 if ( level <= Loglevel )
59c8d63c
BS
250 {
251 ostringstream msg;
77e65070 252 msg << "Bullet proof dynamic dns daemon.\nbpdyndnsd " << MAJOR_VERSION << "." << MINOR_VERSION << endl;
ce70569b 253 log_notice(msg.str());
59c8d63c 254 }
254bbf53
BS
255}
256
257
258/**
259 * Prints out the successful parsing of the command line options.
260 */
b38684ce 261void Logger::print_cmd_parsed() const
254bbf53 262{
cbbdeb6c 263 int level = 1;
d77313ea 264 if ( level <= Loglevel )
59c8d63c 265 {
ce70569b 266 log_notice("Command line options successfully parsed.");
59c8d63c 267 }
254bbf53
BS
268}
269
270
b38684ce 271void Logger::print_conf_files_parsed() const
667c672c 272{
cbbdeb6c 273 int level = 1;
d77313ea 274 if ( level <= Loglevel )
667c672c 275 {
ce70569b 276 log_notice("Config files successfully parsed.");
667c672c
BS
277 }
278}
279
280
254bbf53
BS
281/**
282 * Prints out the successful parsing of the config files.
daf2ea82 283 * @param config_path The specified config path.
254bbf53 284 */
b38684ce 285void Logger::print_conf_loaded(const string& config_path) const
254bbf53 286{
cbbdeb6c 287 int level = 1;
d77313ea 288 if ( level <= Loglevel )
59c8d63c
BS
289 {
290 ostringstream msg;
291 msg << "Config files successfully loaded in: " << config_path << endl;
ce70569b 292 log_notice(msg.str());
59c8d63c 293 }
254bbf53
BS
294}
295
296
297/**
daf2ea82
BS
298 * Prints out the unsuccessful parsing of the config files.
299 * @param config_path The specified config path.
254bbf53 300 */
b38684ce 301void Logger::print_conf_not_loaded(const string& config_path) const
254bbf53 302{
cbbdeb6c 303 int level = 0;
d77313ea 304 if ( level <= Loglevel )
59c8d63c
BS
305 {
306 ostringstream msg;
307 msg << "Config files couldn't be loaded in: " << config_path << endl;
ce70569b 308 log_error(msg.str());
59c8d63c 309 }
254bbf53
BS
310}
311
312
313/**
5b3f3f54
TJ
314 * Prints out the unsuccessful parsing of the config files.
315 */
316void Logger::print_conf_reload_failed_exit() const
317{
318 int level = 0;
319 if ( level <= Loglevel )
320 log_error("Config files couldn't be reloaded. Exiting.");
321}
322
323
324/**
daf2ea82
BS
325 * A file could not be opened for reading.
326 * @param filename The filename.
254bbf53 327 */
b38684ce 328void Logger::print_error_opening_r(const string& filename) const
254bbf53 329{
cbbdeb6c 330 int level = 0;
d77313ea 331 if ( level <= Loglevel )
59c8d63c
BS
332 {
333 ostringstream msg;
334 msg << "Error opening file for reading: " << filename << endl;
ce70569b 335 log_error(msg.str());
59c8d63c 336 }
254bbf53
BS
337}
338
339
340/**
667c672c
BS
341 * A file could not be opened for writing.
342 * @param filename The filename.
343 */
b38684ce 344void Logger::print_error_opening_rw(const string& filename) const
667c672c 345{
cbbdeb6c 346 int level = 0;
d77313ea 347 if ( level <= Loglevel )
667c672c
BS
348 {
349 ostringstream msg;
350 msg << "Error opening file for writing: " << filename << endl;
ce70569b 351 log_error(msg.str());
667c672c
BS
352 }
353}
354
355
356/**
254bbf53
BS
357 * Desctructor of specified class was called.
358 * @param _class Name of the class.
359 */
b38684ce 360void Logger::print_destructor_call(const string& _class) const
254bbf53 361{
cbbdeb6c 362 int level = 1;
d77313ea 363 if ( level <= Loglevel )
59c8d63c
BS
364 {
365 ostringstream msg;
366 msg << "Destructor call: " << _class << endl;
ce70569b 367 log_notice(msg.str());
59c8d63c 368 }
254bbf53
BS
369}
370
371
372/**
373 * Constructor of specified class was called.
374 * @param _class Name of the class.
375 */
b38684ce 376void Logger::print_constructor_call(const string& _class) const
254bbf53 377{
cbbdeb6c 378 int level = 1;
d77313ea 379 if ( level <= Loglevel )
59c8d63c
BS
380 {
381 ostringstream msg;
382 msg << "Constructor call: " << _class << endl;
ce70569b 383 log_notice(msg.str());
59c8d63c 384 }
254bbf53
BS
385}
386
387
388/**
389 * Update method for specified service was called.
0f0908e1 390 * @param service The service for which the update is running.
254bbf53 391 */
b38684ce 392void Logger::print_update_service(const string& service) const
254bbf53 393{
cbbdeb6c 394 int level = 0;
d77313ea 395 if ( level <= Loglevel )
59c8d63c
BS
396 {
397 ostringstream msg;
398 msg << "Running update for service: " << service << endl;
ce70569b 399 log_notice(msg.str());
59c8d63c 400 }
254bbf53
BS
401}
402
0f0908e1
TJ
403/**
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
407 */
408void Logger::print_update_service_is_blocked(const std::string& service, int remaining_seconds) const
409{
410 int level = 1;
411 if ( level <= Loglevel )
412 {
413 ostringstream msg;
414 msg << "Service " << service << " is blocked for " << remaining_seconds << " seconds because of too many errors" << endl;
415 log_notice(msg.str());
416 }
417}
418
419
420/**
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
424 */
425void Logger::print_block_service(const std::string& service, int block_seconds) const
426{
427 int level = 0;
428 if ( level <= Loglevel )
429 {
430 ostringstream msg;
431 msg << "Service " << service << " will be blocked for " << block_seconds << " seconds because of too many errors" << endl;
432 log_notice(msg.str());
433 }
434}
435
254bbf53 436
daf2ea82
BS
437/**
438 * An unknown option on the command line was detected.
439 * @param unknown_option The unknown option.
440 */
b38684ce 441void Logger::print_unknown_cmd_option(const string& unknown_option) const
254bbf53 442{
cbbdeb6c 443 int level = 0;
d77313ea 444 if ( level <= Loglevel )
59c8d63c
BS
445 {
446 ostringstream msg;
447 msg << "Unknown option on command line detected: " << unknown_option << endl;
ce70569b 448 log_error(msg.str());
59c8d63c 449 }
254bbf53
BS
450}
451
452
daf2ea82
BS
453/**
454 * An unknown protocol was specified.
455 * @param protocol The unknown protocol.
456 */
b38684ce 457void Logger::print_unknown_protocol(const string& protocol) const
254bbf53 458{
cbbdeb6c 459 int level = 0;
d77313ea 460 if ( level <= Loglevel )
59c8d63c
BS
461 {
462 ostringstream msg;
463 msg << "Unknown protocol defined: " << protocol << endl;
ce70569b 464 log_error(msg.str());
59c8d63c 465 }
254bbf53
BS
466}
467
468
daf2ea82
BS
469/**
470 * Loading a service config file.
471 * @param filename The service config file.
472 */
b38684ce 473void Logger::print_load_service_conf(const string& filename) const
254bbf53 474{
cbbdeb6c 475 int level = 1;
d77313ea 476 if ( level <= Loglevel )
59c8d63c
BS
477 {
478 ostringstream msg;
479 msg << "Loading service config file: " << filename << endl;
ce70569b 480 log_notice(msg.str());
59c8d63c 481 }
254bbf53
BS
482}
483
484
daf2ea82
BS
485/**
486 * Loading the main config file.
487 * @param filename The main config file.
488 */
b38684ce 489void Logger::print_load_main_conf(const string& filename) const
254bbf53 490{
cbbdeb6c 491 int level = 1;
d77313ea 492 if ( level <= Loglevel )
59c8d63c
BS
493 {
494 ostringstream msg;
495 msg << "Loading main config file: " << filename << endl;
ce70569b 496 log_notice(msg.str());
59c8d63c 497 }
254bbf53
BS
498}
499
500
daf2ea82
BS
501/**
502 * There is an unknown option in a service config file.
503 * @param unknown_option The unknown option.
504 */
8a00a649 505void Logger::print_unknown_service_conf_option(const string& service_conf_file, const string& unknown_option) const
254bbf53 506{
cbbdeb6c 507 int level = 0;
d77313ea 508 if ( level <= Loglevel )
59c8d63c
BS
509 {
510 ostringstream msg;
8a00a649 511 msg << "Unknown option in service config file detected: " << service_conf_file << " Unknown option: " << unknown_option << endl;
ce70569b 512 log_error(msg.str());
59c8d63c 513 }
254bbf53
BS
514}
515
516
daf2ea82
BS
517/**
518 * There is an unknown option in the main config file.
519 * @param unknown_option The unknown option.
520 */
b38684ce 521void Logger::print_unknown_main_conf_option(const string& unknown_option) const
254bbf53 522{
cbbdeb6c 523 int level = 0;
d77313ea 524 if ( level <= Loglevel )
59c8d63c
BS
525 {
526 ostringstream msg;
527 msg << "Unknown option in main config file detected: " << unknown_option << endl;
ce70569b 528 log_error(msg.str());
59c8d63c 529 }
254bbf53
BS
530}
531
532
daf2ea82
BS
533/**
534 * The defined config path doesn't exist or is not a directory.
535 * @param config_path The defined config path.
536 */
b38684ce 537void Logger::print_error_config_path(const string& config_path) const
254bbf53 538{
cbbdeb6c 539 int level = 0;
d77313ea 540 if ( level <= Loglevel )
59c8d63c
BS
541 {
542 ostringstream msg;
543 msg << "Config path doesn't exists or is not a diretory: " << config_path << endl;
ce70569b 544 log_error(msg.str());
59c8d63c 545 }
254bbf53
BS
546}
547
548
daf2ea82
BS
549/**
550 * There is a missing command line option to initialize a service object.
551 */
b38684ce 552void Logger::print_missing_cmd_service_option() const
254bbf53 553{
cbbdeb6c 554 int level = 0;
d77313ea 555 if ( level <= Loglevel )
59c8d63c 556 {
ce70569b 557 log_error("Missing option to initialize service. Protocol, host, login and password must be specified.");
59c8d63c 558 }
254bbf53 559}
388f4ab0
BS
560
561
c5675c01 562/**
8a00a649
BS
563 * Missing option in service config file.
564 * @param service_conf_file Service config file
565 */
566void Logger::print_missing_service_conf_option(const string& service_conf_file) const
567{
cbbdeb6c 568 int level = 0;
d77313ea 569 if ( level <= Loglevel )
8a00a649
BS
570 {
571 ostringstream msg;
572 msg << "Missing option in service config file " << service_conf_file << " to initialize service. Protocol, host, login and password must be specified." << endl;
ce70569b 573 log_error(msg.str());
8a00a649
BS
574 }
575}
576
577
578/**
c5675c01
BS
579 * Process running as daemon.
580 * @param pid The pid of the daemon.
581 */
b38684ce 582void Logger::print_runnig_as_daemon(const int pid) const
388f4ab0 583{
cbbdeb6c 584 int level = 1;
d77313ea 585 if ( level <= Loglevel )
59c8d63c
BS
586 {
587 ostringstream msg;
f3141675 588 msg << "Running as daemon: " << pid << endl;
ce70569b 589 log_notice(msg.str());
59c8d63c 590 }
388f4ab0
BS
591}
592
c5675c01
BS
593
594/**
595 * Prints out the daemon mode.
596 * @param daemon_mode The daemon mode.
597 */
b38684ce 598void Logger::print_daemon_mode(const bool daemon_mode) const
388f4ab0 599{
cbbdeb6c 600 int level = 1;
d77313ea 601 if ( level <= Loglevel )
59c8d63c
BS
602 {
603 string mode = "disabled";
604 if (daemon_mode == true)
605 mode = "enabled";
606 ostringstream msg;
607 msg << "Daemon mode is " << mode << "." << endl;
ce70569b 608 log_notice(msg.str());
59c8d63c 609 }
388f4ab0
BS
610}
611
612
c5675c01
BS
613/**
614 * There was an error while trying to fork.
615 */
b38684ce 616void Logger::print_error_fork() const
388f4ab0 617{
cbbdeb6c 618 int level = 0;
d77313ea 619 if ( level <= Loglevel )
59c8d63c 620 {
ce70569b 621 log_error("Error while trying to fork.");
59c8d63c 622 }
388f4ab0
BS
623}
624
625
c5675c01
BS
626/**
627 * A pid in the pidfile was found.
628 * @param pid The pid found in the pidfile.
629 */
b38684ce 630void Logger::print_pid_found(const int pid) const
388f4ab0 631{
cbbdeb6c 632 int level = 1;
d77313ea 633 if ( level <= Loglevel )
59c8d63c
BS
634 {
635 ostringstream msg;
636 msg << "Pidfile found: " << pid << ". Checking if process is still runnig..." << endl;
ce70569b 637 log_notice(msg.str());
59c8d63c 638 }
388f4ab0
BS
639}
640
641
c5675c01
BS
642/**
643 * Another process is already running.
644 * @param pid The pid of the other process.
645 */
b38684ce 646void Logger::print_process_already_running(const int pid) const
388f4ab0 647{
cbbdeb6c 648 int level = 0;
d77313ea 649 if ( level <= Loglevel )
59c8d63c
BS
650 {
651 ostringstream msg;
652 msg << "Another bpdyndnsd process with PID " << pid << " is already running!" << endl;
ce70569b 653 log_error(msg.str());
59c8d63c 654 }
388f4ab0 655}
c5675c01
BS
656
657
658/**
659 * SIGTERM caught.
660 */
b38684ce 661void Logger::print_caught_sigterm() const
c5675c01 662{
cbbdeb6c 663 int level = 0;
d77313ea 664 if ( level <= Loglevel )
59c8d63c 665 {
ce70569b 666 log_notice("Caught SIGTERM. Exiting...");
59c8d63c 667 }
c5675c01
BS
668}
669
670
671/**
672 * SIGUSR1 caught.
673 */
b38684ce 674void Logger::print_caught_siguser1() const
c5675c01 675{
cbbdeb6c 676 int level = 0;
d77313ea 677 if ( level <= Loglevel )
59c8d63c 678 {
ce70569b 679 log_notice("Caught SIGUSR1. Switching to offline mode...");
59c8d63c 680 }
c5675c01
BS
681}
682
683
684/**
685 * SIGHUP caught.
686 */
b38684ce 687void Logger::print_caught_sighup() const
c5675c01 688{
60657d55 689 int level = 0;
d77313ea 690 if ( level <= Loglevel )
59c8d63c 691 {
64ff14c3
BS
692 log_notice("Caught SIGHUP. Reloading config...");
693 }
694}
695
696
697/**
698 * SIGUSR2 caught.
699 */
700void Logger::print_caught_siguser2() const
701{
60657d55 702 int level = 0;
64ff14c3
BS
703 if ( level <= Loglevel )
704 {
705 log_notice("Caught SIGUSR2. Switching to online mode...");
706 }
707}
708
709
710/**
711 * SIGRTMIN caught.
712 */
713void Logger::print_caught_sigrtmin() const
714{
60657d55 715 int level = 0;
64ff14c3
BS
716 if ( level <= Loglevel )
717 {
718 log_notice("Caught SIGRTMIN. Switching to online mode with webcheck enabled...");
59c8d63c 719 }
c5675c01 720}
8bca3c5d
BS
721
722
723/**
3f39b968
TJ
724 * SIGRTMAX caught.
725 */
726void Logger::print_caught_sigrtmax(int new_loglevel) const
727{
728 int level = 0;
729 if ( level <= Loglevel )
730 {
731 ostringstream msg;
732 msg << "Caught SIGRTMAX. Increasing log level to " << new_loglevel << endl;
733 log_error(msg.str());
734 }
735}
736
737
738/**
8bca3c5d
BS
739 * Error while setting signal handler.
740 */
b38684ce 741void Logger::print_error_setting_signal(const string& signal) const
8bca3c5d 742{
cbbdeb6c 743 int level = 0;
d77313ea 744 if ( level <= Loglevel )
59c8d63c
BS
745 {
746 ostringstream msg;
667c672c 747 msg << "Error while setting signal handler for: " << signal << endl;
ce70569b 748 log_error(msg.str());
59c8d63c 749 }
8bca3c5d
BS
750}
751
752
753/**
754 * Error while setting signal handler.
755 */
b38684ce 756void Logger::print_init_log_facility() const
8bca3c5d 757{
cbbdeb6c 758 int level = 1;
d77313ea 759 if ( level <= Loglevel )
59c8d63c
BS
760 {
761 ostringstream msg;
4475e30a 762 msg << "Initialized logging facility." << " Loglevel: " << Loglevel << " Syslog: " << Syslog << " ExternalLogOnlyOnce: " << ExternalLogOnlyOnce << endl;
ce70569b 763 log_notice(msg.str());
59c8d63c 764 }
8bca3c5d
BS
765}
766
27baf279 767
8bca3c5d
BS
768/**
769 * Be verbose. Currently we are in offline mode.
770 */
b38684ce 771void Logger::print_offline_mode() const
8bca3c5d 772{
60657d55 773 int level = 1;
d77313ea 774 if ( level <= Loglevel )
59c8d63c 775 {
ce70569b 776 log_notice("Offline mode...");
59c8d63c 777 }
8bca3c5d 778}
27baf279
BS
779
780
781/**
c7a2055a
TJ
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
784 */
785void Logger::print_sleep_dialup_mode(time_t sleep_until) const
786{
787 int level = 1;
788 if ( level <= Loglevel )
789 {
790 ostringstream msg;
791 msg << "Skipped update in dialup mode (" << sleep_until-time(NULL) << " more seconds)" << endl;
792 log_error(msg.str());
793 }
794}
795
796/**
27baf279
BS
797 * Objects successfully serialized.
798 */
b38684ce 799void Logger::print_serialized_objects_success() const
27baf279 800{
cbbdeb6c 801 int level = 1;
d77313ea 802 if ( level <= Loglevel )
27baf279 803 {
ce70569b 804 log_notice("Serialized objects successfully.");
27baf279
BS
805 }
806}
807
808
809/**
810 * Objects successfully de-serialized.
811 */
b38684ce 812void Logger::print_deserialized_objects_success() const
27baf279 813{
cbbdeb6c 814 int level = 1;
d77313ea 815 if ( level <= Loglevel )
27baf279 816 {
ce70569b 817 log_notice("De-serialized objects successfully.");
27baf279
BS
818 }
819}
820
821
5d38cfe6
BS
822/**
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.
7335d7a7 831 * @param activated Service's activated.
5d38cfe6 832 */
7335d7a7 833void 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
27baf279 834{
cbbdeb6c 835 int level = 1;
d77313ea 836 if ( level <= Loglevel )
27baf279
BS
837 {
838 ostringstream msg;
839 msg << message << endl;
3c0cd271
BS
840 msg << "\t" << "Protocol: " << protocol << endl;
841 msg << "\t" << "Hostname: " << hostname << endl;
842 msg << "\t" << "Login: " << login << endl;
04d261b9
GE
843
844 if (LogPasswords)
845 msg << "\t" << "Password: " << password << endl;
846 else
847 msg << "\t" << "Password: (*hidden*)" << endl;
848
3c0cd271
BS
849 msg << "\t" << "Update Interval: " << update_interval << endl;
850 msg << "\t" << "Max Updates: " << max_updates_within_interval << endl;
4553e833 851 msg << "\t" << "Max equal Updates:" << max_equal_updates_in_succession << endl;
d5a516ba 852 msg << "\t" << "DNS Cache TTL: " << dns_cache_ttl << endl;
3c0cd271 853 msg << "\t" << "Actual_IP: " << actual_ip << endl;
4553e833 854 for ( std::map<time_t,std::string>::reverse_iterator r_iter = lastupdated.rbegin(); r_iter != lastupdated.rend(); r_iter++ )
3c0cd271 855 {
4553e833 856 msg << "\t" << "Lastupdated: " << r_iter->first << " -> " << r_iter->second << endl;
3c0cd271 857 }
7335d7a7 858 msg << "\t" << "Activated: " << activated << endl;
ce70569b 859 log_notice(msg.str());
27baf279
BS
860 }
861}
5d38cfe6
BS
862
863
864/**
865 * Caught exception while serialize.
866 * @param exception Exception message.
867 */
ce70569b 868void Logger::print_exception_serialize(const string& errMsg) const
5d38cfe6 869{
cbbdeb6c 870 int level = 0;
d77313ea 871 if ( level <= Loglevel )
667c672c
BS
872 {
873 ostringstream msg;
ce70569b
BS
874 msg << "Error while trying to serialize Serviceholder object: " << errMsg << endl;
875 log_error(msg.str());
667c672c
BS
876 }
877}
878
879
880/**
881 * Caught exception while de-serialize.
882 * @param exception Exception message.
883 */
ce70569b 884void Logger::print_exception_deserialize(const string& errMsg) const
667c672c 885{
cbbdeb6c 886 int level = 0;
d77313ea 887 if ( level <= Loglevel )
667c672c
BS
888 {
889 ostringstream msg;
6741febc 890 msg << "Error while trying to de-serialize Serviceholder object: " << errMsg << ". Continue without recovering state from old services!" << endl;
ce70569b 891 log_error(msg.str());
667c672c
BS
892 }
893}
894
895
896/**
897 * Child couldn't be killed by parent.
898 * @param pid Pid of the child.
899 */
b38684ce 900void Logger::print_error_kill_child(const int pid) const
667c672c 901{
cbbdeb6c 902 int level = 0;
d77313ea 903 if ( level <= Loglevel )
667c672c
BS
904 {
905 ostringstream msg;
906 msg << "Could not kill child process with PID: " << pid << endl;
ce70569b 907 log_error(msg.str());
667c672c
BS
908 }
909}
910
911
0665b239
BS
912/**
913 * Child was killed by parent because of error.
914 * @param pid The pid (child) killed.
915 */
b38684ce 916void Logger::print_child_killed(const int pid) const
584b9407 917{
cbbdeb6c 918 int level = 1;
d77313ea 919 if ( level <= Loglevel )
584b9407
BS
920 {
921 ostringstream msg;
922 msg << "Killed child process with PID: " << pid << endl;
ce70569b 923 log_notice(msg.str());
584b9407
BS
924 }
925}
926
927
667c672c
BS
928/**
929 * There is no object file.
930 * @param object_file The object file.
931 */
e8787e2e 932void Logger::print_no_object_file(const string& object_file)
667c672c 933{
cbbdeb6c
BS
934 int level = 1;
935 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
667c672c
BS
936 {
937 ostringstream msg;
938 msg << "There is no object file: " << object_file << ". Continue without recovering state from old services!" << endl;
cbbdeb6c 939 log_warning(msg.str(),level);
667c672c 940 }
5d38cfe6 941}
0665b239
BS
942
943
944/**
945 * Prints out the given hostname
946 * @param hostname Hostname as string.
947 */
e8d4a6f8 948void Logger::print_hostname(const string& hostname) const
0665b239 949{
cbbdeb6c 950 int level = 1;
d77313ea 951 if ( level <= Loglevel )
0665b239
BS
952 {
953 ostringstream msg;
954 msg << "Detected following hostname of localhost: " << hostname << endl;
ce70569b 955 log_notice(msg.str());
0665b239
BS
956 }
957}
958
959
960/**
019dc0d9 961 * Prints out the detected own ipv4 address
0665b239
BS
962 * @param ip_addr String representation of the detected ip.
963 */
c3dea5dc 964void Logger::print_own_ipv4(const string& ip_addr_v4, const string& hostname) const
0665b239 965{
cbbdeb6c 966 int level = 1;
d77313ea 967 if ( level <= Loglevel )
0665b239
BS
968 {
969 ostringstream msg;
c3dea5dc 970 msg << "Detected following IPv4-Address of host: " << hostname << " : " << ip_addr_v4 << endl;
ce70569b 971 log_notice(msg.str());
019dc0d9
BS
972 }
973}
974
975
976/**
977 * Prints out the detected own ipv5 address
978 * @param ip_addr String representation of the detected ip.
979 */
c3dea5dc 980void Logger::print_own_ipv6(const string& ip_addr_v6, const string& hostname) const
019dc0d9 981{
cbbdeb6c 982 int level = 1;
d77313ea 983 if ( level <= Loglevel )
019dc0d9
BS
984 {
985 ostringstream msg;
c3dea5dc 986 msg << "Detected following IPv6-Address of host: " << hostname << " : " << ip_addr_v6 << endl;
ce70569b 987 log_notice(msg.str());
0665b239
BS
988 }
989}
990
991
992/**
993 * Exception while trying to resolve hostname to ip.
994 * @param exception The exception caught.
995 * @param hostname The hostname.
996 */
ce70569b 997void Logger::print_error_hostname_to_ip(const string& errMsg, const string& hostname) const
0665b239 998{
a03fb896 999 int level = 0;
d77313ea 1000 if ( level <= Loglevel )
0665b239
BS
1001 {
1002 ostringstream msg;
ce70569b 1003 msg << "Could not resolve the hostname: " << hostname << " to an IP-Address: " << errMsg << endl;
e8787e2e 1004 log_error(msg.str());
0665b239
BS
1005 }
1006}
68c6b4af
BS
1007
1008
1c0908b5
BS
1009/**
1010 * The update of the given service was successful.
1011 * @param service The service.
1012 */
75a3fba6 1013void Logger::print_update_service_successful(const string& service, const std::string& ip_addr) const
68c6b4af 1014{
cbbdeb6c 1015 int level = 0;
d77313ea 1016 if ( level <= Loglevel )
68c6b4af
BS
1017 {
1018 ostringstream msg;
75a3fba6 1019 msg << "Updated service successful: " << service << " to IP: " << ip_addr << endl;
ce70569b 1020 log_notice(msg.str());
1c0908b5
BS
1021 }
1022}
1023
1024
1025/**
1026 * No ip could be determined through webcheck
1027 */
e8787e2e 1028void Logger::print_webcheck_no_ip()
1c0908b5 1029{
cbbdeb6c
BS
1030 int level = 0;
1031 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1c0908b5 1032 {
20cc3887 1033 log_warning("IP-Address of this host could not be determined through any configured webcheck url.",level);
1c0908b5
BS
1034 }
1035}
1036
1037
1038/**
1039 * Connection problem while trying to get ip through webcheck url
1040 * @param curl_err_buff Curl error message
1041 * @param url the url
1042 */
e8787e2e 1043void Logger::print_webcheck_url_connection_problem(const char * curl_err_buff, const string& url)
1c0908b5 1044{
cbbdeb6c
BS
1045 int level = 1;
1046 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1c0908b5
BS
1047 {
1048 ostringstream msg;
1049 msg << "There was a problem while trying to connect to following URL: " << url << " CURL error: " << curl_err_buff << endl;
cbbdeb6c 1050 log_warning(msg.str(),level);
1c0908b5
BS
1051 }
1052}
1053
1054
1055/**
1056 * Prints out curl error.
1057 * @param curl_err_buff Curl error message.
1058 * @param url URL
1059 */
1060void Logger::print_webcheck_error(const char * curl_err_buff, const string& url) const
1061{
cbbdeb6c 1062 int level = 0;
d77313ea 1063 if ( level <= Loglevel )
1c0908b5
BS
1064 {
1065 ostringstream msg;
1066 msg << "There was an error while trying to connect to following URL: " << url << " CURL error: " << curl_err_buff << endl;
ce70569b 1067 log_error(msg.str());
68c6b4af
BS
1068 }
1069}
1c0908b5
BS
1070
1071
1072/**
1073 * Prints out the received data through curl.
1074 * @param curl_data Data string
1075 */
1076void Logger::print_received_curl_data(const string& curl_data) const
1077{
cbbdeb6c 1078 int level = 1;
d77313ea 1079 if ( level <= Loglevel )
1c0908b5
BS
1080 {
1081 ostringstream msg;
1082 msg << "Received CURL data: " << curl_data << endl;
ce70569b 1083 log_notice(msg.str());
1c0908b5
BS
1084 }
1085}
1086
1087
1088/**
1089 * IP was foudn through regex
1090 * @param ip The IP found.
1091 */
1092void Logger::print_regex_found_ip(const string& ip) const
1093{
cbbdeb6c 1094 int level = 1;
d77313ea 1095 if ( level <= Loglevel )
1c0908b5
BS
1096 {
1097 ostringstream msg;
1098 msg << "Found IP-Address via regex: " << ip << endl;
ce70569b 1099 log_notice(msg.str());
1c0908b5
BS
1100 }
1101}
1102
1103
1104/**
1105 * No IP was found through regex.
1106 * @param data The data string which should contain a valid IP.s
1107 */
e8787e2e 1108void Logger::print_regex_ip_not_found(const string& data)
1c0908b5 1109{
efbde536 1110 int level = 1;
cbbdeb6c 1111 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1c0908b5
BS
1112 {
1113 ostringstream msg;
1114 msg << "Could not extract an IP-Address via regex from following data:\n" << data << endl;
cbbdeb6c 1115 log_warning(msg.str(),level);
1c0908b5
BS
1116 }
1117}
3c0cd271
BS
1118
1119
1120/**
1121 * Detected multiple occurrences of the same option.
1122 * @param message Error message.
1123 */
1124void Logger::print_multiple_cmd_option(const string& message) const
1125{
cbbdeb6c 1126 int level = 0;
d77313ea 1127 if ( level <= Loglevel )
3c0cd271
BS
1128 {
1129 ostringstream msg;
1130 msg << "The same option is only allowed once: " << message << endl;
ce70569b 1131 log_error(msg.str());
3c0cd271
BS
1132 }
1133}
1134
1135
1136/**
1137 * An update would exceed the update interval. Prints out a warning message.
0ebcd4ef 1138 * @param override_log_level Override log level with zero if true
3c0cd271
BS
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.
1143 */
0ebcd4ef 1144void 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)
3c0cd271 1145{
efbde536 1146 int level = 1;
0ebcd4ef
TJ
1147
1148 if (override_log_level)
1149 level = 0;
1150
cbbdeb6c 1151 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
3c0cd271
BS
1152 {
1153 ostringstream msg;
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;
cbbdeb6c 1155 log_warning(msg.str(),level);
3c0cd271
BS
1156 }
1157}
1158
1159
1160/**
1161 * Failure while running update for service.
1162 * @param service Services' name.
1163 */
e8787e2e 1164void Logger::print_update_service_failure(const string& service)
3c0cd271 1165{
cbbdeb6c
BS
1166 int level = 0;
1167 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
3c0cd271
BS
1168 {
1169 ostringstream msg;
1170 msg << "Could not update service: " << service << endl;
cbbdeb6c 1171 log_warning(msg.str(),level);
3c0cd271
BS
1172 }
1173}
e304c27b 1174
e38d7604
TJ
1175/**
1176 * Start message
1177 */
1178void Logger::print_started() const
1179{
1180 int level = 0;
1181 if ( level <= Loglevel )
1182 {
1183 log_notice("Started");
1184 }
1185}
1186
e304c27b
BS
1187
1188/**
1189 * Starting shutdown
1190 */
1191void Logger::print_starting_shutdown() const
1192{
cbbdeb6c 1193 int level = 0;
d77313ea 1194 if ( level <= Loglevel )
e304c27b 1195 {
ce70569b 1196 log_notice("Shutting down ...");
e304c27b
BS
1197 }
1198}
1199
1200
1201/**
1202 * Shutdown complete
1203 */
1204void Logger::print_shutdown_succeeded() const
1205{
cbbdeb6c 1206 int level = 0;
d77313ea 1207 if ( level <= Loglevel )
e304c27b 1208 {
ce70569b 1209 log_notice("Shutting down complete ...");
e304c27b
BS
1210 }
1211}
1212
1213
1214/**
1215 * Shutdown parent succeeded
1216 */
1217void Logger::print_shutdown_parent_succeeded() const
1218{
cbbdeb6c 1219 int level = 0;
d77313ea 1220 if ( level <= Loglevel )
e304c27b 1221 {
ce70569b 1222 log_notice("Shutting down parent process completed ...");
e304c27b
BS
1223 }
1224}
1225
1226
1227/**
1228 * Starting shutdown parent
1229 */
1230void Logger::print_starting_shutdown_parent() const
1231{
cbbdeb6c 1232 int level = 0;
d77313ea 1233 if ( level <= Loglevel )
e304c27b 1234 {
ce70569b 1235 log_notice("Shutting down parent process ...");
e304c27b
BS
1236 }
1237}
0541cd71
BS
1238
1239
1240/**
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
1246 */
8a00a649 1247void Logger::print_recheck_dns_entry(const string& hostname, const int lastupdated, const int dns_cache_ttl, const int current_time) const
0541cd71 1248{
cbbdeb6c 1249 int level = 1;
d77313ea 1250 if ( level <= Loglevel )
0541cd71
BS
1251 {
1252 ostringstream msg;
d5a516ba 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;
ce70569b 1254 log_notice(msg.str());
0541cd71
BS
1255 }
1256}
1257
1258
1259/**
8a00a649
BS
1260 * Missing proxy option on command line.
1261 */
1262void Logger::print_missing_cmd_proxy_option() const
1263{
cbbdeb6c 1264 int level = 0;
d77313ea 1265 if ( level <= Loglevel )
8a00a649 1266 {
ce70569b 1267 log_error("Missing option to initialize proxy. http_proxy and http_proxy_port must be specified.");
8a00a649
BS
1268 }
1269}
1270
1271
1272/**
1273 * Multiple option in service config file.
1274 * @param service_conf_file Service config file
1275 * @param message Multiple option text
1276 */
1277void Logger::print_multiple_service_conf_option(const string& service_conf_file, const string& message) const
1278{
cbbdeb6c 1279 int level = 0;
d77313ea 1280 if ( level <= Loglevel )
8a00a649
BS
1281 {
1282 ostringstream msg;
1283 msg << "Multiple occurrences of the same option in service config file detected: " << service_conf_file << " " << message << endl;
ce70569b 1284 log_error(msg.str());
8a00a649
BS
1285 }
1286}
1287
1288
1289/**
1290 * Multiple option in main config file.
1291 * @param service_conf_file Service config file
1292 * @param message Multiple option text
1293 */
1294void Logger::print_multiple_main_conf_option(const string& main_conf_file, const string& message) const
1295{
cbbdeb6c 1296 int level = 0;
d77313ea 1297 if ( level <= Loglevel )
8a00a649
BS
1298 {
1299 ostringstream msg;
1300 msg << "Multiple occurrences of the same option in main config file detected: " << main_conf_file << " " << message << endl;
ce70569b 1301 log_error(msg.str());
8a00a649
BS
1302 }
1303}
1304
1305
1306/**
1307 * Missing proxy option in main config file.
2dd2db3e 1308 * @param main_conf_filename The concerning config file.
8a00a649
BS
1309 */
1310void Logger::print_missing_conf_proxy_option(const string& main_conf_filename) const
1311{
cbbdeb6c 1312 int level = 0;
d77313ea 1313 if ( level <= Loglevel )
8a00a649
BS
1314 {
1315 ostringstream msg;
1316 msg << "Missing option to initialize proxy in main config file: " << main_conf_filename << " http_proxy and http_proxy_port must be specified." << endl;
ce70569b 1317 log_error(msg.str());
8a00a649
BS
1318 }
1319}
2dd2db3e
BS
1320
1321
1322/**
1323 * There is no domain part in the given hostname
1324 * @param hostname The hostname with no domain part in it.
1325 */
d5a516ba 1326void Logger::print_no_domain_part(const string& hostname) const
2dd2db3e
BS
1327{
1328 int level = 0;
d77313ea 1329 if ( level <= Loglevel )
2dd2db3e
BS
1330 {
1331 ostringstream msg;
1332 msg << "There is no domain part in the given hostname: " << hostname << endl;
ce70569b 1333 log_notice(msg.str());
2dd2db3e
BS
1334 }
1335}
d5a516ba
BS
1336
1337
1338/**
31af6a2e
BS
1339 * Service is not initialized properly.
1340 * @param service The service.
1341 */
e8787e2e 1342void Logger::print_httphelper_not_initialized() const
31af6a2e
BS
1343{
1344 int level = 0;
1345 if ( level <= Loglevel )
1346 {
e8787e2e 1347 log_error("HTTP-Helper is not initialized properly.");
31af6a2e
BS
1348 }
1349}
1350
1351
1352/**
de5e00b1 1353 * A curl error occurred.
31af6a2e
BS
1354 * @param msg The error message
1355 * @param curl_err_code The resulting curl error code
1356 */
c730deea 1357void Logger::print_curl_error_init(const std::string& err_msg, const CURLcode curl_err_code) const
31af6a2e
BS
1358{
1359 string curl_err = "";
1360
1361 if ( curl_err_code == CURLE_FAILED_INIT )
1362 curl_err = "CURLE_FAILED_INIT";
1363 else
1364 curl_err = "UNKNOWN";
1365
1366 int level = 0;
e8787e2e 1367 if ( (level <= Loglevel) )
31af6a2e
BS
1368 {
1369 ostringstream msg;
557b6f56 1370 msg << "Curl error: " << err_msg << " Curl error code: " << curl_err_code << " " << curl_err << endl; /*lint !e641 */
ce70569b 1371 log_error(msg.str());
31af6a2e
BS
1372 }
1373}
1374
1375
1376/**
de5e00b1 1377 * A curl error occurred.
d5a516ba
BS
1378 * @param url The url requested by the curl operation
1379 * @param curl_err_code The resulting curl error code
1380 */
31af6a2e 1381void Logger::print_curl_error(const string& url, const CURLcode curl_err_code) const
d5a516ba
BS
1382{
1383 string curl_err = "";
1384
31af6a2e 1385 if ( curl_err_code == CURLE_URL_MALFORMAT )
d5a516ba 1386 curl_err = "CURLE_URL_MALFORMAT";
31af6a2e 1387 else if ( curl_err_code == CURLE_COULDNT_RESOLVE_HOST )
d5a516ba 1388 curl_err = "CURLE_COULDNT_RESOLVE_HOST";
31af6a2e 1389 else if ( curl_err_code == CURLE_COULDNT_CONNECT )
d5a516ba
BS
1390 curl_err = "CURLE_COULDNT_CONNECT";
1391 else
1392 curl_err = "UNKNOWN";
1393
1394 int level = 0;
e8787e2e 1395 if ( (level <= Loglevel) )
d5a516ba
BS
1396 {
1397 ostringstream msg;
557b6f56 1398 msg << "Curl error while requesting following url: " << url << " Curl error code: " << curl_err_code << " " << curl_err << endl; /*lint !e641 */
e8787e2e 1399 log_error(msg.str());
d5a516ba
BS
1400 }
1401}
1402
1403
1404/**
de5e00b1 1405 * A curl error occurred.
d5a516ba
BS
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
1409 */
c730deea 1410void Logger::print_curl_error(const string& url, const CURLcode curl_err_code, const char * curl_err_buff) const
d5a516ba
BS
1411{
1412 string curl_err = "";
1413
c730deea 1414 if ( curl_err_code == CURLE_URL_MALFORMAT )
d5a516ba 1415 curl_err = "CURLE_URL_MALFORMAT";
c730deea 1416 else if ( curl_err_code == CURLE_COULDNT_RESOLVE_HOST )
d5a516ba 1417 curl_err = "CURLE_COULDNT_RESOLVE_HOST";
c730deea 1418 else if ( curl_err_code == CURLE_COULDNT_CONNECT )
d5a516ba
BS
1419 curl_err = "CURLE_COULDNT_CONNECT";
1420 else
1421 curl_err = "UNKNOWN";
1422
1423 int level = 0;
e8787e2e 1424 if ( (level <= Loglevel) )
d5a516ba
BS
1425 {
1426 ostringstream msg;
557b6f56 1427 msg << "Curl error while requesting following url: " << url << " Curl error code: " << curl_err_code << " " << curl_err << " " << curl_err_buff << endl; /*lint !e641 */
e8787e2e 1428 log_error(msg.str());
d5a516ba
BS
1429 }
1430}
1431
1432
1433/**
1434 * Prints out the data received by curl operation
43f4565e 1435 * @param CurlWritedataBuff
d5a516ba
BS
1436 */
1437void Logger::print_curl_data(const string& curl_writedata_buff) const
1438{
1439 int level = 1;
d77313ea 1440 if ( level <= Loglevel )
d5a516ba
BS
1441 {
1442 ostringstream msg;
b6228761 1443 msg << "Data received by curl: " << curl_writedata_buff << endl;
ce70569b 1444 log_notice(msg.str());
d5a516ba
BS
1445 }
1446}
1447
1448
1449/**
a03fb896
BS
1450 * Not authorized to perform requested update operation
1451 * @param service The requested service.
d5a516ba
BS
1452 * @param username Username
1453 * @param password Password
1454 */
a03fb896 1455void Logger::print_service_not_authorized(const string& service, const string& username, const string& password) const
d5a516ba
BS
1456{
1457 int level = 0;
e8787e2e 1458 if ( level <= Loglevel )
d5a516ba
BS
1459 {
1460 ostringstream msg;
04d261b9
GE
1461 msg << "Not authorized to perform update operation on service: " << service << " Please check username and password: " << username << ":";
1462
1463 if (LogPasswords)
1464 msg << password << endl;
1465 else
1466 msg << "(*hidden*)" << endl;
1467
e8787e2e 1468 log_notice(msg.str());
d5a516ba
BS
1469 }
1470}
1471
1472
1473/**
1474 * Prints out the http status code
1475 * @param url Url
1476 * @param output HTTP status code
1477 */
1af7c124 1478void Logger::print_http_status_code(const string& url, const long http_code) const
d5a516ba
BS
1479{
1480 int level = 1;
d77313ea 1481 if ( level <= Loglevel )
d5a516ba
BS
1482 {
1483 ostringstream msg;
1484 msg << "Requested URL: " << url << " Received HTTP status code: " << http_code << endl;
ce70569b 1485 log_notice(msg.str());
d5a516ba
BS
1486 }
1487}
b6228761
BS
1488
1489
1490/**
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
1494 */
1495void Logger::print_update_failure(const string& url, const string& curl_data) const
1496{
1497 int level = 0;
e8787e2e 1498 if ( (level <= Loglevel) )
b6228761
BS
1499 {
1500 ostringstream msg;
1501 msg << "Problem while trying to updating service. Requested URL: " << url << " Error Code from Server: " << curl_data << endl;
e8787e2e 1502 log_error(msg.str());
b6228761
BS
1503 }
1504}
1a00eac6
BS
1505
1506
1507/**
b17fd691
BS
1508 * Generic failure while trying to update service
1509 * @param url The requested URL
1510 * @param http_status_code The received http status code
1511 */
1512void Logger::print_update_failure(const string& url, const long http_status_code) const
1513{
1514 int level = 0;
e8787e2e 1515 if ( level <= Loglevel )
b17fd691
BS
1516 {
1517 ostringstream msg;
1518 msg << "Problem while trying to updating service. Requested URL: " << url << " Error Code from Server: " << http_status_code << endl;
e8787e2e 1519 log_error(msg.str());
b17fd691
BS
1520 }
1521}
1522
1523/**
1a00eac6 1524 * Hostname is invalid, contains no or only one domain part.
43f4565e 1525 * @param hostname The full qualified host name.
1a00eac6 1526 */
e8787e2e 1527void Logger::print_invalid_hostname(const string& hostname)
1a00eac6
BS
1528{
1529 int level = 0;
1530 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1531 {
1532 ostringstream msg;
1533 msg << "The configured hostname: " << hostname << " is invalid. Please add the corresponding domain part." << endl;
1534 log_warning(msg.str(),level);
1535 }
1536}
4ef36a12
BS
1537
1538
1539/**
1540 * An IP in a private range was detected
1541 * @param ip The private IP
1542 */
e8787e2e 1543void Logger::print_ip_is_local(const string& ip)
4ef36a12 1544{
60657d55 1545 int level = 1;
4ef36a12
BS
1546 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1547 {
1548 ostringstream msg;
1549 msg << "The detected IP is within a private IP range: " << ip << endl;
1550 log_warning(msg.str(),level);
1551 }
1552}
1553
1554
1555/**
1556 * Regex is matching in string
1557 * @param regex The regex pattern
1558 * @param matching_string The string
1559 */
a78b44b5 1560void Logger::print_regex_match(const string& regex, const string& matching_string) const
4ef36a12
BS
1561{
1562 int level = 1;
d77313ea 1563 if ( level <= Loglevel )
4ef36a12
BS
1564 {
1565 ostringstream msg;
1566 msg << "Regex: " << regex << " is matching in: " << matching_string << endl;
ce70569b 1567 log_notice(msg.str());
4ef36a12
BS
1568 }
1569}
a78b44b5
BS
1570
1571
1572/**
1573 * Regex is not matching
1574 * @param regex Regex
1575 * @param not_matching_string String
1576 */
e8787e2e 1577void Logger::print_no_regex_match(const string& regex, const string& not_matching_string)
a78b44b5
BS
1578{
1579 int level = 1;
1580 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1581 {
1582 ostringstream msg;
1583 msg << "Regex: " << regex << " is not matching in: " << not_matching_string << endl;
1584 log_warning(msg.str(),level);
1585 }
1586}
1587
1588
1589/**
1590 * Could not parse gnudip initial reply.
1591 * @param curl_data The data received from gnudip server which should contain salt, time and sign.
1592 */
1593void Logger::print_could_not_parse_received_data(const string& curl_data) const
1594{
1595 int level = 0;
e8787e2e 1596 if ( (level <= Loglevel) )
a78b44b5
BS
1597 {
1598 ostringstream msg;
1599 msg << "Could not parse salt, time and sign from initial gnudip server reply: " << curl_data << endl;
e8787e2e 1600 log_error(msg.str());
a78b44b5
BS
1601 }
1602}
1603
1604
1605/**
1606 * Gnudip salt, time and sign could not be got from map
1607 */
1608void Logger::print_could_not_get_initial_gnudip_data() const
1609{
1610 int level = 0;
e8787e2e 1611 if ( (level <= Loglevel) )
a78b44b5 1612 {
e8787e2e 1613 log_error("Could not get salt, time and sign from map.");
a78b44b5
BS
1614 }
1615}
1616
1617
a2f5be94
BS
1618
1619/**
1620 * Gnudip protocol requires explicit declaration of a servername.
1621 */
e8787e2e 1622void Logger::print_gnudip_requires_servername()
a78b44b5
BS
1623{
1624 int level = 0;
1625 if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
1626 {
20cc3887 1627 log_warning("Gnudip requires explicit definition of servername via config!",level);
a78b44b5
BS
1628 }
1629}
a2f5be94
BS
1630
1631
1632/**
de5e00b1
TJ
1633 * An exception occurred while computing the md5 sum.
1634 * @param what The exception occurred.
a2f5be94 1635 */
c1b8cb79 1636void Logger::print_exception_md5_sum(const string& what) const
a2f5be94
BS
1637{
1638 int level = 0;
d77313ea 1639 if ( level <= Loglevel )
a2f5be94
BS
1640 {
1641 ostringstream msg;
de5e00b1 1642 msg << "An exception occurred while computing a md5 sum: " << what << endl;
ce70569b 1643 log_error(msg.str());
a2f5be94
BS
1644 }
1645}
a03fb896
BS
1646
1647
1648/**
de5e00b1
TJ
1649 * A network exception occurred.
1650 * @param what The exception occurred.
a03fb896 1651 */
c1b8cb79 1652void Logger::print_network_error(const string& what) const
a03fb896
BS
1653{
1654 int level = 0;
d77313ea 1655 if ( level <= Loglevel )
a03fb896
BS
1656 {
1657 ostringstream msg;
de5e00b1 1658 msg << "A network exception occurred: " << what << endl;
ce70569b 1659 log_error(msg.str());
a03fb896
BS
1660 }
1661}
1662
1663
1664/**
de5e00b1 1665 * An undefined protocol error occurred.
a03fb896
BS
1666 * @param protocol The protocol
1667 * @param error The error
1668 */
c1b8cb79 1669void Logger::print_undefined_protocol_error(const string& protocol, const string& error) const
a03fb896
BS
1670{
1671 int level = 0;
d77313ea 1672 if ( level <= Loglevel )
a03fb896
BS
1673 {
1674 ostringstream msg;
de5e00b1 1675 msg << "An undefined protocol error occurred. Protocol: " << protocol << " Error: " << error << endl;
ce70569b 1676 log_error(msg.str());
a03fb896
BS
1677 }
1678}
d77313ea
BS
1679
1680/**
1681 * Error while trying to log through external program.
1682 * @param external_prog The external program called.
1683 */
c1b8cb79 1684void Logger::print_error_external_logging(const string& external_prog) const
d77313ea
BS
1685{
1686 int level = 0;
1687 if ( level <= Loglevel )
1688 {
1689 ostringstream msg;
1690 msg << "Error while trying to log through external program: " << external_prog << endl;
ce70569b 1691 log_notice(msg.str());
d77313ea
BS
1692 }
1693}
c1b8cb79
BS
1694
1695
1696/**
1697 * Error while parsing config file.
de5e00b1 1698 * @param error Error occurred.
c1b8cb79
BS
1699 * @param config_file Config file.
1700 */
1701void Logger::print_error_parsing_config_file(const string& filename, const string& error) const
1702{
1703 int level = 0;
1704 if ( level <= Loglevel )
1705 {
1706 ostringstream msg;
1707 msg << "Error while parsing config file: " << filename << " Error: " << error << endl;
ce70569b 1708 log_notice(msg.str());
c1b8cb79
BS
1709 }
1710}
1711
1712
1713/**
1714 * Error while parsing cmd option
1715 * @param error Error
1716 */
1717void Logger::print_error_parsing_cmd(const string& error) const
1718{
1719 int level = 0;
1720 if ( level <= Loglevel )
1721 {
1722 ostringstream msg;
1723 msg << "Error while parsing cmd options: " << error << endl;
ce70569b 1724 log_error(msg.str());
c1b8cb79
BS
1725 }
1726}
2b0f7c11
BS
1727
1728
1729/**
1730 * The webcheck interval was exceeded, so webcheck not allowed.
1731 * @param last_webcheck Time of last webcheck.
3c4705d9 1732 * @param webcheck_interval Webcheck interval time in seconds.
2b0f7c11
BS
1733 * @param current_time Current system time.
1734 */
c730deea 1735void Logger::print_webcheck_exceed_interval( const time_t last_webcheck, const int webcheck_interval, const time_t current_time ) const
2b0f7c11 1736{
60657d55 1737 int level = 1;
2b0f7c11
BS
1738 if ( level <= Loglevel )
1739 {
1740 ostringstream msg;
3c4705d9
TJ
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;
ce70569b 1743 log_notice(msg.str());
2b0f7c11
BS
1744 }
1745}
1af7c124
BS
1746
1747
1748/**
1749 * Checking if hosts needs update.
1750 * @param hostname Hostname
1751 * @param current_time Current time
1752 * @param lastupdated Last updated
1753 */
c730deea 1754void Logger::print_check_service_update(const string& hostname, const time_t current_time, const time_t lastupdated) const
1af7c124
BS
1755{
1756 int level = 1;
1757 if ( level <= Loglevel )
1758 {
1759 ostringstream msg;
1760 msg << "Checking if host: " << hostname << " needs update. Current time: " << current_time << " Last updated: " << lastupdated << endl;
ce70569b 1761 log_notice(msg.str());
1af7c124
BS
1762 }
1763}
1764
1765
1766/**
1767 * Cached DNS entry
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
1772 */
1773void Logger::print_cached_dns_entry(const string& hostname, const string& ip_dns_recheck, const string& ip_last_update, const string& ip_host) const
1774{
1775 int level = 1;
1776 if ( level <= Loglevel )
1777 {
1778 ostringstream msg;
1779 msg << "Cached DNS record for host <" << hostname << "> : " << ip_dns_recheck << " Last updated IP: " << ip_last_update << " Hosts IP: " << ip_host << endl;
ce70569b 1780 log_notice(msg.str());
1af7c124
BS
1781 }
1782}
1783
1784
1785/**
1af7c124
BS
1786 * Updating service
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
1792 */
08a5a621 1793void 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{
1af7c124
BS
1794 int level = 1;
1795 if ( level <= Loglevel )
1796 {
1797 ostringstream msg;
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;
ce70569b 1799 log_notice(msg.str());
1af7c124
BS
1800 }
1801}
1802
1803
1804/**
1805 * TTL expired
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
1813 */
08a5a621 1814void 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
1af7c124
BS
1815{
1816 int level = 1;
1817 if ( level <= Loglevel )
1818 {
1819 ostringstream msg;
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;
ce70569b 1821 log_notice(msg.str());
1af7c124
BS
1822 }
1823}
1824
1825
1826/**
1827 * TTL expired
d55e13a6 1828 * @param override_log_level Override log level with zero if true
1af7c124
BS
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
1836 */
d55e13a6 1837void 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
1af7c124
BS
1838{
1839 int level = 1;
d55e13a6
TJ
1840
1841 if (override_log_level)
1842 level = 0;
1843
1af7c124
BS
1844 if ( level <= Loglevel )
1845 {
1846 ostringstream msg;
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;
ce70569b 1848 log_notice(msg.str());
1af7c124
BS
1849 }
1850}
1851
1852
1853/**
1854 * No update needed
d55e13a6 1855 * @param override_log_level Override log level with zero if true
1af7c124
BS
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
1861 */
d55e13a6 1862void 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
1af7c124
BS
1863{
1864 int level = 1;
d55e13a6
TJ
1865
1866 if (override_log_level)
1867 level = 0;
1868
1af7c124
BS
1869 if ( level <= Loglevel )
1870 {
1871 ostringstream msg;
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;
ce70569b 1873 log_notice(msg.str());
1af7c124
BS
1874 }
1875}
1d2e2f56
BS
1876
1877
1878/**
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.
1882 */
1883void Logger::print_error_getting_local_wan_ip(const std::string& system_call, const std::string& error) const
1884{
1885 int level = 0;
1886 if ( level <= Loglevel )
1887 {
1888 ostringstream msg;
1889 msg << "Error while trying to get local wan interface IP address through '" << system_call << "': " << error << endl;
ce70569b 1890 log_error(msg.str());
1d2e2f56
BS
1891 }
1892}
1893
1894
1895/**
1896 * Could not get IP address of local wan interface.
f3141675
TJ
1897 * @param override_log_level Override log level with zero if true
1898 */
1899void Logger::print_no_wan_ip(bool override_log_level) const
1900{
1901 int level = 1;
1902
1903 if (override_log_level)
1904 level = 0;
1905
1906 if ( level <= Loglevel )
1907 {
1908 ostringstream msg;
1909 msg << "Could not get IP address of local wan interface. Will retry later." << endl;
1910 log_error(msg.str());
1911 }
1912}
1913
62956d9a
TJ
1914/**
1915 * Print extenral WAN IP
1916 * @param override_log_level Override log level with zero if true
1917 * @param ip External WAN IP
1918 */
1919void Logger::print_external_wan_ip(bool override_log_level, const std::string &ip) const
1920{
1921 int level = 1;
1922
1923 if (override_log_level)
1924 level = 0;
1925
1926 if ( level <= Loglevel )
1927 {
1928 ostringstream msg;
1929 msg << "Determined WAN interface IP: " << ip << endl;
1930 log_error(msg.str());
1931 }
1932}
f3141675
TJ
1933
1934/**
1935 * Could not resolve current IP for DNS name
1936 * @param override_log_level Override log level with zero if true
1d2e2f56 1937 */
f3141675 1938void Logger::print_dns_lookup_failed(bool override_log_level, const std::string &hostname) const
1d2e2f56 1939{
60657d55 1940 int level = 1;
f3141675
TJ
1941
1942 if (override_log_level)
1943 level = 0;
1944
1d2e2f56
BS
1945 if ( level <= Loglevel )
1946 {
1947 ostringstream msg;
f3141675 1948 msg << "Could not resolve IP address for host " << hostname << ". Will retry later." << endl;
ce70569b 1949 log_error(msg.str());
1d2e2f56
BS
1950 }
1951}
60657d55
BS
1952
1953
1954/**
1955 * Invalid service config was detected.
1956 */
1957void Logger::print_invalid_service_config() const
1958{
1959 int level = 0;
1960 if ( level <= Loglevel )
1961 {
1962 log_error("Ignoring invalid service. Please check.\n");
1963 }
1964}
4475e30a
BS
1965
1966
1967/**
1968 * Print simple message.
1969 */
1970void Logger::print_msg( const string& msg ) const
1971{
1972 int level = 0;
1973 if ( level <= Loglevel )
1974 {
1975 log_error(msg);
1976 }
1977}
4553e833
BS
1978
1979
1980/**
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.
1986 */
08e6f339 1987void 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
4553e833
BS
1988{
1989 int level = 1;
1990
1991 if ( level <= Loglevel )
1992 {
1993 ostringstream msg;
08e6f339 1994 msg << "Last updates for " << servicename << " : ";
4553e833
BS
1995
1996 int i = 0;
08e6f339 1997
63e1bf1f 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++ )
4553e833
BS
1999 {
2000 msg << r_iter->first << "->" << r_iter->second;
2001 i++;
2002 }
08e6f339
BS
2003
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;
4553e833
BS
2009
2010 log_notice(msg.str());
2011 }
2012}
2013
2014
2015/**
2016 * Print the burnt IP.
2017 * @param ip_host Actual host IP which is burnt.
2018 * @param hostname The service hostname.
2019 */
2020void Logger::print_ip_burnt( const std::string& ip_host, const std::string& hostname ) const
2021{
2022 int level = 0;
2023 if ( level <= Loglevel )
2024 {
2025 ostringstream msg;
1a149548 2026 msg << "IP address " << ip_host << " was updated too often in succession, host " << hostname << " blocked until IP address will be different." << endl;
4553e833
BS
2027 log_error(msg.str());
2028 }
2029}