Separated Service update into update and perform_update.
[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
11#include "logger.h"
12
88a594e8
BS
13#include <syslog.h>
14#include <sstream>
15
8bca3c5d
BS
16namespace po = boost::program_options;
17
18using namespace std;
254bbf53
BS
19
20/**
21 * Default Constructor
22 */
23Logger::Logger()
8bca3c5d 24 : Loglevel(0)
2e956a36 25 , Syslog(false)
254bbf53 26{
59c8d63c 27 set_log_facility(Loglevel,Syslog);
254bbf53
BS
28}
29
30
31/**
32 * Default Destructor
33 */
34Logger::~Logger()
35{
254bbf53
BS
36}
37
38
39/**
59c8d63c
BS
40 * Decides if Logging through syslog if enabled or through std.
41 * @param msg The message to log.
42 */
b38684ce 43void Logger::log_notice(const string& msg) const
59c8d63c
BS
44{
45 if ( Syslog )
46 syslog(LOG_NOTICE,msg.c_str());
47 else
27baf279 48 cout << msg << endl;;
59c8d63c
BS
49}
50
51
52/**
53 * Decides if Logging through syslog if enabled or through std.
54 * @param msg The message to log.
55 */
b38684ce 56void Logger::log_warning(const string& msg) const
59c8d63c
BS
57{
58 if ( Syslog )
59 syslog(LOG_WARNING,msg.c_str());
60 else
27baf279 61 cout << msg << endl;
59c8d63c
BS
62}
63
64
65/**
66 * Decides if Logging through syslog if enabled or through std.
67 * @param msg The message to log.
68 */
b38684ce 69void Logger::log_error(const string& msg) const
59c8d63c
BS
70{
71 if ( Syslog )
72 syslog(LOG_ERR,msg.c_str());
73 else
27baf279 74 cerr << msg << endl;
59c8d63c
BS
75}
76
77
78/**
8bca3c5d
BS
79 * Setter for member Loglevel.
80 * @param _loglevel Value to set Loglevel to.
81 */
82void Logger::set_loglevel(const int _loglevel)
83{
84 Loglevel = _loglevel;
2e956a36 85 cout << "Loglevel set" << endl;
8bca3c5d
BS
86}
87
88
89/**
90 * Getter for member Loglevel.
91 * @return Loglevel.
92 */
b38684ce 93int Logger::get_loglevel() const
8bca3c5d
BS
94{
95 return Loglevel;
96}
97
98
99/**
100 * Setter for member Syslog.
101 * @param _syslog Wether to log through syslog or not.
102 */
103void Logger::set_syslog(const bool _syslog)
104{
105 Syslog = _syslog;
106}
107
108
109/**
110 * Getter for member Syslog.
111 * @return True if logging through syslog is enabled, false otherwise.
112 */
b38684ce 113bool Logger::get_syslog() const
8bca3c5d
BS
114{
115 return Syslog;
116}
117
118
119/**
120 * Initialize the logging facility.
121 */
122void Logger::set_log_facility(const int _loglevel, const bool _syslog)
123{
124 Loglevel = _loglevel;
125 Syslog = _syslog;
126
127 if ( Syslog )
128 openlog("bpdyndnsd",LOG_PID,LOG_DAEMON);
129}
130
131
132/**
254bbf53
BS
133 * Prints out the usage to the command line.
134 */
b38684ce 135void Logger::print_usage(const Options_descriptionPtr opt_desc) const
254bbf53 136{
59c8d63c
BS
137 if ( 0 <= Loglevel )
138 {
139 ostringstream msg;
140 msg << "Usage: bpdyndnsd [Command line options]" << "\n" << endl;
141 msg << *opt_desc << endl;
142 log_notice(msg.str());
143 }
254bbf53
BS
144}
145
146
147/**
148 * Prints out the programm name and the given version string on stdout.
149 * @param version Version string.
150 */
b38684ce 151void Logger::print_version() const
254bbf53 152{
59c8d63c
BS
153 if ( 0 <= Loglevel )
154 {
155 ostringstream msg;
156 msg << "Bullet proof dynamic dns daemon.\nbpdyndnsd " << VERSION << "." << REVISION << "." << RELEASE << endl;
157 log_notice(msg.str());
158 }
254bbf53
BS
159}
160
161
162/**
163 * Prints out the successful parsing of the command line options.
164 */
b38684ce 165void Logger::print_cmd_parsed() const
254bbf53 166{
59c8d63c
BS
167 if ( 1 <= Loglevel )
168 {
169 ostringstream msg;
170 msg << "Command line options successfully parsed." << endl;
171 log_notice(msg.str());
172 }
254bbf53
BS
173}
174
175
b38684ce 176void Logger::print_conf_files_parsed() const
667c672c
BS
177{
178 if ( 1 <= Loglevel )
179 {
180 ostringstream msg;
181 msg << "Config files successfully parsed." << endl;
182 log_notice(msg.str());
183 }
184}
185
186
254bbf53
BS
187/**
188 * Prints out the successful parsing of the config files.
daf2ea82 189 * @param config_path The specified config path.
254bbf53 190 */
b38684ce 191void Logger::print_conf_loaded(const string& config_path) const
254bbf53 192{
59c8d63c
BS
193 if ( 1 <= Loglevel )
194 {
195 ostringstream msg;
196 msg << "Config files successfully loaded in: " << config_path << endl;
197 log_notice(msg.str());
198 }
254bbf53
BS
199}
200
201
202/**
daf2ea82
BS
203 * Prints out the unsuccessful parsing of the config files.
204 * @param config_path The specified config path.
254bbf53 205 */
b38684ce 206void Logger::print_conf_not_loaded(const string& config_path) const
254bbf53 207{
59c8d63c
BS
208 if ( 0 <= Loglevel )
209 {
210 ostringstream msg;
211 msg << "Config files couldn't be loaded in: " << config_path << endl;
212 log_error(msg.str());
213 }
254bbf53
BS
214}
215
216
217/**
daf2ea82
BS
218 * A file could not be opened for reading.
219 * @param filename The filename.
254bbf53 220 */
b38684ce 221void Logger::print_error_opening_r(const string& filename) const
254bbf53 222{
59c8d63c
BS
223 if ( 0 <= Loglevel )
224 {
225 ostringstream msg;
226 msg << "Error opening file for reading: " << filename << endl;
227 log_error(msg.str());
228 }
254bbf53
BS
229}
230
231
232/**
667c672c
BS
233 * A file could not be opened for writing.
234 * @param filename The filename.
235 */
b38684ce 236void Logger::print_error_opening_rw(const string& filename) const
667c672c
BS
237{
238 if ( 0 <= Loglevel )
239 {
240 ostringstream msg;
241 msg << "Error opening file for writing: " << filename << endl;
242 log_error(msg.str());
243 }
244}
245
246
247/**
254bbf53
BS
248 * Desctructor of specified class was called.
249 * @param _class Name of the class.
250 */
b38684ce 251void Logger::print_destructor_call(const string& _class) const
254bbf53 252{
59c8d63c
BS
253 if ( 1 <= Loglevel )
254 {
255 ostringstream msg;
256 msg << "Destructor call: " << _class << endl;
257 log_notice(msg.str());
258 }
254bbf53
BS
259}
260
261
262/**
263 * Constructor of specified class was called.
264 * @param _class Name of the class.
265 */
b38684ce 266void Logger::print_constructor_call(const string& _class) const
254bbf53 267{
59c8d63c
BS
268 if ( 1 <= Loglevel )
269 {
270 ostringstream msg;
271 msg << "Constructor call: " << _class << endl;
272 log_notice(msg.str());
273 }
254bbf53
BS
274}
275
276
277/**
278 * Update method for specified service was called.
daf2ea82 279 * @param service The service for which is the update running.
254bbf53 280 */
b38684ce 281void Logger::print_update_service(const string& service) const
254bbf53 282{
59c8d63c
BS
283 if ( 0 <= Loglevel )
284 {
285 ostringstream msg;
286 msg << "Running update for service: " << service << endl;
287 log_notice(msg.str());
288 }
254bbf53
BS
289}
290
291
daf2ea82
BS
292/**
293 * An unknown option on the command line was detected.
294 * @param unknown_option The unknown option.
295 */
b38684ce 296void Logger::print_unknown_cmd_option(const string& unknown_option) const
254bbf53 297{
59c8d63c
BS
298 if ( 0 <= Loglevel )
299 {
300 ostringstream msg;
301 msg << "Unknown option on command line detected: " << unknown_option << endl;
302 log_error(msg.str());
303 }
254bbf53
BS
304}
305
306
daf2ea82
BS
307/**
308 * An unknown protocol was specified.
309 * @param protocol The unknown protocol.
310 */
b38684ce 311void Logger::print_unknown_protocol(const string& protocol) const
254bbf53 312{
59c8d63c
BS
313 if ( 0 <= Loglevel )
314 {
315 ostringstream msg;
316 msg << "Unknown protocol defined: " << protocol << endl;
317 log_error(msg.str());
318 }
254bbf53
BS
319}
320
321
daf2ea82
BS
322/**
323 * Loading a service config file.
324 * @param filename The service config file.
325 */
b38684ce 326void Logger::print_load_service_conf(const string& filename) const
254bbf53 327{
59c8d63c
BS
328 if ( 1 <= Loglevel )
329 {
330 ostringstream msg;
331 msg << "Loading service config file: " << filename << endl;
332 log_notice(msg.str());
333 }
254bbf53
BS
334}
335
336
daf2ea82
BS
337/**
338 * Loading the main config file.
339 * @param filename The main config file.
340 */
b38684ce 341void Logger::print_load_main_conf(const string& filename) const
254bbf53 342{
59c8d63c
BS
343 if ( 1 <= Loglevel )
344 {
345 ostringstream msg;
346 msg << "Loading main config file: " << filename << endl;
347 log_notice(msg.str());
348 }
254bbf53
BS
349}
350
351
daf2ea82
BS
352/**
353 * There is an unknown option in a service config file.
354 * @param unknown_option The unknown option.
355 */
b38684ce 356void Logger::print_unknown_service_conf_option(const string& unknown_option) const
254bbf53 357{
59c8d63c
BS
358 if ( 0 <= Loglevel )
359 {
360 ostringstream msg;
361 msg << "Unknown option in service config file detected: " << unknown_option << endl;
362 log_error(msg.str());
363 }
254bbf53
BS
364}
365
366
daf2ea82
BS
367/**
368 * There is an unknown option in the main config file.
369 * @param unknown_option The unknown option.
370 */
b38684ce 371void Logger::print_unknown_main_conf_option(const string& unknown_option) const
254bbf53 372{
59c8d63c
BS
373 if ( 0 <= Loglevel )
374 {
375 ostringstream msg;
376 msg << "Unknown option in main config file detected: " << unknown_option << endl;
377 log_error(msg.str());
378 }
254bbf53
BS
379}
380
381
daf2ea82
BS
382/**
383 * The defined config path doesn't exist or is not a directory.
384 * @param config_path The defined config path.
385 */
b38684ce 386void Logger::print_error_config_path(const string& config_path) const
254bbf53 387{
59c8d63c
BS
388 if ( 0 <= Loglevel )
389 {
390 ostringstream msg;
391 msg << "Config path doesn't exists or is not a diretory: " << config_path << endl;
392 log_error(msg.str());
393 }
254bbf53
BS
394}
395
396
daf2ea82
BS
397/**
398 * There is a missing command line option to initialize a service object.
399 */
b38684ce 400void Logger::print_missing_cmd_service_option() const
254bbf53 401{
59c8d63c
BS
402 if ( 0 <= Loglevel )
403 {
404 ostringstream msg;
405 msg << "Missing option to initialize service. Protocol, host, login and password must be specified." << endl;
406 log_error(msg.str());
407 }
254bbf53 408}
388f4ab0
BS
409
410
c5675c01
BS
411/**
412 * Process running as daemon.
413 * @param pid The pid of the daemon.
414 */
b38684ce 415void Logger::print_runnig_as_daemon(const int pid) const
388f4ab0 416{
59c8d63c
BS
417 if ( 1 <= Loglevel )
418 {
419 ostringstream msg;
420 msg << "Runnig as daemon: " << pid << endl;
421 log_notice(msg.str());
422 }
388f4ab0
BS
423}
424
c5675c01
BS
425
426/**
427 * Prints out the daemon mode.
428 * @param daemon_mode The daemon mode.
429 */
b38684ce 430void Logger::print_daemon_mode(const bool daemon_mode) const
388f4ab0 431{
59c8d63c
BS
432 if ( 1 <= Loglevel )
433 {
434 string mode = "disabled";
435 if (daemon_mode == true)
436 mode = "enabled";
437 ostringstream msg;
438 msg << "Daemon mode is " << mode << "." << endl;
439 log_notice(msg.str());
440 }
388f4ab0
BS
441}
442
443
c5675c01
BS
444/**
445 * There was an error while trying to fork.
446 */
b38684ce 447void Logger::print_error_fork() const
388f4ab0 448{
59c8d63c
BS
449 if ( 0 <= Loglevel )
450 {
451 ostringstream msg;
452 msg << "Error while trying to fork." << endl;
453 log_notice(msg.str());
454 }
388f4ab0
BS
455}
456
457
c5675c01
BS
458/**
459 * A pid in the pidfile was found.
460 * @param pid The pid found in the pidfile.
461 */
b38684ce 462void Logger::print_pid_found(const int pid) const
388f4ab0 463{
59c8d63c
BS
464 if ( 1 <= Loglevel )
465 {
466 ostringstream msg;
467 msg << "Pidfile found: " << pid << ". Checking if process is still runnig..." << endl;
468 log_notice(msg.str());
469 }
388f4ab0
BS
470}
471
472
c5675c01
BS
473/**
474 * Another process is already running.
475 * @param pid The pid of the other process.
476 */
b38684ce 477void Logger::print_process_already_running(const int pid) const
388f4ab0 478{
59c8d63c
BS
479 if ( 0 <= Loglevel )
480 {
481 ostringstream msg;
482 msg << "Another bpdyndnsd process with PID " << pid << " is already running!" << endl;
483 log_error(msg.str());
484 }
388f4ab0 485}
c5675c01
BS
486
487
488/**
489 * SIGTERM caught.
490 */
b38684ce 491void Logger::print_caught_sigterm() const
c5675c01 492{
59c8d63c
BS
493 if ( 0 <= Loglevel )
494 {
495 ostringstream msg;
496 msg << "Caught SIGTERM. Exiting..." << endl;
497 log_notice(msg.str());
498 }
c5675c01
BS
499}
500
501
502/**
503 * SIGUSR1 caught.
504 */
b38684ce 505void Logger::print_caught_siguser1() const
c5675c01 506{
59c8d63c
BS
507 if ( 0 <= Loglevel )
508 {
509 ostringstream msg;
510 msg << "Caught SIGUSR1. Switching to offline mode..." << endl;
511 log_notice(msg.str());
512 }
c5675c01
BS
513}
514
515
516/**
517 * SIGHUP caught.
518 */
b38684ce 519void Logger::print_caught_sighup() const
c5675c01 520{
59c8d63c
BS
521 if ( 0 <= Loglevel )
522 {
523 ostringstream msg;
524 msg << "Caught SIGHUP. Reloading config and switching to online mode..." << endl;
525 log_notice(msg.str());
526 }
c5675c01 527}
8bca3c5d
BS
528
529
530/**
531 * Error while setting signal handler.
532 */
b38684ce 533void Logger::print_error_setting_signal(const string& signal) const
8bca3c5d 534{
59c8d63c
BS
535 if ( 0 <= Loglevel )
536 {
537 ostringstream msg;
667c672c 538 msg << "Error while setting signal handler for: " << signal << endl;
59c8d63c
BS
539 log_error(msg.str());
540 }
8bca3c5d
BS
541}
542
543
544/**
545 * Error while setting signal handler.
546 */
b38684ce 547void Logger::print_init_log_facility() const
8bca3c5d 548{
59c8d63c
BS
549 if ( 1 <= Loglevel )
550 {
551 ostringstream msg;
552 msg << "Initialized logging facility." << " Loglevel: " << Loglevel << " Syslog: " << Syslog << endl;
553 log_notice(msg.str());
554 }
8bca3c5d
BS
555}
556
27baf279 557
8bca3c5d
BS
558/**
559 * Be verbose. Currently we are in offline mode.
560 */
b38684ce 561void Logger::print_offline_mode() const
8bca3c5d 562{
59c8d63c
BS
563 if ( 0 <= Loglevel )
564 {
565 ostringstream msg;
566 msg << "Offline mode..." << endl;
567 log_notice(msg.str());
568 }
8bca3c5d 569}
27baf279
BS
570
571
572/**
573 * Objects successfully serialized.
574 */
b38684ce 575void Logger::print_serialized_objects_success() const
27baf279
BS
576{
577 if ( 1 <= Loglevel )
578 {
579 ostringstream msg;
580 msg << "Serialized objects successfully." << endl;
581 log_notice(msg.str());
582 }
583}
584
585
586/**
587 * Objects successfully de-serialized.
588 */
b38684ce 589void Logger::print_deserialized_objects_success() const
27baf279
BS
590{
591 if ( 1 <= Loglevel )
592 {
593 ostringstream msg;
594 msg << "De-serialized objects successfully." << endl;
595 log_notice(msg.str());
596 }
597}
598
599
5d38cfe6
BS
600/**
601 * Prints out the content of a service object.
602 * @param message Message to be added on output first.
603 * @param protocol Service's protocol.
604 * @param hostname Service's hostname.
605 * @param login Service's login.
606 * @param password Service's password.
607 * @param actual_ip Service's actual_ip.
608 * @param lastupdated Service's lastupdated.
609 */
b38684ce 610void Logger::print_service_object(const string& message, const string& protocol, const string& hostname, const string& login, const string& password, const string& actual_ip, const int lastupdated) const
27baf279
BS
611{
612 if ( 1 <= Loglevel )
613 {
614 ostringstream msg;
615 msg << message << endl;
616 msg << "\t" << "Protocol: " << protocol << endl;
617 msg << "\t" << "Hostname: " << hostname << endl;
618 msg << "\t" << "Login: " <<login << endl;
619 msg << "\t" << "Password: " <<password << endl;
620 msg << "\t" << "Actual_IP: " <<actual_ip << endl;
621 msg << "\t" << "Lastupdated: " <<lastupdated << endl;
622 log_notice(msg.str());
623 }
624}
5d38cfe6
BS
625
626
627/**
628 * Caught exception while serialize.
629 * @param exception Exception message.
630 */
b38684ce 631void Logger::print_exception_serialize(const string& exception) const
5d38cfe6 632{
667c672c
BS
633 if ( 0 <= Loglevel )
634 {
635 ostringstream msg;
636 msg << "Error while trying to serialize Serviceholder object: " << exception << endl;
637 log_error(msg.str());
638 }
639}
640
641
642/**
643 * Caught exception while de-serialize.
644 * @param exception Exception message.
645 */
b38684ce 646void Logger::print_exception_deserialize(const std::string& exception) const
667c672c
BS
647{
648 if ( 0 <= Loglevel )
649 {
650 ostringstream msg;
651 msg << "Error while trying to de-serialize Serviceholder object: " << exception << endl;
652 log_error(msg.str());
653 }
654}
655
656
657/**
658 * Child couldn't be killed by parent.
659 * @param pid Pid of the child.
660 */
b38684ce 661void Logger::print_error_kill_child(const int pid) const
667c672c
BS
662{
663 if ( 0 <= Loglevel )
664 {
665 ostringstream msg;
666 msg << "Could not kill child process with PID: " << pid << endl;
667 log_error(msg.str());
668 }
669}
670
671
0665b239
BS
672/**
673 * Child was killed by parent because of error.
674 * @param pid The pid (child) killed.
675 */
b38684ce 676void Logger::print_child_killed(const int pid) const
584b9407
BS
677{
678 if ( 0 <= Loglevel )
679 {
680 ostringstream msg;
681 msg << "Killed child process with PID: " << pid << endl;
682 log_notice(msg.str());
683 }
684}
685
686
667c672c
BS
687/**
688 * There is no object file.
689 * @param object_file The object file.
690 */
b38684ce 691void Logger::print_no_object_file(const std::string& object_file) const
667c672c
BS
692{
693 if ( 1 <= Loglevel )
694 {
695 ostringstream msg;
696 msg << "There is no object file: " << object_file << ". Continue without recovering state from old services!" << endl;
697 log_warning(msg.str());
698 }
5d38cfe6 699}
0665b239
BS
700
701
702/**
703 * Prints out the given hostname
704 * @param hostname Hostname as string.
705 */
706void Logger::print_hostname(const std::string& hostname) const
707{
708 if ( 1 <= Loglevel )
709 {
710 ostringstream msg;
711 msg << "Detected following hostname of localhost: " << hostname << endl;
712 log_notice(msg.str());
713 }
714}
715
716
717/**
718 * Prints out the detected own ip address
719 * @param ip_addr String representation of the detected ip.
720 */
721void Logger::print_own_ip(const std::string& ip_addr_v4, const std::string& ip_addr_v6) const
722{
723 if ( 1 <= Loglevel )
724 {
725 ostringstream msg;
726 msg << "Detected following IPv4-Address of this host: " << ip_addr_v4 << endl;
727 msg << "Detected following IPv6-Address of this host: " << ip_addr_v6 << endl;
728 log_notice(msg.str());
729 }
730}
731
732
733/**
734 * Exception while trying to resolve hostname to ip.
735 * @param exception The exception caught.
736 * @param hostname The hostname.
737 */
738void Logger::print_error_hostname_to_ip(const std::string& exception, const std::string& hostname) const
739{
740 if ( 1 <= Loglevel )
741 {
742 ostringstream msg;
743 msg << "Could not resolve the hostname: " << hostname << "to an IP-Address: " << exception << endl;
744 log_error(msg.str());
745 }
746}
68c6b4af
BS
747
748
749void Logger::print_update_service_successful(const std::string& service)
750{
751 if ( 0 <= Loglevel )
752 {
753 ostringstream msg;
754 msg << "Updated service successful: " << service << endl;
755 log_error(msg.str());
756 }
757}