logging.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 <stdio.h>
  14. #include <stdarg.h>
  15. #ifdef HAVE_SYSLOG_H
  16. #include <syslog.h>
  17. #else
  18. enum {
  19. LOG_ERR,
  20. LOG_WARNING,
  21. LOG_NOTICE,
  22. LOG_INFO,
  23. LOG_DEBUG,
  24. };
  25. #endif
  26. extern int bfg_snprintf(char * const buf, size_t, const char * const fmt, ...) FORMAT_SYNTAX_CHECK(printf, 3, 4);
  27. /* debug flags */
  28. extern bool opt_debug;
  29. extern bool opt_debug_console;
  30. extern bool opt_log_output;
  31. extern bool opt_log_microseconds;
  32. extern bool opt_realquiet;
  33. extern bool want_per_device_stats;
  34. /* global log_level, messages with lower or equal prio are logged */
  35. extern int opt_log_level;
  36. #define LOGBUFSIZ 256
  37. extern void _applog(int prio, const char *str);
  38. #define applog(prio, ...) do { \
  39. if (opt_debug || prio != LOG_DEBUG) { \
  40. char tmp42[LOGBUFSIZ]; \
  41. bfg_snprintf(tmp42, sizeof(tmp42), __VA_ARGS__); \
  42. _applog(prio, tmp42); \
  43. } \
  44. } while (0)
  45. #define applogr(rv, prio, ...) do { \
  46. applog(prio, __VA_ARGS__); \
  47. return rv; \
  48. } while (0)
  49. extern void _bfg_clean_up(void);
  50. #define quit(status, fmt, ...) do { \
  51. _bfg_clean_up(); \
  52. if (fmt) { \
  53. fprintf(stderr, fmt, ##__VA_ARGS__); \
  54. } \
  55. fprintf(stderr, "\n"); \
  56. fflush(stderr); \
  57. _quit(status); \
  58. } while (0)
  59. #ifdef HAVE_CURSES
  60. #define wlog(...) do { \
  61. char tmp42[LOGBUFSIZ]; \
  62. bfg_snprintf(tmp42, sizeof(tmp42), __VA_ARGS__); \
  63. _wlog(tmp42); \
  64. } while (0)
  65. #define wlogprint(...) do { \
  66. char tmp42[LOGBUFSIZ]; \
  67. bfg_snprintf(tmp42, sizeof(tmp42), __VA_ARGS__); \
  68. _wlogprint(tmp42); \
  69. } while (0)
  70. #endif
  71. extern void hexdump(const void *, unsigned int len);
  72. #endif /* __LOGGING_H__ */