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 #ifdef APP_PRINTING
59 #define Print_Log(fmt, ...) UartPrint_printf(fmt, ##__VA_ARGS__)
60 #ifndef DEBUG_LOG_UART_BAUDRATE
61 #define DEBUG_LOG_UART_BAUDRATE 115200
62 #endif
63 #define LOG_INIT() UartPrint_init(DEBUG_LOG_UART_BAUDRATE)
64 #else
65 #define Print_Log(fmt, ...)
66 #define LOG_INIT()
67 #endif
68 
74 #ifndef DEBUG_LOG_MAX_LEVEL
75 /* By default only errors are displayed */
76 #define DEBUG_LOG_MAX_LEVEL LVL_ERROR
77 #endif
78 
82 #define LVL_DEBUG 4
83 #define LVL_INFO 3
84 #define LVL_WARNING 2
85 #define LVL_ERROR 1
86 #define LVL_NOLOG 0
87 
88 /* Do not modify; number in macro name must match the defined log levels
89  * above.
90  */
91 #define LVL_STRING_4 "D"
92 #define LVL_STRING_3 "I"
93 #define LVL_STRING_2 "W"
94 #define LVL_STRING_1 "E"
95 #define LVL_STRING_0 ""
96 
100 #define DEBUG_LVL_TO_STRING(level) LVL_STRING_##level
101 
105 #define FLUSH_DELAY_MS 45
106 
107 #ifndef DEBUG_LOG_CUSTOM
108 # ifndef DEBUG_LOG_MODULE_NAME
109  /* Name of the module must be defined. */
110 # error "No module name set for logger"
111 #endif
112  /* Use "[Module name][Time] Log level: " log prefix by default. */
113 # define DEBUG_LOG_PRINT_MODULE_NAME
114 # define DEBUG_LOG_PRINT_TIME
115 # define DEBUG_LOG_PRINT_LEVEL
116 # undef DEBUG_LOG_PRINT_FUNCTION
117 # undef DEBUG_LOG_PRINT_LINE
118 #endif
119 
120 /* Module name string. */
121 #ifdef DEBUG_LOG_PRINT_MODULE_NAME
122 # define S_MOD_NAME_PREFIX "["DEBUG_LOG_MODULE_NAME"]"
123 #else
124 # define S_MOD_NAME_PREFIX
125 #endif
126 /* Timestamp string. */
127 #ifdef DEBUG_LOG_PRINT_TIME_HP
128 # define S_TIME_PREFIX "[%09u]"
129 # define S_TIME_SUFFIX , lib_time->getTimestampHp()
130 #elif defined(DEBUG_LOG_PRINT_TIME_MS)
131 # define S_TIME_PREFIX "[%09u]"
132 # define S_TIME_SUFFIX , (lib_time->getTimestampCoarse() * 125) >> 4
133 #elif defined(DEBUG_LOG_PRINT_TIME)
134 # define S_TIME_PREFIX "[%09u]"
135 # define S_TIME_SUFFIX , lib_time->getTimestampCoarse()
136 #else
137 # define S_TIME_PREFIX
138 # define S_TIME_SUFFIX
139 #endif
140 /* Log level string. */
141 #ifdef DEBUG_LOG_PRINT_LEVEL
142 # define S_LEVEL_PREFIX(level) " "DEBUG_LVL_TO_STRING(level)": "
143 #else
144 # define S_LEVEL_PREFIX(level)
145 #endif
146 /* Function string. */
147 #ifdef DEBUG_LOG_PRINT_FUNCTION
148 # define S_FUNCTION_PREFIX "func:%s, "
149 # define S_FUNCTION_SUFFIX , __FUNCTION__
150 #else
151 # define S_FUNCTION_PREFIX
152 # define S_FUNCTION_SUFFIX
153 #endif
154 /* Line string. */
155 #ifdef DEBUG_LOG_PRINT_LINE
156 # define S_LINE_PREFIX "line: %03d, "
157 # define S_LINE_SUFFIX , __LINE__
158 #else
159 # define S_LINE_PREFIX
160 # define S_LINE_SUFFIX
161 #endif
162 
173 #define LOG(level, fmt, ...) \
174 { \
175  ((uint8_t) level) <= ((uint8_t) DEBUG_LOG_MAX_LEVEL) ? \
176  Print_Log( \
177  S_MOD_NAME_PREFIX S_TIME_PREFIX S_LEVEL_PREFIX(level) \
178  S_FUNCTION_PREFIX S_LINE_PREFIX \
179  fmt"\n" S_TIME_SUFFIX S_FUNCTION_SUFFIX S_LINE_SUFFIX \
180  , ##__VA_ARGS__) : \
181  (void)NULL; \
182 }
183 
191 #define LOGE(fmt, ...) LOG(LVL_ERROR, fmt __VA_OPT__(,) __VA_ARGS__)
192 #define LOGW(fmt, ...) LOG(LVL_WARNING, fmt __VA_OPT__(,) __VA_ARGS__)
193 #define LOGI(fmt, ...) LOG(LVL_DEBUG, fmt __VA_OPT__(,) __VA_ARGS__)
194 #define LOGD(fmt, ...) LOG(LVL_INFO, fmt __VA_OPT__(,) __VA_ARGS__)
195 
206 #define LOG_BUFFER(level, buffer, size) \
207 { \
208  if(((uint8_t) level) <= ((uint8_t) DEBUG_LOG_MAX_LEVEL)) { \
209  for (uint16_t i = 0; i < size; i++) \
210  { \
211  Print_Log("%02X ", buffer[i]); \
212  if ((i & 0xF) == 0xF && i != (uint8_t)(size-1)) \
213  { \
214  Print_Log("\n"); \
215  } \
216  } \
217  Print_Log("\n"); \
218  } \
219 }
220 
226 #define LOG_FLUSH(level) \
227 { \
228  if(((uint8_t) level) <= ((uint8_t) DEBUG_LOG_MAX_LEVEL)) { \
229  app_lib_time_timestamp_hp_t end; \
230  end = lib_time->addUsToHpTimestamp(lib_time->getTimestampHp(), \
231  FLUSH_DELAY_MS * 1000); \
232  while (lib_time->isHpTimestampBefore(lib_time->getTimestampHp(),end)); \
233  } \
234 }
uart_print.h
api.h