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