logging.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 __LOGGING_H__
  11. #define __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 LOGBUFSIZ 0x1000
  39. extern void _applog(int prio, const char *str);
  40. #define IN_FMT_FFL " in %s %s():%d"
  41. #define applog(prio, fmt, ...) do { \
  42. if (opt_debug || prio != LOG_DEBUG) { \
  43. char tmp42[LOGBUFSIZ]; \
  44. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  45. _applog(prio, tmp42); \
  46. } \
  47. } while (0)
  48. #define applogsiz(prio, _SIZ, fmt, ...) do { \
  49. if (opt_debug || prio != LOG_DEBUG) { \
  50. char tmp42[_SIZ]; \
  51. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  52. _applog(prio, tmp42); \
  53. } \
  54. } while (0)
  55. #define applogr(rv, prio, ...) do { \
  56. applog(prio, __VA_ARGS__); \
  57. return rv; \
  58. } while (0)
  59. #define appperror(prio, s) do { \
  60. const char *_tmp43 = bfg_strerror(errno, BST_ERRNO); \
  61. if (s && s[0]) \
  62. applog(prio, "%s: %s", s, _tmp43); \
  63. else \
  64. _applog(prio, _tmp43); \
  65. } while (0)
  66. #define perror(s) appperror(LOG_ERR, s)
  67. #define applogfailinfo(prio, failed, fmt, ...) do { \
  68. applog(prio, "Failed to %s"IN_FMT_FFL": "fmt, \
  69. failed, \
  70. __FILE__, __func__, __LINE__, \
  71. __VA_ARGS__); \
  72. } while (0)
  73. #define applogfailinfor(rv, prio, failed, fmt, ...) do { \
  74. applogfailinfo(prio, failed, fmt, __VA_ARGS__); \
  75. return rv; \
  76. } while (0)
  77. #define applogfail(prio, failed) do { \
  78. applog(prio, "Failed to %s"IN_FMT_FFL, \
  79. failed, \
  80. __FILE__, __func__, __LINE__); \
  81. } while (0)
  82. #define applogfailr(rv, prio, failed) do { \
  83. applogfail(prio, failed); \
  84. return rv; \
  85. } while (0)
  86. extern void _bfg_clean_up(bool);
  87. #define quit(status, fmt, ...) do { \
  88. _bfg_clean_up(false); \
  89. if (fmt) { \
  90. fprintf(stderr, fmt, ##__VA_ARGS__); \
  91. } \
  92. fprintf(stderr, "\n"); \
  93. fflush(stderr); \
  94. _quit(status); \
  95. } while (0)
  96. #define quithere(status, fmt, ...) do { \
  97. if (fmt) { \
  98. char tmp42[LOGBUFSIZ]; \
  99. snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
  100. ##__VA_ARGS__, __FILE__, __func__, __LINE__); \
  101. _applog(LOG_ERR, tmp42); \
  102. } \
  103. _quit(status); \
  104. } while (0)
  105. #define quitfrom(status, _file, _func, _line, fmt, ...) do { \
  106. if (fmt) { \
  107. char tmp42[LOGBUFSIZ]; \
  108. snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
  109. ##__VA_ARGS__, _file, _func, _line); \
  110. _applog(LOG_ERR, tmp42); \
  111. } \
  112. _quit(status); \
  113. } while (0)
  114. #ifdef HAVE_CURSES
  115. #define wlog(fmt, ...) do { \
  116. char tmp42[LOGBUFSIZ]; \
  117. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  118. _wlog(tmp42); \
  119. } while (0)
  120. #define wlogprint(fmt, ...) do { \
  121. char tmp42[LOGBUFSIZ]; \
  122. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  123. _wlogprint(tmp42); \
  124. } while (0)
  125. #endif
  126. extern void hexdump(const void *, unsigned int len);
  127. #endif /* __LOGGING_H__ */