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 11 #define _STDOUT_BUF_SIZE 256 12 static char stdout_buff[_STDOUT_BUF_SIZE]; 13 static int n_pend; /* Number of pending characters in buffer */ 14 print_char(int c)15static int print_char(int c) 16 { 17 int printnow = 0; 18 19 if ((c != '\n') && (c != '\r')) { 20 stdout_buff[n_pend++] = c; 21 stdout_buff[n_pend] = 0; 22 } else { 23 printnow = 1; 24 } 25 26 if (n_pend >= _STDOUT_BUF_SIZE - 1) { 27 printnow = 1; 28 } 29 30 if (printnow) { 31 posix_print_trace("%s\n", stdout_buff); 32 n_pend = 0; 33 stdout_buff[0] = 0; 34 } 35 return c; 36 } 37 38 /** 39 * Ensure that whatever was written thru printk is displayed now 40 */ posix_flush_stdout(void)41void posix_flush_stdout(void) 42 { 43 if (n_pend) { 44 stdout_buff[n_pend] = 0; 45 posix_print_trace("%s", stdout_buff); 46 n_pend = 0; 47 stdout_buff[0] = 0; 48 } 49 } 50 posix_arch_console_init(void)51static int posix_arch_console_init(void) 52 { 53 #ifdef CONFIG_PRINTK 54 extern void __printk_hook_install(int (*fn)(int)); 55 __printk_hook_install(print_char); 56 #endif 57 #ifdef CONFIG_STDOUT_CONSOLE 58 extern void __stdout_hook_install(int (*fn)(int)); 59 __stdout_hook_install(print_char); 60 #endif 61 return 0; 62 } 63 64 SYS_INIT(posix_arch_console_init, PRE_KERNEL_1, 65 CONFIG_POSIX_ARCH_CONSOLE_INIT_PRIORITY); 66