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