./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
140 / 161
87.0%
Branches:
75 / 100
75.0%
Functions:
22 / 25
88.0%

kfsw-services/src/log.c
Line Branch Exec Source
1 #include <stdarg.h>
2 #include <errno.h>
3 #include <stddef.h>
4 #if CONFIG_KFSW_LOG_SEMIHOST
5 #include <string.h>
6 #include <kfsw/platform/semihost.h>
7 #endif
8
9 #include <zephyr/sys/atomic.h>
10 #include <zephyr/sys/printk.h>
11 #include <zephyr/sys/util.h>
12 #if CONFIG_KFSW_LOG_SHELL
13 #include <zephyr/shell/shell.h>
14 #endif
15
16 #include <kfsw/services/log.h>
17 #if CONFIG_KFSW_LOG_HISTORY
18 #include "log_history_internal.h"
19 #endif
20 #if CONFIG_KFSW_LOG_REMOTE
21 #include <kfsw/services/log_remote.h>
22 #endif
23 #if CONFIG_KFSW_PARAM
24 #include <kfsw/services/parameter.h>
25 #endif
26 #if CONFIG_KFSW_CSP
27 #include <csp/csp_debug.h>
28 #endif
29
30 #define KFSW_LOG_MESSAGE_SIZE 192U
31
32 static atomic_t kfsw_log_level = ATOMIC_INIT(CONFIG_KFSW_LOG_MIN_LEVEL);
33
34 /*
35 * One minimum level per module. A message has to pass both the global level
36 * and its module's level.
37 */
38 static uint8_t kfsw_log_module_levels[KFSW_LOG_MODULE_COUNT];
39
40 static const char *const kfsw_log_module_names[KFSW_LOG_MODULE_COUNT] = {
41 "app", "boot", "log", "param", "storage", "csp", "uart", "ftp",
42 "fwu", "health", "command", "event", "radio", "hk", "fbo", "can",
43 "table", "remexec", "resmon", "gndwdt", "scrub", "limits",
44 };
45
46 ✗ const char *kfsw_log_module_name(enum kfsw_log_module module)
47 {
48
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if ((unsigned int)module >= KFSW_LOG_MODULE_COUNT) {
49 ✗ return "unknown";
50 }
51 ✗ return kfsw_log_module_names[module];
52 }
53
54 4988 int kfsw_log_set_module_level(enum kfsw_log_module module, uint8_t level)
55 {
56
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4988 times.
4988 if ((unsigned int)module >= KFSW_LOG_MODULE_COUNT) {
57 ✗ return -EINVAL;
58 }
59
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4988 times.
4988 if (level > 4U) {
60 ✗ return -ERANGE;
61 }
62 4988 kfsw_log_module_levels[module] = level;
63 4988 return 0;
64 }
65
66 ✗ uint8_t kfsw_log_get_module_level(enum kfsw_log_module module)
67 {
68
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if ((unsigned int)module >= KFSW_LOG_MODULE_COUNT) {
69 ✗ return 4U;
70 }
71 ✗ return kfsw_log_module_levels[module];
72 }
73
74 /* Used by the write path, so not behind the parameter guard. */
75 static uint8_t kfsw_log_color_value = IS_ENABLED(CONFIG_KFSW_LOG_COLOR);
76 #if CONFIG_KFSW_LOG_SHELL
77 static uint8_t kfsw_log_shell_value = 1U;
78
79 16 static void mirror_to_shell(const char *level, const char *message)
80 {
81 /* Shell output is a thread API; the console still covers early boot/ISRs. */
82
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 12 times.
16 if (kfsw_log_shell_value == 0U || k_is_in_isr()) {
83 4 return;
84 }
85
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✓ Branch 7 taken 12 times.
✓ Branch 8 taken 12 times.
24 STRUCT_SECTION_FOREACH(shell, sh)
86 {
87
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 10 times.
12 if (!shell_ready(sh)) {
88 2 continue;
89 }
90
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 if (level != NULL) {
91 8 shell_print(sh, "[%s] %s", level, message);
92 } else {
93 2 shell_print(sh, "%s", message);
94 }
95 }
96 }
97 #endif
98
99 static atomic_t kfsw_log_emitted;
100 static atomic_t kfsw_log_dropped;
101
102 #if CONFIG_KFSW_LOG_REMOTE
103 /* Read by the remote log server for each read; a parameter when PARAM is built. */
104 #define KFSW_LOG_REMOTE_DEFAULT_FORMAT \
105 (IS_ENABLED(CONFIG_KFSW_LOG_REMOTE_DEFAULT_DICTIONARY) ? KFSW_LOG_REMOTE_DICTIONARY \
106 : KFSW_LOG_REMOTE_TEXT)
107 static uint8_t kfsw_log_remote_format_value = KFSW_LOG_REMOTE_DEFAULT_FORMAT;
108
109 96 uint8_t kfsw_log_remote_format(void)
110 {
111 96 return kfsw_log_remote_format_value;
112 }
113
114 144 int kfsw_log_remote_set_format(uint8_t format)
115 {
116
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 138 times.
144 if (format > KFSW_LOG_REMOTE_DICTIONARY) {
117 6 return -ERANGE;
118 }
119 138 kfsw_log_remote_format_value = format;
120 138 return 0;
121 }
122 #endif
123
124 #if CONFIG_KFSW_PARAM
125 static uint8_t kfsw_log_param_value = CONFIG_KFSW_LOG_MIN_LEVEL;
126
127 148 static int validate_log_level(const union kfsw_param_scalar *value)
128 {
129
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 4 times.
148 return (value->u8 <= 4U) ? 0 : -ERANGE;
130 }
131
132 118 static void apply_log_level(const union kfsw_param_scalar *value)
133 {
134
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 118 times.
118 if (kfsw_log_set_level(value->u8) != 0) {
135 ✗ kfsw_log_param_value = CONFIG_KFSW_LOG_MIN_LEVEL;
136 ✗ (void)kfsw_log_set_level(kfsw_log_param_value);
137 }
138 118 }
139
140 /* Sample the level so the table shows the level in use. */
141 92 static void sample_log_level(void *value)
142 {
143 92 *(uint8_t *)value = kfsw_log_get_level();
144 92 }
145
146 4 static void sample_emitted(void *value)
147 {
148 4 *(uint32_t *)value = (uint32_t)atomic_get(&kfsw_log_emitted);
149 4 }
150
151 ✗ static void sample_dropped(void *value)
152 {
153 ✗ *(uint32_t *)value = (uint32_t)atomic_get(&kfsw_log_dropped);
154 ✗ }
155
156 static uint32_t kfsw_log_emitted_value;
157 static uint32_t kfsw_log_dropped_value;
158
159 32 static int validate_log_bool(const union kfsw_param_scalar *value)
160 {
161
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 30 times.
32 return (value->u8 > 1U) ? -ERANGE : 0;
162 }
163
164 #if CONFIG_KFSW_LOG_REMOTE
165 18 static int validate_remote_format(const union kfsw_param_scalar *value)
166 {
167
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 12 times.
18 return (value->u8 > KFSW_LOG_REMOTE_DICTIONARY) ? -ERANGE : 0;
168 }
169 #endif
170
171 static uint8_t kfsw_log_levels_value[KFSW_LOG_MODULE_COUNT];
172
173 64 static void sample_module_levels(void *value)
174 {
175 64 uint8_t *levels = value;
176
177
2/2
✓ Branch 0 taken 1408 times.
✓ Branch 1 taken 64 times.
1472 for (unsigned int index = 0U; index < KFSW_LOG_MODULE_COUNT; index++) {
178 1408 levels[index] = kfsw_log_module_levels[index];
179 }
180 64 }
181
182 122 static int validate_module_levels(const uint8_t *data, size_t size)
183 {
184 /* Validate the whole array at once. */
185
2/2
✓ Branch 0 taken 2684 times.
✓ Branch 1 taken 122 times.
2806 for (size_t index = 0U; index < size; index++) {
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2684 times.
2684 if (data[index] > 4U) {
187 ✗ return -ERANGE;
188 }
189 }
190 122 return 0;
191 }
192
193 98 static void apply_module_levels(const uint8_t *data, size_t size)
194 {
195
2/2
✓ Branch 0 taken 2156 times.
✓ Branch 1 taken 98 times.
2254 for (size_t index = 0U; index < size; index++) {
196 2156 (void)kfsw_log_set_module_level((enum kfsw_log_module)index, data[index]);
197 }
198 98 }
199
200 static const struct kfsw_param_definition log_param_definitions[] = {
201 {
202 .offset = 0x00U,
203 .type = KFSW_PARAM_U8,
204 .flags = KFSW_PARAM_FLAG_CONFIGURATION | KFSW_PARAM_FLAG_PERSISTENT,
205 .name = "log_level",
206 .description = "Runtime logging policy value",
207 .value = &kfsw_log_param_value,
208 .default_value = {.u8 = CONFIG_KFSW_LOG_MIN_LEVEL},
209 .validate = validate_log_level,
210 .changed = apply_log_level,
211 .sample = sample_log_level,
212 },
213 {
214 .offset = 0x10U,
215 .type = KFSW_PARAM_DATA,
216 .capacity = KFSW_LOG_MODULE_COUNT,
217 /* One level per module, in enum kfsw_log_module order. Persistent. */
218 .flags = KFSW_PARAM_FLAG_CONFIGURATION | KFSW_PARAM_FLAG_PERSISTENT,
219 .name = "log_levels",
220 .description = "Minimum level per module, raising the global one for that module",
221 .value = kfsw_log_levels_value,
222 .validate_data = validate_module_levels,
223 .changed_data = apply_module_levels,
224 .sample = sample_module_levels,
225 },
226 {
227 .offset = 0x04U,
228 .type = KFSW_PARAM_U32,
229 .flags = KFSW_PARAM_FLAG_READ_ONLY,
230 .name = "log_emitted",
231 .description = "Messages written since boot",
232 .value = &kfsw_log_emitted_value,
233 .sample = sample_emitted,
234 },
235 {
236 .offset = 0x08U,
237 .type = KFSW_PARAM_U32,
238 .flags = KFSW_PARAM_FLAG_READ_ONLY,
239 .name = "log_dropped",
240 .description = "Messages below the active level, so not written",
241 .value = &kfsw_log_dropped_value,
242 .sample = sample_dropped,
243 },
244 {
245 .offset = 0x0cU,
246 .type = KFSW_PARAM_U8,
247 .flags = KFSW_PARAM_FLAG_CONFIGURATION | KFSW_PARAM_FLAG_LIVE,
248 .name = "log_color",
249 .description = "Colour log lines by severity; read every line",
250 .value = &kfsw_log_color_value,
251 .default_value = {.u8 = IS_ENABLED(CONFIG_KFSW_LOG_COLOR)},
252 .validate = validate_log_bool,
253 },
254 #if CONFIG_KFSW_LOG_SHELL
255 {
256 .offset = 0x0dU,
257 .type = KFSW_PARAM_U8,
258 .flags = KFSW_PARAM_FLAG_CONFIGURATION | KFSW_PARAM_FLAG_LIVE,
259 .name = "log_shell",
260 .description = "Mirror logs to active shells; 0 off, 1 on",
261 .value = &kfsw_log_shell_value,
262 .default_value = {.u8 = 1U},
263 .validate = validate_log_bool,
264 },
265 #endif
266 #if CONFIG_KFSW_LOG_REMOTE
267 {
268 .offset = 0x20U,
269 .type = KFSW_PARAM_U8,
270 .flags = KFSW_PARAM_FLAG_CONFIGURATION | KFSW_PARAM_FLAG_PERSISTENT |
271 KFSW_PARAM_FLAG_LIVE,
272 .name = "log_remote_format",
273 .description = "How log remote sends messages: 0 text, 1 dictionary",
274 .value = &kfsw_log_remote_format_value,
275 .default_value = {.u8 = KFSW_LOG_REMOTE_DEFAULT_FORMAT},
276 .validate = validate_remote_format,
277 },
278 #endif
279 };
280
281 KFSW_PARAM_TABLE_FITS_THE_LINK(log_param_definitions);
282
283 const struct kfsw_param_definition_set kfsw_log_param_definitions = {
284 .table = KFSW_LOG_PARAM_TABLE_ID,
285 .name = KFSW_LOG_PARAM_TABLE_NAME,
286 .description = "Log levels, colour and counters",
287 .definitions = log_param_definitions,
288 .count = ARRAY_SIZE(log_param_definitions),
289 };
290 #endif
291
292 296 int kfsw_log_set_level(uint8_t level)
293 {
294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 296 times.
296 if (level > 4U) {
295 ✗ return -ERANGE;
296 }
297
298 296 atomic_set(&kfsw_log_level, level);
299 296 return 0;
300 }
301
302 7142 uint8_t kfsw_log_get_level(void)
303 {
304 7142 return (uint8_t)atomic_get(&kfsw_log_level);
305 }
306
307 #define KFSW_LOG_COLOR_RESET "\033[0m"
308 #define KFSW_LOG_COLOR_ERROR "\033[31m"
309 #define KFSW_LOG_COLOR_WARNING "\033[33m"
310 #define KFSW_LOG_COLOR_INFO "\033[37m"
311 #define KFSW_LOG_COLOR_DEBUG "\033[90m"
312
313 7016 static const char *severity_color(uint8_t severity)
314 {
315
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5840 times.
5840 if (!IS_ENABLED(CONFIG_KFSW_LOG_COLOR) || (kfsw_log_color_value == 0U)) {
316 1176 return "";
317 }
318
3/4
✓ Branch 0 taken 332 times.
✓ Branch 1 taken 706 times.
✓ Branch 2 taken 4802 times.
✗ Branch 3 not taken.
5840 switch (severity) {
319 332 case 3U:
320 332 return KFSW_LOG_COLOR_ERROR;
321 706 case 2U:
322 706 return KFSW_LOG_COLOR_WARNING;
323 4802 case 1U:
324 4802 return KFSW_LOG_COLOR_INFO;
325 ✗ default:
326 ✗ return KFSW_LOG_COLOR_DEBUG;
327 }
328 }
329
330 #if CONFIG_KFSW_LOG_SEMIHOST
331 16 static void mirror_to_semihost(const char *color, const char *level, const char *message,
332 const char *reset)
333 {
334 char line[KFSW_LOG_MESSAGE_SIZE + sizeof("\033[90m[WARNING] \033[0m\n")];
335 BUILD_ASSERT(sizeof(line) <= KFSW_SEMIHOST_MAX_WRITE);
336 16 size_t used = 0U;
337 64 const char *parts[] = {color,
338
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
16 level != NULL ? "[" : "",
339
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
16 level != NULL ? level : "",
340
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
16 level != NULL ? "] " : "",
341 message,
342 reset,
343 "\n"};
344
345
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 16 times.
128 for (size_t i = 0U; i < ARRAY_SIZE(parts); i++) {
346 112 size_t size = strlen(parts[i]);
347
348
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
112 if (size > sizeof(line) - used) {
349 ✗ return;
350 }
351 112 memcpy(&line[used], parts[i], size);
352 112 used += size;
353 }
354 16 (void)kfsw_semihost_write(line, used);
355 }
356 #endif
357
358 #if CONFIG_KFSW_LOG_MIN_LEVEL < 4
359 7052 static void kfsw_log_vwrite(uint8_t module, uint8_t severity, const char *level, const char *format,
360 va_list args)
361 {
362 char message[KFSW_LOG_MESSAGE_SIZE];
363 va_list copy;
364 int length;
365 size_t i;
366
367 /* A message must pass the global level and its module's level. */
368
3/4
✓ Branch 0 taken 7052 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 7036 times.
7052 if ((module < KFSW_LOG_MODULE_COUNT) && (severity < kfsw_log_module_levels[module])) {
369 16 (void)atomic_inc(&kfsw_log_dropped);
370 48 return;
371 }
372
2/2
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 7004 times.
7036 if (severity < kfsw_log_get_level()) {
373 /* Count messages dropped by the level filter. */
374 32 (void)atomic_inc(&kfsw_log_dropped);
375 32 return;
376 }
377 7004 (void)atomic_inc(&kfsw_log_emitted);
378
379 7004 va_copy(copy, args);
380 7004 length = vsnprintk(message, sizeof(message), format, copy);
381 7004 va_end(copy);
382
383
2/2
✓ Branch 0 taken 238754 times.
✓ Branch 1 taken 7004 times.
245758 for (i = 0U; message[i] != '\0'; i++) {
384
4/4
✓ Branch 0 taken 238742 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 238730 times.
238754 if ((message[i] == '\n') || (message[i] == '\r')) {
385 24 message[i] = ' ';
386 }
387 }
388
389 #if CONFIG_KFSW_LOG_HISTORY
390 1376 kfsw_log_history_append(module, severity, format, args, message,
391
3/4
✓ Branch 0 taken 1376 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✓ Branch 3 taken 1352 times.
1376 (length < 0) || ((size_t)length >= sizeof(message)));
392 #else
393 ARG_UNUSED(length);
394 #endif
395
396 /* The colour wraps the whole line so "[LEVEL] message" stays intact. */
397 7004 printk("%s[%s] %s%s\n", severity_color(severity), level, message,
398
2/4
✓ Branch 0 taken 5610 times.
✓ Branch 0 taken 224 times.
✗ Branch 1 not taken.
✗ Branch 1 not taken.
5834 (IS_ENABLED(CONFIG_KFSW_LOG_COLOR) && (kfsw_log_color_value != 0U))
399 ? KFSW_LOG_COLOR_RESET
400 : "");
401 #if CONFIG_KFSW_LOG_SEMIHOST
402 12 mirror_to_semihost(severity_color(severity), level, message,
403
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 (IS_ENABLED(CONFIG_KFSW_LOG_COLOR) && (kfsw_log_color_value != 0U))
404 ? KFSW_LOG_COLOR_RESET
405 : "");
406 #endif
407 #if CONFIG_KFSW_LOG_SHELL
408 14 mirror_to_shell(level, message);
409 #endif
410 }
411 #endif
412
413 32 void kfsw_log_write_marker(uint8_t module, const char *format, ...)
414 {
415 char message[KFSW_LOG_MESSAGE_SIZE];
416 va_list args;
417 int length;
418
419 32 va_start(args, format);
420 32 length = vsnprintk(message, sizeof(message), format, args);
421 32 va_end(args);
422
423 32 (void)atomic_inc(&kfsw_log_emitted);
424 #if CONFIG_KFSW_LOG_HISTORY
425 30 va_start(args, format);
426 30 kfsw_log_history_append(module, 1U, format, args, message,
427
2/4
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 30 times.
30 (length < 0) || ((size_t)length >= sizeof(message)));
428 30 va_end(args);
429 #else
430 ARG_UNUSED(module);
431 ARG_UNUSED(length);
432 #endif
433 32 printk("%s\n", message);
434 #if CONFIG_KFSW_LOG_SEMIHOST
435 4 mirror_to_semihost("", NULL, message, "");
436 #endif
437 #if CONFIG_KFSW_LOG_SHELL
438 2 mirror_to_shell(NULL, message);
439 #endif
440 32 }
441
442 #if CONFIG_KFSW_LOG_MIN_LEVEL < 4
443 /*
444 * Called by the macros in the header.
445 */
446 7052 void kfsw_log_write(uint8_t module, uint8_t severity, const char *format, ...)
447 {
448 static const char *const names[] = {"DEBUG", "INFO", "WARNING", "ERROR"};
449 va_list args;
450
451
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7052 times.
7052 if (severity > 3U) {
452 ✗ return;
453 }
454 7052 va_start(args, format);
455 7052 kfsw_log_vwrite(module, severity, names[severity], format, args);
456 7052 va_end(args);
457 }
458 #endif
459
460 #if CONFIG_KFSW_CSP && (CONFIG_KFSW_LOG_MIN_LEVEL <= 1)
461 /* libcsp ends each line itself and colours it; the log does both its own way. */
462 24 static void strip_line_formatting(char *text)
463 {
464 24 size_t read = 0U;
465 24 size_t write = 0U;
466
467
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 24 times.
240 while (text[read] != '\0') {
468
3/4
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 192 times.
216 if ((text[read] == '\n') || (text[read] == '\r')) {
469 24 read++;
470 24 continue;
471 }
472
2/2
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 24 times.
192 if (text[read] != '\033') {
473 168 text[write++] = text[read++];
474 168 continue;
475 }
476
3/4
✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
✓ Branch 3 taken 24 times.
104 while ((text[read] != '\0') && (text[read] != 'm')) {
477 80 read++;
478 }
479
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (text[read] == 'm') {
480 24 read++;
481 }
482 }
483 24 text[write] = '\0';
484 24 }
485
486 /* libcsp prints its packet trace (csp debug on) through this hook. */
487 24 void csp_print_func(const char *fmt, ...)
488 {
489 char line[KFSW_LOG_MESSAGE_SIZE];
490 va_list args;
491
492 24 va_start(args, fmt);
493 24 (void)vsnprintk(line, sizeof(line), fmt, args);
494 24 va_end(args);
495
496 24 strip_line_formatting(line);
497
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
24 if (line[0] != '\0') {
498 16 kfsw_log_write(KFSW_LOG_MODULE_CSP, 1U, "%s", line);
499 }
500 24 }
501 #endif
502