Line | Branch | Exec | Source |
---|---|---|---|
1 | #include <errno.h> | ||
2 | #include <stdarg.h> | ||
3 | #include <stdio.h> | ||
4 | #include <string.h> | ||
5 | #include "error.h" | ||
6 | #include "command/run.h" | ||
7 | #include "util/log.h" | ||
8 | #include "util/xstdio.h" | ||
9 | |||
10 | VPRINTF(3) | ||
11 | 172 | static void error_msgv(ErrorBuffer *eb, const char *cmd, const char *format, va_list ap) | |
12 | { | ||
13 | 172 | const size_t size = sizeof(eb->buf); | |
14 | 172 | const char *file = eb->config_filename; | |
15 | 172 | unsigned int line = eb->config_line; | |
16 | 172 | int pos = 0; | |
17 | |||
18 |
2/2✓ Branch 0 (2→3) taken 3 times.
✓ Branch 1 (2→4) taken 169 times.
|
172 | if (file && cmd) { |
19 | 3 | pos = snprintf(eb->buf, size, "%s:%u: %s: ", file, line, cmd); | |
20 |
2/2✓ Branch 0 (4→5) taken 11 times.
✓ Branch 1 (4→6) taken 158 times.
|
169 | } else if (file) { |
21 | 11 | pos = snprintf(eb->buf, size, "%s:%u: ", file, line); | |
22 |
2/2✓ Branch 0 (6→7) taken 145 times.
✓ Branch 1 (6→11) taken 13 times.
|
158 | } else if (cmd) { |
23 | 145 | pos = snprintf(eb->buf, size, "%s: ", cmd); | |
24 | } | ||
25 | |||
26 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→11) taken 159 times.
|
159 | if (unlikely(pos < 0)) { |
27 | // Note: POSIX snprintf(3) *does* set errno on failure (unlike ISO C) | ||
28 | ✗ | LOG_ERRNO("snprintf"); | |
29 | ✗ | pos = 0; | |
30 | } | ||
31 | |||
32 |
1/2✓ Branch 0 (11→12) taken 172 times.
✗ Branch 1 (11→13) not taken.
|
172 | if (likely(pos < (size - 3))) { |
33 | 172 | vsnprintf(eb->buf + pos, size - pos, format, ap); | |
34 | } else { | ||
35 | ✗ | LOG_WARNING("no buffer space left for error message"); | |
36 | } | ||
37 | |||
38 |
2/2✓ Branch 0 (14→15) taken 5 times.
✓ Branch 1 (14→18) taken 167 times.
|
172 | if (eb->print_to_stderr) { |
39 | 5 | xfputs(eb->buf, stderr); | |
40 | 5 | xfputc('\n', stderr); | |
41 | 5 | eb->stderr_errors_printed = true; | |
42 | } | ||
43 | |||
44 | 172 | eb->is_error = true; | |
45 | 172 | eb->nr_errors++; | |
46 | 172 | LOG_INFO("%s", eb->buf); | |
47 | 172 | } | |
48 | |||
49 | 151 | bool error_msg(ErrorBuffer *eb, const char *format, ...) | |
50 | { | ||
51 | 151 | va_list ap; | |
52 | 151 | va_start(ap, format); | |
53 | 151 | error_msgv(eb, eb->command_name, format, ap); | |
54 | 151 | va_end(ap); | |
55 | 151 | return false; // To allow tail-calling from command handlers | |
56 | } | ||
57 | |||
58 | 21 | bool error_msg_for_cmd(ErrorBuffer *eb, const char *cmd, const char *format, ...) | |
59 | { | ||
60 | 21 | va_list ap; | |
61 | 21 | va_start(ap, format); | |
62 | 21 | error_msgv(eb, cmd, format, ap); | |
63 | 21 | va_end(ap); | |
64 | 21 | return false; | |
65 | } | ||
66 | |||
67 | 1 | bool error_msg_errno(ErrorBuffer *eb, const char *prefix) | |
68 | { | ||
69 | 1 | return error_msg(eb, "%s: %s", prefix, strerror(errno)); | |
70 | } | ||
71 | |||
72 | 14 | bool info_msg(ErrorBuffer *eb, const char *format, ...) | |
73 | { | ||
74 | 14 | va_list ap; | |
75 | 14 | va_start(ap, format); | |
76 | 14 | vsnprintf(eb->buf, sizeof(eb->buf), format, ap); | |
77 | 14 | va_end(ap); | |
78 | 14 | eb->is_error = false; | |
79 | 14 | return true; // To allow tail-calling from command handlers | |
80 | } | ||
81 | |||
82 | 155 | void clear_error(ErrorBuffer *eb) | |
83 | { | ||
84 | 155 | eb->buf[0] = '\0'; | |
85 | 155 | } | |
86 |