logging.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright 2013 Luke Dashjr
  3. * Copyright 2012 zefir
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #ifndef BFG_LOGGING_H
  11. #define BFG_LOGGING_H
  12. #include "config.h"
  13. #include <errno.h>
  14. #include <stdbool.h>
  15. #include <stdio.h>
  16. #include <stdarg.h>
  17. #ifdef HAVE_SYSLOG_H
  18. #include <syslog.h>
  19. #else
  20. enum {
  21. LOG_ERR,
  22. LOG_WARNING,
  23. LOG_NOTICE,
  24. LOG_INFO,
  25. LOG_DEBUG,
  26. };
  27. #endif
  28. #include "util.h"
  29. /* debug flags */
  30. extern bool opt_debug;
  31. extern bool opt_debug_console;
  32. extern bool opt_log_output;
  33. extern bool opt_log_microseconds;
  34. extern bool opt_realquiet;
  35. extern bool want_per_device_stats;
  36. /* global log_level, messages with lower or equal prio are logged */
  37. extern int opt_log_level;
  38. #define return_via(label, stmt) do { \
  39. stmt; \
  40. goto label; \
  41. } while (0)
  42. #define LOGBUFSIZ 0x1000
  43. extern void _applog(int prio, const char *str);
  44. #define IN_FMT_FFL " in %s %s():%d"
  45. #define applog(prio, fmt, ...) do { \
  46. if (opt_debug || prio != LOG_DEBUG) { \
  47. char tmp42[LOGBUFSIZ]; \
  48. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  49. _applog(prio, tmp42); \
  50. } \
  51. } while (0)
  52. #define applogsiz(prio, _SIZ, fmt, ...) do { \
  53. if (opt_debug || prio != LOG_DEBUG) { \
  54. char tmp42[_SIZ]; \
  55. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  56. _applog(prio, tmp42); \
  57. } \
  58. } while (0)
  59. #define applogr(rv, prio, ...) do { \
  60. applog(prio, __VA_ARGS__); \
  61. return rv; \
  62. } while (0)
  63. #define return_via_applog(label, expr, prio, ...) do { \
  64. applog(prio, __VA_ARGS__); \
  65. expr; \
  66. goto label; \
  67. } while (0)
  68. #define appperror(prio, s) do { \
  69. const char *_tmp43 = bfg_strerror(errno, BST_ERRNO); \
  70. if (s && s[0]) \
  71. applog(prio, "%s: %s", s, _tmp43); \
  72. else \
  73. _applog(prio, _tmp43); \
  74. } while (0)
  75. #define perror(s) appperror(LOG_ERR, s)
  76. #define applogfailinfo(prio, failed, fmt, ...) do { \
  77. applog(prio, "Failed to %s"IN_FMT_FFL": "fmt, \
  78. failed, \
  79. __FILE__, __func__, __LINE__, \
  80. __VA_ARGS__); \
  81. } while (0)
  82. #define applogfailinfor(rv, prio, failed, fmt, ...) do { \
  83. applogfailinfo(prio, failed, fmt, __VA_ARGS__); \
  84. return rv; \
  85. } while (0)
  86. #define return_via_applogfailinfo(label, expr, prio, failed, fmt, ...) do { \
  87. applogfailinfo(prio, failed, fmt, __VA_ARGS__); \
  88. expr; \
  89. goto label; \
  90. } while (0)
  91. #define applogfail(prio, failed) do { \
  92. applog(prio, "Failed to %s"IN_FMT_FFL, \
  93. failed, \
  94. __FILE__, __func__, __LINE__); \
  95. } while (0)
  96. #define applogfailr(rv, prio, failed) do { \
  97. applogfail(prio, failed); \
  98. return rv; \
  99. } while (0)
  100. #define return_via_applogfail(label, expr, prio, failed) do { \
  101. applogfail(prio, failed); \
  102. expr; \
  103. goto label; \
  104. } while (0)
  105. extern void _bfg_clean_up(bool);
  106. #define quit(status, fmt, ...) do { \
  107. _bfg_clean_up(false); \
  108. if (fmt) { \
  109. fprintf(stderr, "\n" fmt, ##__VA_ARGS__); \
  110. } \
  111. fprintf(stderr, "\n"); \
  112. fflush(stderr); \
  113. _quit(status); \
  114. } while (0)
  115. #define quithere(status, fmt, ...) do { \
  116. if (fmt) { \
  117. char tmp42[LOGBUFSIZ]; \
  118. snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
  119. ##__VA_ARGS__, __FILE__, __func__, __LINE__); \
  120. _applog(LOG_ERR, tmp42); \
  121. } \
  122. _quit(status); \
  123. } while (0)
  124. #define quitfrom(status, _file, _func, _line, fmt, ...) do { \
  125. if (fmt) { \
  126. char tmp42[LOGBUFSIZ]; \
  127. snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
  128. ##__VA_ARGS__, _file, _func, _line); \
  129. _applog(LOG_ERR, tmp42); \
  130. } \
  131. _quit(status); \
  132. } while (0)
  133. #ifdef HAVE_CURSES
  134. #define wlog(fmt, ...) do { \
  135. char tmp42[LOGBUFSIZ]; \
  136. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  137. _wlog(tmp42); \
  138. } while (0)
  139. #define wlogprint(fmt, ...) do { \
  140. char tmp42[LOGBUFSIZ]; \
  141. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  142. _wlogprint(tmp42); \
  143. } while (0)
  144. #endif
  145. extern void hexdump(const void *, unsigned int len);
  146. #endif /* __LOGGING_H__ */