logging.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #define IN_FMT_FFL " in %s %s():%d"
  27. #define applog(prio, fmt, ...) do { \
  28. if (opt_debug || prio != LOG_DEBUG) { \
  29. if (use_syslog || opt_log_output || prio <= opt_log_level) { \
  30. char tmp42[LOGBUFSIZ]; \
  31. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  32. _applog(prio, tmp42, false); \
  33. } \
  34. } \
  35. } while (0)
  36. #define applogsiz(prio, _SIZ, fmt, ...) do { \
  37. if (opt_debug || prio != LOG_DEBUG) { \
  38. if (use_syslog || opt_log_output || prio <= opt_log_level) { \
  39. char tmp42[_SIZ]; \
  40. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  41. _applog(prio, tmp42, false); \
  42. } \
  43. } \
  44. } while (0)
  45. #define forcelog(prio, fmt, ...) do { \
  46. if (opt_debug || prio != LOG_DEBUG) { \
  47. if (use_syslog || opt_log_output || prio <= opt_log_level) { \
  48. char tmp42[LOGBUFSIZ]; \
  49. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  50. _applog(prio, tmp42, true); \
  51. } \
  52. } \
  53. } while (0)
  54. #define quit(status, fmt, ...) do { \
  55. if (fmt) { \
  56. char tmp42[LOGBUFSIZ]; \
  57. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  58. _applog(LOG_ERR, tmp42, true); \
  59. } \
  60. _quit(status); \
  61. } while (0)
  62. #define quithere(status, fmt, ...) do { \
  63. if (fmt) { \
  64. char tmp42[LOGBUFSIZ]; \
  65. snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
  66. ##__VA_ARGS__, __FILE__, __func__, __LINE__); \
  67. _applog(LOG_ERR, tmp42, true); \
  68. } \
  69. _quit(status); \
  70. } while (0)
  71. #define quitfrom(status, _file, _func, _line, fmt, ...) do { \
  72. if (fmt) { \
  73. char tmp42[LOGBUFSIZ]; \
  74. snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
  75. ##__VA_ARGS__, _file, _func, _line); \
  76. _applog(LOG_ERR, tmp42, true); \
  77. } \
  78. _quit(status); \
  79. } while (0)
  80. #ifdef HAVE_CURSES
  81. #define wlog(fmt, ...) do { \
  82. char tmp42[LOGBUFSIZ]; \
  83. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  84. _wlog(tmp42); \
  85. } while (0)
  86. #define wlogprint(fmt, ...) do { \
  87. char tmp42[LOGBUFSIZ]; \
  88. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  89. _wlogprint(tmp42); \
  90. } while (0)
  91. #endif
  92. #endif /* __LOGGING_H__ */