logging.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright 2012 zefir
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #ifndef __LOGGING_H__
  10. #define __LOGGING_H__
  11. #include "config.h"
  12. #include <stdbool.h>
  13. #include <stdarg.h>
  14. #ifdef HAVE_SYSLOG_H
  15. #include <syslog.h>
  16. #else
  17. enum {
  18. LOG_ERR,
  19. LOG_WARNING,
  20. LOG_NOTICE,
  21. LOG_INFO,
  22. LOG_DEBUG,
  23. };
  24. #endif
  25. /* debug flags */
  26. extern bool opt_debug;
  27. extern bool opt_debug_console;
  28. extern bool opt_log_output;
  29. extern bool opt_log_microseconds;
  30. extern bool opt_realquiet;
  31. extern bool want_per_device_stats;
  32. /* global log_level, messages with lower or equal prio are logged */
  33. extern int opt_log_level;
  34. #define LOGBUFSIZ 256
  35. extern void _applog(int prio, const char *str);
  36. #define applog(prio, fmt, ...) do { \
  37. if (opt_debug || prio != LOG_DEBUG) { \
  38. char tmp42[LOGBUFSIZ]; \
  39. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  40. _applog(prio, tmp42); \
  41. } \
  42. } while (0)
  43. #define applogr(rv, prio, ...) do { \
  44. applog(prio, __VA_ARGS__); \
  45. return rv; \
  46. } while (0)
  47. #define quit(status, fmt, ...) do { \
  48. if (fmt) { \
  49. char tmp42[LOGBUFSIZ]; \
  50. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  51. _applog(LOG_ERR, tmp42); \
  52. } \
  53. _quit(status); \
  54. } while (0)
  55. #ifdef HAVE_CURSES
  56. #define wlog(fmt, ...) do { \
  57. char tmp42[LOGBUFSIZ]; \
  58. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  59. _wlog(tmp42); \
  60. } while (0)
  61. #define wlogprint(fmt, ...) do { \
  62. char tmp42[LOGBUFSIZ]; \
  63. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  64. _wlogprint(tmp42); \
  65. } while (0)
  66. #endif
  67. extern void hexdump(const void *, unsigned int len);
  68. #endif /* __LOGGING_H__ */