xref: /nrf52832-nimble/rt-thread/components/utilities/ulog/ulog_def.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  * 2018-08-25     armink       the first version
9  */
10 
11 #ifndef _ULOG_DEF_H_
12 #define _ULOG_DEF_H_
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 /* logger level, the number is compatible for syslog */
19 #define LOG_LVL_ASSERT                 0
20 #define LOG_LVL_ERROR                  3
21 #define LOG_LVL_WARNING                4
22 #define LOG_LVL_INFO                   6
23 #define LOG_LVL_DBG                    7
24 
25 /* the output silent level and all level for filter setting */
26 #ifndef ULOG_USING_SYSLOG
27 #define LOG_FILTER_LVL_SILENT         0
28 #define LOG_FILTER_LVL_ALL            7
29 #else
30 #define LOG_FILTER_LVL_SILENT         1
31 #define LOG_FILTER_LVL_ALL            255
32 #endif /* ULOG_USING_SYSLOG */
33 
34 /* compatible for rtdbg */
35 #undef LOG_D
36 #undef LOG_I
37 #undef LOG_W
38 #undef LOG_E
39 #undef LOG_RAW
40 #undef DBG_ERROR
41 #undef DBG_WARNING
42 #undef DBG_INFO
43 #undef DBG_LOG
44 #undef dbg_log
45 #define DBG_ERROR                      LOG_LVL_ERROR
46 #define DBG_WARNING                    LOG_LVL_WARNING
47 #define DBG_INFO                       LOG_LVL_INFO
48 #define DBG_LOG                        LOG_LVL_DBG
49 #define dbg_log(level, ...)                                \
50     if ((level) <= DBG_LEVEL)                              \
51     {                                                      \
52         ulog_output(level, LOG_TAG, RT_FALSE, __VA_ARGS__);\
53     }
54 
55 #if !defined(LOG_TAG)
56     /* compatible for rtdbg */
57     #if defined(DBG_SECTION_NAME)
58         #define LOG_TAG                DBG_SECTION_NAME
59     #else
60         #define LOG_TAG                "NO_TAG"
61     #endif
62 #endif /* !defined(LOG_TAG) */
63 
64 #if !defined(LOG_LVL)
65     /* compatible for rtdbg */
66     #if defined(DBG_LEVEL)
67         #define LOG_LVL                DBG_LEVEL
68     #else
69         #define LOG_LVL                LOG_LVL_DBG
70     #endif
71 #endif /* !defined(LOG_LVL) */
72 
73 #if (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG)
74     #define ulog_d(TAG, ...)           ulog_output(LOG_LVL_DBG, TAG, RT_TRUE, __VA_ARGS__)
75 #else
76     #define ulog_d(TAG, ...)
77 #endif /* (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG) */
78 
79 #if (LOG_LVL >= LOG_LVL_INFO) && (ULOG_OUTPUT_LVL >= LOG_LVL_INFO)
80     #define ulog_i(TAG, ...)           ulog_output(LOG_LVL_INFO, TAG, RT_TRUE, __VA_ARGS__)
81 #else
82     #define ulog_i(TAG, ...)
83 #endif /* (LOG_LVL >= LOG_LVL_INFO) && (ULOG_OUTPUT_LVL >= LOG_LVL_INFO) */
84 
85 #if (LOG_LVL >= LOG_LVL_WARNING) && (ULOG_OUTPUT_LVL >= LOG_LVL_WARNING)
86     #define ulog_w(TAG, ...)           ulog_output(LOG_LVL_WARNING, TAG, RT_TRUE, __VA_ARGS__)
87 #else
88     #define ulog_w(TAG, ...)
89 #endif /* (LOG_LVL >= LOG_LVL_WARNING) && (ULOG_OUTPUT_LVL >= LOG_LVL_WARNING) */
90 
91 #if (LOG_LVL >= LOG_LVL_ERROR) && (ULOG_OUTPUT_LVL >= LOG_LVL_ERROR)
92     #define ulog_e(TAG, ...)           ulog_output(LOG_LVL_ERROR, TAG, RT_TRUE, __VA_ARGS__)
93 #else
94     #define ulog_e(TAG, ...)
95 #endif /* (LOG_LVL >= LOG_LVL_ERROR) && (ULOG_OUTPUT_LVL >= LOG_LVL_ERROR) */
96 
97 #if (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG)
98     #define ulog_hex(TAG, width, buf, size)     ulog_hexdump(TAG, width, buf, size)
99 #else
100     #define ulog_hex(TAG, width, buf, size)
101 #endif /* (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG) */
102 
103 /* assert for developer. */
104 #ifdef ULOG_ASSERT_ENABLE
105     #define ULOG_ASSERT(EXPR)                                                 \
106     if (!(EXPR))                                                              \
107     {                                                                         \
108         ulog_output(LOG_LVL_ASSERT, LOG_TAG, RT_TRUE, "(%s) has assert failed at %s:%ld.", #EXPR, __FUNCTION__, __LINE__); \
109         ulog_flush();                                                         \
110         while (1);                                                            \
111     }
112 #else
113     #define ULOG_ASSERT(EXPR)
114 #endif
115 
116 /* ASSERT API definition */
117 #if !defined(ASSERT)
118     #define ASSERT           ULOG_ASSERT
119 #endif
120 
121 /* compatible for elog */
122 #undef assert
123 #undef log_e
124 #undef log_w
125 #undef log_i
126 #undef log_d
127 #undef log_v
128 #undef ELOG_LVL_ASSERT
129 #undef ELOG_LVL_ERROR
130 #undef ELOG_LVL_WARN
131 #undef ELOG_LVL_INFO
132 #undef ELOG_LVL_DEBUG
133 #undef ELOG_LVL_VERBOSE
134 #define assert                         ASSERT
135 #define log_e                          LOG_E
136 #define log_w                          LOG_W
137 #define log_i                          LOG_I
138 #define log_d                          LOG_D
139 #define log_v                          LOG_D
140 #define log_raw                        LOG_RAW
141 #define log_hex                        LOG_HEX
142 #define ELOG_LVL_ASSERT                LOG_LVL_ASSERT
143 #define ELOG_LVL_ERROR                 LOG_LVL_ERROR
144 #define ELOG_LVL_WARN                  LOG_LVL_WARNING
145 #define ELOG_LVL_INFO                  LOG_LVL_INFO
146 #define ELOG_LVL_DEBUG                 LOG_LVL_DBG
147 #define ELOG_LVL_VERBOSE               LOG_LVL_DBG
148 
149 /* setting static output log level */
150 #ifndef ULOG_OUTPUT_LVL
151 #define ULOG_OUTPUT_LVL                LOG_LVL_DBG
152 #endif
153 
154 /* buffer size for every line's log */
155 #ifndef ULOG_LINE_BUF_SIZE
156 #define ULOG_LINE_BUF_SIZE             128
157 #endif
158 
159 /* output filter's tag max length */
160 #ifndef ULOG_FILTER_TAG_MAX_LEN
161 #define ULOG_FILTER_TAG_MAX_LEN        23
162 #endif
163 
164 /* output filter's keyword max length */
165 #ifndef ULOG_FILTER_KW_MAX_LEN
166 #define ULOG_FILTER_KW_MAX_LEN         15
167 #endif
168 
169 #ifndef ULOG_NEWLINE_SIGN
170 #define ULOG_NEWLINE_SIGN              "\r\n"
171 #endif
172 
173 #define ULOG_FRAME_MAGIC               0x10
174 
175 /* tag's level filter */
176 struct ulog_tag_lvl_filter
177 {
178     char tag[ULOG_FILTER_TAG_MAX_LEN + 1];
179     rt_uint32_t level;
180     rt_slist_t list;
181 };
182 typedef struct ulog_tag_lvl_filter *ulog_tag_lvl_filter_t;
183 
184 struct ulog_frame
185 {
186     /* magic word is 0x10 ('lo') */
187     rt_uint32_t magic:8;
188     rt_uint32_t is_raw:1;
189     rt_uint32_t log_len:23;
190     rt_uint32_t level;
191     const char *log;
192     const char *tag;
193 };
194 typedef struct ulog_frame *ulog_frame_t;
195 
196 struct ulog_backend
197 {
198     char name[RT_NAME_MAX];
199     rt_bool_t support_color;
200     void (*init)  (struct ulog_backend *backend);
201     void (*output)(struct ulog_backend *backend, rt_uint32_t level, const char *tag, rt_bool_t is_raw, const char *log, size_t len);
202     void (*flush) (struct ulog_backend *backend);
203     void (*deinit)(struct ulog_backend *backend);
204     rt_slist_t list;
205 };
206 typedef struct ulog_backend *ulog_backend_t;
207 
208 #ifdef __cplusplus
209 }
210 #endif
211 
212 #endif /* _ULOG_DEF_H_ */
213