logging.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. extern void _bfg_clean_up(void);
  68. #define quit(status, fmt, ...) do { \
  69. _bfg_clean_up(); \
  70. if (fmt) { \
  71. fprintf(stderr, fmt, ##__VA_ARGS__); \
  72. } \
  73. fprintf(stderr, "\n"); \
  74. fflush(stderr); \
  75. _quit(status); \
  76. } while (0)
  77. #define quithere(status, fmt, ...) do { \
  78. if (fmt) { \
  79. char tmp42[LOGBUFSIZ]; \
  80. snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
  81. ##__VA_ARGS__, __FILE__, __func__, __LINE__); \
  82. _applog(LOG_ERR, tmp42); \
  83. } \
  84. _quit(status); \
  85. } while (0)
  86. #define quitfrom(status, _file, _func, _line, fmt, ...) do { \
  87. if (fmt) { \
  88. char tmp42[LOGBUFSIZ]; \
  89. snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
  90. ##__VA_ARGS__, _file, _func, _line); \
  91. _applog(LOG_ERR, tmp42); \
  92. } \
  93. _quit(status); \
  94. } while (0)
  95. #ifdef HAVE_CURSES
  96. #define wlog(fmt, ...) do { \
  97. char tmp42[LOGBUFSIZ]; \
  98. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  99. _wlog(tmp42); \
  100. } while (0)
  101. #define wlogprint(fmt, ...) do { \
  102. char tmp42[LOGBUFSIZ]; \
  103. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  104. _wlogprint(tmp42); \
  105. } while (0)
  106. #endif
  107. extern void hexdump(const void *, unsigned int len);
  108. #endif /* __LOGGING_H__ */