logging.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #define quit(status, fmt, ...) do { \
  49. if (fmt) { \
  50. char tmp42[LOGBUFSIZ]; \
  51. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  52. _applog(LOG_ERR, tmp42); \
  53. } \
  54. _quit(status); \
  55. } while (0)
  56. #ifdef HAVE_CURSES
  57. #define wlog(fmt, ...) do { \
  58. char tmp42[LOGBUFSIZ]; \
  59. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  60. _wlog(tmp42); \
  61. } while (0)
  62. #define wlogprint(fmt, ...) do { \
  63. char tmp42[LOGBUFSIZ]; \
  64. snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
  65. _wlogprint(tmp42); \
  66. } while (0)
  67. #endif
  68. extern void hexdump(const void *, unsigned int len);
  69. #endif /* __LOGGING_H__ */