1 /*
2 * SPDX-License-Identifier: Apache-2.0
3 *
4 * Date Author Notes
5 * 2018-12-27 ChenYong first implementation
6 */
7
8 #include <string.h>
9
10 #include <rtthread.h>
11 #include <rthw.h>
12
13 #include <modlog/modlog.h>
14
15 /* This log is the first line of logs */
16 static rt_bool_t console_is_fline = RT_TRUE;
17
18 #ifdef NIMBLE_DEBUG_LEVEL
19 #define MODLOG_DEBUG_LEVEL NIMBLE_DEBUG_LEVEL
20 #else
21 #define MODLOG_DEBUG_LEVEL MODLOG_LEVEL_INFO
22 #endif /* NIMBLE_DBG_LEVEL */
23
24 #define DBG_ENABLE
25 #define DBG_COLOR
26 #define DBG_LEVEL MODLOG_DEBUG_LEVEL
27
28 #ifndef DBG_SECTION_NAME
29 #define DBG_SECTION_NAME "nimble"
30 #endif
31
32 #include <rtdbg.h>
33
modlog_vprintf(const char * fmt,va_list args)34 static rt_size_t modlog_vprintf(const char *fmt, va_list args)
35 {
36 static char modlog_buf[RT_CONSOLEBUF_SIZE];
37 rt_device_t dev = rt_console_get_device();
38 rt_size_t length;
39
40 length = rt_vsnprintf(modlog_buf, sizeof(modlog_buf) - 1, fmt, args);
41
42 #ifdef RT_USING_DEVICE
43 if (dev == RT_NULL)
44 {
45 rt_hw_console_output(modlog_buf);
46 }
47 else
48 {
49 rt_uint16_t old_flag = dev->open_flag;
50
51 dev->open_flag |= RT_DEVICE_FLAG_STREAM;
52 rt_device_write(dev, 0, modlog_buf, length);
53 dev->open_flag = old_flag;
54 }
55 #else
56 rt_hw_console_output(log);
57 #endif
58
59 return length;
60 }
61
modlog_dummy(uint8_t level,const char * msg,...)62 void modlog_dummy(uint8_t level, const char *msg, ...)
63 {
64 va_list args;
65
66 RT_ASSERT(msg);
67
68 va_start(args, msg);
69 switch (level)
70 {
71 case MODLOG_LEVEL_CRITICAL:
72 #if (MODLOG_DEBUG_LEVEL >= MODLOG_LEVEL_LOG)
73 case MODLOG_LEVEL_LOG:
74 if(console_is_fline) rt_kprintf("[D/%s] ", DBG_SECTION_NAME);
75 modlog_vprintf(msg, args);
76 break;
77 #endif
78 #if (MODLOG_DEBUG_LEVEL >= MODLOG_LEVEL_INFO)
79 case MODLOG_LEVEL_INFO:
80 if(console_is_fline) rt_kprintf("[I/%s] ", DBG_SECTION_NAME);
81 modlog_vprintf(msg, args);
82 break;
83 #endif
84 #if (MODLOG_DEBUG_LEVEL >= MODLOG_LEVEL_WARING)
85 case MODLOG_LEVEL_WARING:
86 if(console_is_fline) rt_kprintf("[W/%s] ", DBG_SECTION_NAME);
87 modlog_vprintf(msg, args);
88 break;
89 #endif
90 #if (MODLOG_DEBUG_LEVEL >= MODLOG_LEVEL_ERROR)
91 case MODLOG_LEVEL_ERROR:
92 if(console_is_fline) rt_kprintf("[E/%s] ", DBG_SECTION_NAME);
93 modlog_vprintf(msg, args);
94 break;
95 #endif
96 }
97
98 if (strchr(msg, '\n') != RT_NULL)
99 {
100 console_is_fline = RT_TRUE;
101 }
102 else
103 {
104 console_is_fline = RT_FALSE;
105 }
106
107 va_end(args);
108 }
109