err.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "defaults.h"
  2. #include "progname.h"
  3. #include "compat.h"
  4. #include "error.h"
  5. #include <util/generic/scope.h>
  6. #include <util/system/compat.h>
  7. #include <util/stream/printf.h>
  8. #include <util/stream/output.h>
  9. void vwarnx(const char* fmt, va_list args) {
  10. Cerr << GetProgramName() << ": ";
  11. if (fmt) {
  12. Printf(Cerr, fmt, args);
  13. }
  14. Cerr << '\n';
  15. }
  16. void vwarn(const char* fmt, va_list args) {
  17. int curErrNo = errno;
  18. auto curErrText = LastSystemErrorText();
  19. Y_DEFER {
  20. errno = curErrNo;
  21. };
  22. Cerr << GetProgramName() << ": ";
  23. if (fmt) {
  24. Printf(Cerr, fmt, args);
  25. Cerr << ": ";
  26. }
  27. Cerr << curErrText << '\n';
  28. }
  29. void warn(const char* fmt, ...) {
  30. va_list args;
  31. va_start(args, fmt);
  32. vwarn(fmt, args);
  33. va_end(args);
  34. }
  35. void warnx(const char* fmt, ...) {
  36. va_list args;
  37. va_start(args, fmt);
  38. vwarnx(fmt, args);
  39. va_end(args);
  40. }
  41. [[noreturn]] void verr(int status, const char* fmt, va_list args) {
  42. vwarn(fmt, args);
  43. std::exit(status);
  44. }
  45. [[noreturn]] void err(int status, const char* fmt, ...) {
  46. va_list args;
  47. va_start(args, fmt);
  48. verr(status, fmt, args);
  49. va_end(args);
  50. }
  51. [[noreturn]] void verrx(int status, const char* fmt, va_list args) {
  52. vwarnx(fmt, args);
  53. std::exit(status);
  54. }
  55. [[noreturn]] void errx(int status, const char* fmt, ...) {
  56. va_list args;
  57. va_start(args, fmt);
  58. verrx(status, fmt, args);
  59. va_end(args);
  60. }