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 0 (9→4) taken 192 times.
✓ Branch 1 (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 | 4 | LogLevel log_level_default(void) | |
44 | { | ||
45 | 4 | return DEBUG_LOGGING_ENABLED ? LOG_LEVEL_DEBUG : LOG_LEVEL_INFO; | |
46 | } | ||
47 | |||
48 | 559 | static LogLevel log_level_max(void) | |
49 | { | ||
50 | 559 | return TRACE_LOGGING_ENABLED ? LOG_LEVEL_TRACE : log_level_default(); | |
51 | } | ||
52 | |||
53 | 14 | LogLevel log_level_from_str(const char *str) | |
54 | { | ||
55 |
4/4✓ Branch 0 (2→3) taken 13 times.
✓ Branch 1 (2→4) taken 1 times.
✓ Branch 2 (3→4) taken 1 times.
✓ Branch 3 (3→7) taken 12 times.
|
14 | if (!str || str[0] == '\0') { |
56 | 2 | return log_level_default(); | |
57 | } | ||
58 |
2/2✓ Branch 0 (7→5) taken 68 times.
✓ Branch 1 (7→8) taken 4 times.
|
72 | for (LogLevel i = 0; i < ARRAYLEN(log_level_map); i++) { |
59 |
2/2✓ Branch 0 (5→6) taken 60 times.
✓ Branch 1 (5→8) taken 8 times.
|
68 | if (streq(str, log_level_map[i].name)) { |
60 | return i; | ||
61 | } | ||
62 | } | ||
63 | return LOG_LEVEL_INVALID; | ||
64 | } | ||
65 | |||
66 | 8 | const char *log_level_to_str(LogLevel level) | |
67 | { | ||
68 | 8 | BUG_ON(level < LOG_LEVEL_NONE); | |
69 | 8 | BUG_ON(level > LOG_LEVEL_TRACE); | |
70 | 8 | return log_level_map[level].name; | |
71 | } | ||
72 | |||
73 | 4 | LogLevel log_open(const char *filename, LogLevel level, bool use_color) | |
74 | { | ||
75 | 4 | BUG_ON(!filename); | |
76 | 4 | BUG_ON(level < LOG_LEVEL_NONE); | |
77 | 4 | BUG_ON(level > LOG_LEVEL_TRACE); | |
78 | 4 | BUG_ON(logfd >= 0); | |
79 | 4 | BUG_ON(log_level != LOG_LEVEL_NONE); | |
80 | |||
81 |
2/2✓ Branch 0 (12→13) taken 3 times.
✓ Branch 1 (12→30) taken 1 times.
|
4 | if (level == LOG_LEVEL_NONE) { |
82 | return LOG_LEVEL_NONE; | ||
83 | } | ||
84 | |||
85 | 3 | int oflags = O_WRONLY | O_CREAT | O_APPEND | O_TRUNC | O_CLOEXEC; | |
86 | 3 | int fd = xopen(filename, oflags, 0666); | |
87 |
2/2✓ Branch 0 (14→15) taken 2 times.
✓ Branch 1 (14→30) taken 1 times.
|
3 | if (unlikely(fd < 0)) { |
88 | return LOG_LEVEL_NONE; | ||
89 | } | ||
90 | |||
91 | 2 | bool ctty = is_controlling_tty(fd); | |
92 |
3/4✓ Branch 0 (16→17) taken 2 times.
✗ Branch 1 (16→21) not taken.
✓ Branch 2 (18→19) taken 1 times.
✓ Branch 3 (18→23) taken 1 times.
|
2 | if (unlikely(ctty || xwrite_all(fd, "\n", 1) != 1)) { |
93 |
1/2✓ Branch 0 (19→20) taken 1 times.
✗ Branch 1 (19→21) not taken.
|
1 | errno = ctty ? EINVAL : errno; |
94 | 1 | xclose(fd); | |
95 | 1 | return LOG_LEVEL_NONE; | |
96 | } | ||
97 | |||
98 |
1/4✗ Branch 0 (23→24) not taken.
✓ Branch 1 (23→26) taken 1 times.
✗ Branch 2 (25→26) not taken.
✗ Branch 3 (25→27) not taken.
|
1 | if (!use_color || !isatty(fd)) { |
99 | 1 | dim[0] = '\0'; | |
100 | 1 | sgr0[0] = '\0'; | |
101 | } | ||
102 | |||
103 | 1 | logfd = fd; | |
104 |
1/2✓ Branch 0 (27→28) taken 1 times.
✗ Branch 1 (27→29) not taken.
|
1 | log_level = MIN(level, log_level_max()); |
105 | 1 | return log_level; | |
106 | } | ||
107 | |||
108 | 9 | bool log_close(void) | |
109 | { | ||
110 |
3/4✓ Branch 0 (2→3) taken 1 times.
✓ Branch 1 (2→6) taken 8 times.
✓ Branch 2 (4→5) taken 1 times.
✗ Branch 3 (4→6) not taken.
|
9 | return log_level == LOG_LEVEL_NONE || xclose(logfd) == 0; |
111 | } | ||
112 | |||
113 | 558 | bool log_level_enabled(LogLevel level) | |
114 | { | ||
115 | 558 | BUG_ON(level <= LOG_LEVEL_NONE || level > LOG_LEVEL_TRACE); | |
116 | 558 | BUG_ON(log_level < LOG_LEVEL_NONE || log_level > log_level_max()); | |
117 | 558 | return level <= log_level; | |
118 | } | ||
119 | |||
120 | // This function should be kept async signal safe | ||
121 | 2 | void log_write(LogLevel level, const char *str, size_t len) | |
122 | { | ||
123 | // log_level is a non-const global, but as noted above, it's never | ||
124 | // modified after early startup and so poses no signal safety issues | ||
125 | // (see https://austingroupbugs.net/view.php?id=728#c6430) | ||
126 |
1/2✓ Branch 0 (3→4) taken 2 times.
✗ Branch 1 (3→8) not taken.
|
2 | if (!log_level_enabled(level)) { |
127 | return; | ||
128 | } | ||
129 | |||
130 | // xwrite_all() only calls write(3), which is async signal safe | ||
131 | // (see signal-safety(7)) | ||
132 | 2 | BUG_ON(logfd < 0); | |
133 | 2 | (void)!xwrite_all(logfd, str, len); | |
134 | 2 | (void)!xwrite_all(logfd, "\n", 1); | |
135 | } | ||
136 | |||
137 | 530 | void log_msgv(LogLevel level, const char *file, int line, const char *fmt, va_list ap) | |
138 | { | ||
139 |
2/2✓ Branch 0 (3→4) taken 117 times.
✓ Branch 1 (3→5) taken 413 times.
|
530 | if (!log_level_enabled(level)) { |
140 | 117 | return; | |
141 | } | ||
142 | |||
143 | 413 | BUG_ON(logfd < 0); | |
144 | 413 | char buf[2048]; | |
145 | 413 | const int saved_errno = errno; | |
146 | 413 | const size_t buf_size = sizeof(buf) - 1; | |
147 | 413 | const char *prefix = log_level_map[level].prefix; | |
148 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 413 times.
|
413 | const char *color = sgr0[0] ? log_level_map[level].color : ""; |
149 |
1/2✓ Branch 0 (9→10) taken 413 times.
✗ Branch 1 (9→11) not taken.
|
413 | const char *reset = color[0] ? sgr0 : ""; |
150 | |||
151 | 413 | int len1 = snprintf ( | |
152 | buf, buf_size, | ||
153 | "%s%s%s: %s%s:%d:%s ", | ||
154 | color, prefix, reset, | ||
155 | dim, file, line, sgr0 | ||
156 | ); | ||
157 | |||
158 |
1/2✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→13) taken 413 times.
|
413 | if (unlikely(len1 < 0 || len1 >= buf_size)) { |
159 | ✗ | len1 = 0; | |
160 | } | ||
161 | |||
162 | 413 | int len2 = vsnprintf(buf + len1, buf_size - len1, fmt, ap); | |
163 | 413 | size_t n = MIN(len1 + MAX(len2, 0), buf_size); | |
164 | 413 | buf[n++] = '\n'; | |
165 | 413 | (void)!xwrite_all(logfd, buf, n); | |
166 | 413 | errno = saved_errno; | |
167 | } | ||
168 | |||
169 | 396 | void log_msg(LogLevel level, const char *file, int line, const char *fmt, ...) | |
170 | { | ||
171 | 396 | va_list ap; | |
172 | 396 | va_start(ap, fmt); | |
173 | 396 | log_msgv(level, file, line, fmt, ap); | |
174 | 396 | va_end(ap); | |
175 | 396 | } | |
176 | |||
177 | ✗ | SystemErrno log_errno(const char *file, int line, const char *prefix) | |
178 | { | ||
179 | ✗ | SystemErrno err = errno; | |
180 | ✗ | if (log_level_enabled(LOG_LEVEL_ERROR)) { | |
181 | ✗ | log_msg(LOG_LEVEL_ERROR, file, line, "%s: %s", prefix, strerror(err)); | |
182 | } | ||
183 | ✗ | return err; | |
184 | } | ||
185 |