Commented all config options for testing purposes and added next TODO's.
[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 */
43void Logger::log_notice(const string& msg)
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 */
56void Logger::log_warning(const string& msg)
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 */
69void Logger::log_error(const string& msg)
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 */
93int Logger::get_loglevel()
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 */
113bool Logger::get_syslog()
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 */
7f3ced8c 135void Logger::print_usage(const Options_descriptionPtr opt_desc)
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 */
151void Logger::print_version()
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 */
165void Logger::print_cmd_parsed()
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
667c672c
BS
176void Logger::print_conf_files_parsed()
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
BS
190 */
191void Logger::print_conf_loaded(const string& config_path)
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
BS
205 */
206void Logger::print_conf_not_loaded(const string& config_path)
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 */
667c672c 221void Logger::print_error_opening_r(const string& filename)
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 */
236void Logger::print_error_opening_rw(const string& filename)
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 */
251void Logger::print_destructor_call(const string& _class)
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 */
266void Logger::print_constructor_call(const string& _class)
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
BS
280 */
281void Logger::print_update_service(const string& service)
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 */
254bbf53
BS
296void Logger::print_unknown_cmd_option(const string& unknown_option)
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 */
254bbf53
BS
311void Logger::print_unknown_protocol(const string& protocol)
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 */
254bbf53
BS
326void Logger::print_load_service_conf(const string& filename)
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 */
254bbf53
BS
341void Logger::print_load_main_conf(const string& filename)
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 */
254bbf53
BS
356void Logger::print_unknown_service_conf_option(const string& unknown_option)
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 */
254bbf53
BS
371void Logger::print_unknown_main_conf_option(const string& unknown_option)
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 */
254bbf53
BS
386void Logger::print_error_config_path(const string& config_path)
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 */
254bbf53
BS
400void Logger::print_missing_cmd_service_option()
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 */
388f4ab0
BS
415void Logger::print_runnig_as_daemon(const int pid)
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 */
388f4ab0
BS
430void Logger::print_daemon_mode(const bool daemon_mode)
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 */
388f4ab0
BS
447void Logger::print_error_fork()
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 */
388f4ab0
BS
462void Logger::print_pid_found(const int pid)
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 */
388f4ab0
BS
477void Logger::print_process_already_running(const int pid)
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 */
491void Logger::print_caught_sigterm()
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 */
505void Logger::print_caught_siguser1()
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 */
519void Logger::print_caught_sighup()
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 */
667c672c 533void Logger::print_error_setting_signal(const string& signal)
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 */
547void Logger::print_init_log_facility()
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 */
561void Logger::print_offline_mode()
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 */
575void Logger::print_serialized_objects_success()
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 */
589void Logger::print_deserialized_objects_success()
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 */
27baf279
BS
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)
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 */
88a594e8 631void Logger::print_exception_serialize(const string& exception)
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 */
646void Logger::print_exception_deserialize(const std::string& exception)
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 */
661void Logger::print_error_kill_child(const int pid)
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
584b9407
BS
672void Logger::print_child_killed(const int pid)
673{
674 if ( 0 <= Loglevel )
675 {
676 ostringstream msg;
677 msg << "Killed child process with PID: " << pid << endl;
678 log_notice(msg.str());
679 }
680}
681
682
667c672c
BS
683/**
684 * There is no object file.
685 * @param object_file The object file.
686 */
687void Logger::print_no_object_file(const std::string& object_file)
688{
689 if ( 1 <= Loglevel )
690 {
691 ostringstream msg;
692 msg << "There is no object file: " << object_file << ". Continue without recovering state from old services!" << endl;
693 log_warning(msg.str());
694 }
5d38cfe6 695}