| 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 | |
| 11 | #include "logger.hpp" |
| 12 | #include "version_info.h" |
| 13 | |
| 14 | #include <iostream> |
| 15 | #include <syslog.h> |
| 16 | #include <sstream> |
| 17 | |
| 18 | #include <boost/foreach.hpp> |
| 19 | |
| 20 | namespace po = boost::program_options; |
| 21 | |
| 22 | typedef boost::shared_ptr<boost::program_options::options_description> Options_descriptionPtr; |
| 23 | |
| 24 | using namespace std; |
| 25 | |
| 26 | /** |
| 27 | * Default Constructor |
| 28 | */ |
| 29 | Logger::Logger() |
| 30 | : Loglevel(0) |
| 31 | , Syslog(false) |
| 32 | , ExternalWarningLevel(0) |
| 33 | , ExternalLogOnlyOnce(false) |
| 34 | , LogPasswords(false) |
| 35 | { |
| 36 | set_log_facility(Loglevel,Syslog,ExternalWarningLog,ExternalWarningLevel,ExternalLogOnlyOnce); |
| 37 | } |
| 38 | |
| 39 | |
| 40 | /** |
| 41 | * Default Destructor |
| 42 | */ |
| 43 | Logger::~Logger() |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | /** |
| 51 | * Decides if a external log message can be send. |
| 52 | * @param msg The message to log. |
| 53 | */ |
| 54 | bool 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 | */ |
| 65 | void Logger::clear_external_send_messages() |
| 66 | { |
| 67 | ExternalSendMessages.clear(); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | /** |
| 72 | * Decides if Logging through syslog if enabled or through std. |
| 73 | * @param msg The message to log. |
| 74 | */ |
| 75 | void Logger::log_notice(const string& msg) const |
| 76 | { |
| 77 | if ( Syslog ) |
| 78 | syslog(LOG_NOTICE,msg.c_str()); |
| 79 | else |
| 80 | cout << msg << endl;; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /** |
| 85 | * Escape shell arguments. |
| 86 | * @param input The input to escape. |
| 87 | * @return The escaped string ready to use for the shell. |
| 88 | */ |
| 89 | string 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 | /** |
| 107 | * Decides if Logging through syslog if enabled or through std. |
| 108 | * @param msg The message to log. |
| 109 | */ |
| 110 | void Logger::log_warning(const string& msg, int level) |
| 111 | { |
| 112 | if ( Syslog ) |
| 113 | syslog(LOG_WARNING,msg.c_str()); |
| 114 | else |
| 115 | cout << msg << endl; |
| 116 | |
| 117 | if ( (level <= ExternalWarningLevel) && (!ExternalWarningLog.empty()) && (is_allowed_to_send(msg)) ) |
| 118 | { |
| 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 */ |
| 123 | |
| 124 | message = escape_shellarg(message); |
| 125 | |
| 126 | string external = ExternalWarningLog; |
| 127 | external.append(" "); |
| 128 | external.append(message); |
| 129 | if ( system(external.c_str()) != 0 ) |
| 130 | print_error_external_logging(external); |
| 131 | else |
| 132 | ExternalSendMessages.insert(msg); /*lint !e534 */ |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | |
| 137 | /** |
| 138 | * Decides if Logging through syslog if enabled or through std. |
| 139 | * @param msg The message to log. |
| 140 | */ |
| 141 | void Logger::log_error(const string& msg) const |
| 142 | { |
| 143 | if ( Syslog ) |
| 144 | syslog(LOG_ERR,msg.c_str()); |
| 145 | else |
| 146 | cerr << msg << endl; |
| 147 | } |
| 148 | |
| 149 | |
| 150 | /** |
| 151 | * Setter for member Loglevel. |
| 152 | * @param _loglevel Value to set Loglevel to. |
| 153 | */ |
| 154 | void Logger::set_external_log_only_once( const bool _external_log_only_once ) |
| 155 | { |
| 156 | ExternalLogOnlyOnce = _external_log_only_once; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | /** |
| 161 | * Setter for member LogPasswords. |
| 162 | * @param _log_passwords If we want to log passwords or not. |
| 163 | */ |
| 164 | void Logger::set_log_passwords( const bool _log_passwords ) |
| 165 | { |
| 166 | LogPasswords = _log_passwords; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | /** |
| 171 | * Setter for member Loglevel. |
| 172 | * @param _loglevel Value to set Loglevel to. |
| 173 | */ |
| 174 | void Logger::set_loglevel(const int _loglevel) |
| 175 | { |
| 176 | Loglevel = _loglevel; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | /** |
| 181 | * Getter for member Loglevel. |
| 182 | * @return Loglevel. |
| 183 | */ |
| 184 | int Logger::get_loglevel() const |
| 185 | { |
| 186 | return Loglevel; |
| 187 | } |
| 188 | |
| 189 | |
| 190 | /** |
| 191 | * Setter for member Syslog. |
| 192 | * @param _syslog Wether to log through syslog or not. |
| 193 | */ |
| 194 | void 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 | */ |
| 204 | bool Logger::get_syslog() const |
| 205 | { |
| 206 | return Syslog; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | /** |
| 211 | * Initialize the logging facility. |
| 212 | */ |
| 213 | void Logger::set_log_facility(const int _loglevel, const bool _syslog, const string& _external_error_log, const int _external_error_level, const bool _external_log_only_once ) |
| 214 | { |
| 215 | Loglevel = _loglevel; |
| 216 | Syslog = _syslog; |
| 217 | ExternalWarningLog = _external_error_log; |
| 218 | ExternalWarningLevel = _external_error_level; |
| 219 | ExternalLogOnlyOnce = _external_log_only_once; |
| 220 | |
| 221 | if ( Syslog ) |
| 222 | openlog("bpdyndnsd",LOG_PID,LOG_DAEMON); |
| 223 | } |
| 224 | |
| 225 | |
| 226 | /** |
| 227 | * Prints out the usage to the command line. |
| 228 | */ |
| 229 | void Logger::print_usage(const Options_descriptionPtr opt_desc) const |
| 230 | { |
| 231 | int level = 0; |
| 232 | if ( level <= Loglevel ) |
| 233 | { |
| 234 | ostringstream msg; |
| 235 | msg << "Usage: bpdyndnsd [Command line options]" << "\n" << endl; |
| 236 | msg << *opt_desc << endl; |
| 237 | log_notice(msg.str()); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | |
| 242 | /** |
| 243 | * Prints out the programm name and the given version string on stdout. |
| 244 | * @param version Version string. |
| 245 | */ |
| 246 | void Logger::print_version() const |
| 247 | { |
| 248 | int level = 0; |
| 249 | if ( level <= Loglevel ) |
| 250 | { |
| 251 | ostringstream msg; |
| 252 | msg << "Bullet proof dynamic dns daemon.\nbpdyndnsd " << MAJOR_VERSION << "." << MINOR_VERSION << endl; |
| 253 | log_notice(msg.str()); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | |
| 258 | /** |
| 259 | * Prints out the successful parsing of the command line options. |
| 260 | */ |
| 261 | void Logger::print_cmd_parsed() const |
| 262 | { |
| 263 | int level = 1; |
| 264 | if ( level <= Loglevel ) |
| 265 | { |
| 266 | log_notice("Command line options successfully parsed."); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | |
| 271 | void Logger::print_conf_files_parsed() const |
| 272 | { |
| 273 | int level = 1; |
| 274 | if ( level <= Loglevel ) |
| 275 | { |
| 276 | log_notice("Config files successfully parsed."); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | |
| 281 | /** |
| 282 | * Prints out the successful parsing of the config files. |
| 283 | * @param config_path The specified config path. |
| 284 | */ |
| 285 | void Logger::print_conf_loaded(const string& config_path) const |
| 286 | { |
| 287 | int level = 1; |
| 288 | if ( level <= Loglevel ) |
| 289 | { |
| 290 | ostringstream msg; |
| 291 | msg << "Config files successfully loaded in: " << config_path << endl; |
| 292 | log_notice(msg.str()); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | |
| 297 | /** |
| 298 | * Prints out the unsuccessful parsing of the config files. |
| 299 | * @param config_path The specified config path. |
| 300 | */ |
| 301 | void Logger::print_conf_not_loaded(const string& config_path) const |
| 302 | { |
| 303 | int level = 0; |
| 304 | if ( level <= Loglevel ) |
| 305 | { |
| 306 | ostringstream msg; |
| 307 | msg << "Config files couldn't be loaded in: " << config_path << endl; |
| 308 | log_error(msg.str()); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | |
| 313 | /** |
| 314 | * Prints out the unsuccessful parsing of the config files. |
| 315 | */ |
| 316 | void 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 | /** |
| 325 | * A file could not be opened for reading. |
| 326 | * @param filename The filename. |
| 327 | */ |
| 328 | void Logger::print_error_opening_r(const string& filename) const |
| 329 | { |
| 330 | int level = 0; |
| 331 | if ( level <= Loglevel ) |
| 332 | { |
| 333 | ostringstream msg; |
| 334 | msg << "Error opening file for reading: " << filename << endl; |
| 335 | log_error(msg.str()); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | |
| 340 | /** |
| 341 | * A file could not be opened for writing. |
| 342 | * @param filename The filename. |
| 343 | */ |
| 344 | void Logger::print_error_opening_rw(const string& filename) const |
| 345 | { |
| 346 | int level = 0; |
| 347 | if ( level <= Loglevel ) |
| 348 | { |
| 349 | ostringstream msg; |
| 350 | msg << "Error opening file for writing: " << filename << endl; |
| 351 | log_error(msg.str()); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | |
| 356 | /** |
| 357 | * Desctructor of specified class was called. |
| 358 | * @param _class Name of the class. |
| 359 | */ |
| 360 | void Logger::print_destructor_call(const string& _class) const |
| 361 | { |
| 362 | int level = 1; |
| 363 | if ( level <= Loglevel ) |
| 364 | { |
| 365 | ostringstream msg; |
| 366 | msg << "Destructor call: " << _class << endl; |
| 367 | log_notice(msg.str()); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | |
| 372 | /** |
| 373 | * Constructor of specified class was called. |
| 374 | * @param _class Name of the class. |
| 375 | */ |
| 376 | void Logger::print_constructor_call(const string& _class) const |
| 377 | { |
| 378 | int level = 1; |
| 379 | if ( level <= Loglevel ) |
| 380 | { |
| 381 | ostringstream msg; |
| 382 | msg << "Constructor call: " << _class << endl; |
| 383 | log_notice(msg.str()); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | |
| 388 | /** |
| 389 | * Update method for specified service was called. |
| 390 | * @param service The service for which the update is running. |
| 391 | */ |
| 392 | void Logger::print_update_service(const string& service) const |
| 393 | { |
| 394 | int level = 0; |
| 395 | if ( level <= Loglevel ) |
| 396 | { |
| 397 | ostringstream msg; |
| 398 | msg << "Running update for service: " << service << endl; |
| 399 | log_notice(msg.str()); |
| 400 | } |
| 401 | } |
| 402 | |
| 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 | */ |
| 408 | void 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 | */ |
| 425 | void 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 | |
| 436 | |
| 437 | /** |
| 438 | * An unknown option on the command line was detected. |
| 439 | * @param unknown_option The unknown option. |
| 440 | */ |
| 441 | void Logger::print_unknown_cmd_option(const string& unknown_option) const |
| 442 | { |
| 443 | int level = 0; |
| 444 | if ( level <= Loglevel ) |
| 445 | { |
| 446 | ostringstream msg; |
| 447 | msg << "Unknown option on command line detected: " << unknown_option << endl; |
| 448 | log_error(msg.str()); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | |
| 453 | /** |
| 454 | * An unknown protocol was specified. |
| 455 | * @param protocol The unknown protocol. |
| 456 | */ |
| 457 | void Logger::print_unknown_protocol(const string& protocol) const |
| 458 | { |
| 459 | int level = 0; |
| 460 | if ( level <= Loglevel ) |
| 461 | { |
| 462 | ostringstream msg; |
| 463 | msg << "Unknown protocol defined: " << protocol << endl; |
| 464 | log_error(msg.str()); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | |
| 469 | /** |
| 470 | * Loading a service config file. |
| 471 | * @param filename The service config file. |
| 472 | */ |
| 473 | void Logger::print_load_service_conf(const string& filename) const |
| 474 | { |
| 475 | int level = 1; |
| 476 | if ( level <= Loglevel ) |
| 477 | { |
| 478 | ostringstream msg; |
| 479 | msg << "Loading service config file: " << filename << endl; |
| 480 | log_notice(msg.str()); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | |
| 485 | /** |
| 486 | * Loading the main config file. |
| 487 | * @param filename The main config file. |
| 488 | */ |
| 489 | void Logger::print_load_main_conf(const string& filename) const |
| 490 | { |
| 491 | int level = 1; |
| 492 | if ( level <= Loglevel ) |
| 493 | { |
| 494 | ostringstream msg; |
| 495 | msg << "Loading main config file: " << filename << endl; |
| 496 | log_notice(msg.str()); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | |
| 501 | /** |
| 502 | * There is an unknown option in a service config file. |
| 503 | * @param unknown_option The unknown option. |
| 504 | */ |
| 505 | void Logger::print_unknown_service_conf_option(const string& service_conf_file, const string& unknown_option) const |
| 506 | { |
| 507 | int level = 0; |
| 508 | if ( level <= Loglevel ) |
| 509 | { |
| 510 | ostringstream msg; |
| 511 | msg << "Unknown option in service config file detected: " << service_conf_file << " Unknown option: " << unknown_option << endl; |
| 512 | log_error(msg.str()); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | |
| 517 | /** |
| 518 | * There is an unknown option in the main config file. |
| 519 | * @param unknown_option The unknown option. |
| 520 | */ |
| 521 | void Logger::print_unknown_main_conf_option(const string& unknown_option) const |
| 522 | { |
| 523 | int level = 0; |
| 524 | if ( level <= Loglevel ) |
| 525 | { |
| 526 | ostringstream msg; |
| 527 | msg << "Unknown option in main config file detected: " << unknown_option << endl; |
| 528 | log_error(msg.str()); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | |
| 533 | /** |
| 534 | * The defined config path doesn't exist or is not a directory. |
| 535 | * @param config_path The defined config path. |
| 536 | */ |
| 537 | void Logger::print_error_config_path(const string& config_path) const |
| 538 | { |
| 539 | int level = 0; |
| 540 | if ( level <= Loglevel ) |
| 541 | { |
| 542 | ostringstream msg; |
| 543 | msg << "Config path doesn't exists or is not a diretory: " << config_path << endl; |
| 544 | log_error(msg.str()); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | |
| 549 | /** |
| 550 | * There is a missing command line option to initialize a service object. |
| 551 | */ |
| 552 | void Logger::print_missing_cmd_service_option() const |
| 553 | { |
| 554 | int level = 0; |
| 555 | if ( level <= Loglevel ) |
| 556 | { |
| 557 | log_error("Missing option to initialize service. Protocol, host, login and password must be specified."); |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | |
| 562 | /** |
| 563 | * Missing option in service config file. |
| 564 | * @param service_conf_file Service config file |
| 565 | */ |
| 566 | void Logger::print_missing_service_conf_option(const string& service_conf_file) const |
| 567 | { |
| 568 | int level = 0; |
| 569 | if ( level <= Loglevel ) |
| 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; |
| 573 | log_error(msg.str()); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | |
| 578 | /** |
| 579 | * Process running as daemon. |
| 580 | * @param pid The pid of the daemon. |
| 581 | */ |
| 582 | void Logger::print_runnig_as_daemon(const int pid) const |
| 583 | { |
| 584 | int level = 1; |
| 585 | if ( level <= Loglevel ) |
| 586 | { |
| 587 | ostringstream msg; |
| 588 | msg << "Running as daemon: " << pid << endl; |
| 589 | log_notice(msg.str()); |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | |
| 594 | /** |
| 595 | * Prints out the daemon mode. |
| 596 | * @param daemon_mode The daemon mode. |
| 597 | */ |
| 598 | void Logger::print_daemon_mode(const bool daemon_mode) const |
| 599 | { |
| 600 | int level = 1; |
| 601 | if ( level <= Loglevel ) |
| 602 | { |
| 603 | string mode = "disabled"; |
| 604 | if (daemon_mode == true) |
| 605 | mode = "enabled"; |
| 606 | ostringstream msg; |
| 607 | msg << "Daemon mode is " << mode << "." << endl; |
| 608 | log_notice(msg.str()); |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | |
| 613 | /** |
| 614 | * There was an error while trying to fork. |
| 615 | */ |
| 616 | void Logger::print_error_fork() const |
| 617 | { |
| 618 | int level = 0; |
| 619 | if ( level <= Loglevel ) |
| 620 | { |
| 621 | log_error("Error while trying to fork."); |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | |
| 626 | /** |
| 627 | * A pid in the pidfile was found. |
| 628 | * @param pid The pid found in the pidfile. |
| 629 | */ |
| 630 | void Logger::print_pid_found(const int pid) const |
| 631 | { |
| 632 | int level = 1; |
| 633 | if ( level <= Loglevel ) |
| 634 | { |
| 635 | ostringstream msg; |
| 636 | msg << "Pidfile found: " << pid << ". Checking if process is still runnig..." << endl; |
| 637 | log_notice(msg.str()); |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | |
| 642 | /** |
| 643 | * Another process is already running. |
| 644 | * @param pid The pid of the other process. |
| 645 | */ |
| 646 | void Logger::print_process_already_running(const int pid) const |
| 647 | { |
| 648 | int level = 0; |
| 649 | if ( level <= Loglevel ) |
| 650 | { |
| 651 | ostringstream msg; |
| 652 | msg << "Another bpdyndnsd process with PID " << pid << " is already running!" << endl; |
| 653 | log_error(msg.str()); |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | |
| 658 | /** |
| 659 | * SIGTERM caught. |
| 660 | */ |
| 661 | void Logger::print_caught_sigterm() const |
| 662 | { |
| 663 | int level = 0; |
| 664 | if ( level <= Loglevel ) |
| 665 | { |
| 666 | log_notice("Caught SIGTERM. Exiting..."); |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | |
| 671 | /** |
| 672 | * SIGUSR1 caught. |
| 673 | */ |
| 674 | void Logger::print_caught_siguser1() const |
| 675 | { |
| 676 | int level = 0; |
| 677 | if ( level <= Loglevel ) |
| 678 | { |
| 679 | log_notice("Caught SIGUSR1. Switching to offline mode..."); |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | |
| 684 | /** |
| 685 | * SIGHUP caught. |
| 686 | */ |
| 687 | void Logger::print_caught_sighup() const |
| 688 | { |
| 689 | int level = 0; |
| 690 | if ( level <= Loglevel ) |
| 691 | { |
| 692 | log_notice("Caught SIGHUP. Reloading config..."); |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | |
| 697 | /** |
| 698 | * SIGUSR2 caught. |
| 699 | */ |
| 700 | void Logger::print_caught_siguser2() const |
| 701 | { |
| 702 | int level = 0; |
| 703 | if ( level <= Loglevel ) |
| 704 | { |
| 705 | log_notice("Caught SIGUSR2. Switching to online mode..."); |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | |
| 710 | /** |
| 711 | * SIGRTMIN caught. |
| 712 | */ |
| 713 | void Logger::print_caught_sigrtmin() const |
| 714 | { |
| 715 | int level = 0; |
| 716 | if ( level <= Loglevel ) |
| 717 | { |
| 718 | log_notice("Caught SIGRTMIN. Switching to online mode with webcheck enabled..."); |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | |
| 723 | /** |
| 724 | * SIGRTMAX caught. |
| 725 | */ |
| 726 | void 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 | /** |
| 739 | * Error while setting signal handler. |
| 740 | */ |
| 741 | void Logger::print_error_setting_signal(const string& signal) const |
| 742 | { |
| 743 | int level = 0; |
| 744 | if ( level <= Loglevel ) |
| 745 | { |
| 746 | ostringstream msg; |
| 747 | msg << "Error while setting signal handler for: " << signal << endl; |
| 748 | log_error(msg.str()); |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | |
| 753 | /** |
| 754 | * Error while setting signal handler. |
| 755 | */ |
| 756 | void Logger::print_init_log_facility() const |
| 757 | { |
| 758 | int level = 1; |
| 759 | if ( level <= Loglevel ) |
| 760 | { |
| 761 | ostringstream msg; |
| 762 | msg << "Initialized logging facility." << " Loglevel: " << Loglevel << " Syslog: " << Syslog << " ExternalLogOnlyOnce: " << ExternalLogOnlyOnce << endl; |
| 763 | log_notice(msg.str()); |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | |
| 768 | /** |
| 769 | * Be verbose. Currently we are in offline mode. |
| 770 | */ |
| 771 | void Logger::print_offline_mode() const |
| 772 | { |
| 773 | int level = 1; |
| 774 | if ( level <= Loglevel ) |
| 775 | { |
| 776 | log_notice("Offline mode..."); |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | |
| 781 | /** |
| 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 | */ |
| 785 | void 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 | /** |
| 797 | * Objects successfully serialized. |
| 798 | */ |
| 799 | void Logger::print_serialized_objects_success() const |
| 800 | { |
| 801 | int level = 1; |
| 802 | if ( level <= Loglevel ) |
| 803 | { |
| 804 | log_notice("Serialized objects successfully."); |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | |
| 809 | /** |
| 810 | * Objects successfully de-serialized. |
| 811 | */ |
| 812 | void Logger::print_deserialized_objects_success() const |
| 813 | { |
| 814 | int level = 1; |
| 815 | if ( level <= Loglevel ) |
| 816 | { |
| 817 | log_notice("De-serialized objects successfully."); |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | |
| 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. |
| 831 | * @param activated Service's activated. |
| 832 | */ |
| 833 | void Logger::print_service_object(const string& message, const string& protocol, const string& hostname, const string& login, const string& password, const int update_interval, const int max_updates_within_interval, const int max_equal_updates_in_succession, const int dns_cache_ttl , const string& actual_ip, std::map<time_t,std::string> lastupdated, const bool activated) const |
| 834 | { |
| 835 | int level = 1; |
| 836 | if ( level <= Loglevel ) |
| 837 | { |
| 838 | ostringstream msg; |
| 839 | msg << message << endl; |
| 840 | msg << "\t" << "Protocol: " << protocol << endl; |
| 841 | msg << "\t" << "Hostname: " << hostname << endl; |
| 842 | msg << "\t" << "Login: " << login << endl; |
| 843 | |
| 844 | if (LogPasswords) |
| 845 | msg << "\t" << "Password: " << password << endl; |
| 846 | else |
| 847 | msg << "\t" << "Password: (*hidden*)" << endl; |
| 848 | |
| 849 | msg << "\t" << "Update Interval: " << update_interval << endl; |
| 850 | msg << "\t" << "Max Updates: " << max_updates_within_interval << endl; |
| 851 | msg << "\t" << "Max equal Updates:" << max_equal_updates_in_succession << endl; |
| 852 | msg << "\t" << "DNS Cache TTL: " << dns_cache_ttl << endl; |
| 853 | msg << "\t" << "Actual_IP: " << actual_ip << endl; |
| 854 | for ( std::map<time_t,std::string>::reverse_iterator r_iter = lastupdated.rbegin(); r_iter != lastupdated.rend(); r_iter++ ) |
| 855 | { |
| 856 | msg << "\t" << "Lastupdated: " << r_iter->first << " -> " << r_iter->second << endl; |
| 857 | } |
| 858 | msg << "\t" << "Activated: " << activated << endl; |
| 859 | log_notice(msg.str()); |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | |
| 864 | /** |
| 865 | * Caught exception while serialize. |
| 866 | * @param exception Exception message. |
| 867 | */ |
| 868 | void Logger::print_exception_serialize(const string& errMsg) const |
| 869 | { |
| 870 | int level = 0; |
| 871 | if ( level <= Loglevel ) |
| 872 | { |
| 873 | ostringstream msg; |
| 874 | msg << "Error while trying to serialize Serviceholder object: " << errMsg << endl; |
| 875 | log_error(msg.str()); |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | |
| 880 | /** |
| 881 | * Caught exception while de-serialize. |
| 882 | * @param exception Exception message. |
| 883 | */ |
| 884 | void Logger::print_exception_deserialize(const string& errMsg) const |
| 885 | { |
| 886 | int level = 0; |
| 887 | if ( level <= Loglevel ) |
| 888 | { |
| 889 | ostringstream msg; |
| 890 | msg << "Error while trying to de-serialize Serviceholder object: " << errMsg << ". Continue without recovering state from old services!" << endl; |
| 891 | log_error(msg.str()); |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | |
| 896 | /** |
| 897 | * Child couldn't be killed by parent. |
| 898 | * @param pid Pid of the child. |
| 899 | */ |
| 900 | void Logger::print_error_kill_child(const int pid) const |
| 901 | { |
| 902 | int level = 0; |
| 903 | if ( level <= Loglevel ) |
| 904 | { |
| 905 | ostringstream msg; |
| 906 | msg << "Could not kill child process with PID: " << pid << endl; |
| 907 | log_error(msg.str()); |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | |
| 912 | /** |
| 913 | * Child was killed by parent because of error. |
| 914 | * @param pid The pid (child) killed. |
| 915 | */ |
| 916 | void Logger::print_child_killed(const int pid) const |
| 917 | { |
| 918 | int level = 1; |
| 919 | if ( level <= Loglevel ) |
| 920 | { |
| 921 | ostringstream msg; |
| 922 | msg << "Killed child process with PID: " << pid << endl; |
| 923 | log_notice(msg.str()); |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | |
| 928 | /** |
| 929 | * There is no object file. |
| 930 | * @param object_file The object file. |
| 931 | */ |
| 932 | void Logger::print_no_object_file(const string& object_file) |
| 933 | { |
| 934 | int level = 1; |
| 935 | if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) ) |
| 936 | { |
| 937 | ostringstream msg; |
| 938 | msg << "There is no object file: " << object_file << ". Continue without recovering state from old services!" << endl; |
| 939 | log_warning(msg.str(),level); |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | |
| 944 | /** |
| 945 | * Prints out the given hostname |
| 946 | * @param hostname Hostname as string. |
| 947 | */ |
| 948 | void Logger::print_hostname(const string& hostname) const |
| 949 | { |
| 950 | int level = 1; |
| 951 | if ( level <= Loglevel ) |
| 952 | { |
| 953 | ostringstream msg; |
| 954 | msg << "Detected following hostname of localhost: " << hostname << endl; |
| 955 | log_notice(msg.str()); |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | |
| 960 | /** |
| 961 | * Prints out the detected own ipv4 address |
| 962 | * @param ip_addr String representation of the detected ip. |
| 963 | */ |
| 964 | void Logger::print_own_ipv4(const string& ip_addr_v4, const string& hostname) const |
| 965 | { |
| 966 | int level = 1; |
| 967 | if ( level <= Loglevel ) |
| 968 | { |
| 969 | ostringstream msg; |
| 970 | msg << "Detected following IPv4-Address of host: " << hostname << " : " << ip_addr_v4 << endl; |
| 971 | log_notice(msg.str()); |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | |
| 976 | /** |
| 977 | * Prints out the detected own ipv5 address |
| 978 | * @param ip_addr String representation of the detected ip. |
| 979 | */ |
| 980 | void Logger::print_own_ipv6(const string& ip_addr_v6, const string& hostname) const |
| 981 | { |
| 982 | int level = 1; |
| 983 | if ( level <= Loglevel ) |
| 984 | { |
| 985 | ostringstream msg; |
| 986 | msg << "Detected following IPv6-Address of host: " << hostname << " : " << ip_addr_v6 << endl; |
| 987 | log_notice(msg.str()); |
| 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 | */ |
| 997 | void Logger::print_error_hostname_to_ip(const string& errMsg, const string& hostname) const |
| 998 | { |
| 999 | int level = 0; |
| 1000 | if ( level <= Loglevel ) |
| 1001 | { |
| 1002 | ostringstream msg; |
| 1003 | msg << "Could not resolve the hostname: " << hostname << " to an IP-Address: " << errMsg << endl; |
| 1004 | log_error(msg.str()); |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | |
| 1009 | /** |
| 1010 | * The update of the given service was successful. |
| 1011 | * @param service The service. |
| 1012 | */ |
| 1013 | void Logger::print_update_service_successful(const string& service, const std::string& ip_addr) const |
| 1014 | { |
| 1015 | int level = 0; |
| 1016 | if ( level <= Loglevel ) |
| 1017 | { |
| 1018 | ostringstream msg; |
| 1019 | msg << "Updated service successful: " << service << " to IP: " << ip_addr << endl; |
| 1020 | log_notice(msg.str()); |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | |
| 1025 | /** |
| 1026 | * No ip could be determined through webcheck |
| 1027 | */ |
| 1028 | void Logger::print_webcheck_no_ip() |
| 1029 | { |
| 1030 | int level = 0; |
| 1031 | if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) ) |
| 1032 | { |
| 1033 | log_warning("IP-Address of this host could not be determined through any configured webcheck url.",level); |
| 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 | */ |
| 1043 | void Logger::print_webcheck_url_connection_problem(const char * curl_err_buff, const string& url) |
| 1044 | { |
| 1045 | int level = 1; |
| 1046 | if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) ) |
| 1047 | { |
| 1048 | ostringstream msg; |
| 1049 | msg << "There was a problem while trying to connect to following URL: " << url << " CURL error: " << curl_err_buff << endl; |
| 1050 | log_warning(msg.str(),level); |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | |
| 1055 | /** |
| 1056 | * Prints out curl error. |
| 1057 | * @param curl_err_buff Curl error message. |
| 1058 | * @param url URL |
| 1059 | */ |
| 1060 | void Logger::print_webcheck_error(const char * curl_err_buff, const string& url) const |
| 1061 | { |
| 1062 | int level = 0; |
| 1063 | if ( level <= Loglevel ) |
| 1064 | { |
| 1065 | ostringstream msg; |
| 1066 | msg << "There was an error while trying to connect to following URL: " << url << " CURL error: " << curl_err_buff << endl; |
| 1067 | log_error(msg.str()); |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | |
| 1072 | /** |
| 1073 | * Prints out the received data through curl. |
| 1074 | * @param curl_data Data string |
| 1075 | */ |
| 1076 | void Logger::print_received_curl_data(const string& curl_data) const |
| 1077 | { |
| 1078 | int level = 1; |
| 1079 | if ( level <= Loglevel ) |
| 1080 | { |
| 1081 | ostringstream msg; |
| 1082 | msg << "Received CURL data: " << curl_data << endl; |
| 1083 | log_notice(msg.str()); |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | |
| 1088 | /** |
| 1089 | * IP was foudn through regex |
| 1090 | * @param ip The IP found. |
| 1091 | */ |
| 1092 | void Logger::print_regex_found_ip(const string& ip) const |
| 1093 | { |
| 1094 | int level = 1; |
| 1095 | if ( level <= Loglevel ) |
| 1096 | { |
| 1097 | ostringstream msg; |
| 1098 | msg << "Found IP-Address via regex: " << ip << endl; |
| 1099 | log_notice(msg.str()); |
| 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 | */ |
| 1108 | void Logger::print_regex_ip_not_found(const string& data) |
| 1109 | { |
| 1110 | int level = 1; |
| 1111 | if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) ) |
| 1112 | { |
| 1113 | ostringstream msg; |
| 1114 | msg << "Could not extract an IP-Address via regex from following data:\n" << data << endl; |
| 1115 | log_warning(msg.str(),level); |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | |
| 1120 | /** |
| 1121 | * Detected multiple occurrences of the same option. |
| 1122 | * @param message Error message. |
| 1123 | */ |
| 1124 | void Logger::print_multiple_cmd_option(const string& message) const |
| 1125 | { |
| 1126 | int level = 0; |
| 1127 | if ( level <= Loglevel ) |
| 1128 | { |
| 1129 | ostringstream msg; |
| 1130 | msg << "The same option is only allowed once: " << message << endl; |
| 1131 | log_error(msg.str()); |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | |
| 1136 | /** |
| 1137 | * An update would exceed the update interval. Prints out a warning message. |
| 1138 | * @param override_log_level Override log level with zero if true |
| 1139 | * @param current_time Current time. |
| 1140 | * @param old_time Time of update #MaxUpdatesWithinInterval ago. |
| 1141 | * @param MaxUpdatesWithinInterval Number of allowed updates in one update interval. |
| 1142 | * @param service The service which exceeds update interval. |
| 1143 | */ |
| 1144 | void Logger::print_update_not_allowed(bool override_log_level, const time_t current_time, const time_t old_time, const int MaxUpdatesWithinInterval, const string& service) |
| 1145 | { |
| 1146 | int level = 1; |
| 1147 | |
| 1148 | if (override_log_level) |
| 1149 | level = 0; |
| 1150 | |
| 1151 | if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) ) |
| 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; |
| 1155 | log_warning(msg.str(),level); |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | |
| 1160 | /** |
| 1161 | * Failure while running update for service. |
| 1162 | * @param service Services' name. |
| 1163 | */ |
| 1164 | void Logger::print_update_service_failure(const string& service) |
| 1165 | { |
| 1166 | int level = 0; |
| 1167 | if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) ) |
| 1168 | { |
| 1169 | ostringstream msg; |
| 1170 | msg << "Could not update service: " << service << endl; |
| 1171 | log_warning(msg.str(),level); |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | /** |
| 1176 | * Start message |
| 1177 | */ |
| 1178 | void Logger::print_started() const |
| 1179 | { |
| 1180 | int level = 0; |
| 1181 | if ( level <= Loglevel ) |
| 1182 | { |
| 1183 | log_notice("Started"); |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | |
| 1188 | /** |
| 1189 | * Starting shutdown |
| 1190 | */ |
| 1191 | void Logger::print_starting_shutdown() const |
| 1192 | { |
| 1193 | int level = 0; |
| 1194 | if ( level <= Loglevel ) |
| 1195 | { |
| 1196 | log_notice("Shutting down ..."); |
| 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | |
| 1201 | /** |
| 1202 | * Shutdown complete |
| 1203 | */ |
| 1204 | void Logger::print_shutdown_succeeded() const |
| 1205 | { |
| 1206 | int level = 0; |
| 1207 | if ( level <= Loglevel ) |
| 1208 | { |
| 1209 | log_notice("Shutting down complete ..."); |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | |
| 1214 | /** |
| 1215 | * Shutdown parent succeeded |
| 1216 | */ |
| 1217 | void Logger::print_shutdown_parent_succeeded() const |
| 1218 | { |
| 1219 | int level = 0; |
| 1220 | if ( level <= Loglevel ) |
| 1221 | { |
| 1222 | log_notice("Shutting down parent process completed ..."); |
| 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | |
| 1227 | /** |
| 1228 | * Starting shutdown parent |
| 1229 | */ |
| 1230 | void Logger::print_starting_shutdown_parent() const |
| 1231 | { |
| 1232 | int level = 0; |
| 1233 | if ( level <= Loglevel ) |
| 1234 | { |
| 1235 | log_notice("Shutting down parent process ..."); |
| 1236 | } |
| 1237 | } |
| 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 | */ |
| 1247 | void Logger::print_recheck_dns_entry(const string& hostname, const int lastupdated, const int dns_cache_ttl, const int current_time) const |
| 1248 | { |
| 1249 | int level = 1; |
| 1250 | if ( level <= Loglevel ) |
| 1251 | { |
| 1252 | ostringstream msg; |
| 1253 | msg << "DNS cache record for host <" << hostname << "> expired or host will be updated for the first time: Lastupdated: " << lastupdated << " DNS cache ttl: " << dns_cache_ttl << " Current time: " << current_time << " Checking current DNS cache status." << endl; |
| 1254 | log_notice(msg.str()); |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | |
| 1259 | /** |
| 1260 | * Missing proxy option on command line. |
| 1261 | */ |
| 1262 | void Logger::print_missing_cmd_proxy_option() const |
| 1263 | { |
| 1264 | int level = 0; |
| 1265 | if ( level <= Loglevel ) |
| 1266 | { |
| 1267 | log_error("Missing option to initialize proxy. http_proxy and http_proxy_port must be specified."); |
| 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 | */ |
| 1277 | void Logger::print_multiple_service_conf_option(const string& service_conf_file, const string& message) const |
| 1278 | { |
| 1279 | int level = 0; |
| 1280 | if ( level <= Loglevel ) |
| 1281 | { |
| 1282 | ostringstream msg; |
| 1283 | msg << "Multiple occurrences of the same option in service config file detected: " << service_conf_file << " " << message << endl; |
| 1284 | log_error(msg.str()); |
| 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 | */ |
| 1294 | void Logger::print_multiple_main_conf_option(const string& main_conf_file, const string& message) const |
| 1295 | { |
| 1296 | int level = 0; |
| 1297 | if ( level <= Loglevel ) |
| 1298 | { |
| 1299 | ostringstream msg; |
| 1300 | msg << "Multiple occurrences of the same option in main config file detected: " << main_conf_file << " " << message << endl; |
| 1301 | log_error(msg.str()); |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | |
| 1306 | /** |
| 1307 | * Missing proxy option in main config file. |
| 1308 | * @param main_conf_filename The concerning config file. |
| 1309 | */ |
| 1310 | void Logger::print_missing_conf_proxy_option(const string& main_conf_filename) const |
| 1311 | { |
| 1312 | int level = 0; |
| 1313 | if ( level <= Loglevel ) |
| 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; |
| 1317 | log_error(msg.str()); |
| 1318 | } |
| 1319 | } |
| 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 | */ |
| 1326 | void Logger::print_no_domain_part(const string& hostname) const |
| 1327 | { |
| 1328 | int level = 0; |
| 1329 | if ( level <= Loglevel ) |
| 1330 | { |
| 1331 | ostringstream msg; |
| 1332 | msg << "There is no domain part in the given hostname: " << hostname << endl; |
| 1333 | log_notice(msg.str()); |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | |
| 1338 | /** |
| 1339 | * Service is not initialized properly. |
| 1340 | * @param service The service. |
| 1341 | */ |
| 1342 | void Logger::print_httphelper_not_initialized() const |
| 1343 | { |
| 1344 | int level = 0; |
| 1345 | if ( level <= Loglevel ) |
| 1346 | { |
| 1347 | log_error("HTTP-Helper is not initialized properly."); |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | |
| 1352 | /** |
| 1353 | * A curl error occurred. |
| 1354 | * @param msg The error message |
| 1355 | * @param curl_err_code The resulting curl error code |
| 1356 | */ |
| 1357 | void Logger::print_curl_error_init(const std::string& err_msg, const CURLcode curl_err_code) const |
| 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; |
| 1367 | if ( (level <= Loglevel) ) |
| 1368 | { |
| 1369 | ostringstream msg; |
| 1370 | msg << "Curl error: " << err_msg << " Curl error code: " << curl_err_code << " " << curl_err << endl; /*lint !e641 */ |
| 1371 | log_error(msg.str()); |
| 1372 | } |
| 1373 | } |
| 1374 | |
| 1375 | |
| 1376 | /** |
| 1377 | * A curl error occurred. |
| 1378 | * @param url The url requested by the curl operation |
| 1379 | * @param curl_err_code The resulting curl error code |
| 1380 | */ |
| 1381 | void Logger::print_curl_error(const string& url, const CURLcode curl_err_code) const |
| 1382 | { |
| 1383 | string curl_err = ""; |
| 1384 | |
| 1385 | if ( curl_err_code == CURLE_URL_MALFORMAT ) |
| 1386 | curl_err = "CURLE_URL_MALFORMAT"; |
| 1387 | else if ( curl_err_code == CURLE_COULDNT_RESOLVE_HOST ) |
| 1388 | curl_err = "CURLE_COULDNT_RESOLVE_HOST"; |
| 1389 | else if ( curl_err_code == CURLE_COULDNT_CONNECT ) |
| 1390 | curl_err = "CURLE_COULDNT_CONNECT"; |
| 1391 | else |
| 1392 | curl_err = "UNKNOWN"; |
| 1393 | |
| 1394 | int level = 0; |
| 1395 | if ( (level <= Loglevel) ) |
| 1396 | { |
| 1397 | ostringstream msg; |
| 1398 | msg << "Curl error while requesting following url: " << url << " Curl error code: " << curl_err_code << " " << curl_err << endl; /*lint !e641 */ |
| 1399 | log_error(msg.str()); |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | |
| 1404 | /** |
| 1405 | * A curl error occurred. |
| 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 | */ |
| 1410 | void Logger::print_curl_error(const string& url, const CURLcode curl_err_code, const char * curl_err_buff) const |
| 1411 | { |
| 1412 | string curl_err = ""; |
| 1413 | |
| 1414 | if ( curl_err_code == CURLE_URL_MALFORMAT ) |
| 1415 | curl_err = "CURLE_URL_MALFORMAT"; |
| 1416 | else if ( curl_err_code == CURLE_COULDNT_RESOLVE_HOST ) |
| 1417 | curl_err = "CURLE_COULDNT_RESOLVE_HOST"; |
| 1418 | else if ( curl_err_code == CURLE_COULDNT_CONNECT ) |
| 1419 | curl_err = "CURLE_COULDNT_CONNECT"; |
| 1420 | else |
| 1421 | curl_err = "UNKNOWN"; |
| 1422 | |
| 1423 | int level = 0; |
| 1424 | if ( (level <= Loglevel) ) |
| 1425 | { |
| 1426 | ostringstream msg; |
| 1427 | msg << "Curl error while requesting following url: " << url << " Curl error code: " << curl_err_code << " " << curl_err << " " << curl_err_buff << endl; /*lint !e641 */ |
| 1428 | log_error(msg.str()); |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | |
| 1433 | /** |
| 1434 | * Prints out the data received by curl operation |
| 1435 | * @param CurlWritedataBuff |
| 1436 | */ |
| 1437 | void Logger::print_curl_data(const string& curl_writedata_buff) const |
| 1438 | { |
| 1439 | int level = 1; |
| 1440 | if ( level <= Loglevel ) |
| 1441 | { |
| 1442 | ostringstream msg; |
| 1443 | msg << "Data received by curl: " << curl_writedata_buff << endl; |
| 1444 | log_notice(msg.str()); |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | |
| 1449 | /** |
| 1450 | * Not authorized to perform requested update operation |
| 1451 | * @param service The requested service. |
| 1452 | * @param username Username |
| 1453 | * @param password Password |
| 1454 | */ |
| 1455 | void Logger::print_service_not_authorized(const string& service, const string& username, const string& password) const |
| 1456 | { |
| 1457 | int level = 0; |
| 1458 | if ( level <= Loglevel ) |
| 1459 | { |
| 1460 | ostringstream msg; |
| 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 | |
| 1468 | log_notice(msg.str()); |
| 1469 | } |
| 1470 | } |
| 1471 | |
| 1472 | |
| 1473 | /** |
| 1474 | * Prints out the http status code |
| 1475 | * @param url Url |
| 1476 | * @param output HTTP status code |
| 1477 | */ |
| 1478 | void Logger::print_http_status_code(const string& url, const long http_code) const |
| 1479 | { |
| 1480 | int level = 1; |
| 1481 | if ( level <= Loglevel ) |
| 1482 | { |
| 1483 | ostringstream msg; |
| 1484 | msg << "Requested URL: " << url << " Received HTTP status code: " << http_code << endl; |
| 1485 | log_notice(msg.str()); |
| 1486 | } |
| 1487 | } |
| 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 | */ |
| 1495 | void Logger::print_update_failure(const string& url, const string& curl_data) const |
| 1496 | { |
| 1497 | int level = 0; |
| 1498 | if ( (level <= Loglevel) ) |
| 1499 | { |
| 1500 | ostringstream msg; |
| 1501 | msg << "Problem while trying to updating service. Requested URL: " << url << " Error Code from Server: " << curl_data << endl; |
| 1502 | log_error(msg.str()); |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | |
| 1507 | /** |
| 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 | */ |
| 1512 | void Logger::print_update_failure(const string& url, const long http_status_code) const |
| 1513 | { |
| 1514 | int level = 0; |
| 1515 | if ( level <= Loglevel ) |
| 1516 | { |
| 1517 | ostringstream msg; |
| 1518 | msg << "Problem while trying to updating service. Requested URL: " << url << " Error Code from Server: " << http_status_code << endl; |
| 1519 | log_error(msg.str()); |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | /** |
| 1524 | * Hostname is invalid, contains no or only one domain part. |
| 1525 | * @param hostname The full qualified host name. |
| 1526 | */ |
| 1527 | void Logger::print_invalid_hostname(const string& hostname) |
| 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 | } |
| 1537 | |
| 1538 | |
| 1539 | /** |
| 1540 | * An IP in a private range was detected |
| 1541 | * @param ip The private IP |
| 1542 | */ |
| 1543 | void Logger::print_ip_is_local(const string& ip) |
| 1544 | { |
| 1545 | int level = 1; |
| 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 | */ |
| 1560 | void Logger::print_regex_match(const string& regex, const string& matching_string) const |
| 1561 | { |
| 1562 | int level = 1; |
| 1563 | if ( level <= Loglevel ) |
| 1564 | { |
| 1565 | ostringstream msg; |
| 1566 | msg << "Regex: " << regex << " is matching in: " << matching_string << endl; |
| 1567 | log_notice(msg.str()); |
| 1568 | } |
| 1569 | } |
| 1570 | |
| 1571 | |
| 1572 | /** |
| 1573 | * Regex is not matching |
| 1574 | * @param regex Regex |
| 1575 | * @param not_matching_string String |
| 1576 | */ |
| 1577 | void Logger::print_no_regex_match(const string& regex, const string& not_matching_string) |
| 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 | */ |
| 1593 | void Logger::print_could_not_parse_received_data(const string& curl_data) const |
| 1594 | { |
| 1595 | int level = 0; |
| 1596 | if ( (level <= Loglevel) ) |
| 1597 | { |
| 1598 | ostringstream msg; |
| 1599 | msg << "Could not parse salt, time and sign from initial gnudip server reply: " << curl_data << endl; |
| 1600 | log_error(msg.str()); |
| 1601 | } |
| 1602 | } |
| 1603 | |
| 1604 | |
| 1605 | /** |
| 1606 | * Gnudip salt, time and sign could not be got from map |
| 1607 | */ |
| 1608 | void Logger::print_could_not_get_initial_gnudip_data() const |
| 1609 | { |
| 1610 | int level = 0; |
| 1611 | if ( (level <= Loglevel) ) |
| 1612 | { |
| 1613 | log_error("Could not get salt, time and sign from map."); |
| 1614 | } |
| 1615 | } |
| 1616 | |
| 1617 | |
| 1618 | |
| 1619 | /** |
| 1620 | * Gnudip protocol requires explicit declaration of a servername. |
| 1621 | */ |
| 1622 | void Logger::print_gnudip_requires_servername() |
| 1623 | { |
| 1624 | int level = 0; |
| 1625 | if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) ) |
| 1626 | { |
| 1627 | log_warning("Gnudip requires explicit definition of servername via config!",level); |
| 1628 | } |
| 1629 | } |
| 1630 | |
| 1631 | |
| 1632 | /** |
| 1633 | * An exception occurred while computing the md5 sum. |
| 1634 | * @param what The exception occurred. |
| 1635 | */ |
| 1636 | void Logger::print_exception_md5_sum(const string& what) const |
| 1637 | { |
| 1638 | int level = 0; |
| 1639 | if ( level <= Loglevel ) |
| 1640 | { |
| 1641 | ostringstream msg; |
| 1642 | msg << "An exception occurred while computing a md5 sum: " << what << endl; |
| 1643 | log_error(msg.str()); |
| 1644 | } |
| 1645 | } |
| 1646 | |
| 1647 | |
| 1648 | /** |
| 1649 | * A network exception occurred. |
| 1650 | * @param what The exception occurred. |
| 1651 | */ |
| 1652 | void Logger::print_network_error(const string& what) const |
| 1653 | { |
| 1654 | int level = 0; |
| 1655 | if ( level <= Loglevel ) |
| 1656 | { |
| 1657 | ostringstream msg; |
| 1658 | msg << "A network exception occurred: " << what << endl; |
| 1659 | log_error(msg.str()); |
| 1660 | } |
| 1661 | } |
| 1662 | |
| 1663 | |
| 1664 | /** |
| 1665 | * An undefined protocol error occurred. |
| 1666 | * @param protocol The protocol |
| 1667 | * @param error The error |
| 1668 | */ |
| 1669 | void Logger::print_undefined_protocol_error(const string& protocol, const string& error) const |
| 1670 | { |
| 1671 | int level = 0; |
| 1672 | if ( level <= Loglevel ) |
| 1673 | { |
| 1674 | ostringstream msg; |
| 1675 | msg << "An undefined protocol error occurred. Protocol: " << protocol << " Error: " << error << endl; |
| 1676 | log_error(msg.str()); |
| 1677 | } |
| 1678 | } |
| 1679 | |
| 1680 | /** |
| 1681 | * Error while trying to log through external program. |
| 1682 | * @param external_prog The external program called. |
| 1683 | */ |
| 1684 | void Logger::print_error_external_logging(const string& external_prog) const |
| 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; |
| 1691 | log_notice(msg.str()); |
| 1692 | } |
| 1693 | } |
| 1694 | |
| 1695 | |
| 1696 | /** |
| 1697 | * Error while parsing config file. |
| 1698 | * @param error Error occurred. |
| 1699 | * @param config_file Config file. |
| 1700 | */ |
| 1701 | void 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; |
| 1708 | log_notice(msg.str()); |
| 1709 | } |
| 1710 | } |
| 1711 | |
| 1712 | |
| 1713 | /** |
| 1714 | * Error while parsing cmd option |
| 1715 | * @param error Error |
| 1716 | */ |
| 1717 | void 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; |
| 1724 | log_error(msg.str()); |
| 1725 | } |
| 1726 | } |
| 1727 | |
| 1728 | |
| 1729 | /** |
| 1730 | * The webcheck interval was exceeded, so webcheck not allowed. |
| 1731 | * @param last_webcheck Time of last webcheck. |
| 1732 | * @param webcheck_interval Webcheck interval time in seconds. |
| 1733 | * @param current_time Current system time. |
| 1734 | */ |
| 1735 | void Logger::print_webcheck_exceed_interval( const time_t last_webcheck, const int webcheck_interval, const time_t current_time ) const |
| 1736 | { |
| 1737 | int level = 1; |
| 1738 | if ( level <= Loglevel ) |
| 1739 | { |
| 1740 | ostringstream msg; |
| 1741 | msg << "Skipped webcheck in idle period (" << (last_webcheck+webcheck_interval)-current_time << " more seconds"; |
| 1742 | msg << ", last_webcheck: " << last_webcheck << ", webcheck_interval(sec): " << webcheck_interval << ")" << endl; |
| 1743 | log_notice(msg.str()); |
| 1744 | } |
| 1745 | } |
| 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 | */ |
| 1754 | void Logger::print_check_service_update(const string& hostname, const time_t current_time, const time_t lastupdated) const |
| 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; |
| 1761 | log_notice(msg.str()); |
| 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 | */ |
| 1773 | void Logger::print_cached_dns_entry(const string& hostname, const string& ip_dns_recheck, const string& ip_last_update, const string& ip_host) const |
| 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; |
| 1780 | log_notice(msg.str()); |
| 1781 | } |
| 1782 | } |
| 1783 | |
| 1784 | |
| 1785 | /** |
| 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 | */ |
| 1793 | void Logger::print_update_service(const string& hostname, const string& ip_dns_recheck, const string& ip_last_update, const string& ip_host, const time_t lastupdated) const{ |
| 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; |
| 1799 | log_notice(msg.str()); |
| 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 | */ |
| 1814 | void Logger::print_update_service_ttl_expired(const string& hostname, const string& ip_dns_recheck, const string& ip_last_update, const string& ip_host, const time_t lastupdated, const int dns_cache_ttl, const time_t current_time) const |
| 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; |
| 1821 | log_notice(msg.str()); |
| 1822 | } |
| 1823 | } |
| 1824 | |
| 1825 | |
| 1826 | /** |
| 1827 | * TTL expired |
| 1828 | * @param override_log_level Override log level with zero if true |
| 1829 | * @param hostname Hostname |
| 1830 | * @param ip_dns_recheck Cached DNS entry |
| 1831 | * @param ip_last_update IP set in last update |
| 1832 | * @param ip_host Hosts IP |
| 1833 | * @param lastupdated Last updated |
| 1834 | * @param dns_cache_ttl DNS cache ttl |
| 1835 | * @param current_time Current time |
| 1836 | */ |
| 1837 | void Logger::print_update_service_ttl_not_expired(bool override_log_level, const string& hostname, const string& ip_dns_recheck, const string& ip_last_update, const string& ip_host, const time_t lastupdated, const int dns_cache_ttl, const time_t current_time) const |
| 1838 | { |
| 1839 | int level = 1; |
| 1840 | |
| 1841 | if (override_log_level) |
| 1842 | level = 0; |
| 1843 | |
| 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; |
| 1848 | log_notice(msg.str()); |
| 1849 | } |
| 1850 | } |
| 1851 | |
| 1852 | |
| 1853 | /** |
| 1854 | * No update needed |
| 1855 | * @param override_log_level Override log level with zero if true |
| 1856 | * @param hostname Hostname |
| 1857 | * @param ip_dns_recheck Cached DNS entry |
| 1858 | * @param ip_last_update IP set in last update |
| 1859 | * @param ip_host Hosts IP |
| 1860 | * @param lastupdated Last updated |
| 1861 | */ |
| 1862 | void Logger::print_no_update_needed(bool override_log_level, const string& hostname, const string& ip_dns_recheck, const string& ip_last_update, const string& ip_host, const time_t lastupdated) const |
| 1863 | { |
| 1864 | int level = 1; |
| 1865 | |
| 1866 | if (override_log_level) |
| 1867 | level = 0; |
| 1868 | |
| 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; |
| 1873 | log_notice(msg.str()); |
| 1874 | } |
| 1875 | } |
| 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 | */ |
| 1883 | void 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; |
| 1890 | log_error(msg.str()); |
| 1891 | } |
| 1892 | } |
| 1893 | |
| 1894 | |
| 1895 | /** |
| 1896 | * Could not get IP address of local wan interface. |
| 1897 | * @param override_log_level Override log level with zero if true |
| 1898 | */ |
| 1899 | void 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 | |
| 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 | */ |
| 1919 | void 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 | } |
| 1933 | |
| 1934 | /** |
| 1935 | * Could not resolve current IP for DNS name |
| 1936 | * @param override_log_level Override log level with zero if true |
| 1937 | */ |
| 1938 | void Logger::print_dns_lookup_failed(bool override_log_level, const std::string &hostname) const |
| 1939 | { |
| 1940 | int level = 1; |
| 1941 | |
| 1942 | if (override_log_level) |
| 1943 | level = 0; |
| 1944 | |
| 1945 | if ( level <= Loglevel ) |
| 1946 | { |
| 1947 | ostringstream msg; |
| 1948 | msg << "Could not resolve IP address for host " << hostname << ". Will retry later." << endl; |
| 1949 | log_error(msg.str()); |
| 1950 | } |
| 1951 | } |
| 1952 | |
| 1953 | |
| 1954 | /** |
| 1955 | * Invalid service config was detected. |
| 1956 | */ |
| 1957 | void 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 | } |
| 1965 | |
| 1966 | |
| 1967 | /** |
| 1968 | * Print simple message. |
| 1969 | */ |
| 1970 | void Logger::print_msg( const string& msg ) const |
| 1971 | { |
| 1972 | int level = 0; |
| 1973 | if ( level <= Loglevel ) |
| 1974 | { |
| 1975 | log_error(msg); |
| 1976 | } |
| 1977 | } |
| 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 | */ |
| 1987 | void Logger::print_last_updates( const std::string& ip_host, const time_t current_time, const int update_interval, const int max_updates_within_interval, const int max_equal_updates_in_succession, const std::map<time_t,std::string>& last_updates, const std::string& servicename ) const |
| 1988 | { |
| 1989 | int level = 1; |
| 1990 | |
| 1991 | if ( level <= Loglevel ) |
| 1992 | { |
| 1993 | ostringstream msg; |
| 1994 | msg << "Last updates for " << servicename << " : "; |
| 1995 | |
| 1996 | int i = 0; |
| 1997 | |
| 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++ ) |
| 1999 | { |
| 2000 | msg << r_iter->first << "->" << r_iter->second; |
| 2001 | i++; |
| 2002 | } |
| 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; |
| 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 | */ |
| 2020 | void 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; |
| 2026 | msg << "IP address " << ip_host << " was updated too often in succession, host " << hostname << " blocked until IP address will be different." << endl; |
| 2027 | log_error(msg.str()); |
| 2028 | } |
| 2029 | } |