Commit | Line | Data |
---|---|---|
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 | ||
8bca3c5d BS |
13 | namespace po = boost::program_options; |
14 | ||
15 | using namespace std; | |
254bbf53 BS |
16 | |
17 | /** | |
18 | * Default Constructor | |
19 | */ | |
20 | Logger::Logger() | |
8bca3c5d | 21 | : Loglevel(0) |
2e956a36 | 22 | , Syslog(false) |
254bbf53 | 23 | { |
59c8d63c | 24 | set_log_facility(Loglevel,Syslog); |
254bbf53 BS |
25 | } |
26 | ||
27 | ||
28 | /** | |
29 | * Default Destructor | |
30 | */ | |
31 | Logger::~Logger() | |
32 | { | |
254bbf53 BS |
33 | } |
34 | ||
35 | ||
36 | /** | |
59c8d63c BS |
37 | * Decides if Logging through syslog if enabled or through std. |
38 | * @param msg The message to log. | |
39 | */ | |
40 | void Logger::log_notice(const string& msg) | |
41 | { | |
42 | if ( Syslog ) | |
43 | syslog(LOG_NOTICE,msg.c_str()); | |
44 | else | |
27baf279 | 45 | cout << msg << endl;; |
59c8d63c BS |
46 | } |
47 | ||
48 | ||
49 | /** | |
50 | * Decides if Logging through syslog if enabled or through std. | |
51 | * @param msg The message to log. | |
52 | */ | |
53 | void Logger::log_warning(const string& msg) | |
54 | { | |
55 | if ( Syslog ) | |
56 | syslog(LOG_WARNING,msg.c_str()); | |
57 | else | |
27baf279 | 58 | cout << msg << endl; |
59c8d63c BS |
59 | } |
60 | ||
61 | ||
62 | /** | |
63 | * Decides if Logging through syslog if enabled or through std. | |
64 | * @param msg The message to log. | |
65 | */ | |
66 | void Logger::log_error(const string& msg) | |
67 | { | |
68 | if ( Syslog ) | |
69 | syslog(LOG_ERR,msg.c_str()); | |
70 | else | |
27baf279 | 71 | cerr << msg << endl; |
59c8d63c BS |
72 | } |
73 | ||
74 | ||
75 | /** | |
8bca3c5d BS |
76 | * Setter for member Loglevel. |
77 | * @param _loglevel Value to set Loglevel to. | |
78 | */ | |
79 | void Logger::set_loglevel(const int _loglevel) | |
80 | { | |
81 | Loglevel = _loglevel; | |
2e956a36 | 82 | cout << "Loglevel set" << endl; |
8bca3c5d BS |
83 | } |
84 | ||
85 | ||
86 | /** | |
87 | * Getter for member Loglevel. | |
88 | * @return Loglevel. | |
89 | */ | |
90 | int Logger::get_loglevel() | |
91 | { | |
92 | return Loglevel; | |
93 | } | |
94 | ||
95 | ||
96 | /** | |
97 | * Setter for member Syslog. | |
98 | * @param _syslog Wether to log through syslog or not. | |
99 | */ | |
100 | void Logger::set_syslog(const bool _syslog) | |
101 | { | |
102 | Syslog = _syslog; | |
103 | } | |
104 | ||
105 | ||
106 | /** | |
107 | * Getter for member Syslog. | |
108 | * @return True if logging through syslog is enabled, false otherwise. | |
109 | */ | |
110 | bool Logger::get_syslog() | |
111 | { | |
112 | return Syslog; | |
113 | } | |
114 | ||
115 | ||
116 | /** | |
117 | * Initialize the logging facility. | |
118 | */ | |
119 | void Logger::set_log_facility(const int _loglevel, const bool _syslog) | |
120 | { | |
121 | Loglevel = _loglevel; | |
122 | Syslog = _syslog; | |
123 | ||
124 | if ( Syslog ) | |
125 | openlog("bpdyndnsd",LOG_PID,LOG_DAEMON); | |
126 | } | |
127 | ||
128 | ||
129 | /** | |
254bbf53 BS |
130 | * Prints out the usage to the command line. |
131 | */ | |
7f3ced8c | 132 | void Logger::print_usage(const Options_descriptionPtr opt_desc) |
254bbf53 | 133 | { |
59c8d63c BS |
134 | if ( 0 <= Loglevel ) |
135 | { | |
136 | ostringstream msg; | |
137 | msg << "Usage: bpdyndnsd [Command line options]" << "\n" << endl; | |
138 | msg << *opt_desc << endl; | |
139 | log_notice(msg.str()); | |
140 | } | |
254bbf53 BS |
141 | } |
142 | ||
143 | ||
144 | /** | |
145 | * Prints out the programm name and the given version string on stdout. | |
146 | * @param version Version string. | |
147 | */ | |
148 | void Logger::print_version() | |
149 | { | |
59c8d63c BS |
150 | if ( 0 <= Loglevel ) |
151 | { | |
152 | ostringstream msg; | |
153 | msg << "Bullet proof dynamic dns daemon.\nbpdyndnsd " << VERSION << "." << REVISION << "." << RELEASE << endl; | |
154 | log_notice(msg.str()); | |
155 | } | |
254bbf53 BS |
156 | } |
157 | ||
158 | ||
159 | /** | |
160 | * Prints out the successful parsing of the command line options. | |
161 | */ | |
162 | void Logger::print_cmd_parsed() | |
163 | { | |
59c8d63c BS |
164 | if ( 1 <= Loglevel ) |
165 | { | |
166 | ostringstream msg; | |
167 | msg << "Command line options successfully parsed." << endl; | |
168 | log_notice(msg.str()); | |
169 | } | |
254bbf53 BS |
170 | } |
171 | ||
172 | ||
173 | /** | |
174 | * Prints out the successful parsing of the config files. | |
daf2ea82 | 175 | * @param config_path The specified config path. |
254bbf53 BS |
176 | */ |
177 | void Logger::print_conf_loaded(const string& config_path) | |
178 | { | |
59c8d63c BS |
179 | if ( 1 <= Loglevel ) |
180 | { | |
181 | ostringstream msg; | |
182 | msg << "Config files successfully loaded in: " << config_path << endl; | |
183 | log_notice(msg.str()); | |
184 | } | |
254bbf53 BS |
185 | } |
186 | ||
187 | ||
188 | /** | |
daf2ea82 BS |
189 | * Prints out the unsuccessful parsing of the config files. |
190 | * @param config_path The specified config path. | |
254bbf53 BS |
191 | */ |
192 | void Logger::print_conf_not_loaded(const string& config_path) | |
193 | { | |
59c8d63c BS |
194 | if ( 0 <= Loglevel ) |
195 | { | |
196 | ostringstream msg; | |
197 | msg << "Config files couldn't be loaded in: " << config_path << endl; | |
198 | log_error(msg.str()); | |
199 | } | |
254bbf53 BS |
200 | } |
201 | ||
202 | ||
203 | /** | |
daf2ea82 BS |
204 | * A file could not be opened for reading. |
205 | * @param filename The filename. | |
254bbf53 BS |
206 | */ |
207 | void Logger::print_error_opening(const string& filename) | |
208 | { | |
59c8d63c BS |
209 | if ( 0 <= Loglevel ) |
210 | { | |
211 | ostringstream msg; | |
212 | msg << "Error opening file for reading: " << filename << endl; | |
213 | log_error(msg.str()); | |
214 | } | |
254bbf53 BS |
215 | } |
216 | ||
217 | ||
218 | /** | |
219 | * Desctructor of specified class was called. | |
220 | * @param _class Name of the class. | |
221 | */ | |
222 | void Logger::print_destructor_call(const string& _class) | |
223 | { | |
59c8d63c BS |
224 | if ( 1 <= Loglevel ) |
225 | { | |
226 | ostringstream msg; | |
227 | msg << "Destructor call: " << _class << endl; | |
228 | log_notice(msg.str()); | |
229 | } | |
254bbf53 BS |
230 | } |
231 | ||
232 | ||
233 | /** | |
234 | * Constructor of specified class was called. | |
235 | * @param _class Name of the class. | |
236 | */ | |
237 | void Logger::print_constructor_call(const string& _class) | |
238 | { | |
59c8d63c BS |
239 | if ( 1 <= Loglevel ) |
240 | { | |
241 | ostringstream msg; | |
242 | msg << "Constructor call: " << _class << endl; | |
243 | log_notice(msg.str()); | |
244 | } | |
254bbf53 BS |
245 | } |
246 | ||
247 | ||
248 | /** | |
249 | * Update method for specified service was called. | |
daf2ea82 | 250 | * @param service The service for which is the update running. |
254bbf53 BS |
251 | */ |
252 | void Logger::print_update_service(const string& service) | |
253 | { | |
59c8d63c BS |
254 | if ( 0 <= Loglevel ) |
255 | { | |
256 | ostringstream msg; | |
257 | msg << "Running update for service: " << service << endl; | |
258 | log_notice(msg.str()); | |
259 | } | |
254bbf53 BS |
260 | } |
261 | ||
262 | ||
daf2ea82 BS |
263 | /** |
264 | * An unknown option on the command line was detected. | |
265 | * @param unknown_option The unknown option. | |
266 | */ | |
254bbf53 BS |
267 | void Logger::print_unknown_cmd_option(const string& unknown_option) |
268 | { | |
59c8d63c BS |
269 | if ( 0 <= Loglevel ) |
270 | { | |
271 | ostringstream msg; | |
272 | msg << "Unknown option on command line detected: " << unknown_option << endl; | |
273 | log_error(msg.str()); | |
274 | } | |
254bbf53 BS |
275 | } |
276 | ||
277 | ||
daf2ea82 BS |
278 | /** |
279 | * An unknown protocol was specified. | |
280 | * @param protocol The unknown protocol. | |
281 | */ | |
254bbf53 BS |
282 | void Logger::print_unknown_protocol(const string& protocol) |
283 | { | |
59c8d63c BS |
284 | if ( 0 <= Loglevel ) |
285 | { | |
286 | ostringstream msg; | |
287 | msg << "Unknown protocol defined: " << protocol << endl; | |
288 | log_error(msg.str()); | |
289 | } | |
254bbf53 BS |
290 | } |
291 | ||
292 | ||
daf2ea82 BS |
293 | /** |
294 | * Loading a service config file. | |
295 | * @param filename The service config file. | |
296 | */ | |
254bbf53 BS |
297 | void Logger::print_load_service_conf(const string& filename) |
298 | { | |
59c8d63c BS |
299 | if ( 1 <= Loglevel ) |
300 | { | |
301 | ostringstream msg; | |
302 | msg << "Loading service config file: " << filename << endl; | |
303 | log_notice(msg.str()); | |
304 | } | |
254bbf53 BS |
305 | } |
306 | ||
307 | ||
daf2ea82 BS |
308 | /** |
309 | * Loading the main config file. | |
310 | * @param filename The main config file. | |
311 | */ | |
254bbf53 BS |
312 | void Logger::print_load_main_conf(const string& filename) |
313 | { | |
59c8d63c BS |
314 | if ( 1 <= Loglevel ) |
315 | { | |
316 | ostringstream msg; | |
317 | msg << "Loading main config file: " << filename << endl; | |
318 | log_notice(msg.str()); | |
319 | } | |
254bbf53 BS |
320 | } |
321 | ||
322 | ||
daf2ea82 BS |
323 | /** |
324 | * There is an unknown option in a service config file. | |
325 | * @param unknown_option The unknown option. | |
326 | */ | |
254bbf53 BS |
327 | void Logger::print_unknown_service_conf_option(const string& unknown_option) |
328 | { | |
59c8d63c BS |
329 | if ( 0 <= Loglevel ) |
330 | { | |
331 | ostringstream msg; | |
332 | msg << "Unknown option in service config file detected: " << unknown_option << endl; | |
333 | log_error(msg.str()); | |
334 | } | |
254bbf53 BS |
335 | } |
336 | ||
337 | ||
daf2ea82 BS |
338 | /** |
339 | * There is an unknown option in the main config file. | |
340 | * @param unknown_option The unknown option. | |
341 | */ | |
254bbf53 BS |
342 | void Logger::print_unknown_main_conf_option(const string& unknown_option) |
343 | { | |
59c8d63c BS |
344 | if ( 0 <= Loglevel ) |
345 | { | |
346 | ostringstream msg; | |
347 | msg << "Unknown option in main config file detected: " << unknown_option << endl; | |
348 | log_error(msg.str()); | |
349 | } | |
254bbf53 BS |
350 | } |
351 | ||
352 | ||
daf2ea82 BS |
353 | /** |
354 | * The defined config path doesn't exist or is not a directory. | |
355 | * @param config_path The defined config path. | |
356 | */ | |
254bbf53 BS |
357 | void Logger::print_error_config_path(const string& config_path) |
358 | { | |
59c8d63c BS |
359 | if ( 0 <= Loglevel ) |
360 | { | |
361 | ostringstream msg; | |
362 | msg << "Config path doesn't exists or is not a diretory: " << config_path << endl; | |
363 | log_error(msg.str()); | |
364 | } | |
254bbf53 BS |
365 | } |
366 | ||
367 | ||
daf2ea82 BS |
368 | /** |
369 | * There is a missing command line option to initialize a service object. | |
370 | */ | |
254bbf53 BS |
371 | void Logger::print_missing_cmd_service_option() |
372 | { | |
59c8d63c BS |
373 | if ( 0 <= Loglevel ) |
374 | { | |
375 | ostringstream msg; | |
376 | msg << "Missing option to initialize service. Protocol, host, login and password must be specified." << endl; | |
377 | log_error(msg.str()); | |
378 | } | |
254bbf53 | 379 | } |
388f4ab0 BS |
380 | |
381 | ||
c5675c01 BS |
382 | /** |
383 | * Process running as daemon. | |
384 | * @param pid The pid of the daemon. | |
385 | */ | |
388f4ab0 BS |
386 | void Logger::print_runnig_as_daemon(const int pid) |
387 | { | |
59c8d63c BS |
388 | if ( 1 <= Loglevel ) |
389 | { | |
390 | ostringstream msg; | |
391 | msg << "Runnig as daemon: " << pid << endl; | |
392 | log_notice(msg.str()); | |
393 | } | |
388f4ab0 BS |
394 | } |
395 | ||
c5675c01 BS |
396 | |
397 | /** | |
398 | * Prints out the daemon mode. | |
399 | * @param daemon_mode The daemon mode. | |
400 | */ | |
388f4ab0 BS |
401 | void Logger::print_daemon_mode(const bool daemon_mode) |
402 | { | |
59c8d63c BS |
403 | if ( 1 <= Loglevel ) |
404 | { | |
405 | string mode = "disabled"; | |
406 | if (daemon_mode == true) | |
407 | mode = "enabled"; | |
408 | ostringstream msg; | |
409 | msg << "Daemon mode is " << mode << "." << endl; | |
410 | log_notice(msg.str()); | |
411 | } | |
388f4ab0 BS |
412 | } |
413 | ||
414 | ||
c5675c01 BS |
415 | /** |
416 | * There was an error while trying to fork. | |
417 | */ | |
388f4ab0 BS |
418 | void Logger::print_error_fork() |
419 | { | |
59c8d63c BS |
420 | if ( 0 <= Loglevel ) |
421 | { | |
422 | ostringstream msg; | |
423 | msg << "Error while trying to fork." << endl; | |
424 | log_notice(msg.str()); | |
425 | } | |
388f4ab0 BS |
426 | } |
427 | ||
428 | ||
c5675c01 BS |
429 | /** |
430 | * A pid in the pidfile was found. | |
431 | * @param pid The pid found in the pidfile. | |
432 | */ | |
388f4ab0 BS |
433 | void Logger::print_pid_found(const int pid) |
434 | { | |
59c8d63c BS |
435 | if ( 1 <= Loglevel ) |
436 | { | |
437 | ostringstream msg; | |
438 | msg << "Pidfile found: " << pid << ". Checking if process is still runnig..." << endl; | |
439 | log_notice(msg.str()); | |
440 | } | |
388f4ab0 BS |
441 | } |
442 | ||
443 | ||
c5675c01 BS |
444 | /** |
445 | * Another process is already running. | |
446 | * @param pid The pid of the other process. | |
447 | */ | |
388f4ab0 BS |
448 | void Logger::print_process_already_running(const int pid) |
449 | { | |
59c8d63c BS |
450 | if ( 0 <= Loglevel ) |
451 | { | |
452 | ostringstream msg; | |
453 | msg << "Another bpdyndnsd process with PID " << pid << " is already running!" << endl; | |
454 | log_error(msg.str()); | |
455 | } | |
388f4ab0 | 456 | } |
c5675c01 BS |
457 | |
458 | ||
459 | /** | |
460 | * SIGTERM caught. | |
461 | */ | |
462 | void Logger::print_caught_sigterm() | |
463 | { | |
59c8d63c BS |
464 | if ( 0 <= Loglevel ) |
465 | { | |
466 | ostringstream msg; | |
467 | msg << "Caught SIGTERM. Exiting..." << endl; | |
468 | log_notice(msg.str()); | |
469 | } | |
c5675c01 BS |
470 | } |
471 | ||
472 | ||
473 | /** | |
474 | * SIGUSR1 caught. | |
475 | */ | |
476 | void Logger::print_caught_siguser1() | |
477 | { | |
59c8d63c BS |
478 | if ( 0 <= Loglevel ) |
479 | { | |
480 | ostringstream msg; | |
481 | msg << "Caught SIGUSR1. Switching to offline mode..." << endl; | |
482 | log_notice(msg.str()); | |
483 | } | |
c5675c01 BS |
484 | } |
485 | ||
486 | ||
487 | /** | |
488 | * SIGHUP caught. | |
489 | */ | |
490 | void Logger::print_caught_sighup() | |
491 | { | |
59c8d63c BS |
492 | if ( 0 <= Loglevel ) |
493 | { | |
494 | ostringstream msg; | |
495 | msg << "Caught SIGHUP. Reloading config and switching to online mode..." << endl; | |
496 | log_notice(msg.str()); | |
497 | } | |
c5675c01 | 498 | } |
8bca3c5d BS |
499 | |
500 | ||
501 | /** | |
502 | * Error while setting signal handler. | |
503 | */ | |
504 | void Logger::print_error_setting_signal() | |
505 | { | |
59c8d63c BS |
506 | if ( 0 <= Loglevel ) |
507 | { | |
508 | ostringstream msg; | |
509 | msg << "Error while setting signal handler." << endl; | |
510 | log_error(msg.str()); | |
511 | } | |
8bca3c5d BS |
512 | } |
513 | ||
514 | ||
515 | /** | |
516 | * Error while setting signal handler. | |
517 | */ | |
518 | void Logger::print_init_log_facility() | |
519 | { | |
59c8d63c BS |
520 | if ( 1 <= Loglevel ) |
521 | { | |
522 | ostringstream msg; | |
523 | msg << "Initialized logging facility." << " Loglevel: " << Loglevel << " Syslog: " << Syslog << endl; | |
524 | log_notice(msg.str()); | |
525 | } | |
8bca3c5d BS |
526 | } |
527 | ||
27baf279 | 528 | |
8bca3c5d BS |
529 | /** |
530 | * Be verbose. Currently we are in offline mode. | |
531 | */ | |
532 | void Logger::print_offline_mode() | |
533 | { | |
59c8d63c BS |
534 | if ( 0 <= Loglevel ) |
535 | { | |
536 | ostringstream msg; | |
537 | msg << "Offline mode..." << endl; | |
538 | log_notice(msg.str()); | |
539 | } | |
8bca3c5d | 540 | } |
27baf279 BS |
541 | |
542 | ||
543 | /** | |
544 | * Objects successfully serialized. | |
545 | */ | |
546 | void Logger::print_serialized_objects_success() | |
547 | { | |
548 | if ( 1 <= Loglevel ) | |
549 | { | |
550 | ostringstream msg; | |
551 | msg << "Serialized objects successfully." << endl; | |
552 | log_notice(msg.str()); | |
553 | } | |
554 | } | |
555 | ||
556 | ||
557 | /** | |
558 | * Objects successfully de-serialized. | |
559 | */ | |
560 | void Logger::print_deserialized_objects_success() | |
561 | { | |
562 | if ( 1 <= Loglevel ) | |
563 | { | |
564 | ostringstream msg; | |
565 | msg << "De-serialized objects successfully." << endl; | |
566 | log_notice(msg.str()); | |
567 | } | |
568 | } | |
569 | ||
570 | ||
571 | void 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) | |
572 | { | |
573 | if ( 1 <= Loglevel ) | |
574 | { | |
575 | ostringstream msg; | |
576 | msg << message << endl; | |
577 | msg << "\t" << "Protocol: " << protocol << endl; | |
578 | msg << "\t" << "Hostname: " << hostname << endl; | |
579 | msg << "\t" << "Login: " <<login << endl; | |
580 | msg << "\t" << "Password: " <<password << endl; | |
581 | msg << "\t" << "Actual_IP: " <<actual_ip << endl; | |
582 | msg << "\t" << "Lastupdated: " <<lastupdated << endl; | |
583 | log_notice(msg.str()); | |
584 | } | |
585 | } |