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 | 18 | UNITTEST { | |
35 | 18 | CHECK_STRUCT_ARRAY(log_level_map, name); | |
36 |
2/2✓ Branch 0 (9→4) taken 144 times.
✓ Branch 1 (9→10) taken 18 times.
|
162 | for (size_t i = 0; i < ARRAYLEN(log_level_map); i++) { |
37 | 144 | const struct llmap *m = &log_level_map[i]; | |
38 | 144 | BUG_ON(m->prefix[sizeof(m->prefix) - 1] != '\0'); | |
39 | 144 | BUG_ON(m->color[sizeof(m->color) - 1] != '\0'); | |
40 | } | ||
41 | 18 | } | |
42 | |||
43 | 3 | LogLevel log_level_default(void) | |
44 | { | ||
45 | 3 | return (DEBUG >= 2) ? LOG_LEVEL_DEBUG : LOG_LEVEL_INFO; | |
46 | } | ||
47 | |||
48 | 441 | static LogLevel log_level_max(void) | |
49 | { | ||
50 | 441 | return (DEBUG >= 3) ? 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 flags = O_WRONLY | O_CREAT | O_APPEND | O_TRUNC | O_CLOEXEC; | |
86 | 3 | int fd = xopen(filename, flags, 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 | int err = ctty ? EINVAL : errno; |
94 | 1 | xclose(fd); | |
95 | 1 | errno = err; | |
96 | 1 | return LOG_LEVEL_NONE; | |
97 | } | ||
98 | |||
99 |
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)) { |
100 | 1 | dim[0] = '\0'; | |
101 | 1 | sgr0[0] = '\0'; | |
102 | } | ||
103 | |||
104 | 1 | logfd = fd; | |
105 |
1/2✗ Branch 0 (27→28) not taken.
✓ Branch 1 (27→29) taken 1 times.
|
1 | log_level = MIN(level, log_level_max()); |
106 | 1 | return log_level; | |
107 | } | ||
108 | |||
109 | 6 | bool log_close(void) | |
110 | { | ||
111 |
3/4✓ Branch 0 (2→3) taken 1 times.
✓ Branch 1 (2→6) taken 5 times.
✓ Branch 2 (4→5) taken 1 times.
✗ Branch 3 (4→6) not taken.
|
6 | return log_level == LOG_LEVEL_NONE || xclose(logfd) == 0; |
112 | } | ||
113 | |||
114 | 440 | bool log_level_enabled(LogLevel level) | |
115 | { | ||
116 | 440 | BUG_ON(level <= LOG_LEVEL_NONE || level > LOG_LEVEL_TRACE); | |
117 | 440 | BUG_ON(log_level < LOG_LEVEL_NONE || log_level > log_level_max()); | |
118 | 440 | return level <= log_level; | |
119 | } | ||
120 | |||
121 | // This function should be kept async signal safe | ||
122 | 2 | void log_write(LogLevel level, const char *str, size_t len) | |
123 | { | ||
124 | // log_level is a non-const global, but as noted above, it's never | ||
125 | // modified after early startup and so poses no signal safety issues | ||
126 | // (see https://austingroupbugs.net/view.php?id=728#c6430) | ||
127 |
2/2✓ Branch 0 (3→4) taken 1 times.
✓ Branch 1 (3→8) taken 1 times.
|
2 | if (!log_level_enabled(level)) { |
128 | return; | ||
129 | } | ||
130 | |||
131 | // xwrite_all() only calls write(3), which is async signal safe | ||
132 | // (see signal-safety(7)) | ||
133 | 1 | BUG_ON(logfd < 0); | |
134 | 1 | (void)!xwrite_all(logfd, str, len); | |
135 | 1 | (void)!xwrite_all(logfd, "\n", 1); | |
136 | } | ||
137 | |||
138 | 433 | void log_msgv(LogLevel level, const char *file, int line, const char *fmt, va_list ap) | |
139 | { | ||
140 |
2/2✓ Branch 0 (3→4) taken 429 times.
✓ Branch 1 (3→5) taken 4 times.
|
433 | if (!log_level_enabled(level)) { |
141 | 429 | return; | |
142 | } | ||
143 | |||
144 | 4 | BUG_ON(logfd < 0); | |
145 | 4 | char buf[2048]; | |
146 | 4 | const size_t buf_size = sizeof(buf) - 1; | |
147 | 4 | const char *prefix = log_level_map[level].prefix; | |
148 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 4 times.
|
4 | const char *color = sgr0[0] ? log_level_map[level].color : ""; |
149 |
1/2✓ Branch 0 (9→10) taken 4 times.
✗ Branch 1 (9→11) not taken.
|
4 | const char *reset = color[0] ? sgr0 : ""; |
150 | |||
151 | 4 | 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 4 times.
|
4 | if (unlikely(len1 < 0 || len1 >= buf_size)) { |
159 | ✗ | len1 = 0; | |
160 | } | ||
161 | |||
162 | 4 | int len2 = vsnprintf(buf + len1, buf_size - len1, fmt, ap); | |
163 | 4 | size_t n = MIN(len1 + MAX(len2, 0), buf_size); | |
164 | 4 | buf[n++] = '\n'; | |
165 | 4 | (void)!xwrite_all(logfd, buf, n); | |
166 | } | ||
167 | |||
168 | 344 | void log_msg(LogLevel level, const char *file, int line, const char *fmt, ...) | |
169 | { | ||
170 | 344 | va_list ap; | |
171 | 344 | va_start(ap, fmt); | |
172 | 344 | log_msgv(level, file, line, fmt, ap); | |
173 | 344 | va_end(ap); | |
174 | 344 | } | |
175 |