dte test coverage


Directory: ./
File: src/util/log.c
Date: 2025-11-12 12:04:10
Coverage Exec Excl Total
Lines: 92.2% 71 0 77
Functions: 90.0% 9 0 10
Branches: 69.0% 29 0 42

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 llmap {
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 llmap *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, bool use_color)
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 → 30 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 → 30 taken 1 time.
3 if (unlikely(fd < 0)) {
78 return LOG_LEVEL_NONE;
79 }
80
81 2 bool ctty = is_controlling_tty(fd);
82
3/4
✓ Branch 16 → 17 taken 2 times.
✗ Branch 16 → 21 not taken.
✓ Branch 18 → 19 taken 1 time.
✓ Branch 18 → 23 taken 1 time.
2 if (unlikely(ctty || xwrite_all(fd, "\n", 1) != 1)) {
83
1/2
✓ Branch 19 → 20 taken 1 time.
✗ Branch 19 → 21 not taken.
1 errno = ctty ? EINVAL : errno;
84 1 xclose(fd);
85 1 return LOG_LEVEL_NONE;
86 }
87
88
1/4
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 26 taken 1 time.
✗ Branch 25 → 26 not taken.
✗ Branch 25 → 27 not taken.
1 if (!use_color || !isatty(fd)) {
89 1 dim[0] = '\0';
90 1 sgr0[0] = '\0';
91 }
92
93 1 logfd = fd;
94
1/2
✓ Branch 27 → 28 taken 1 time.
✗ Branch 27 → 29 not taken.
1 log_level = MIN(level, log_level_max());
95 1 return log_level;
96 }
97
98 9 bool log_close(void)
99 {
100
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;
101 }
102
103 558 bool log_level_enabled(LogLevel level)
104 {
105 558 BUG_ON(level <= LOG_LEVEL_NONE || level > LOG_LEVEL_TRACE);
106 558 BUG_ON(log_level < LOG_LEVEL_NONE || log_level > log_level_max());
107 558 return level <= log_level;
108 }
109
110 // This function should be kept async signal safe
111 2 void log_write(LogLevel level, const char *str, size_t len)
112 {
113 // log_level is a non-const global, but as noted above, it's never
114 // modified after early startup and so poses no signal safety issues
115 // (see https://austingroupbugs.net/view.php?id=728#c6430)
116
1/2
✓ Branch 3 → 4 taken 2 times.
✗ Branch 3 → 8 not taken.
2 if (!log_level_enabled(level)) {
117 return;
118 }
119
120 // xwrite_all() only calls write(3), which is async signal safe
121 // (see signal-safety(7))
122 2 BUG_ON(logfd < 0);
123 2 (void)!xwrite_all(logfd, str, len);
124 2 (void)!xwrite_all(logfd, "\n", 1);
125 }
126
127 530 void log_msgv(LogLevel level, const char *file, int line, const char *fmt, va_list ap)
128 {
129
2/2
✓ Branch 3 → 4 taken 117 times.
✓ Branch 3 → 5 taken 413 times.
530 if (!log_level_enabled(level)) {
130 117 return;
131 }
132
133 413 BUG_ON(logfd < 0);
134 413 char buf[2048];
135 413 const int saved_errno = errno;
136 413 const size_t buf_size = sizeof(buf) - 1;
137 413 const char *prefix = log_level_map[level].prefix;
138
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 413 times.
413 const char *color = sgr0[0] ? log_level_map[level].color : "";
139
1/2
✓ Branch 9 → 10 taken 413 times.
✗ Branch 9 → 11 not taken.
413 const char *reset = color[0] ? sgr0 : "";
140
141 413 int len1 = snprintf (
142 buf, buf_size,
143 "%s%s%s: %s%s:%d:%s ",
144 color, prefix, reset,
145 dim, file, line, sgr0
146 );
147
148
1/2
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 413 times.
413 if (unlikely(len1 < 0 || len1 >= buf_size)) {
149 len1 = 0;
150 }
151
152 413 int len2 = vsnprintf(buf + len1, buf_size - len1, fmt, ap);
153 413 size_t n = MIN(len1 + MAX(len2, 0), buf_size);
154 413 buf[n++] = '\n';
155 413 (void)!xwrite_all(logfd, buf, n);
156 413 errno = saved_errno;
157 }
158
159 396 void log_msg(LogLevel level, const char *file, int line, const char *fmt, ...)
160 {
161 396 va_list ap;
162 396 va_start(ap, fmt);
163 396 log_msgv(level, file, line, fmt, ap);
164 396 va_end(ap);
165 396 }
166
167 SystemErrno log_errno(const char *file, int line, const char *prefix)
168 {
169 SystemErrno err = errno;
170 if (log_level_enabled(LOG_LEVEL_ERROR)) {
171 log_msg(LOG_LEVEL_ERROR, file, line, "%s: %s", prefix, strerror(err));
172 }
173 return err;
174 }
175