dte test coverage


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 50.0% high: ≥ 85.0%
Coverage Exec / Excl / Total
Lines: 98.7% 77 / 0 / 78
Functions: 100.0% 10 / 0 / 10
Branches: 70.5% 31 / 28 / 72

src/util/log.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <stdio.h>
3 #include <unistd.h>
4 #include "log.h"
5 #include "array.h"
6 #include "fd.h"
7 #include "xreadwrite.h"
8 #include "xstring.h"
9
10 // These are initialized during early startup and then never changed,
11 // so they're deemed an acceptable use of non-const globals:
12 // NOLINTBEGIN(*-avoid-non-const-global-variables)
13 static LogLevel log_level = LOG_LEVEL_NONE;
14 static int logfd = -1;
15 static char dim[] = "\033[2m";
16 static char sgr0[] = "\033[0m";
17 // NOLINTEND(*-avoid-non-const-global-variables)
18
19 static const struct LogLevelMap {
20 char name[8];
21 char prefix[5];
22 char color[8];
23 } log_level_map[] = {
24 [LOG_LEVEL_NONE] = {"none", "_", ""},
25 [LOG_LEVEL_CRITICAL] = {"crit", "crit", "\033[1;31m"},
26 [LOG_LEVEL_ERROR] = {"error", " err", "\033[31m"},
27 [LOG_LEVEL_WARNING] = {"warning", "warn", "\033[33m"},
28 [LOG_LEVEL_NOTICE] = {"notice", "note", "\033[34m"},
29 [LOG_LEVEL_INFO] = {"info", "info", ""},
30 [LOG_LEVEL_DEBUG] = {"debug", "dbug", ""},
31 [LOG_LEVEL_TRACE] = {"trace", "trce", ""},
32 };
33
34 24 UNITTEST {
35 24 CHECK_STRUCT_ARRAY(log_level_map, name);
36
2/2
✓ Branch 9 → 4 taken 192 times.
✓ Branch 9 → 10 taken 24 times.
216 for (size_t i = 0; i < ARRAYLEN(log_level_map); i++) {
37 192 const struct LogLevelMap *m = &log_level_map[i];
38 192 BUG_ON(m->prefix[sizeof(m->prefix) - 1] != '\0');
39 192 BUG_ON(m->color[sizeof(m->color) - 1] != '\0');
40 }
41 24 }
42
43 14 LogLevel log_level_from_str(const char *str)
44 {
45
4/4
✓ Branch 2 → 3 taken 13 times.
✓ Branch 2 → 4 taken 1 time.
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 7 taken 12 times.
14 if (!str || str[0] == '\0') {
46 2 return log_level_default();
47 }
48
2/2
✓ Branch 7 → 5 taken 68 times.
✓ Branch 7 → 8 taken 4 times.
72 for (LogLevel i = 0; i < ARRAYLEN(log_level_map); i++) {
49
2/2
✓ Branch 5 → 6 taken 60 times.
✓ Branch 5 → 8 taken 8 times.
68 if (streq(str, log_level_map[i].name)) {
50 return i;
51 }
52 }
53 return LOG_LEVEL_INVALID;
54 }
55
56 8 const char *log_level_to_str(LogLevel level)
57 {
58 8 BUG_ON(level < LOG_LEVEL_NONE);
59 8 BUG_ON(level > LOG_LEVEL_TRACE);
60 8 return log_level_map[level].name;
61 }
62
63 4 LogLevel log_open(const char *filename, LogLevel level, LogOpenFlags logflags)
64 {
65 4 BUG_ON(!filename);
66 4 BUG_ON(level < LOG_LEVEL_NONE);
67 4 BUG_ON(level > LOG_LEVEL_TRACE);
68 4 BUG_ON(logfd >= 0);
69 4 BUG_ON(log_level != LOG_LEVEL_NONE);
70
71
2/2
✓ Branch 12 → 13 taken 3 times.
✓ Branch 12 → 31 taken 1 time.
4 if (level == LOG_LEVEL_NONE) {
72 return LOG_LEVEL_NONE;
73 }
74
75 3 int oflags = O_WRONLY | O_CREAT | O_APPEND | O_TRUNC | O_CLOEXEC;
76 3 int fd = xopen(filename, oflags, 0666);
77
2/2
✓ Branch 14 → 15 taken 2 times.
✓ Branch 14 → 31 taken 1 time.
3 if (unlikely(fd < 0)) {
78 return LOG_LEVEL_NONE;
79 }
80
81
2/4
✓ Branch 15 → 16 taken 2 times.
✗ Branch 15 → 18 not taken.
✓ Branch 17 → 18 taken 2 times.
✗ Branch 17 → 20 not taken.
2 bool ctty_err = !(logflags & LOGOPEN_ALLOW_CTTY) && is_controlling_tty(fd);
82
2/2
✓ Branch 19 → 20 taken 1 time.
✓ Branch 19 → 24 taken 1 time.
2 if (unlikely(ctty_err || xwrite_all(fd, "\n", 1) != 1)) {
83
1/2
✓ Branch 20 → 21 taken 1 time.
✗ Branch 20 → 22 not taken.
1 errno = ctty_err ? EINVAL : errno;
84 1 xclose(fd);
85 1 return LOG_LEVEL_NONE;
86 }
87
88 1 bool use_color = (logflags & LOGOPEN_USE_COLOR);
89
1/4
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 27 taken 1 time.
✗ Branch 26 → 27 not taken.
✗ Branch 26 → 28 not taken.
1 if (!use_color || !isatty(fd)) {
90 1 dim[0] = '\0';
91 1 sgr0[0] = '\0';
92 }
93
94 1 logfd = fd;
95
1/2
✓ Branch 28 → 29 taken 1 time.
✗ Branch 28 → 30 not taken.
1 log_level = MIN(level, log_level_max());
96 1 return log_level;
97 }
98
99 9 bool log_close(void)
100 {
101
3/4
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 6 taken 8 times.
✓ Branch 4 → 5 taken 1 time.
✗ Branch 4 → 6 not taken.
9 return log_level == LOG_LEVEL_NONE || xclose(logfd) == 0;
102 }
103
104 559 bool log_level_enabled(LogLevel level)
105 {
106 559 BUG_ON(level <= LOG_LEVEL_NONE || level > LOG_LEVEL_TRACE);
107 559 BUG_ON(log_level < LOG_LEVEL_NONE || log_level > log_level_max());
108 559 return level <= log_level;
109 }
110
111 // This function should be kept async signal safe
112 2 void log_write(LogLevel level, const char *str, size_t len)
113 {
114 // log_level is a non-const global, but as noted above, it's never
115 // modified after early startup and so poses no signal safety issues
116 // (see https://austingroupbugs.net/view.php?id=728#c6430)
117
1/2
✓ Branch 3 → 4 taken 2 times.
✗ Branch 3 → 8 not taken.
2 if (!log_level_enabled(level)) {
118 return;
119 }
120
121 // xwrite_all() only calls write(3), which is async signal safe
122 // (see signal-safety(7))
123 2 BUG_ON(logfd < 0);
124 2 (void)!xwrite_all(logfd, str, len);
125 2 (void)!xwrite_all(logfd, "\n", 1);
126 }
127
128 530 void log_msgv(LogLevel level, const char *file, int line, const char *fmt, va_list ap)
129 {
130
2/2
✓ Branch 3 → 4 taken 117 times.
✓ Branch 3 → 5 taken 413 times.
530 if (!log_level_enabled(level)) {
131 117 return;
132 }
133
134 413 BUG_ON(logfd < 0);
135 413 char buf[2048];
136 413 const int saved_errno = errno;
137 413 const size_t buf_size = sizeof(buf) - 1;
138 413 const char *prefix = log_level_map[level].prefix;
139
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 413 times.
413 const char *color = sgr0[0] ? log_level_map[level].color : "";
140
1/2
✓ Branch 9 → 10 taken 413 times.
✗ Branch 9 → 11 not taken.
413 const char *reset = color[0] ? sgr0 : "";
141
142 413 int len1 = snprintf (
143 buf, buf_size,
144 "%s%s%s: %s%s:%d:%s ",
145 color, prefix, reset,
146 dim, file, line, sgr0
147 );
148
149
1/2
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 413 times.
413 if (unlikely(len1 < 0 || len1 >= buf_size)) {
150 len1 = 0;
151 }
152
153 413 int len2 = vsnprintf(buf + len1, buf_size - len1, fmt, ap);
154 413 size_t n = MIN(len1 + MAX(len2, 0), buf_size);
155 413 buf[n++] = '\n';
156 413 (void)!xwrite_all(logfd, buf, n);
157 413 errno = saved_errno;
158 }
159
160 396 void log_msg(LogLevel level, const char *file, int line, const char *fmt, ...)
161 {
162 396 va_list ap;
163 396 va_start(ap, fmt);
164 396 log_msgv(level, file, line, fmt, ap);
165 396 va_end(ap);
166 396 }
167
168 1 SystemErrno log_errno(const char *file, int line, const char *prefix)
169 {
170 1 SystemErrno err = errno;
171
1/2
✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 6 not taken.
1 if (log_level_enabled(LOG_LEVEL_ERROR)) {
172 1 log_msg(LOG_LEVEL_ERROR, file, line, "%s: %s", prefix, strerror(err));
173 }
174 1 return err;
175 }
176