logging.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright 2012 zefir
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #ifndef __LOGGING_H__
  10. #define __LOGGING_H__
  11. #include "config.h"
  12. #include <errno.h>
  13. #include <stdbool.h>
  14. #include <stdio.h>
  15. #include <stdarg.h>
  16. #ifdef HAVE_SYSLOG_H
  17. #include <syslog.h>
  18. #else
  19. enum {
  20. LOG_ERR,
  21. LOG_WARNING,
  22. LOG_NOTICE,
  23. LOG_INFO,
  24. LOG_DEBUG,
  25. };
  26. #endif
  27. #include "util.h"
  28. /* debug flags */
  29. extern bool opt_debug;
  30. extern bool opt_debug_console;
  31. extern bool opt_log_output;
  32. extern bool opt_log_microseconds;
  33. extern bool opt_realquiet;
  34. extern bool want_per_device_stats;
  35. /* global log_level, messages with lower or equal prio are logged */
  36. extern int opt_log_level;
  37. #define LOGBUFSIZ 0x1000
  38. extern void _applog(int prio, const char *str);
  39. #define IN_FMT_FFL " in %s %s():%d"
  40. #define applog(prio, fmt, ...) do { \
  41. if (opt_debug || prio != LOG_DEBUG) { \
  42. char tmp42[LOGBUFSIZ]; \
  43. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  44. _applog(prio, tmp42); \
  45. } \
  46. } while (0)
  47. #define applogr(rv, prio, ...) do { \
  48. applog(prio, __VA_ARGS__); \
  49. return rv; \
  50. } while (0)
  51. #define appperror(prio, s) do { \
  52. const char *_tmp43 = bfg_strerror(errno, BST_ERRNO); \
  53. if (s && s[0]) \
  54. applog(prio, "%s: %s", s, _tmp43); \
  55. else \
  56. _applog(prio, _tmp43); \
  57. } while (0)
  58. #define perror(s) appperror(LOG_ERR, s)
  59. extern void _bfg_clean_up(void);
  60. #define quit(status, fmt, ...) do { \
  61. _bfg_clean_up(); \
  62. if (fmt) { \
  63. fprintf(stderr, fmt, ##__VA_ARGS__); \
  64. } \
  65. fprintf(stderr, "\n"); \
  66. fflush(stderr); \
  67. _quit(status); \
  68. } while (0)
  69. #define quithere(status, fmt, ...) do { \
  70. if (fmt) { \
  71. char tmp42[LOGBUFSIZ]; \
  72. snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
  73. ##__VA_ARGS__, __FILE__, __func__, __LINE__); \
  74. _applog(LOG_ERR, tmp42); \
  75. } \
  76. _quit(status); \
  77. } while (0)
  78. #define quitfrom(status, _file, _func, _line, fmt, ...) do { \
  79. if (fmt) { \
  80. char tmp42[LOGBUFSIZ]; \
  81. snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
  82. ##__VA_ARGS__, _file, _func, _line); \
  83. _applog(LOG_ERR, tmp42); \
  84. } \
  85. _quit(status); \
  86. } while (0)
  87. #ifdef HAVE_CURSES
  88. #define wlog(fmt, ...) do { \
  89. char tmp42[LOGBUFSIZ]; \
  90. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  91. _wlog(tmp42); \
  92. } while (0)
  93. #define wlogprint(fmt, ...) do { \
  94. char tmp42[LOGBUFSIZ]; \
  95. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  96. _wlogprint(tmp42); \
  97. } while (0)
  98. #endif
  99. extern void hexdump(const void *, unsigned int len);
  100. #endif /* __LOGGING_H__ */