1 /*
2  * Copyright (c) 2006-2021, 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_TAG           "MOD_TAG"
22  * #define DBG_LVL           DBG_INFO
23  * #include <rtdbg.h>          // must after of DBG_LVL, DBG_TAG or other options
24  *
25  * Then in your C/C++ file, you can use LOG_X macro to print out logs:
26  * LOG_D("this is a debug log!");
27  * LOG_E("this is a error log!");
28  */
29 
30 #ifndef RT_DBG_H__
31 #define RT_DBG_H__
32 
33 #include <rtconfig.h>
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 /* the debug log will force enable when RT_USING_DEBUG macro is defined */
40 #if defined(RT_USING_DEBUG) && !defined(DBG_ENABLE)
41 #define DBG_ENABLE
42 #endif
43 
44 /* it will force output color log when RT_DEBUGING_COLOR macro is defined */
45 #if defined(RT_DEBUGING_COLOR) && !defined(DBG_COLOR)
46 #define DBG_COLOR
47 #endif
48 
49 /* for dlog */
50 #ifdef PKG_USING_DLOG
51 #include <dlog.h>
52 #else
53 #define DLOG(...)
54 #endif
55 
56 #if defined(RT_USING_ULOG)
57 /* using ulog compatible with rtdbg  */
58 #include <ulog.h>
59 #else
60 
61 /* DEBUG level */
62 #define DBG_ERROR           0
63 #define DBG_WARNING         1
64 #define DBG_INFO            2
65 #define DBG_LOG             3
66 
67 #ifdef DBG_TAG
68 #ifndef DBG_SECTION_NAME
69 #define DBG_SECTION_NAME    DBG_TAG
70 #endif
71 #else
72 /* compatible with old version */
73 #ifndef DBG_SECTION_NAME
74 #define DBG_SECTION_NAME    "DBG"
75 #endif
76 #endif /* DBG_TAG */
77 
78 #ifdef DBG_ENABLE
79 
80 #ifdef DBG_LVL
81 #ifndef DBG_LEVEL
82 #define DBG_LEVEL         DBG_LVL
83 #endif
84 #else
85 /* compatible with old version */
86 #ifndef DBG_LEVEL
87 #define DBG_LEVEL         DBG_WARNING
88 #endif
89 #endif /* DBG_LVL */
90 
91 /*
92  * The color for terminal (foreground)
93  * BLACK    30
94  * RED      31
95  * GREEN    32
96  * YELLOW   33
97  * BLUE     34
98  * PURPLE   35
99  * CYAN     36
100  * WHITE    37
101  */
102 #ifdef DBG_COLOR
103 #define _DBG_COLOR(n)        rt_kprintf("\033["#n"m")
104 #define _DBG_LOG_HDR(lvl_name, color_n)                    \
105     rt_kprintf("\033["#color_n"m[" lvl_name "/" DBG_SECTION_NAME "] ")
106 #define _DBG_LOG_X_END                                     \
107     rt_kprintf("\033[0m\n")
108 #else
109 #define _DBG_COLOR(n)
110 #define _DBG_LOG_HDR(lvl_name, color_n)                    \
111     rt_kprintf("[" lvl_name "/" DBG_SECTION_NAME "] ")
112 #define _DBG_LOG_X_END                                     \
113     rt_kprintf("\n")
114 #endif /* DBG_COLOR */
115 
116 #define dbg_log_line(lvl, color_n, fmt, ...)                \
117     do                                                      \
118     {                                                       \
119         _DBG_LOG_HDR(lvl, color_n);                         \
120         rt_kprintf(fmt, ##__VA_ARGS__);                     \
121         _DBG_LOG_X_END;                                     \
122     }                                                       \
123     while (0)
124 
125 #define dbg_raw(...)         rt_kprintf(__VA_ARGS__);
126 
127 #else
128 #define dbg_log_line(lvl, color_n, fmt, ...)
129 #define dbg_raw(...)
130 #endif /* DBG_ENABLE */
131 
132 #if (DBG_LEVEL >= DBG_LOG)
133 #define LOG_D(fmt, ...)      dbg_log_line("D", 0, fmt, ##__VA_ARGS__)
134 #else
135 #define LOG_D(...)
136 #endif
137 
138 #if (DBG_LEVEL >= DBG_INFO)
139 #define LOG_I(fmt, ...)      dbg_log_line("I", 32, fmt, ##__VA_ARGS__)
140 #else
141 #define LOG_I(...)
142 #endif
143 
144 #if (DBG_LEVEL >= DBG_WARNING)
145 #define LOG_W(fmt, ...)      dbg_log_line("W", 33, fmt, ##__VA_ARGS__)
146 #else
147 #define LOG_W(...)
148 #endif
149 
150 #if (DBG_LEVEL >= DBG_ERROR)
151 #define LOG_E(fmt, ...)      dbg_log_line("E", 31, fmt, ##__VA_ARGS__)
152 #else
153 #define LOG_E(...)
154 #endif
155 
156 #define LOG_RAW(...)         dbg_raw(__VA_ARGS__)
157 
158 #define LOG_HEX(name, width, buf, size)
159 
160 #endif /* defined(RT_USING_ULOG) && define(DBG_ENABLE) */
161 
162 #ifdef __cplusplus
163 }
164 #endif
165 
166 #endif /* RT_DBG_H__ */
167