Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2018 Nordic Semiconductor ASA
3 : : *
4 : : * SPDX-License-Identifier: Apache-2.0
5 : : */
6 : :
7 : : #undef _POSIX_C_SOURCE
8 : : #define _POSIX_C_SOURCE 200809L
9 : : #include <zephyr/logging/log_output.h>
10 : : #include <zephyr/logging/log_ctrl.h>
11 : : #include <zephyr/logging/log_output_custom.h>
12 : : #include <zephyr/logging/log.h>
13 : : #include <zephyr/sys/__assert.h>
14 : : #include <zephyr/sys/cbprintf.h>
15 : : #include <ctype.h>
16 : : #include <time.h>
17 : : #include <stdio.h>
18 : : #include <stdbool.h>
19 : :
20 : : #define LOG_COLOR_CODE_DEFAULT "\x1B[0m"
21 : : #define LOG_COLOR_CODE_RED "\x1B[1;31m"
22 : : #define LOG_COLOR_CODE_GREEN "\x1B[1;32m"
23 : : #define LOG_COLOR_CODE_YELLOW "\x1B[1;33m"
24 : : #define LOG_COLOR_CODE_BLUE "\x1B[1;34m"
25 : :
26 : : #define HEXDUMP_BYTES_IN_LINE 16
27 : :
28 : : #define DROPPED_COLOR_PREFIX \
29 : : COND_CODE_1(CONFIG_LOG_BACKEND_SHOW_COLOR, (LOG_COLOR_CODE_RED), ())
30 : :
31 : : #define DROPPED_COLOR_POSTFIX \
32 : : COND_CODE_1(CONFIG_LOG_BACKEND_SHOW_COLOR, (LOG_COLOR_CODE_DEFAULT), ())
33 : :
34 : : static const char *const severity[] = {
35 : : NULL,
36 : : "err",
37 : : "wrn",
38 : : "inf",
39 : : "dbg"
40 : : };
41 : :
42 : : static const char *const colors[] = {
43 : : NULL,
44 : : IS_ENABLED(CONFIG_LOG_BACKEND_SHOW_COLOR) ? LOG_COLOR_CODE_RED : NULL, /* err */
45 : : IS_ENABLED(CONFIG_LOG_BACKEND_SHOW_COLOR) ? LOG_COLOR_CODE_YELLOW : NULL, /* warn */
46 : : IS_ENABLED(CONFIG_LOG_INFO_COLOR_GREEN) ? LOG_COLOR_CODE_GREEN : NULL, /* info */
47 : : IS_ENABLED(CONFIG_LOG_DBG_COLOR_BLUE) ? LOG_COLOR_CODE_BLUE : NULL, /* dbg */
48 : : };
49 : :
50 : : static const bool func_prefix_used = IS_ENABLED(CONFIG_LOG_FUNC_NAME_PREFIX_ERR) ||
51 : : IS_ENABLED(CONFIG_LOG_FUNC_NAME_PREFIX_WRN) ||
52 : : IS_ENABLED(CONFIG_LOG_FUNC_NAME_PREFIX_INF) ||
53 : : IS_ENABLED(CONFIG_LOG_FUNC_NAME_PREFIX_DBG);
54 : : static const bool func_on_lut[] = {
55 : : false,
56 : : IS_ENABLED(CONFIG_LOG_FUNC_NAME_PREFIX_ERR),
57 : : IS_ENABLED(CONFIG_LOG_FUNC_NAME_PREFIX_WRN),
58 : : IS_ENABLED(CONFIG_LOG_FUNC_NAME_PREFIX_INF),
59 : : IS_ENABLED(CONFIG_LOG_FUNC_NAME_PREFIX_DBG),
60 : : };
61 : :
62 : : static uint32_t freq;
63 : : static log_timestamp_t timestamp_div;
64 : :
65 : : /* The RFC 5424 allows very flexible mapping and suggest the value 0 being the
66 : : * highest severity and 7 to be the lowest (debugging level) severity.
67 : : *
68 : : * 0 Emergency System is unusable
69 : : * 1 Alert Action must be taken immediately
70 : : * 2 Critical Critical conditions
71 : : * 3 Error Error conditions
72 : : * 4 Warning Warning conditions
73 : : * 5 Notice Normal but significant condition
74 : : * 6 Informational Informational messages
75 : : * 7 Debug Debug-level messages
76 : : */
77 : : static int level_to_rfc5424_severity(uint32_t level)
78 : : {
79 : : uint8_t ret;
80 : :
81 : : switch (level) {
82 : : case LOG_LEVEL_NONE:
83 : : ret = 7U;
84 : : break;
85 : : case LOG_LEVEL_ERR:
86 : : ret = 3U;
87 : : break;
88 : : case LOG_LEVEL_WRN:
89 : : ret = 4U;
90 : : break;
91 : : case LOG_LEVEL_INF:
92 : : ret = 6U;
93 : : break;
94 : : case LOG_LEVEL_DBG:
95 : : ret = 7U;
96 : : break;
97 : : default:
98 : : ret = 7U;
99 : : break;
100 : : }
101 : :
102 : : return ret;
103 : : }
104 : :
105 : 10793 : static int out_func(int c, void *ctx)
106 : : {
107 : 10793 : const struct log_output *out_ctx = (const struct log_output *)ctx;
108 : 10793 : int idx;
109 : :
110 : 10793 : if (IS_ENABLED(CONFIG_LOG_MODE_IMMEDIATE)) {
111 : : /* Backend must be thread safe in synchronous operation. */
112 : : /* Need that step for big endian */
113 : 10793 : char x = (char)c;
114 : :
115 : 10793 : out_ctx->func((uint8_t *)&x, 1, out_ctx->control_block->ctx);
116 : 10793 : return 0;
117 : : }
118 : :
119 : : if (out_ctx->control_block->offset == out_ctx->size) {
120 : : log_output_flush(out_ctx);
121 : : }
122 : :
123 : : idx = atomic_inc(&out_ctx->control_block->offset);
124 : : out_ctx->buf[idx] = (uint8_t)c;
125 : :
126 : : __ASSERT_NO_MSG(out_ctx->control_block->offset <= out_ctx->size);
127 : :
128 : : return 0;
129 : : }
130 : :
131 : 10581 : static int cr_out_func(int c, void *ctx)
132 : : {
133 [ + + ]: 10581 : if (c == '\n') {
134 : 212 : out_func((int)'\r', ctx);
135 : : }
136 : 10581 : out_func(c, ctx);
137 : :
138 : 10581 : return 0;
139 : : }
140 : :
141 : 0 : static __printf_like(2, 3) int print_formatted(const struct log_output *output,
142 : : const char *fmt, ...)
143 : : {
144 : 0 : va_list args;
145 : 0 : int length = 0;
146 : :
147 : 0 : va_start(args, fmt);
148 : 0 : length = cbvprintf(out_func, (void *)output, fmt, args);
149 : 0 : va_end(args);
150 : :
151 : 0 : return length;
152 : : }
153 : :
154 : 0 : static int timestamp_print(const struct log_output *output,
155 : : uint32_t flags, log_timestamp_t timestamp)
156 : : {
157 : 0 : bool format =
158 : : (flags & LOG_OUTPUT_FLAG_FORMAT_TIMESTAMP) |
159 : : (flags & LOG_OUTPUT_FLAG_FORMAT_SYSLOG) |
160 : 0 : IS_ENABLED(CONFIG_LOG_OUTPUT_FORMAT_LINUX_TIMESTAMP) |
161 : : IS_ENABLED(CONFIG_LOG_OUTPUT_FORMAT_CUSTOM_TIMESTAMP);
162 : : #ifdef CONFIG_LOG_TIMESTAMP_64BIT
163 : : uint64_t total_seconds;
164 : : #else
165 : 0 : uint32_t total_seconds;
166 : : #endif
167 : 0 : uint32_t remainder;
168 : 0 : uint32_t ms;
169 : 0 : uint32_t us;
170 : : #ifdef CONFIG_LOG_TIMESTAMP_64BIT
171 : : uint64_t hours;
172 : : #else
173 : 0 : uint32_t hours;
174 : : #endif
175 : 0 : uint32_t mins;
176 : 0 : uint32_t seconds;
177 : :
178 [ # # ]: 0 : if (!format) {
179 : : #ifndef CONFIG_LOG_TIMESTAMP_64BIT
180 : 0 : return print_formatted(output, "[%010u] ", timestamp);
181 : : #else
182 : : return print_formatted(output, "[%020llu] ", timestamp);
183 : : #endif
184 : : }
185 : :
186 [ # # ]: 0 : if (freq == 0U) {
187 : : return 0;
188 : : }
189 : :
190 : 0 : timestamp /= timestamp_div;
191 : 0 : total_seconds = timestamp / freq;
192 : :
193 : 0 : remainder = timestamp % freq;
194 : 0 : ms = (remainder * 1000U) / freq;
195 : 0 : us = (1000 * (remainder * 1000U - (ms * freq))) / freq;
196 : :
197 : : #if defined(CONFIG_POSIX_C_LANG_SUPPORT_R)
198 : 0 : if (IS_ENABLED(CONFIG_LOG_BACKEND_NET) && flags & LOG_OUTPUT_FLAG_FORMAT_SYSLOG) {
199 : : struct tm tm_timestamp = {0};
200 : : time_t time_seconds = total_seconds;
201 : :
202 : : gmtime_r(&time_seconds, &tm_timestamp);
203 : : #if defined(CONFIG_REQUIRES_FULL_LIBC)
204 : : char time_str[sizeof("1970-01-01T00:00:00")];
205 : :
206 : : strftime(time_str, sizeof(time_str), "%FT%T", &tm_timestamp);
207 : :
208 : : return print_formatted(output, "%s.%06uZ ", time_str, ms * 1000U + us);
209 : : #else /* CONFIG_REQUIRES_FULL_LIBC */
210 : : return print_formatted(output, "%04u-%02u-%02uT%02u:%02u:%02u.%06uZ ",
211 : : tm_timestamp.tm_year + 1900, tm_timestamp.tm_mon + 1,
212 : : tm_timestamp.tm_mday, tm_timestamp.tm_hour,
213 : : tm_timestamp.tm_min, tm_timestamp.tm_sec, ms * 1000U + us);
214 : : #endif /* CONFIG_REQUIRES_FULL_LIBC */
215 : : }
216 : : #endif /* CONFIG_POSIX_C_LANG_SUPPORT_R */
217 : :
218 : 0 : if (IS_ENABLED(CONFIG_LOG_OUTPUT_FORMAT_CUSTOM_TIMESTAMP)) {
219 : : return log_custom_timestamp_print(output, timestamp, print_formatted);
220 : : }
221 : :
222 : 0 : if (IS_ENABLED(CONFIG_LOG_OUTPUT_FORMAT_LINUX_TIMESTAMP)) {
223 : : return print_formatted(output,
224 : : #if defined(CONFIG_LOG_TIMESTAMP_64BIT)
225 : : "[%5llu.%06d] ",
226 : : #else
227 : : "[%5u.%06d] ",
228 : : #endif
229 : : total_seconds, ms * 1000U + us);
230 : : }
231 : :
232 : : #if defined(CONFIG_POSIX_C_LANG_SUPPORT_R)
233 : 0 : if (IS_ENABLED(CONFIG_LOG_OUTPUT_FORMAT_DATE_TIMESTAMP)) {
234 : : struct tm tm_timestamp = {0};
235 : : time_t time_seconds = total_seconds;
236 : :
237 : : gmtime_r(&time_seconds, &tm_timestamp);
238 : : #if defined(CONFIG_REQUIRES_FULL_LIBC)
239 : : char time_str[sizeof("1970-01-01 00:00:00")];
240 : :
241 : : strftime(time_str, sizeof(time_str), "%F %T", &tm_timestamp);
242 : :
243 : : return print_formatted(output, "[%s.%03u,%03u] ", time_str, ms, us);
244 : : #else /* CONFIG_REQUIRES_FULL_LIBC */
245 : : return print_formatted(output, "[%04u-%02u-%02u %02u:%02u:%02u.%03u,%03u] ",
246 : : tm_timestamp.tm_year + 1900, tm_timestamp.tm_mon + 1,
247 : : tm_timestamp.tm_mday, tm_timestamp.tm_hour,
248 : : tm_timestamp.tm_min, tm_timestamp.tm_sec, ms, us);
249 : : #endif /* CONFIG_REQUIRES_FULL_LIBC */
250 : : }
251 : :
252 : 0 : if (IS_ENABLED(CONFIG_LOG_OUTPUT_FORMAT_ISO8601_TIMESTAMP)) {
253 : : struct tm tm_timestamp = {0};
254 : : time_t time_seconds = total_seconds;
255 : :
256 : : gmtime_r(&time_seconds, &tm_timestamp);
257 : : #if defined(CONFIG_REQUIRES_FULL_LIBC)
258 : : char time_str[sizeof("1970-01-01T00:00:00")];
259 : :
260 : : strftime(time_str, sizeof(time_str), "%FT%T", &tm_timestamp);
261 : :
262 : : return print_formatted(output, "[%s,%06uZ] ", time_str, ms * 1000U + us);
263 : : #else /* CONFIG_REQUIRES_FULL_LIBC */
264 : : return print_formatted(output, "[%04u-%02u-%02uT%02u:%02u:%02u,%06uZ] ",
265 : : tm_timestamp.tm_year + 1900, tm_timestamp.tm_mon + 1,
266 : : tm_timestamp.tm_mday, tm_timestamp.tm_hour,
267 : : tm_timestamp.tm_min, tm_timestamp.tm_sec, ms * 1000U + us);
268 : : #endif /* CONFIG_REQUIRES_FULL_LIBC */
269 : : }
270 : : #endif /* CONFIG_POSIX_C_LANG_SUPPORT_R */
271 : :
272 : 0 : hours = total_seconds / 3600U;
273 : 0 : seconds = (uint32_t)(total_seconds - hours * 3600U);
274 : 0 : mins = seconds / 60U;
275 : 0 : seconds -= mins * 60U;
276 : :
277 : 0 : return print_formatted(output,
278 : : #ifndef CONFIG_LOG_TIMESTAMP_64BIT
279 : : "[%02u:%02u:%02u.%03u,%03u] ",
280 : : #else
281 : : "[%02llu:%02u:%02u.%03u,%03u] ",
282 : : #endif
283 : : hours, mins, seconds, ms, us);
284 : : }
285 : :
286 : 0 : static void color_print(const struct log_output *output,
287 : : bool color, bool start, uint32_t level)
288 : : {
289 [ # # ]: 0 : if (color) {
290 [ # # ]: 0 : const char *log_color = start && (colors[level] != NULL) ?
291 [ # # ]: 0 : colors[level] : LOG_COLOR_CODE_DEFAULT;
292 : 0 : print_formatted(output, "%s", log_color);
293 : : }
294 : 0 : }
295 : :
296 : 0 : static void color_prefix(const struct log_output *output,
297 : : bool color, uint32_t level)
298 : : {
299 : 0 : color_print(output, color, true, level);
300 : 0 : }
301 : :
302 : 0 : static void color_postfix(const struct log_output *output,
303 : : bool color, uint32_t level)
304 : : {
305 : 0 : color_print(output, color, false, level);
306 : 0 : }
307 : :
308 : :
309 : 0 : static int ids_print(const struct log_output *output,
310 : : bool level_on,
311 : : bool func_on,
312 : : bool thread_on,
313 : : bool core_on,
314 : : const char *domain,
315 : : const char *source,
316 : : k_tid_t tid,
317 : : uint8_t core_id,
318 : : uint32_t level)
319 : : {
320 : 0 : int total = 0;
321 : :
322 [ # # ]: 0 : if (level_on) {
323 : 0 : total += print_formatted(output, "<%s> ", severity[level]);
324 : : }
325 : :
326 : 0 : if (IS_ENABLED(CONFIG_LOG_THREAD_ID_PREFIX) && thread_on) {
327 : : if (tid == NULL) {
328 : : total += print_formatted(output, "[irq] ");
329 : : } else if (IS_ENABLED(CONFIG_THREAD_NAME)) {
330 : : total += print_formatted(output,
331 : : "[%3d %s] ",
332 : : k_thread_priority_get(tid),
333 : : k_thread_name_get(tid));
334 : : } else {
335 : : total += print_formatted(output,
336 : : "[%3d %p] ",
337 : : k_thread_priority_get(tid),
338 : : tid);
339 : : }
340 : : }
341 : :
342 : 0 : if (IS_ENABLED(CONFIG_LOG_CORE_ID_PREFIX) && core_on) {
343 : : total += print_formatted(output, "[core %d] ", core_id);
344 : : }
345 : :
346 [ # # ]: 0 : if (domain) {
347 : 0 : total += print_formatted(output, "%s/", domain);
348 : : }
349 : :
350 [ # # ]: 0 : if (source) {
351 : 0 : total += print_formatted(output,
352 [ # # ]: 0 : (func_on &&
353 : : ((1 << level) & LOG_FUNCTION_PREFIX_MASK)) ?
354 : : "%s." : "%s: ",
355 : : source);
356 : : }
357 : :
358 : 0 : return total;
359 : : }
360 : :
361 : 0 : static void newline_print(const struct log_output *ctx, uint32_t flags)
362 : : {
363 : 0 : if (IS_ENABLED(CONFIG_LOG_BACKEND_NET) &&
364 : : flags & LOG_OUTPUT_FLAG_FORMAT_SYSLOG) {
365 : : return;
366 : : }
367 : :
368 [ # # ]: 0 : if ((flags & LOG_OUTPUT_FLAG_CRLF_NONE) != 0U) {
369 : : return;
370 : : }
371 : :
372 [ # # ]: 0 : if ((flags & LOG_OUTPUT_FLAG_CRLF_LFONLY) != 0U) {
373 : 0 : print_formatted(ctx, "\n");
374 : : } else {
375 : 0 : print_formatted(ctx, "\r\n");
376 : : }
377 : : }
378 : :
379 : 0 : static void hexdump_line_print(const struct log_output *output,
380 : : const uint8_t *data, uint32_t length,
381 : : int prefix_offset, uint32_t flags)
382 : : {
383 : 0 : newline_print(output, flags);
384 : :
385 [ # # ]: 0 : for (int i = 0; i < prefix_offset; i++) {
386 : 0 : print_formatted(output, " ");
387 : : }
388 : :
389 [ # # ]: 0 : for (int i = 0; i < HEXDUMP_BYTES_IN_LINE; i++) {
390 [ # # # # ]: 0 : if (i > 0 && !(i % 8)) {
391 : 0 : print_formatted(output, " ");
392 : : }
393 : :
394 [ # # ]: 0 : if (i < length) {
395 : 0 : print_formatted(output, "%02x ", data[i]);
396 : : } else {
397 : 0 : print_formatted(output, " ");
398 : : }
399 : : }
400 : :
401 : 0 : print_formatted(output, "|");
402 : :
403 [ # # ]: 0 : for (int i = 0; i < HEXDUMP_BYTES_IN_LINE; i++) {
404 [ # # # # ]: 0 : if (i > 0 && !(i % 8)) {
405 : 0 : print_formatted(output, " ");
406 : : }
407 : :
408 [ # # ]: 0 : if (i < length) {
409 : 0 : unsigned char c = (unsigned char)data[i];
410 : :
411 : 0 : print_formatted(output, "%c",
412 [ # # ]: 0 : isprint((int)c) != 0 ? c : '.');
413 : : } else {
414 : 0 : print_formatted(output, " ");
415 : : }
416 : : }
417 : 0 : }
418 : :
419 : 0 : static void log_msg_hexdump(const struct log_output *output,
420 : : uint8_t *data, uint32_t len,
421 : : int prefix_offset, uint32_t flags)
422 : : {
423 : 0 : size_t length;
424 : :
425 : 0 : do {
426 : 0 : length = MIN(len, HEXDUMP_BYTES_IN_LINE);
427 : :
428 : 0 : hexdump_line_print(output, data, length,
429 : : prefix_offset, flags);
430 : 0 : data += length;
431 : 0 : len -= length;
432 [ # # ]: 0 : } while (len);
433 : 0 : }
434 : :
435 : : #if defined(CONFIG_LOG_BACKEND_NET_RFC5424_SDATA_SEQID)
436 : : static int32_t get_sequence_id(void)
437 : : {
438 : : static int32_t id;
439 : :
440 : : if (++id < 0) {
441 : : id = 1;
442 : : }
443 : :
444 : : return id;
445 : : }
446 : : #endif
447 : :
448 : : #if defined(CONFIG_LOG_BACKEND_NET_RFC5424_SDATA_TZKNOWN)
449 : : static bool is_tzknown(void)
450 : : {
451 : : /* TODO: use proper implementation */
452 : : return false;
453 : : }
454 : : #endif
455 : :
456 : : #if defined(CONFIG_LOG_BACKEND_NET_RFC5424_SDATA_ISSYNCED)
457 : : static bool is_synced(void)
458 : : {
459 : : /* TODO: use proper implementation */
460 : : return IS_ENABLED(CONFIG_SNTP);
461 : : }
462 : : #endif
463 : :
464 : : static int syslog_print(const struct log_output *output,
465 : : bool level_on,
466 : : bool func_on,
467 : : bool *thread_on,
468 : : const char *domain,
469 : : const char *source,
470 : : k_tid_t tid,
471 : : uint32_t level,
472 : : uint32_t length)
473 : : {
474 : : uint32_t len = length;
475 : :
476 : : /* The syslog output format is:
477 : : * HOSTNAME SP APP-NAME SP PROCID SP MSGID SP STRUCTURED-DATA
478 : : */
479 : :
480 : : /* First HOSTNAME */
481 : : len += print_formatted(output, "%s ",
482 : : output->control_block->hostname ?
483 : : output->control_block->hostname :
484 : : "zephyr");
485 : :
486 : : /* Then APP-NAME. We use the thread name here. It should not
487 : : * contain any space characters.
488 : : */
489 : : if (*thread_on) {
490 : : if (IS_ENABLED(CONFIG_THREAD_NAME)) {
491 : : const char *tname = (tid == NULL) ? "irq" : k_thread_name_get(tid);
492 : :
493 : : if (strstr(tname, " ") != NULL) {
494 : : goto do_not_print_name;
495 : : }
496 : :
497 : : len += print_formatted(output, "%s ", tname);
498 : : } else {
499 : : do_not_print_name:
500 : : len += print_formatted(output, "%p ", tid);
501 : : }
502 : :
503 : : /* Do not print thread id in the message as it was already
504 : : * printed above.
505 : : */
506 : : *thread_on = false;
507 : : } else {
508 : : /* No APP-NAME */
509 : : len += print_formatted(output, "- ");
510 : : }
511 : :
512 : : if (!IS_ENABLED(CONFIG_LOG_BACKEND_NET_RFC5424_STRUCTURED_DATA)) {
513 : : /* No PROCID, MSGID or STRUCTURED-DATA */
514 : : len += print_formatted(output, "- - - ");
515 : :
516 : : return len;
517 : : }
518 : :
519 : :
520 : : #if defined(CONFIG_LOG_BACKEND_NET_RFC5424_SDATA_SOFTWARE) || \
521 : : defined(CONFIG_LOG_BACKEND_NET_RFC5424_SDATA_SOFTWARE_VERSION)
522 : : #define STRUCTURED_DATA_ORIGIN_START "[origin"
523 : : #define STRUCTURED_DATA_ORIGIN_SW " software=\"%s\""
524 : : #define STRUCTURED_DATA_ORIGIN_SW_VERSION " swVersion=\"%u\""
525 : : #define STRUCTURED_DATA_ORIGIN_END "]"
526 : : #define STRUCTURED_DATA_ORIGIN \
527 : : STRUCTURED_DATA_ORIGIN_START \
528 : : COND_CODE_1(CONFIG_LOG_BACKEND_NET_RFC5424_SDATA_SOFTWARE, \
529 : : (STRUCTURED_DATA_ORIGIN_SW), ("%s")) \
530 : : COND_CODE_1(CONFIG_LOG_BACKEND_NET_RFC5424_SDATA_SOFTWARE_VERSION,\
531 : : (STRUCTURED_DATA_ORIGIN_SW_VERSION), ("%s")) \
532 : : STRUCTURED_DATA_ORIGIN_END
533 : : #else
534 : : #define STRUCTURED_DATA_ORIGIN "%s%s"
535 : : #endif
536 : :
537 : : #if defined(CONFIG_LOG_BACKEND_NET_RFC5424_SDATA_SEQID) || \
538 : : defined(CONFIG_LOG_BACKEND_NET_RFC5424_SDATA_UPTIME)
539 : : #define STRUCTURED_DATA_META_START "[meta"
540 : : #define STRUCTURED_DATA_META_SEQID " sequenceId=\"%d\""
541 : : #define STRUCTURED_DATA_META_UPTIME " sysUpTime=\"%d\""
542 : : #define STRUCTURED_DATA_META_END "]"
543 : : #define STRUCTURED_DATA_META \
544 : : STRUCTURED_DATA_META_START \
545 : : COND_CODE_1(CONFIG_LOG_BACKEND_NET_RFC5424_SDATA_SEQID, \
546 : : (STRUCTURED_DATA_META_SEQID), ("%s")) \
547 : : COND_CODE_1(CONFIG_LOG_BACKEND_NET_RFC5424_SDATA_UPTIME, \
548 : : (STRUCTURED_DATA_META_UPTIME), ("%s")) \
549 : : STRUCTURED_DATA_META_END
550 : : #else
551 : : #define STRUCTURED_DATA_META "%s%s"
552 : : #endif
553 : :
554 : : #if defined(CONFIG_LOG_BACKEND_NET_RFC5424_SDATA_TZKNOWN) || \
555 : : defined(CONFIG_LOG_BACKEND_NET_RFC5424_SDATA_ISSYNCED)
556 : : #define STRUCTURED_DATA_TIMEQUALITY_START "[timeQuality"
557 : : #define STRUCTURED_DATA_TIMEQUALITY_TZKNOWN " tzKnown=\"%d\""
558 : : #define STRUCTURED_DATA_TIMEQUALITY_ISSYNCED " isSynced=\"%d\""
559 : : #define STRUCTURED_DATA_TIMEQUALITY_END "]"
560 : : #define STRUCTURED_DATA_TIMEQUALITY \
561 : : STRUCTURED_DATA_TIMEQUALITY_START \
562 : : COND_CODE_1(CONFIG_LOG_BACKEND_NET_RFC5424_SDATA_TZKNOWN, \
563 : : (STRUCTURED_DATA_TIMEQUALITY_TZKNOWN), ("%s")) \
564 : : COND_CODE_1(CONFIG_LOG_BACKEND_NET_RFC5424_SDATA_ISSYNCED, \
565 : : (STRUCTURED_DATA_TIMEQUALITY_ISSYNCED), ("%s")) \
566 : : STRUCTURED_DATA_TIMEQUALITY_END
567 : : #else
568 : : #define STRUCTURED_DATA_TIMEQUALITY "%s%s"
569 : : #endif
570 : :
571 : : /* No PROCID or MSGID, but there is structured data.
572 : : */
573 : : len += print_formatted(output,
574 : : "- - "
575 : : STRUCTURED_DATA_META
576 : : STRUCTURED_DATA_ORIGIN
577 : : STRUCTURED_DATA_TIMEQUALITY,
578 : : COND_CODE_1(CONFIG_LOG_BACKEND_NET_RFC5424_SDATA_SEQID,
579 : : (get_sequence_id()), ("")),
580 : : COND_CODE_1(CONFIG_LOG_BACKEND_NET_RFC5424_SDATA_UPTIME,
581 : : /* in hundredths of a sec */
582 : : (k_uptime_get_32() / 10), ("")),
583 : : COND_CODE_1(CONFIG_LOG_BACKEND_NET_RFC5424_SDATA_SOFTWARE,
584 : : (CONFIG_LOG_BACKEND_NET_RFC5424_SDATA_SOFTWARE_VALUE),
585 : : ("")),
586 : : COND_CODE_1(CONFIG_LOG_BACKEND_NET_RFC5424_SDATA_SOFTWARE_VERSION,
587 : : (sys_kernel_version_get()), ("")),
588 : : COND_CODE_1(CONFIG_LOG_BACKEND_NET_RFC5424_SDATA_TZKNOWN,
589 : : (is_tzknown()), ("")),
590 : : COND_CODE_1(CONFIG_LOG_BACKEND_NET_RFC5424_SDATA_ISSYNCED,
591 : : (is_synced()), (""))
592 : : );
593 : :
594 : : return len;
595 : : }
596 : :
597 : 0 : static uint32_t prefix_print(const struct log_output *output,
598 : : uint32_t flags,
599 : : bool func_on,
600 : : log_timestamp_t timestamp,
601 : : const char *domain,
602 : : const char *source,
603 : : k_tid_t tid,
604 : : uint8_t core_id,
605 : : uint8_t level)
606 : : {
607 [ # # ]: 0 : __ASSERT_NO_MSG(level <= LOG_LEVEL_DBG);
608 : 0 : uint32_t length = 0U;
609 : :
610 : 0 : bool stamp = flags & LOG_OUTPUT_FLAG_TIMESTAMP;
611 : 0 : bool colors_on = flags & LOG_OUTPUT_FLAG_COLORS;
612 : 0 : bool level_on = flags & LOG_OUTPUT_FLAG_LEVEL;
613 : 0 : bool thread_on = IS_ENABLED(CONFIG_LOG_THREAD_ID_PREFIX) &&
614 : : (flags & LOG_OUTPUT_FLAG_THREAD);
615 : 0 : bool core_on = IS_ENABLED(CONFIG_LOG_CORE_ID_PREFIX) &&
616 : : (flags & LOG_OUTPUT_FLAG_CORE);
617 : 0 : bool source_off = flags & LOG_OUTPUT_FLAG_SKIP_SOURCE;
618 : 0 : const char *tag = IS_ENABLED(CONFIG_LOG) ? z_log_get_tag() : NULL;
619 : :
620 : 0 : if (IS_ENABLED(CONFIG_LOG_BACKEND_NET) &&
621 : : flags & LOG_OUTPUT_FLAG_FORMAT_SYSLOG) {
622 : : /* TODO: As there is no way to figure out the
623 : : * facility at this point, use a pre-defined value.
624 : : * Change this to use the real facility of the
625 : : * logging call when that info is available.
626 : : */
627 : : static const int facility = 16; /* local0 */
628 : :
629 : : length += print_formatted(
630 : : output,
631 : : /* <PRI>VERSION */
632 : : "<%d>1 ",
633 : : facility * 8 +
634 : : level_to_rfc5424_severity(level));
635 : : }
636 : :
637 [ # # ]: 0 : if (tag) {
638 : 0 : length += print_formatted(output, "%s ", tag);
639 : : }
640 : :
641 [ # # ]: 0 : if (stamp) {
642 : 0 : length += timestamp_print(output, flags, timestamp);
643 : : }
644 : :
645 : 0 : if (IS_ENABLED(CONFIG_LOG_BACKEND_NET) &&
646 : : flags & LOG_OUTPUT_FLAG_FORMAT_SYSLOG) {
647 : : length += syslog_print(output, level_on, func_on, &thread_on, domain,
648 : : source_off ? NULL : source, tid, level, length);
649 : : } else {
650 : 0 : color_prefix(output, colors_on, level);
651 : : }
652 : :
653 [ # # ]: 0 : length += ids_print(output, level_on, func_on, thread_on, core_on, domain,
654 : : source_off ? NULL : source, tid, core_id, level);
655 : :
656 : 0 : return length;
657 : : }
658 : :
659 : 0 : static void postfix_print(const struct log_output *output,
660 : : uint32_t flags, uint8_t level)
661 : : {
662 : 0 : color_postfix(output, (flags & LOG_OUTPUT_FLAG_COLORS),
663 : : level);
664 : 0 : newline_print(output, flags);
665 : 0 : }
666 : :
667 : 209 : void log_output_process(const struct log_output *output,
668 : : log_timestamp_t timestamp,
669 : : const char *domain,
670 : : const char *source,
671 : : k_tid_t tid,
672 : : uint8_t core_id,
673 : : uint8_t level,
674 : : const uint8_t *package,
675 : : const uint8_t *data,
676 : : size_t data_len,
677 : : uint32_t flags)
678 : : {
679 : 209 : bool raw_string = (level == LOG_LEVEL_INTERNAL_RAW_STRING);
680 : 209 : uint32_t prefix_offset;
681 : 209 : cbprintf_cb cb;
682 : :
683 [ - + ]: 209 : if (!raw_string) {
684 : 0 : prefix_offset = prefix_print(output, flags, func_prefix_used && func_on_lut[level],
685 : : timestamp, domain, source, tid, core_id, level);
686 : 0 : cb = out_func;
687 : : } else {
688 : 209 : prefix_offset = 0;
689 : : /* source set to 1 indicates raw string and contrary to printk
690 : : * case it should not append anything to the output (printk is
691 : : * appending <CR> to the new line character).
692 : : */
693 [ + - ]: 209 : cb = ((uintptr_t)source == 1) ? out_func : cr_out_func;
694 : : }
695 : :
696 [ + - ]: 209 : if (package) {
697 : 209 : int err = cbpprintf(cb, (void *)output, (void *)package);
698 : :
699 : 209 : (void)err;
700 [ - + ]: 209 : __ASSERT_NO_MSG(err >= 0);
701 : : }
702 : :
703 [ - + ]: 209 : if (data_len) {
704 : 0 : log_msg_hexdump(output, (uint8_t *)data, data_len, prefix_offset, flags);
705 : : }
706 : :
707 [ - + ]: 209 : if (!raw_string) {
708 : 0 : postfix_print(output, flags, level);
709 : : }
710 : :
711 : 209 : log_output_flush(output);
712 : 209 : }
713 : :
714 : 209 : void log_output_msg_process(const struct log_output *output,
715 : : struct log_msg *msg, uint32_t flags)
716 : : {
717 : 209 : log_timestamp_t timestamp = log_msg_get_timestamp(msg);
718 : 209 : uint8_t level = log_msg_get_level(msg);
719 : 209 : uint8_t domain_id = log_msg_get_domain(msg);
720 : 209 : int16_t source_id = log_msg_get_source_id(msg);
721 : :
722 : 209 : const char *dname = IS_ENABLED(CONFIG_LOG_DOMAIN_NAME_PREFIX) ?
723 : : log_domain_name_get(domain_id) : NULL;
724 [ - + ]: 209 : const char *sname = source_id >= 0 ? log_source_name_get(domain_id, source_id) : NULL;
725 : 209 : size_t plen, dlen;
726 : 209 : uint8_t *package = log_msg_get_package(msg, &plen);
727 : 209 : uint8_t *data = log_msg_get_data(msg, &dlen);
728 : :
729 : 418 : log_output_process(output, timestamp, dname, sname, (k_tid_t)log_msg_get_tid(msg),
730 : 209 : log_msg_get_core_id(msg), level,
731 [ - + ]: 209 : plen > 0 ? package : NULL, data, dlen, flags);
732 : 209 : }
733 : :
734 : 0 : void log_output_dropped_process(const struct log_output *output, uint32_t cnt)
735 : : {
736 : 0 : char buf[5];
737 : 0 : int len;
738 : 0 : static const char prefix[] = DROPPED_COLOR_PREFIX "--- ";
739 : 0 : static const char postfix[] =
740 : : " messages dropped ---\r\n" DROPPED_COLOR_POSTFIX;
741 : 0 : log_output_func_t outf = output->func;
742 : :
743 : 0 : cnt = MIN(cnt, 9999);
744 : 0 : len = snprintk(buf, sizeof(buf), "%d", cnt);
745 : :
746 : 0 : log_output_write(outf, (uint8_t *)prefix, sizeof(prefix) - 1, output->control_block->ctx);
747 : 0 : log_output_write(outf, (uint8_t *)buf, len, output->control_block->ctx);
748 : 0 : log_output_write(outf, (uint8_t *)postfix, sizeof(postfix) - 1, output->control_block->ctx);
749 : 0 : }
750 : :
751 : 1 : void log_output_timestamp_freq_set(uint32_t frequency)
752 : : {
753 : 1 : timestamp_div = 1U;
754 : : /* There is no point to have frequency higher than 1MHz (ns are not
755 : : * printed) and too high frequency leads to overflows in calculations.
756 : : */
757 [ - + ]: 1 : while (frequency > 1000000) {
758 : 0 : frequency /= 2U;
759 : 0 : timestamp_div *= 2U;
760 : : }
761 : :
762 : 1 : freq = frequency;
763 : 1 : }
764 : :
765 : 0 : uint64_t log_output_timestamp_to_us(log_timestamp_t timestamp)
766 : : {
767 [ # # ]: 0 : if (freq == 0U) {
768 : : return 0U;
769 : : }
770 : :
771 : 0 : timestamp /= timestamp_div;
772 : :
773 : 0 : return ((uint64_t) timestamp * 1000000U) / freq;
774 : : }
|