logging.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #ifndef __LOGGING_H__
  2. #define __LOGGING_H__
  3. #include "config.h"
  4. #include <stdbool.h>
  5. #include <stdarg.h>
  6. #ifdef HAVE_SYSLOG_H
  7. #include <syslog.h>
  8. #else
  9. enum {
  10. LOG_ERR,
  11. LOG_WARNING,
  12. LOG_NOTICE,
  13. LOG_INFO,
  14. LOG_DEBUG,
  15. };
  16. #endif
  17. /* debug flags */
  18. extern bool opt_debug;
  19. extern bool opt_decode;
  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. #ifdef USE_XTRANONCE
  26. #define LOGBUFSIZ 2560
  27. #else
  28. #define LOGBUFSIZ 256
  29. #endif
  30. extern void _applog(int prio, const char *str, bool force);
  31. extern void _simplelog(int prio, const char *str, bool force);
  32. #define IN_FMT_FFL " in %s %s():%d"
  33. #define applog(prio, fmt, ...) do { \
  34. if (opt_debug || prio != LOG_DEBUG) { \
  35. if (use_syslog || opt_log_output || prio <= opt_log_level) { \
  36. char tmp42[LOGBUFSIZ]; \
  37. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  38. _applog(prio, tmp42, false); \
  39. } \
  40. } \
  41. } while (0)
  42. #define simplelog(prio, fmt, ...) do { \
  43. if (opt_debug || prio != LOG_DEBUG) { \
  44. if (use_syslog || opt_log_output || prio <= opt_log_level) { \
  45. char tmp42[LOGBUFSIZ]; \
  46. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  47. _simplelog(prio, tmp42, false); \
  48. } \
  49. } \
  50. } while (0)
  51. #define applogsiz(prio, _SIZ, fmt, ...) do { \
  52. if (opt_debug || prio != LOG_DEBUG) { \
  53. if (use_syslog || opt_log_output || prio <= opt_log_level) { \
  54. char tmp42[_SIZ]; \
  55. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  56. _applog(prio, tmp42, false); \
  57. } \
  58. } \
  59. } while (0)
  60. #define forcelog(prio, fmt, ...) do { \
  61. if (opt_debug || prio != LOG_DEBUG) { \
  62. if (use_syslog || opt_log_output || prio <= opt_log_level) { \
  63. char tmp42[LOGBUFSIZ]; \
  64. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  65. _applog(prio, tmp42, true); \
  66. } \
  67. } \
  68. } while (0)
  69. #define quit(status, fmt, ...) do { \
  70. if (fmt) { \
  71. char tmp42[LOGBUFSIZ]; \
  72. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  73. _applog(LOG_ERR, tmp42, true); \
  74. } \
  75. _quit(status); \
  76. } while (0)
  77. #define early_quit(status, fmt, ...) do { \
  78. if (fmt) { \
  79. char tmp42[LOGBUFSIZ]; \
  80. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  81. _applog(LOG_ERR, tmp42, true); \
  82. } \
  83. __quit(status, false); \
  84. } while (0)
  85. #define quithere(status, fmt, ...) do { \
  86. if (fmt) { \
  87. char tmp42[LOGBUFSIZ]; \
  88. snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
  89. ##__VA_ARGS__, __FILE__, __func__, __LINE__); \
  90. _applog(LOG_ERR, tmp42, true); \
  91. } \
  92. _quit(status); \
  93. } while (0)
  94. #define quitfrom(status, _file, _func, _line, fmt, ...) do { \
  95. if (fmt) { \
  96. char tmp42[LOGBUFSIZ]; \
  97. snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
  98. ##__VA_ARGS__, _file, _func, _line); \
  99. _applog(LOG_ERR, tmp42, true); \
  100. } \
  101. _quit(status); \
  102. } while (0)
  103. #ifdef HAVE_CURSES
  104. #define wlog(fmt, ...) do { \
  105. char tmp42[LOGBUFSIZ]; \
  106. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  107. _wlog(tmp42); \
  108. } while (0)
  109. #define wlogprint(fmt, ...) do { \
  110. char tmp42[LOGBUFSIZ]; \
  111. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  112. _wlogprint(tmp42); \
  113. } while (0)
  114. #endif
  115. #endif /* __LOGGING_H__ */