-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.h
302 lines (275 loc) · 8.54 KB
/
log.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/**
* @file log.h
* @author TheSomeMan
* @date 2020-10-24
* @copyright Ruuvi Innovations Ltd, license BSD-3-Clause.
*/
#ifndef RUUVI_LOG_H
#define RUUVI_LOG_H
#include "esp_log.h"
#include "os_task.h"
#include "esp_type_wrapper.h"
#include "snprintf_with_esp_err_desc.h"
#ifdef __cplusplus
extern "C" {
#endif
#define LOG_LEVEL_NONE 0
#define LOG_LEVEL_ERROR 1
#define LOG_LEVEL_WARN 2
#define LOG_LEVEL_INFO 3
#define LOG_LEVEL_DEBUG 4
#define LOG_LEVEL_VERBOSE 5
#ifndef __cplusplus
_Static_assert(LOG_LEVEL_NONE == ESP_LOG_NONE, "LOG_LEVEL_NONE == ESP_LOG_NONE");
_Static_assert(LOG_LEVEL_ERROR == ESP_LOG_ERROR, "LOG_LEVEL_ERROR == ESP_LOG_ERROR");
_Static_assert(LOG_LEVEL_WARN == ESP_LOG_WARN, "LOG_LEVEL_WARN == ESP_LOG_WARN");
_Static_assert(LOG_LEVEL_INFO == ESP_LOG_INFO, "LOG_LEVEL_INFO == ESP_LOG_INFO");
_Static_assert(LOG_LEVEL_DEBUG == ESP_LOG_DEBUG, "LOG_LEVEL_DEBUG == ESP_LOG_DEBUG");
_Static_assert(LOG_LEVEL_VERBOSE == ESP_LOG_VERBOSE, "LOG_LEVEL_VERBOSE == ESP_LOG_VERBOSE");
#endif // __cplusplus
#if !defined(LOG_LOCAL_LEVEL)
#error Macro LOG_LOCAL_LEVEL must be defined before including "log.h"
#endif
#if defined(LOG_LOCAL_DISABLED)
#undef LOG_LOCAL_LEVEL
#define LOG_LOCAL_LEVEL LOG_LEVEL_NONE
#endif
#if !defined(LOG_LOCAL_DISABLED) && !((LOG_LOCAL_LEVEL >= LOG_LEVEL_ERROR) && (LOG_LOCAL_LEVEL <= LOG_LEVEL_VERBOSE))
#error Macro LOG_LOCAL_LEVEL should be defined as one of LOG_LEVEL_ERROR, LOG_LEVEL_WARN, LOG_LEVEL_INFO, LOG_LEVEL_DEBUG, LOG_LEVEL_VERBOSE or LOG_LOCAL_DISABLED should be defined to disable logging
#endif
extern uint32_t
esp_log_timestamp(void);
extern void
log_print_dump(
esp_log_level_t level,
const char* p_tag,
const char* p_log_prefix,
const uint8_t* p_buf,
const uint32_t buf_size);
#if LOG_LOCAL_LEVEL >= LOG_LEVEL_ERROR
#define LOG_ERR(fmt, ...) \
esp_log_write( \
ESP_LOG_ERROR, \
TAG, \
LOG_FORMAT(E, "[%s/%d] %s:%d {%s}: " fmt), \
esp_log_timestamp(), \
TAG, \
os_task_get_name(), \
(printf_int_t)os_task_get_priority(), \
__FILE__, \
__LINE__, \
__func__, \
##__VA_ARGS__)
#define LOG_ERR_ESP(err, fmt, ...) \
do \
{ \
str_buf_t err_desc = esp_err_to_name_with_alloc_str_buf(err); \
esp_log_write( \
ESP_LOG_ERROR, \
TAG, \
LOG_FORMAT(E, "[%s/%d] %s:%d {%s}: " fmt ", err=%d (%s)"), \
esp_log_timestamp(), \
TAG, \
os_task_get_name(), \
(printf_int_t)os_task_get_priority(), \
__FILE__, \
__LINE__, \
__func__, \
##__VA_ARGS__, \
err, \
(NULL != err_desc.buf) ? err_desc.buf : ""); \
str_buf_free_buf(&err_desc); \
} while (0)
#define LOG_ERR_VAL(err, fmt, ...) \
esp_log_write( \
ESP_LOG_ERROR, \
TAG, \
LOG_FORMAT(E, "[%s/%d] %s:%d {%s}: " fmt ", err=%d"), \
esp_log_timestamp(), \
TAG, \
os_task_get_name(), \
(printf_int_t)os_task_get_priority(), \
__FILE__, \
__LINE__, \
__func__, \
##__VA_ARGS__, \
err)
#define LOG_DUMP_ERR(p_buf, buf_size, fmt, ...) \
do \
{ \
esp_log_write( \
ESP_LOG_ERROR, \
TAG, \
LOG_FORMAT(E, "[%s/%d] %s:%d {%s}: " fmt ":"), \
esp_log_timestamp(), \
TAG, \
os_task_get_name(), \
(printf_int_t)os_task_get_priority(), \
__FILE__, \
__LINE__, \
__func__, \
##__VA_ARGS__); \
log_print_dump(ESP_LOG_ERROR, TAG, "E", p_buf, buf_size); \
} while (0)
#else
#define LOG_ERR(fmt, ...) (void)0
#define LOG_ERR_ESP(err, fmt, ...) (void)0
#define LOG_ERR_VAL(err, fmt, ...) (void)0
#define LOG_DUMP_ERR(p_buf, buf_size, fmt, ...) (void)0
#endif
#if LOG_LOCAL_LEVEL >= LOG_LEVEL_WARN
#define LOG_WARN(fmt, ...) \
esp_log_write( \
ESP_LOG_WARN, \
TAG, \
LOG_FORMAT(W, "[%s/%d] " fmt), \
esp_log_timestamp(), \
TAG, \
os_task_get_name(), \
(printf_int_t)os_task_get_priority(), \
##__VA_ARGS__)
#define LOG_WARN_ESP(err, fmt, ...) \
do \
{ \
str_buf_t err_desc = esp_err_to_name_with_alloc_str_buf(err); \
esp_log_write( \
ESP_LOG_ERROR, \
TAG, \
LOG_FORMAT(W, "[%s/%d] " fmt ", err=%d (%s)"), \
esp_log_timestamp(), \
TAG, \
os_task_get_name(), \
(printf_int_t)os_task_get_priority(), \
##__VA_ARGS__, \
err, \
(NULL != err_desc.buf) ? err_desc.buf : ""); \
str_buf_free_buf(&err_desc); \
} while (0)
#define LOG_WARN_VAL(err, fmt, ...) \
esp_log_write( \
ESP_LOG_WARN, \
TAG, \
LOG_FORMAT(W, "[%s/%d] " fmt ", err=%d"), \
esp_log_timestamp(), \
TAG, \
os_task_get_name(), \
(printf_int_t)os_task_get_priority(), \
##__VA_ARGS__, \
err)
#define LOG_DUMP_WARN(p_buf, buf_size, fmt, ...) \
do \
{ \
esp_log_write( \
ESP_LOG_WARN, \
TAG, \
LOG_FORMAT(W, "[%s/%d] " fmt ":"), \
esp_log_timestamp(), \
TAG, \
os_task_get_name(), \
(printf_int_t)os_task_get_priority(), \
##__VA_ARGS__); \
log_print_dump(ESP_LOG_WARN, TAG, "W", p_buf, buf_size); \
} while (0)
#else
#define LOG_WARN(fmt, ...) (void)0
#define LOG_WARN_ESP(err, fmt, ...) (void)0
#define LOG_WARN_VAL(err, fmt, ...) (void)0
#define LOG_DUMP_WARN(p_buf, buf_size, fmt, ...) (void)0
#endif
#if LOG_LOCAL_LEVEL >= LOG_LEVEL_INFO
#define LOG_INFO(fmt, ...) \
esp_log_write( \
ESP_LOG_INFO, \
TAG, \
LOG_FORMAT(I, "[%s/%d] " fmt), \
esp_log_timestamp(), \
TAG, \
os_task_get_name(), \
(printf_int_t)os_task_get_priority(), \
##__VA_ARGS__)
#define LOG_DUMP_INFO(p_buf, buf_size, fmt, ...) \
do \
{ \
esp_log_write( \
ESP_LOG_INFO, \
TAG, \
LOG_FORMAT(I, "[%s/%d] " fmt ":"), \
esp_log_timestamp(), \
TAG, \
os_task_get_name(), \
(printf_int_t)os_task_get_priority(), \
##__VA_ARGS__); \
log_print_dump(ESP_LOG_INFO, TAG, "I", p_buf, buf_size); \
} while (0)
#else
#define LOG_INFO(fmt, ...) (void)0
#define LOG_DUMP_INFO(p_buf, buf_size, fmt, ...) (void)0
#endif
#if LOG_LOCAL_LEVEL >= LOG_LEVEL_DEBUG
#define LOG_DBG(fmt, ...) \
esp_log_write( \
ESP_LOG_DEBUG, \
TAG, \
LOG_FORMAT(D, "[%s/%d] %s:%d {%s}: " fmt), \
esp_log_timestamp(), \
TAG, \
os_task_get_name(), \
(printf_int_t)os_task_get_priority(), \
__FILE__, \
__LINE__, \
__func__, \
##__VA_ARGS__)
#define LOG_DUMP_DBG(p_buf, buf_size, fmt, ...) \
do \
{ \
esp_log_write( \
ESP_LOG_DEBUG, \
TAG, \
LOG_FORMAT(D, "[%s/%d] %s:%d {%s}: " fmt ":"), \
esp_log_timestamp(), \
TAG, \
os_task_get_name(), \
(printf_int_t)os_task_get_priority(), \
__FILE__, \
__LINE__, \
__func__, \
##__VA_ARGS__); \
log_print_dump(ESP_LOG_DEBUG, TAG, "D", p_buf, buf_size); \
} while (0)
#else
#define LOG_DBG(fmt, ...) (void)0
#define LOG_DUMP_DBG(p_buf, buf_size, fmt, ...) (void)0
#endif
#if LOG_LOCAL_LEVEL >= LOG_LEVEL_VERBOSE
#define LOG_VERBOSE(fmt, ...) \
esp_log_write( \
ESP_LOG_VERBOSE, \
TAG, \
LOG_FORMAT(V, "[%s/%d] " fmt), \
esp_log_timestamp(), \
TAG, \
os_task_get_name(), \
(printf_int_t)os_task_get_priority(), \
##__VA_ARGS__)
#define LOG_DUMP_VERBOSE(p_buf, buf_size, fmt, ...) \
do \
{ \
esp_log_write( \
ESP_LOG_VERBOSE, \
TAG, \
LOG_FORMAT(V, "[%s/%d] " fmt ":"), \
esp_log_timestamp(), \
TAG, \
os_task_get_name(), \
(printf_int_t)os_task_get_priority(), \
##__VA_ARGS__); \
log_print_dump(ESP_LOG_VERBOSE, TAG, "V", p_buf, buf_size); \
} while (0)
#else
#define LOG_VERBOSE(fmt, ...) (void)0
#define LOG_DUMP_VERBOSE(p_buf, buf_size, fmt, ...) (void)0
#endif
#ifdef __cplusplus
}
#endif
#endif // RUUVI_LOG_H