warnp.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #ifndef WARNP_H_
  2. #define WARNP_H_
  3. #include <errno.h>
  4. #include <stddef.h>
  5. /* Avoid namespace collisions with BSD <err.h>. */
  6. #define warn libcperciva_warn
  7. #define warnx libcperciva_warnx
  8. /*
  9. * Maximum length of messages sent to syslog; longer warnings will
  10. * be truncated.
  11. */
  12. #define WARNP_SYSLOG_MAX_LINE 4095
  13. /**
  14. * warnp_setprogname(progname):
  15. * Set the program name to be used by warn() and warnx() to ${progname}.
  16. */
  17. void warnp_setprogname(const char *);
  18. #define WARNP_INIT do { \
  19. if (argv[0] != NULL) \
  20. warnp_setprogname(argv[0]); \
  21. } while (0)
  22. /**
  23. * warnp_syslog(enable):
  24. * Send future messages to syslog if ${enable} is non-zero. Messages to
  25. * syslog will be truncated at WARNP_SYSLOG_MAX_LINE characters.
  26. */
  27. void warnp_syslog(int);
  28. /**
  29. * warnp_syslog_priority(priority):
  30. * Tag future syslog messages with priority ${priority}. Do not enable
  31. * syslog messages; for that, use warnp_syslog().
  32. */
  33. void warnp_syslog_priority(int);
  34. /* As in BSD <err.h>. */
  35. void warn(const char *, ...);
  36. void warnx(const char *, ...);
  37. /*
  38. * If compiled with DEBUG defined, print __FILE__ and __LINE__.
  39. */
  40. #ifdef DEBUG
  41. #define warnline do { \
  42. warnx("%s, %d", __FILE__, __LINE__); \
  43. } while (0)
  44. #else
  45. #define warnline
  46. #endif
  47. /*
  48. * Call warn(3) or warnx(3) depending upon whether errno == 0; and clear
  49. * errno (so that the standard error message isn't repeated later).
  50. */
  51. #define warnp(...) do { \
  52. warnline; \
  53. if (errno != 0) { \
  54. warn(__VA_ARGS__); \
  55. errno = 0; \
  56. } else \
  57. warnx(__VA_ARGS__); \
  58. } while (0)
  59. /*
  60. * Call warnx(3) and set errno == 0. Unlike warnp(), this should be used
  61. * in cases where we're reporting a problem which we discover ourselves
  62. * rather than one which is reported to us from a library or the kernel.
  63. */
  64. #define warn0(...) do { \
  65. warnline; \
  66. warnx(__VA_ARGS__); \
  67. errno = 0; \
  68. } while (0)
  69. #endif /* !WARNP_H_ */