logging.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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);
  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); \
  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); \
  42. } \
  43. } \
  44. } while (0)
  45. #define quit(status, fmt, ...) do { \
  46. if (fmt) { \
  47. char tmp42[LOGBUFSIZ]; \
  48. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  49. _applog(LOG_ERR, tmp42); \
  50. } \
  51. _quit(status); \
  52. } while (0)
  53. #define quithere(status, fmt, ...) do { \
  54. if (fmt) { \
  55. char tmp42[LOGBUFSIZ]; \
  56. snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
  57. ##__VA_ARGS__, __FILE__, __func__, __LINE__); \
  58. _applog(LOG_ERR, tmp42); \
  59. } \
  60. _quit(status); \
  61. } while (0)
  62. #define quitfrom(status, _file, _func, _line, 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); \
  68. } \
  69. _quit(status); \
  70. } while (0)
  71. #ifdef HAVE_CURSES
  72. #define wlog(fmt, ...) do { \
  73. char tmp42[LOGBUFSIZ]; \
  74. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  75. _wlog(tmp42); \
  76. } while (0)
  77. #define wlogprint(fmt, ...) do { \
  78. char tmp42[LOGBUFSIZ]; \
  79. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  80. _wlogprint(tmp42); \
  81. } while (0)
  82. #endif
  83. #endif /* __LOGGING_H__ */