3
0

verror_msg.c 1013 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  8. */
  9. #include "libbb.h"
  10. #include <syslog.h>
  11. int logmode = LOGMODE_STDIO;
  12. const char *msg_eol = "\n";
  13. void bb_verror_msg(const char *s, va_list p, const char* strerr)
  14. {
  15. /* va_copy is used because it is not portable
  16. * to use va_list p twice */
  17. va_list p2;
  18. va_copy(p2, p);
  19. if (logmode & LOGMODE_STDIO) {
  20. fflush(stdout);
  21. fprintf(stderr, "%s: ", applet_name);
  22. vfprintf(stderr, s, p);
  23. if (!strerr)
  24. fputs(msg_eol, stderr);
  25. else
  26. fprintf(stderr, "%s%s%s",
  27. s ? ": " : "",
  28. strerr, msg_eol);
  29. }
  30. if (ENABLE_FEATURE_SYSLOG && (logmode & LOGMODE_SYSLOG)) {
  31. if (!strerr)
  32. vsyslog(LOG_ERR, s, p2);
  33. else {
  34. char *msg;
  35. if (vasprintf(&msg, s, p2) < 0)
  36. bb_error_msg_and_die(bb_msg_memory_exhausted);
  37. syslog(LOG_ERR, "%s: %s", msg, strerr);
  38. free(msg);
  39. }
  40. }
  41. va_end(p2);
  42. }