Each Service has Protocol, Hostname, Login, Password, Shared_Ptr to Logging facility...
[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
8bca3c5d
BS
13namespace po = boost::program_options;
14
15using namespace std;
254bbf53
BS
16
17/**
18 * Default Constructor
19 */
20Logger::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 */
32Logger::~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 */
42void Logger::log_notice(const string& msg)
43{
44 if ( Syslog )
45 syslog(LOG_NOTICE,msg.c_str());
46 else
47 cout << msg;
48}
49
50
51/**
52 * Decides if Logging through syslog if enabled or through std.
53 * @param msg The message to log.
54 */
55void Logger::log_warning(const string& msg)
56{
57 if ( Syslog )
58 syslog(LOG_WARNING,msg.c_str());
59 else
60 cout << msg;
61}
62
63
64/**
65 * Decides if Logging through syslog if enabled or through std.
66 * @param msg The message to log.
67 */
68void Logger::log_error(const string& msg)
69{
70 if ( Syslog )
71 syslog(LOG_ERR,msg.c_str());
72 else
73 cerr << msg;
74}
75
76
77/**
8bca3c5d
BS
78 * Setter for member Loglevel.
79 * @param _loglevel Value to set Loglevel to.
80 */
81void Logger::set_loglevel(const int _loglevel)
82{
83 Loglevel = _loglevel;
84}
85
86
87/**
88 * Getter for member Loglevel.
89 * @return Loglevel.
90 */
91int 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 */
101void 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 */
111bool Logger::get_syslog()
112{
113 return Syslog;
114}
115
116
117/**
118 * Initialize the logging facility.
119 */
120void 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 */
133void 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 */
149void 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 */
163void 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 */
178void 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 */
193void 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 */
208void 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 */
223void 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 */
238void 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 */
253void 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
268void 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
283void 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
298void 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
313void 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
328void 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
343void 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
358void 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
372void 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
387void 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
402void 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
419void 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
434void 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
449void 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 */
463void 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 */
477void 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 */
491void 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 */
505void 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 */
519void 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
529/**
530 * Be verbose. Currently we are in offline mode.
531 */
532void 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}