logging.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #ifndef __LOGGING_H__
  2. #define __LOGGING_H__
  3. #include "config.h"
  4. #include <stdbool.h>
  5. #include <stdio.h>
  6. #include <stdarg.h>
  7. #ifdef HAVE_SYSLOG_H
  8. #include <syslog.h>
  9. #else
  10. enum {
  11. LOG_ERR,
  12. LOG_WARNING,
  13. LOG_NOTICE,
  14. LOG_INFO,
  15. LOG_DEBUG,
  16. };
  17. #endif
  18. /* debug flags */
  19. extern bool opt_debug;
  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);
  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 quit(status, fmt, ...) do { \
  37. if (fmt) { \
  38. char tmp42[LOGBUFSIZ]; \
  39. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  40. _applog(LOG_ERR, tmp42); \
  41. } \
  42. _quit(status); \
  43. } while (0)
  44. #ifdef HAVE_CURSES
  45. #define wlog(fmt, ...) do { \
  46. char tmp42[LOGBUFSIZ]; \
  47. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  48. _wlog(tmp42); \
  49. } while (0)
  50. #define wlogprint(fmt, ...) do { \
  51. char tmp42[LOGBUFSIZ]; \
  52. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  53. _wlogprint(tmp42); \
  54. } while (0)
  55. #endif
  56. #endif /* __LOGGING_H__ */