LDM Logging
log.h
Go to the documentation of this file.
1 
12 #ifndef LOG_LOG_H_
13 #define LOG_LOG_H_
14 
15 #include "../misc/ErrObj.h"
16 
17 #include <errno.h>
18 #include <pthread.h>
19 #include <stdarg.h>
20 #include <stdbool.h>
21 #include <stdlib.h>
22 #include <syslog.h>
23 
27 #define LOG_LOCALTIME 0x100u
28 #define LOG_NOTIME 0x200u
29 #define LOG_IDENT 0x400u
30 
32 typedef enum {
41 
42 /*
43  * The declarations in the following header-file are package-private -- so don't
44  * use them.
45  */
46 #include "log_private.h"
47 
48 #ifdef __cplusplus
49  extern "C" {
50 #endif
51 
62 
75 const char* log_get_default_destination(void);
76 
86 bool log_stderr_is_open(void);
87 
94 bool log_amDaemon(void);
95 
110 int log_init(const char* const id);
111 
119 void log_avoid_stderr(void);
120 
132 void log_refresh(void);
133 
138 #define log_fini() do {\
139  LOG_LOC_DECL(loc);\
140  (void)log_fini_located(&loc);\
141 } while (false)
142 
157  const log_level_t level);
158 
170 
178 void log_roll_level(void);
179 
191  const char* const id);
192 
205  const char* const hostId,
206  const bool isFeeder);
207 
216 const char* log_get_id(void);
217 
235  const unsigned options);
236 
251 unsigned log_get_options(void);
252 
266  const int facility);
267 
278 
294  const char* const dest);
295 
308 const char* log_get_destination(void);
309 
316 void log_clear(void);
317 
318 int log_dispose(
319  const log_level_t level,
320  ErrObj* errObj);
321 
330 #define log_free() do {\
331  int prevState; \
332  (void)pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &prevState); \
333  LOG_LOC_DECL(loc);\
334  log_free_located(&loc);\
335  (void)pthread_setcancelstate(prevState, &prevState); \
336 } while (false)
337 
346 bool log_is_level_enabled(
347  const log_level_t level);
348 
353 #define log_is_enabled_warning log_is_level_enabled(LOG_LEVEL_WARNING)
358 #define log_is_enabled_notice log_is_level_enabled(LOG_LEVEL_NOTICE)
363 #define log_is_enabled_info log_is_level_enabled(LOG_LEVEL_INFO)
368 #define log_is_enabled_debug log_is_level_enabled(LOG_LEVEL_DEBUG)
369 
378 #define log_log(level, ...) do {\
379  if ((level) >= log_level) {\
380  LOG_LOC_DECL(loc);\
381  logl_log(&loc, (level), __VA_ARGS__);\
382  }\
383 } while (0)
384 
391 #define log_debug(...) do {\
392  if (LOG_LEVEL_DEBUG >= log_level) {\
393  LOG_LOC_DECL(loc);\
394  logl_log(&loc, LOG_LEVEL_DEBUG, __VA_ARGS__);\
395  }\
396 } while (0)
397 
405 #define log_info(...) do {\
406  if (LOG_LEVEL_INFO >= log_level) {\
407  LOG_LOC_DECL(loc);\
408  logl_log(&loc, LOG_LEVEL_INFO, __VA_ARGS__);\
409  }\
410 } while (0)
411 
419 #define log_notice(...) do {\
420  if (LOG_LEVEL_NOTICE >= log_level) {\
421  LOG_LOC_DECL(loc);\
422  logl_log(&loc, LOG_LEVEL_NOTICE, __VA_ARGS__);\
423  }\
424 } while (0)
425 
433 #define log_warning(...) do {\
434  if (LOG_LEVEL_WARNING >= log_level) {\
435  LOG_LOC_DECL(loc);\
436  logl_log(&loc, LOG_LEVEL_WARNING, __VA_ARGS__);\
437  }\
438 } while (0)
439 
447 #define log_error(...) do {\
448  if (LOG_LEVEL_ERROR >= log_level) {\
449  LOG_LOC_DECL(loc);\
450  logl_log(&loc, LOG_LEVEL_ERROR, __VA_ARGS__);\
451  }\
452 } while (0)
453 
461 #define log_fatal(...) do {\
462  if (LOG_LEVEL_FATAL >= log_level) {\
463  LOG_LOC_DECL(loc);\
464  logl_log(&loc, LOG_LEVEL_FATAL, __VA_ARGS__);\
465  }\
466 } while (0)
467 
476 #define log_errno(errnum, ...) do {\
477  LOG_LOC_DECL(loc);\
478  logl_errno(&loc, errnum, __VA_ARGS__);\
479 } while (0)
480 
488 #define log_syserr(...) log_errno(errno, __VA_ARGS__)
489 
497 #define log_add(...) do { \
498  LOG_LOC_DECL(loc); \
499  logl_add(&loc, __VA_ARGS__);\
500 } while (false)
501 
509 #define log_vadd(fmt, args) do { \
510  LOG_LOC_DECL(loc); \
511  logl_vadd(&loc, fmt, args); \
512 } while (false)
513 
523 #define log_add_errno(n, ...) do {\
524  LOG_LOC_DECL(loc); \
525  logl_add_errno(&loc, n, __VA_ARGS__); \
526 } while (false)
527 
534 #define log_add_syserr(...) log_add_errno(errno, __VA_ARGS__)
535 
551 #define log_error_q(...) LOG_LOG(LOG_LEVEL_ERROR, __VA_ARGS__)
559 #define log_warning_q(...) LOG_LOG(LOG_LEVEL_WARNING, __VA_ARGS__)
567 #define log_notice_q(...) LOG_LOG(LOG_LEVEL_NOTICE, __VA_ARGS__)
575 #define log_info_q(...) LOG_LOG(LOG_LEVEL_INFO, __VA_ARGS__)
583 #define log_debug_q(...) do {\
584  LOG_LOC_DECL(loc);\
585  logl_log_q(&loc, LOG_LEVEL_DEBUG, __VA_ARGS__);\
586 } while (0)
595 #define log_log_q(level, ...) LOG_LOG(level, __VA_ARGS__)
596 
610 int
611 log_flush(const log_level_t level);
612 
617 #define log_flush_fatal() log_flush(LOG_LEVEL_FATAL)
622 #define log_flush_error() log_flush(LOG_LEVEL_ERROR)
627 #define log_flush_warning() log_flush(LOG_LEVEL_WARNING)
632 #define log_flush_notice() log_flush(LOG_LEVEL_NOTICE)
637 #define log_flush_info() log_flush(LOG_LEVEL_INFO)
642 #define log_flush_debug() log_flush(LOG_LEVEL_DEBUG)
643 
654 #define log_malloc(nbytes, msg) logl_malloc(__FILE__, __func__, __LINE__, \
655  nbytes, msg)
656 
668 #define log_realloc(buf, nbytes, msg) logl_realloc(__FILE__, __func__, __LINE__, \
669  buf, nbytes, msg)
670 
677 #define log_abort(...) do { \
678  log_add(__VA_ARGS__); \
679  log_flush(LOG_LEVEL_ERROR); \
680  abort(); \
681 } while (false)
682 
683 #ifdef NDEBUG
684  #define log_assert(expr)
685 #else
694  #define log_assert(expr) do { \
695  int prevState; \
696  (void)pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &prevState); \
697  if (!(expr)) \
698  log_abort("Assertion failure: %s", #expr); \
699  (void)pthread_setcancelstate(prevState, &prevState); \
700  } while (false)
701 #endif
702 
703 #ifdef __cplusplus
704  }
705 #endif
706 
707 #endif /* LOG_LOG_H_ */
int log_set_id(const char *const id)
bool log_stderr_is_open(void)
int log_set_level(const log_level_t level)
int log_set_destination(const char *const dest)
log_level_t log_get_level(void)
void log_roll_level(void)
bool log_amDaemon(void)
void log_refresh(void)
const char * log_get_default_destination(void)
int log_flush(const log_level_t level)
int log_get_facility(void)
const char * log_get_id(void)
int log_init(const char *const id)
void log_clear(void)
unsigned log_get_options(void)
int log_set_upstream_id(const char *const hostId, const bool isFeeder)
log_level_t
Logging levels.
Definition: log.h:32
@ LOG_LEVEL_NOTICE
Notices.
Definition: log.h:35
@ LOG_LEVEL_DEBUG
Debug messages.
Definition: log.h:33
@ LOG_LEVEL_ERROR
Error messages.
Definition: log.h:37
@ LOG_LEVEL_WARNING
Warnings.
Definition: log.h:36
@ LOG_LEVEL_FATAL
Fatal messages.
Definition: log.h:38
@ LOG_LEVEL_COUNT
Number of levels.
Definition: log.h:39
@ LOG_LEVEL_INFO
Informational messages.
Definition: log.h:34
int log_set_options(const unsigned options)
const char * log_get_default_daemon_destination(void)
int log_set_facility(const int facility)
const char * log_get_destination(void)
void log_avoid_stderr(void)