logging.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #ifndef __LOGGING_H__
  2. #define __LOGGING_H__
  3. #include "config.h"
  4. #include <stdbool.h>
  5. #include <stdarg.h>
  6. #ifdef HAVE_SYSLOG_H
  7. #include <syslog.h>
  8. #else
  9. enum {
  10. LOG_ERR,
  11. LOG_WARNING,
  12. LOG_NOTICE,
  13. LOG_INFO,
  14. LOG_DEBUG,
  15. };
  16. #endif
  17. /* debug flags */
  18. extern bool opt_debug;
  19. extern bool opt_decode;
  20. extern bool opt_log_output;
  21. extern bool opt_realquiet;
  22. extern bool want_per_device_stats;
  23. /* global log_level, messages with lower or equal prio are logged */
  24. extern int opt_log_level;
  25. #define LOGBUFSIZ 256
  26. extern void _applog(int prio, const char *str, bool force);
  27. extern void _simplelog(int prio, const char *str, bool force);
  28. #define IN_FMT_FFL " in %s %s():%d"
  29. #define applog(prio, fmt, ...) do { \
  30. if (opt_debug || prio != LOG_DEBUG) { \
  31. if (use_syslog || opt_log_output || prio <= opt_log_level) { \
  32. char tmp42[LOGBUFSIZ]; \
  33. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  34. _applog(prio, tmp42, false); \
  35. } \
  36. } \
  37. } while (0)
  38. #define simplelog(prio, fmt, ...) do { \
  39. if (opt_debug || prio != LOG_DEBUG) { \
  40. if (use_syslog || opt_log_output || prio <= opt_log_level) { \
  41. char tmp42[LOGBUFSIZ]; \
  42. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  43. _simplelog(prio, tmp42, false); \
  44. } \
  45. } \
  46. } while (0)
  47. #define applogsiz(prio, _SIZ, fmt, ...) do { \
  48. if (opt_debug || prio != LOG_DEBUG) { \
  49. if (use_syslog || opt_log_output || prio <= opt_log_level) { \
  50. char tmp42[_SIZ]; \
  51. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  52. _applog(prio, tmp42, false); \
  53. } \
  54. } \
  55. } while (0)
  56. #define forcelog(prio, fmt, ...) do { \
  57. if (opt_debug || prio != LOG_DEBUG) { \
  58. if (use_syslog || opt_log_output || prio <= opt_log_level) { \
  59. char tmp42[LOGBUFSIZ]; \
  60. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  61. _applog(prio, tmp42, true); \
  62. } \
  63. } \
  64. } while (0)
  65. #define quit(status, fmt, ...) do { \
  66. if (fmt) { \
  67. char tmp42[LOGBUFSIZ]; \
  68. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  69. _applog(LOG_ERR, tmp42, true); \
  70. } \
  71. _quit(status); \
  72. } while (0)
  73. #define early_quit(status, fmt, ...) do { \
  74. if (fmt) { \
  75. char tmp42[LOGBUFSIZ]; \
  76. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  77. _applog(LOG_ERR, tmp42, true); \
  78. } \
  79. __quit(status, false); \
  80. } while (0)
  81. #define quithere(status, fmt, ...) do { \
  82. if (fmt) { \
  83. char tmp42[LOGBUFSIZ]; \
  84. snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
  85. ##__VA_ARGS__, __FILE__, __func__, __LINE__); \
  86. _applog(LOG_ERR, tmp42, true); \
  87. } \
  88. _quit(status); \
  89. } while (0)
  90. #define quitfrom(status, _file, _func, _line, fmt, ...) do { \
  91. if (fmt) { \
  92. char tmp42[LOGBUFSIZ]; \
  93. snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
  94. ##__VA_ARGS__, _file, _func, _line); \
  95. _applog(LOG_ERR, tmp42, true); \
  96. } \
  97. _quit(status); \
  98. } while (0)
  99. #ifdef HAVE_CURSES
  100. #define wlog(fmt, ...) do { \
  101. char tmp42[LOGBUFSIZ]; \
  102. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  103. _wlog(tmp42); \
  104. } while (0)
  105. #define wlogprint(fmt, ...) do { \
  106. char tmp42[LOGBUFSIZ]; \
  107. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  108. _wlogprint(tmp42); \
  109. } while (0)
  110. #endif
  111. #endif /* __LOGGING_H__ */