Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2018 Oticon A/S
3 : : * Copyright (c) 2023 Nordic Semiconductor ASA
4 : : *
5 : : * SPDX-License-Identifier: Apache-2.0
6 : : */
7 : :
8 : : #include <zephyr/init.h>
9 : : #include <zephyr/arch/posix/posix_trace.h>
10 : : #include <zephyr/sys/printk-hooks.h>
11 : : #include <zephyr/sys/libc-hooks.h>
12 : :
13 : : #define _STDOUT_BUF_SIZE CONFIG_POSIX_ARCH_CONSOLE_STDOUT_BUF_SIZE
14 : : static char stdout_buff[_STDOUT_BUF_SIZE];
15 : : static int n_pend; /* Number of pending characters in buffer */
16 : :
17 : : #if defined(CONFIG_PRINTK) || defined(CONFIG_STDOUT_CONSOLE)
18 : 0 : static int print_char(int c)
19 : : {
20 [ # # ]: 0 : if (c == '\r') {
21 : : /* Discard carriage return */
22 : : return c;
23 : : }
24 : :
25 : 0 : stdout_buff[n_pend++] = c;
26 : 0 : stdout_buff[n_pend] = 0;
27 : :
28 : : /* Flush if buffer is full or on newline */
29 [ # # # # ]: 0 : if (n_pend >= sizeof(stdout_buff) - 1 || c == '\n') {
30 : 0 : posix_print_trace("%s", stdout_buff);
31 : 0 : n_pend = 0;
32 : 0 : stdout_buff[0] = 0;
33 : : }
34 : :
35 : : return c;
36 : : }
37 : : #endif /* defined(CONFIG_PRINTK) || defined(CONFIG_STDOUT_CONSOLE) */
38 : :
39 : : /**
40 : : * Ensure that whatever was written thru printk is displayed now
41 : : */
42 : 0 : void posix_flush_stdout(void)
43 : : {
44 [ # # ]: 0 : if (n_pend) {
45 : 0 : stdout_buff[n_pend] = 0;
46 : 0 : posix_print_trace("%s", stdout_buff);
47 : 0 : n_pend = 0;
48 : 0 : stdout_buff[0] = 0;
49 : : }
50 : 0 : }
51 : :
52 : 1 : static int posix_arch_console_init(void)
53 : : {
54 : : #ifdef CONFIG_PRINTK
55 : 1 : __printk_hook_install(print_char);
56 : : #endif
57 : : #ifdef CONFIG_STDOUT_CONSOLE
58 : : __stdout_hook_install(print_char);
59 : : #endif
60 : 1 : return 0;
61 : : }
62 : :
63 : : SYS_INIT(posix_arch_console_init, PRE_KERNEL_1,
64 : : CONFIG_POSIX_ARCH_CONSOLE_INIT_PRIORITY);
|