logging.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 applogr(rv, prio, ...) do { \
  49. applog(prio, __VA_ARGS__); \
  50. return rv; \
  51. } while (0)
  52. #define appperror(prio, s) do { \
  53. const char *_tmp43 = bfg_strerror(errno, BST_ERRNO); \
  54. if (s && s[0]) \
  55. applog(prio, "%s: %s", s, _tmp43); \
  56. else \
  57. _applog(prio, _tmp43); \
  58. } while (0)
  59. #define perror(s) appperror(LOG_ERR, s)
  60. extern void _bfg_clean_up(void);
  61. #define quit(status, fmt, ...) do { \
  62. _bfg_clean_up(); \
  63. if (fmt) { \
  64. fprintf(stderr, fmt, ##__VA_ARGS__); \
  65. } \
  66. fprintf(stderr, "\n"); \
  67. fflush(stderr); \
  68. _quit(status); \
  69. } while (0)
  70. #define quithere(status, fmt, ...) do { \
  71. if (fmt) { \
  72. char tmp42[LOGBUFSIZ]; \
  73. snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
  74. ##__VA_ARGS__, __FILE__, __func__, __LINE__); \
  75. _applog(LOG_ERR, tmp42); \
  76. } \
  77. _quit(status); \
  78. } while (0)
  79. #define quitfrom(status, _file, _func, _line, fmt, ...) do { \
  80. if (fmt) { \
  81. char tmp42[LOGBUFSIZ]; \
  82. snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
  83. ##__VA_ARGS__, _file, _func, _line); \
  84. _applog(LOG_ERR, tmp42); \
  85. } \
  86. _quit(status); \
  87. } while (0)
  88. #ifdef HAVE_CURSES
  89. #define wlog(fmt, ...) do { \
  90. char tmp42[LOGBUFSIZ]; \
  91. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  92. _wlog(tmp42); \
  93. } while (0)
  94. #define wlogprint(fmt, ...) do { \
  95. char tmp42[LOGBUFSIZ]; \
  96. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  97. _wlogprint(tmp42); \
  98. } while (0)
  99. #endif
  100. extern void hexdump(const void *, unsigned int len);
  101. #endif /* __LOGGING_H__ */