xref: /nrf52832-nimble/rt-thread/components/utilities/ulog/backend/console_be.c (revision 104654410c56c573564690304ae786df310c91fc)
1 /*
2  * Copyright (c) 2006-2018, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2018-09-04     armink       the first version
9  */
10 
11 #include <rthw.h>
12 #include <ulog.h>
13 
14 #ifdef ULOG_BACKEND_USING_CONSOLE
15 
16 #if defined(ULOG_ASYNC_OUTPUT_BY_THREAD) && ULOG_ASYNC_OUTPUT_THREAD_STACK < 384
17 #error "The thread stack size must more than 384 when using async output by thread (ULOG_ASYNC_OUTPUT_BY_THREAD)"
18 #endif
19 
20 static struct ulog_backend console;
21 
ulog_console_backend_output(struct ulog_backend * backend,rt_uint32_t level,const char * tag,rt_bool_t is_raw,const char * log,size_t len)22 void ulog_console_backend_output(struct ulog_backend *backend, rt_uint32_t level, const char *tag, rt_bool_t is_raw,
23         const char *log, size_t len)
24 {
25     rt_device_t dev = rt_console_get_device();
26 
27 #ifdef RT_USING_DEVICE
28     if (dev == RT_NULL)
29     {
30         rt_hw_console_output(log);
31     }
32     else
33     {
34         rt_uint16_t old_flag = dev->open_flag;
35 
36         dev->open_flag |= RT_DEVICE_FLAG_STREAM;
37         rt_device_write(dev, 0, log, len);
38         dev->open_flag = old_flag;
39     }
40 #else
41     rt_hw_console_output(log);
42 #endif
43 
44 }
45 
ulog_console_backend_init(void)46 int ulog_console_backend_init(void)
47 {
48     ulog_init();
49     console.output = ulog_console_backend_output;
50 
51     ulog_backend_register(&console, "console", RT_TRUE);
52 
53     return 0;
54 }
55 INIT_PREV_EXPORT(ulog_console_backend_init);
56 
57 #endif /* ULOG_BACKEND_USING_CONSOLE */
58