logging.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. extern void _bfg_clean_up(void);
  48. #define quit(status, fmt, ...) do { \
  49. _bfg_clean_up(); \
  50. if (fmt) { \
  51. fprintf(stderr, fmt, ##__VA_ARGS__); \
  52. } \
  53. fprintf(stderr, "\n"); \
  54. fflush(stderr); \
  55. _quit(status); \
  56. } while (0)
  57. #ifdef HAVE_CURSES
  58. #define wlog(fmt, ...) do { \
  59. char tmp42[LOGBUFSIZ]; \
  60. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  61. _wlog(tmp42); \
  62. } while (0)
  63. #define wlogprint(fmt, ...) do { \
  64. char tmp42[LOGBUFSIZ]; \
  65. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  66. _wlogprint(tmp42); \
  67. } while (0)
  68. #endif
  69. extern void hexdump(const void *, unsigned int len);
  70. #endif /* __LOGGING_H__ */