aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/err.cpp
blob: e2e5d8c9fdb333c5a37177599c756cee97915f3b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "defaults.h"
#include "progname.h"
#include "compat.h"
#include "error.h"

#include <util/generic/scope.h>

#include <util/system/compat.h>
#include <util/stream/printf.h>
#include <util/stream/output.h>

void vwarnx(const char* fmt, va_list args) {
    Cerr << GetProgramName() << ": ";

    if (fmt) {
        Printf(Cerr, fmt, args);
    }

    Cerr << '\n';
}

void vwarn(const char* fmt, va_list args) {
    int curErrNo = errno;
    auto curErrText = LastSystemErrorText();

    Y_DEFER {
        errno = curErrNo;
    };

    Cerr << GetProgramName() << ": ";

    if (fmt) {
        Printf(Cerr, fmt, args);
        Cerr << ": ";
    }

    Cerr << curErrText << '\n';
}

void warn(const char* fmt, ...) {
    va_list args;

    va_start(args, fmt);
    vwarn(fmt, args);
    va_end(args);
}

void warnx(const char* fmt, ...) {
    va_list args;

    va_start(args, fmt);
    vwarnx(fmt, args);
    va_end(args);
}

[[noreturn]] void verr(int status, const char* fmt, va_list args) {
    vwarn(fmt, args);
    std::exit(status);
}

[[noreturn]] void err(int status, const char* fmt, ...) {
    va_list args;

    va_start(args, fmt);
    verr(status, fmt, args);
    va_end(args);
}

[[noreturn]] void verrx(int status, const char* fmt, va_list args) {
    vwarnx(fmt, args);
    std::exit(status);
}

[[noreturn]] void errx(int status, const char* fmt, ...) {
    va_list args;

    va_start(args, fmt);
    verrx(status, fmt, args);
    va_end(args);
}