LDM Logging
 All Files Functions Enumerations Enumerator Macros Pages
log.h
Go to the documentation of this file.
1 
12 #ifndef LOG_LOG_H_
13 #define LOG_LOG_H_
14 
15 #include <errno.h>
16 #include <pthread.h>
17 #include <stdarg.h>
18 #include <stdbool.h>
19 #include <stdlib.h>
20 #include <syslog.h>
21 
25 #define LOG_LOCALTIME 0x100u
26 #define LOG_NOTIME 0x200u
27 #define LOG_IDENT 0x400u
28 
29 typedef enum {
38 } log_level_t;
39 
40 /*
41  * The declarations in the following header-file are package-private -- so don't
42  * use them.
43  */
44 #include "log_private.h"
45 
46 #ifdef __cplusplus
47  extern "C" {
48 #endif
49 
59 const char* log_get_default_daemon_destination(void);
60 
73 const char* log_get_default_destination(void);
74 
84 bool log_stderr_is_open(void);
85 
92 bool log_amDaemon(void);
93 
108 int log_init(const char* const id);
109 
117 void log_avoid_stderr(void);
118 
130 void log_refresh(void);
131 
136 #define log_fini() do {\
137  LOG_LOC_DECL(loc);\
138  (void)log_fini_located(&loc);\
139 } while (false)
140 
154 int log_set_level(
155  const log_level_t level);
156 
168 
176 void log_roll_level(void);
177 
188 int log_set_id(
189  const char* const id);
190 
203  const char* const hostId,
204  const bool isFeeder);
205 
214 const char* log_get_id(void);
215 
232 int log_set_options(
233  const unsigned options);
234 
249 unsigned log_get_options(void);
250 
263 int log_set_facility(
264  const int facility);
265 
275 int log_get_facility(void);
276 
292  const char* const dest);
293 
306 const char* log_get_destination(void);
307 
314 void log_clear(void);
315 
324 #define log_free() do {\
325  int prevState; \
326  (void)pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &prevState); \
327  LOG_LOC_DECL(loc);\
328  log_free_located(&loc);\
329  (void)pthread_setcancelstate(prevState, &prevState); \
330 } while (false)
331 
340 bool log_is_level_enabled(
341  const log_level_t level);
342 
347 #define log_is_enabled_warning log_is_level_enabled(LOG_LEVEL_WARNING)
348 
352 #define log_is_enabled_notice log_is_level_enabled(LOG_LEVEL_NOTICE)
353 
357 #define log_is_enabled_info log_is_level_enabled(LOG_LEVEL_INFO)
358 
362 #define log_is_enabled_debug log_is_level_enabled(LOG_LEVEL_DEBUG)
363 
372 #define log_log(level, ...) do {\
373  if ((level) >= log_level) {\
374  LOG_LOC_DECL(loc);\
375  logl_log(&loc, (level), __VA_ARGS__);\
376  }\
377 } while (0)
378 
386 #define log_debug(...) do {\
387  if (LOG_LEVEL_DEBUG >= log_level) {\
388  LOG_LOC_DECL(loc);\
389  logl_log(&loc, LOG_LEVEL_DEBUG, __VA_ARGS__);\
390  }\
391 } while (0)
392 
400 #define log_info(...) do {\
401  if (LOG_LEVEL_INFO >= log_level) {\
402  LOG_LOC_DECL(loc);\
403  logl_log(&loc, LOG_LEVEL_INFO, __VA_ARGS__);\
404  }\
405 } while (0)
406 
414 #define log_notice(...) do {\
415  if (LOG_LEVEL_NOTICE >= log_level) {\
416  LOG_LOC_DECL(loc);\
417  logl_log(&loc, LOG_LEVEL_NOTICE, __VA_ARGS__);\
418  }\
419 } while (0)
420 
428 #define log_warning(...) do {\
429  if (LOG_LEVEL_WARNING >= log_level) {\
430  LOG_LOC_DECL(loc);\
431  logl_log(&loc, LOG_LEVEL_WARNING, __VA_ARGS__);\
432  }\
433 } while (0)
434 
442 #define log_error(...) do {\
443  if (LOG_LEVEL_ERROR >= log_level) {\
444  LOG_LOC_DECL(loc);\
445  logl_log(&loc, LOG_LEVEL_ERROR, __VA_ARGS__);\
446  }\
447 } while (0)
448 
456 #define log_fatal(...) do {\
457  if (LOG_LEVEL_FATAL >= log_level) {\
458  LOG_LOC_DECL(loc);\
459  logl_log(&loc, LOG_LEVEL_FATAL, __VA_ARGS__);\
460  }\
461 } while (0)
462 
471 #define log_errno(errnum, ...) do {\
472  LOG_LOC_DECL(loc);\
473  logl_errno(&loc, errnum, __VA_ARGS__);\
474 } while (0)
475 
483 #define log_syserr(...) log_errno(errno, __VA_ARGS__)
484 
492 #define log_add(...) do { \
493  LOG_LOC_DECL(loc); \
494  logl_add(&loc, __VA_ARGS__);\
495 } while (false)
496 
504 #define log_vadd(fmt, args) do { \
505  LOG_LOC_DECL(loc); \
506  logl_vadd(&loc, fmt, args); \
507 } while (false)
508 
518 #define log_add_errno(n, ...) do {\
519  LOG_LOC_DECL(loc); \
520  logl_add_errno(&loc, n, __VA_ARGS__); \
521 } while (false)
522 
529 #define log_add_syserr(...) log_add_errno(errno, __VA_ARGS__)
530 
546 #define log_error_q(...) LOG_LOG(LOG_LEVEL_ERROR, __VA_ARGS__)
547 
554 #define log_warning_q(...) LOG_LOG(LOG_LEVEL_WARNING, __VA_ARGS__)
555 
562 #define log_notice_q(...) LOG_LOG(LOG_LEVEL_NOTICE, __VA_ARGS__)
563 
570 #define log_info_q(...) LOG_LOG(LOG_LEVEL_INFO, __VA_ARGS__)
571 
578 #define log_debug_q(...) do {\
579  LOG_LOC_DECL(loc);\
580  logl_log_q(&loc, LOG_LEVEL_DEBUG, __VA_ARGS__);\
581 } while (0)
582 
590 #define log_log_q(level, ...) LOG_LOG(level, __VA_ARGS__)
591 
605 int
606 log_flush(const log_level_t level);
607 
612 #define log_flush_fatal() log_flush(LOG_LEVEL_FATAL)
613 
617 #define log_flush_error() log_flush(LOG_LEVEL_ERROR)
618 
622 #define log_flush_warning() log_flush(LOG_LEVEL_WARNING)
623 
627 #define log_flush_notice() log_flush(LOG_LEVEL_NOTICE)
628 
632 #define log_flush_info() log_flush(LOG_LEVEL_INFO)
633 
637 #define log_flush_debug() log_flush(LOG_LEVEL_DEBUG)
638 
649 #define log_malloc(nbytes, msg) logl_malloc(__FILE__, __func__, __LINE__, \
650  nbytes, msg)
651 
663 #define log_realloc(buf, nbytes, msg) logl_realloc(__FILE__, __func__, __LINE__, \
664  buf, nbytes, msg)
665 
672 #define log_abort(...) do { \
673  log_add(__VA_ARGS__); \
674  log_flush(LOG_LEVEL_ERROR); \
675  abort(); \
676 } while (false)
677 
678 #ifdef NDEBUG
679  #define log_assert(expr)
680 #else
681 
689  #define log_assert(expr) do { \
690  int prevState; \
691  (void)pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &prevState); \
692  if (!(expr)) \
693  log_abort("Assertion failure: %s", #expr); \
694  (void)pthread_setcancelstate(prevState, &prevState); \
695  } while (false)
696 #endif
697 
698 #ifdef __cplusplus
699  }
700 #endif
701 
702 #endif /* LOG_LOG_H_ */
int log_set_destination(const char *const dest)
int log_set_facility(const int facility)
log_level_t
Logging levels.
Definition: log.h:30
Error messages.
Definition: log.h:35
Number of levels.
Definition: log.h:37
int log_set_id(const char *const id)
Notices.
Definition: log.h:33
const char * log_get_destination(void)
bool log_amDaemon(void)
bool log_stderr_is_open(void)
log_level_t log_get_level(void)
Debug messages.
Definition: log.h:31
void log_avoid_stderr(void)
int log_init(const char *const id)
int log_flush(const log_level_t level)
void log_roll_level(void)
unsigned log_get_options(void)
int log_get_facility(void)
void log_refresh(void)
Informational messages.
Definition: log.h:32
Fatal messages.
Definition: log.h:36
int log_set_level(const log_level_t level)
const char * log_get_id(void)
const char * log_get_default_daemon_destination(void)
const char * log_get_default_destination(void)
void log_clear(void)
int log_set_upstream_id(const char *const hostId, const bool isFeeder)
int log_set_options(const unsigned options)
Warnings.
Definition: log.h:34