logging.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 <stdbool.h>
  13. #include <stdio.h>
  14. #include <stdarg.h>
  15. #ifdef HAVE_SYSLOG_H
  16. #include <syslog.h>
  17. #else
  18. enum {
  19. LOG_ERR,
  20. LOG_WARNING,
  21. LOG_NOTICE,
  22. LOG_INFO,
  23. LOG_DEBUG,
  24. };
  25. #endif
  26. /* debug flags */
  27. extern bool opt_debug;
  28. extern bool opt_debug_console;
  29. extern bool opt_log_output;
  30. extern bool opt_log_microseconds;
  31. extern bool opt_realquiet;
  32. extern bool want_per_device_stats;
  33. /* global log_level, messages with lower or equal prio are logged */
  34. extern int opt_log_level;
  35. #define LOGBUFSIZ 256
  36. extern void _applog(int prio, const char *str);
  37. #define IN_FMT_FFL " in %s %s():%d"
  38. #define applog(prio, fmt, ...) do { \
  39. if (opt_debug || prio != LOG_DEBUG) { \
  40. char tmp42[LOGBUFSIZ]; \
  41. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  42. _applog(prio, tmp42); \
  43. } \
  44. } while (0)
  45. #define applogr(rv, prio, ...) do { \
  46. applog(prio, __VA_ARGS__); \
  47. return rv; \
  48. } while (0)
  49. extern void _bfg_clean_up(void);
  50. #define quit(status, fmt, ...) do { \
  51. _bfg_clean_up(); \
  52. if (fmt) { \
  53. fprintf(stderr, fmt, ##__VA_ARGS__); \
  54. } \
  55. fprintf(stderr, "\n"); \
  56. fflush(stderr); \
  57. _quit(status); \
  58. } while (0)
  59. #define quithere(status, fmt, ...) do { \
  60. if (fmt) { \
  61. char tmp42[LOGBUFSIZ]; \
  62. snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
  63. ##__VA_ARGS__, __FILE__, __func__, __LINE__); \
  64. _applog(LOG_ERR, tmp42); \
  65. } \
  66. _quit(status); \
  67. } while (0)
  68. #define quitfrom(status, _file, _func, _line, fmt, ...) do { \
  69. if (fmt) { \
  70. char tmp42[LOGBUFSIZ]; \
  71. snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
  72. ##__VA_ARGS__, _file, _func, _line); \
  73. _applog(LOG_ERR, tmp42); \
  74. } \
  75. _quit(status); \
  76. } while (0)
  77. #ifdef HAVE_CURSES
  78. #define wlog(fmt, ...) do { \
  79. char tmp42[LOGBUFSIZ]; \
  80. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  81. _wlog(tmp42); \
  82. } while (0)
  83. #define wlogprint(fmt, ...) do { \
  84. char tmp42[LOGBUFSIZ]; \
  85. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  86. _wlogprint(tmp42); \
  87. } while (0)
  88. #endif
  89. extern void hexdump(const void *, unsigned int len);
  90. #endif /* __LOGGING_H__ */