logging.h 3.0 KB

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