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 <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 
30 typedef enum {
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 
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 
155  const log_level_t level);
156 
168 
176 void log_roll_level(void);
177 
189  const char* const id);
190 
203  const char* const hostId,
204  const bool isFeeder);
205 
214 const char* log_get_id(void);
215 
233  const unsigned options);
234 
249 unsigned log_get_options(void);
250 
264  const int facility);
265 
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)
352 #define log_is_enabled_notice log_is_level_enabled(LOG_LEVEL_NOTICE)
357 #define log_is_enabled_info log_is_level_enabled(LOG_LEVEL_INFO)
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 
385 #define log_debug(...) do {\
386  if (LOG_LEVEL_DEBUG >= log_level) {\
387  LOG_LOC_DECL(loc);\
388  logl_log(&loc, LOG_LEVEL_DEBUG, __VA_ARGS__);\
389  }\
390 } while (0)
391 
399 #define log_info(...) do {\
400  if (LOG_LEVEL_INFO >= log_level) {\
401  LOG_LOC_DECL(loc);\
402  logl_log(&loc, LOG_LEVEL_INFO, __VA_ARGS__);\
403  }\
404 } while (0)
405 
413 #define log_notice(...) do {\
414  if (LOG_LEVEL_NOTICE >= log_level) {\
415  LOG_LOC_DECL(loc);\
416  logl_log(&loc, LOG_LEVEL_NOTICE, __VA_ARGS__);\
417  }\
418 } while (0)
419 
427 #define log_warning(...) do {\
428  if (LOG_LEVEL_WARNING >= log_level) {\
429  LOG_LOC_DECL(loc);\
430  logl_log(&loc, LOG_LEVEL_WARNING, __VA_ARGS__);\
431  }\
432 } while (0)
433 
441 #define log_error(...) do {\
442  if (LOG_LEVEL_ERROR >= log_level) {\
443  LOG_LOC_DECL(loc);\
444  logl_log(&loc, LOG_LEVEL_ERROR, __VA_ARGS__);\
445  }\
446 } while (0)
447 
455 #define log_fatal(...) do {\
456  if (LOG_LEVEL_FATAL >= log_level) {\
457  LOG_LOC_DECL(loc);\
458  logl_log(&loc, LOG_LEVEL_FATAL, __VA_ARGS__);\
459  }\
460 } while (0)
461 
470 #define log_errno(errnum, ...) do {\
471  LOG_LOC_DECL(loc);\
472  logl_errno(&loc, errnum, __VA_ARGS__);\
473 } while (0)
474 
482 #define log_syserr(...) log_errno(errno, __VA_ARGS__)
483 
491 #define log_add(...) do { \
492  LOG_LOC_DECL(loc); \
493  logl_add(&loc, __VA_ARGS__);\
494 } while (false)
495 
503 #define log_vadd(fmt, args) do { \
504  LOG_LOC_DECL(loc); \
505  logl_vadd(&loc, fmt, args); \
506 } while (false)
507 
517 #define log_add_errno(n, ...) do {\
518  LOG_LOC_DECL(loc); \
519  logl_add_errno(&loc, n, __VA_ARGS__); \
520 } while (false)
521 
528 #define log_add_syserr(...) log_add_errno(errno, __VA_ARGS__)
529 
545 #define log_error_q(...) LOG_LOG(LOG_LEVEL_ERROR, __VA_ARGS__)
553 #define log_warning_q(...) LOG_LOG(LOG_LEVEL_WARNING, __VA_ARGS__)
561 #define log_notice_q(...) LOG_LOG(LOG_LEVEL_NOTICE, __VA_ARGS__)
569 #define log_info_q(...) LOG_LOG(LOG_LEVEL_INFO, __VA_ARGS__)
577 #define log_debug_q(...) do {\
578  LOG_LOC_DECL(loc);\
579  logl_log_q(&loc, LOG_LEVEL_DEBUG, __VA_ARGS__);\
580 } while (0)
589 #define log_log_q(level, ...) LOG_LOG(level, __VA_ARGS__)
590 
604 int
605 log_flush(const log_level_t level);
606 
611 #define log_flush_fatal() log_flush(LOG_LEVEL_FATAL)
616 #define log_flush_error() log_flush(LOG_LEVEL_ERROR)
621 #define log_flush_warning() log_flush(LOG_LEVEL_WARNING)
626 #define log_flush_notice() log_flush(LOG_LEVEL_NOTICE)
631 #define log_flush_info() log_flush(LOG_LEVEL_INFO)
636 #define log_flush_debug() log_flush(LOG_LEVEL_DEBUG)
637 
648 #define log_malloc(nbytes, msg) logl_malloc(__FILE__, __func__, __LINE__, \
649  nbytes, msg)
650 
662 #define log_realloc(buf, nbytes, msg) logl_realloc(__FILE__, __func__, __LINE__, \
663  buf, nbytes, msg)
664 
671 #define log_abort(...) do { \
672  log_add(__VA_ARGS__); \
673  log_flush(LOG_LEVEL_ERROR); \
674  abort(); \
675 } while (false)
676 
677 #ifdef NDEBUG
678  #define log_assert(expr)
679 #else
688  #define log_assert(expr) do { \
689  int prevState; \
690  (void)pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &prevState); \
691  if (!(expr)) \
692  log_abort("Assertion failure: %s", #expr); \
693  (void)pthread_setcancelstate(prevState, &prevState); \
694  } while (false)
695 #endif
696 
697 #ifdef __cplusplus
698  }
699 #endif
700 
701 #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:30
@ LOG_LEVEL_NOTICE
Notices.
Definition: log.h:33
@ LOG_LEVEL_DEBUG
Debug messages.
Definition: log.h:31
@ LOG_LEVEL_ERROR
Error messages.
Definition: log.h:35
@ LOG_LEVEL_WARNING
Warnings.
Definition: log.h:34
@ LOG_LEVEL_FATAL
Fatal messages.
Definition: log.h:36
@ LOG_LEVEL_COUNT
Number of levels.
Definition: log.h:37
@ LOG_LEVEL_INFO
Informational messages.
Definition: log.h:32
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)