Wirepas SDK
debug_log.h
Go to the documentation of this file.
1/* Copyright 2019 Wirepas Ltd. All Rights Reserved.
2 *
3 * See file LICENSE.txt for full license details.
4 *
5 */
6
13#include <stdint.h>
14#include <stdarg.h>
15#include "api.h"
16#include "uart_print.h"
17
58/******************************************************************************
59 * DEFINES - LOGGING BACKEND
60 *
61 * Three backends supported:
62 * 1. USE_DEBUG_PRINT_MODULE - Internal debug print module(rtt, uart, multi uart)
63 * 2. APP_PRINTING - App printing via UART
64 * 3. (none) - No-op (logging disabled)
65 ******************************************************************************/
66
67
68#ifdef APP_PRINTING
69
70#ifndef DEBUG_LOG_UART_BAUDRATE
71#define DEBUG_LOG_UART_BAUDRATE 115200
72#endif
73
74#define Print_Log(fmt, ...) UartPrint_printf(fmt, ##__VA_ARGS__)
75#define LOG_INIT() UartPrint_init(DEBUG_LOG_UART_BAUDRATE)
76
77/* Active wait for UART buffer to flush */
78#define Flush_Log() \
79 do { \
80 app_lib_time_timestamp_hp_t _end; \
81 _end = lib_time->addUsToHpTimestamp(lib_time->getTimestampHp(), \
82 FLUSH_DELAY_MS * 1000); \
83 while (lib_time->isHpTimestampBefore(lib_time->getTimestampHp(), \
84 _end)); \
85 } while (0)
86
87#else /* Logging disabled */
88
89#define Print_Log(fmt, ...)
90#define Flush_Log()
91#define LOG_INIT()
92
93#endif /* USE_DEBUG_PRINT_MODULE / APP_PRINTING */
94
95
101#ifndef DEBUG_LOG_MAX_LEVEL
102/* By default only errors are displayed */
103#define DEBUG_LOG_MAX_LEVEL LVL_ERROR
104#endif
105
109#define LVL_DEBUG 4
110#define LVL_INFO 3
111#define LVL_WARNING 2
112#define LVL_ERROR 1
113#define LVL_NOLOG 0
114
115/* Do not modify; number in macro name must match the defined log levels
116 * above.
117 */
118#define LVL_STRING_4 "D"
119#define LVL_STRING_3 "I"
120#define LVL_STRING_2 "W"
121#define LVL_STRING_1 "E"
122#define LVL_STRING_0 ""
123
127#define DEBUG_LVL_TO_STRING(level) LVL_STRING_##level
128
132#define FLUSH_DELAY_MS 45
133
134#ifndef DEBUG_LOG_CUSTOM
135# ifndef DEBUG_LOG_MODULE_NAME
136 /* Name of the module must be defined. */
137# error "No module name set for logger"
138#endif
139 /* Use "[Module name][Time] Log level: " log prefix by default. */
140# define DEBUG_LOG_PRINT_MODULE_NAME
141# define DEBUG_LOG_PRINT_TIME
142# define DEBUG_LOG_PRINT_LEVEL
143# undef DEBUG_LOG_PRINT_FUNCTION
144# undef DEBUG_LOG_PRINT_LINE
145#endif
146
147/* Module name string. */
148#ifdef DEBUG_LOG_PRINT_MODULE_NAME
149# define S_MOD_NAME_PREFIX "["DEBUG_LOG_MODULE_NAME"]"
150#else
151# define S_MOD_NAME_PREFIX
152#endif
153/* Timestamp string. */
154#ifdef DEBUG_LOG_PRINT_TIME_HP
155# define S_TIME_PREFIX "[%09u]"
156# define S_TIME_SUFFIX , lib_time->getTimestampHp()
157#elif defined(DEBUG_LOG_PRINT_TIME_MS)
158# define S_TIME_PREFIX "[%09u]"
159# define S_TIME_SUFFIX , (lib_time->getTimestampCoarse() * 125) >> 4
160#elif defined(DEBUG_LOG_PRINT_TIME)
161# define S_TIME_PREFIX "[%09u]"
162# define S_TIME_SUFFIX , lib_time->getTimestampCoarse()
163#else
164# define S_TIME_PREFIX
165# define S_TIME_SUFFIX
166#endif
167/* Log level string. */
168#ifdef DEBUG_LOG_PRINT_LEVEL
169# define S_LEVEL_PREFIX(level) " "DEBUG_LVL_TO_STRING(level)": "
170#else
171# define S_LEVEL_PREFIX(level)
172#endif
173/* Function string. */
174#ifdef DEBUG_LOG_PRINT_FUNCTION
175# define S_FUNCTION_PREFIX "func:%s, "
176# define S_FUNCTION_SUFFIX , __FUNCTION__
177#else
178# define S_FUNCTION_PREFIX
179# define S_FUNCTION_SUFFIX
180#endif
181/* Line string. */
182#ifdef DEBUG_LOG_PRINT_LINE
183# define S_LINE_PREFIX "line: %03d, "
184# define S_LINE_SUFFIX , __LINE__
185#else
186# define S_LINE_PREFIX
187# define S_LINE_SUFFIX
188#endif
189
190#ifdef DEBUG_LOG_PRINT_CRLF
191# define S_LINEENDING "\r\n"
192#else
193# define S_LINEENDING "\n"
194#endif
195
206#define LOG(level, fmt, ...) \
207{ \
208 ((uint8_t) level) <= ((uint8_t) DEBUG_LOG_MAX_LEVEL) ? \
209 Print_Log( \
210 S_MOD_NAME_PREFIX S_TIME_PREFIX S_LEVEL_PREFIX(level) \
211 S_FUNCTION_PREFIX S_LINE_PREFIX \
212 fmt S_LINEENDING S_TIME_SUFFIX S_FUNCTION_SUFFIX S_LINE_SUFFIX \
213 , ##__VA_ARGS__) : \
214 (void)NULL; \
215}
216
224#define LOGE(fmt, ...) LOG(LVL_ERROR, fmt __VA_OPT__(,) __VA_ARGS__)
225#define LOGW(fmt, ...) LOG(LVL_WARNING, fmt __VA_OPT__(,) __VA_ARGS__)
226#define LOGI(fmt, ...) LOG(LVL_INFO, fmt __VA_OPT__(,) __VA_ARGS__)
227#define LOGD(fmt, ...) LOG(LVL_DEBUG, fmt __VA_OPT__(,) __VA_ARGS__)
228
239#define LOG_BUFFER(level, buffer, size) \
240{ \
241 if(((uint8_t) level) <= ((uint8_t) DEBUG_LOG_MAX_LEVEL)) { \
242 for (uint16_t i = 0; i < size; i++) \
243 { \
244 Print_Log("%02X ", buffer[i]); \
245 if ((i & 0xF) == 0xF && i != (uint8_t)(size-1)) \
246 { \
247 Print_Log(S_LINEENDING); \
248 } \
249 } \
250 Print_Log(S_LINEENDING); \
251 } \
252}
253
259#define LOG_FLUSH(level) \
260{ \
261 if(((uint8_t) level) <= ((uint8_t) DEBUG_LOG_MAX_LEVEL)) { \
262 Flush_Log(); \
263 } \
264}