logging.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 applog(prio, fmt, ...) do { \
  38. if (opt_debug || prio != LOG_DEBUG) { \
  39. char tmp42[LOGBUFSIZ]; \
  40. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  41. _applog(prio, tmp42); \
  42. } \
  43. } while (0)
  44. #define applogr(rv, prio, ...) do { \
  45. applog(prio, __VA_ARGS__); \
  46. return rv; \
  47. } while (0)
  48. extern void _bfg_clean_up(void);
  49. #define quit(status, fmt, ...) do { \
  50. _bfg_clean_up(); \
  51. if (fmt) { \
  52. fprintf(stderr, fmt, ##__VA_ARGS__); \
  53. } \
  54. fprintf(stderr, "\n"); \
  55. fflush(stderr); \
  56. _quit(status); \
  57. } while (0)
  58. #ifdef HAVE_CURSES
  59. #define wlog(fmt, ...) do { \
  60. char tmp42[LOGBUFSIZ]; \
  61. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  62. _wlog(tmp42); \
  63. } while (0)
  64. #define wlogprint(fmt, ...) do { \
  65. char tmp42[LOGBUFSIZ]; \
  66. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  67. _wlogprint(tmp42); \
  68. } while (0)
  69. #endif
  70. extern void hexdump(const void *, unsigned int len);
  71. #endif /* __LOGGING_H__ */