aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/err.cpp
blob: 48bddb783d8e78c779cfc3809bcc19e62048f50d (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
#include "defaults.h" 
#include "progname.h"
#include "compat.h"
#include "error.h"

#include <util/generic/scope.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);
}

void verr(int status, const char* fmt, va_list args) {
    vwarn(fmt, args);
    exit(status);
}

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

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

void verrx(int status, const char* fmt, va_list args) {
    vwarnx(fmt, args);
    exit(status);
}

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

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