xref: /nrf52832-nimble/rt-thread/include/rtdbg.h (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  * 2016-11-12     Bernard      The first version
9  * 2018-05-25     armink       Add simple API, such as LOG_D, LOG_E
10  */
11 
12 /*
13  * The macro definitions for debug
14  *
15  * These macros are defined in static. If you want to use debug macro, you can
16  * use as following code:
17  *
18  * In your C/C++ file, enable/disable DEBUG_ENABLE macro, and then include this
19  * header file.
20  *
21  * #define DBG_SECTION_NAME    "MOD"
22  * #define DBG_ENABLE          // enable debug macro
23  * #define DBG_LEVEL           DBG_INFO
24  * #include <rtdbg.h>          // must after of DEBUG_ENABLE or some other options
25  *
26  * Then in your C/C++ file, you can use LOG_X macro to print out logs:
27  * LOG_D("this is a debug log!");
28  * LOG_E("this is a error log!");
29  *
30  * If you want to use different color for different kinds log, you can
31  * #define DBG_COLOR
32  */
33 
34 #ifndef RT_DBG_H__
35 #define RT_DBG_H__
36 
37 #include <rtconfig.h>
38 
39 #if defined(RT_USING_ULOG) && defined(DBG_ENABLE)
40 /* using ulog compatible with rtdbg  */
41 #include <ulog.h>
42 #else
43 
44 /* DEBUG level */
45 #define DBG_ERROR           0
46 #define DBG_WARNING         1
47 #define DBG_INFO            2
48 #define DBG_LOG             3
49 
50 #ifndef DBG_SECTION_NAME
51 #define DBG_SECTION_NAME    "DBG"
52 #endif
53 
54 #ifdef DBG_ENABLE
55 
56 #ifndef DBG_LEVEL
57 #define DBG_LEVEL         DBG_WARNING
58 #endif
59 
60 /*
61  * The color for terminal (foreground)
62  * BLACK    30
63  * RED      31
64  * GREEN    32
65  * YELLOW   33
66  * BLUE     34
67  * PURPLE   35
68  * CYAN     36
69  * WHITE    37
70  */
71 #ifdef DBG_COLOR
72 #define _DBG_COLOR(n)        rt_kprintf("\033["#n"m")
73 #define _DBG_LOG_HDR(lvl_name, color_n)                    \
74     rt_kprintf("\033["#color_n"m["lvl_name"/"DBG_SECTION_NAME"] ")
75 #define _DBG_LOG_X_END                                     \
76     rt_kprintf("\033[0m\n")
77 #else
78 #define _DBG_COLOR(n)
79 #define _DBG_LOG_HDR(lvl_name, color_n)                    \
80     rt_kprintf("["lvl_name"/"DBG_SECTION_NAME"] ")
81 #define _DBG_LOG_X_END                                     \
82     rt_kprintf("\n")
83 #endif /* DBG_COLOR */
84 
85 /*
86  * static debug routine
87  * NOTE: This is a NOT RECOMMENDED API. Please using LOG_X API.
88  *       It will be DISCARDED later. Because it will take up more resources.
89  */
90 #define dbg_log(level, fmt, ...)                            \
91     if ((level) <= DBG_LEVEL)                               \
92     {                                                       \
93         switch(level)                                       \
94         {                                                   \
95             case DBG_ERROR:   _DBG_LOG_HDR("E", 31); break; \
96             case DBG_WARNING: _DBG_LOG_HDR("W", 33); break; \
97             case DBG_INFO:    _DBG_LOG_HDR("I", 32); break; \
98             case DBG_LOG:     _DBG_LOG_HDR("D", 0); break;  \
99             default: break;                                 \
100         }                                                   \
101         rt_kprintf(fmt, ##__VA_ARGS__);                     \
102         _DBG_COLOR(0);                                      \
103     }
104 
105 #define dbg_here                                            \
106     if ((DBG_LEVEL) <= DBG_LOG){                            \
107         rt_kprintf(DBG_SECTION_NAME " Here %s:%d\n",        \
108             __FUNCTION__, __LINE__);                        \
109     }
110 
111 #define dbg_enter                                           \
112     if ((DBG_LEVEL) <= DBG_LOG){                            \
113         _DBG_COLOR(32);                                     \
114         rt_kprintf(DBG_SECTION_NAME " Enter %s\n",          \
115             __FUNCTION__);                                  \
116         _DBG_COLOR(0);                                      \
117     }
118 
119 #define dbg_exit                                            \
120     if ((DBG_LEVEL) <= DBG_LOG){                            \
121         _DBG_COLOR(32);                                     \
122         rt_kprintf(DBG_SECTION_NAME " Exit  %s:%d\n",       \
123             __FUNCTION__);                                  \
124         _DBG_COLOR(0);                                      \
125     }
126 
127 
128 #define dbg_log_line(lvl, color_n, fmt, ...)                \
129     do                                                      \
130     {                                                       \
131         _DBG_LOG_HDR(lvl, color_n);                         \
132         rt_kprintf(fmt, ##__VA_ARGS__);                     \
133         _DBG_LOG_X_END;                                     \
134     }                                                       \
135     while (0)
136 
137 #define dbg_raw(...)         rt_kprintf(__VA_ARGS__);
138 
139 #else
140 #define dbg_log(level, fmt, ...)
141 #define dbg_here
142 #define dbg_enter
143 #define dbg_exit
144 #define dbg_log_line(lvl, color_n, fmt, ...)
145 #define dbg_raw(...)
146 #endif /* DBG_ENABLE */
147 
148 #if (DBG_LEVEL >= DBG_LOG)
149 #define LOG_D(fmt, ...)      dbg_log_line("D", 0, fmt, ##__VA_ARGS__)
150 #else
151 #define LOG_D(...)
152 #endif
153 
154 #if (DBG_LEVEL >= DBG_INFO)
155 #define LOG_I(fmt, ...)      dbg_log_line("I", 32, fmt, ##__VA_ARGS__)
156 #else
157 #define LOG_I(...)
158 #endif
159 
160 #if (DBG_LEVEL >= DBG_WARNING)
161 #define LOG_W(fmt, ...)      dbg_log_line("W", 33, fmt, ##__VA_ARGS__)
162 #else
163 #define LOG_W(...)
164 #endif
165 
166 #if (DBG_LEVEL >= DBG_ERROR)
167 #define LOG_E(fmt, ...)      dbg_log_line("E", 31, fmt, ##__VA_ARGS__)
168 #else
169 #define LOG_E(...)
170 #endif
171 
172 #define LOG_RAW(...)         dbg_raw(__VA_ARGS__)
173 
174 #endif /* defined(RT_USING_ULOG) && define(DBG_ENABLE) */
175 
176 #endif /* RT_DBG_H__ */
177